Skip to content

Commit 83c188a

Browse files
authored
Merge pull request #20163 from ChayimFriedman2/parser-per-token-edition
fix: Use per-token, not global, edition in the parser
2 parents 450860a + 3b610a6 commit 83c188a

File tree

18 files changed

+58
-67
lines changed

18 files changed

+58
-67
lines changed

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
372372
subtree,
373373
syntax_bridge::TopEntryPoint::MacroItems,
374374
&mut |_| span::Edition::CURRENT,
375-
span::Edition::CURRENT,
376375
);
377376
if parse.errors().is_empty() {
378377
Ok(subtree.clone())
@@ -413,10 +412,7 @@ fn regression_20171() {
413412
#dollar_crate::panic::panic_2021!();
414413
}}
415414
};
416-
token_tree_to_syntax_node(
417-
&tt,
418-
syntax_bridge::TopEntryPoint::MacroStmts,
419-
&mut |_| Edition::CURRENT,
420-
Edition::CURRENT,
421-
);
415+
token_tree_to_syntax_node(&tt, syntax_bridge::TopEntryPoint::MacroStmts, &mut |_| {
416+
Edition::CURRENT
417+
});
422418
}

src/tools/rust-analyzer/crates/hir-expand/src/builtin/derive_macro.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,7 @@ fn to_adt_syntax(
392392
tt: &tt::TopSubtree,
393393
call_site: Span,
394394
) -> Result<(ast::Adt, span::SpanMap<SyntaxContext>), ExpandError> {
395-
let (parsed, tm) = crate::db::token_tree_to_syntax_node(
396-
db,
397-
tt,
398-
crate::ExpandTo::Items,
399-
parser::Edition::CURRENT_FIXME,
400-
);
395+
let (parsed, tm) = crate::db::token_tree_to_syntax_node(db, tt, crate::ExpandTo::Items);
401396
let macro_items = ast::MacroItems::cast(parsed.syntax_node())
402397
.ok_or_else(|| ExpandError::other(call_site, "invalid item definition"))?;
403398
let item =

src/tools/rust-analyzer/crates/hir-expand/src/db.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ pub fn expand_speculative(
315315
let expand_to = loc.expand_to();
316316

317317
fixup::reverse_fixups(&mut speculative_expansion.value, &undo_info);
318-
let (node, rev_tmap) =
319-
token_tree_to_syntax_node(db, &speculative_expansion.value, expand_to, loc.def.edition);
318+
let (node, rev_tmap) = token_tree_to_syntax_node(db, &speculative_expansion.value, expand_to);
320319

321320
let syntax_node = node.syntax_node();
322321
let token = rev_tmap
@@ -358,7 +357,6 @@ fn parse_macro_expansion(
358357
) -> ExpandResult<(Parse<SyntaxNode>, Arc<ExpansionSpanMap>)> {
359358
let _p = tracing::info_span!("parse_macro_expansion").entered();
360359
let loc = db.lookup_intern_macro_call(macro_file);
361-
let def_edition = loc.def.edition;
362360
let expand_to = loc.expand_to();
363361
let mbe::ValueResult { value: (tt, matched_arm), err } = macro_expand(db, macro_file, loc);
364362

@@ -369,7 +367,6 @@ fn parse_macro_expansion(
369367
CowArc::Owned(it) => it,
370368
},
371369
expand_to,
372-
def_edition,
373370
);
374371
rev_token_map.matched_arm = matched_arm;
375372

@@ -731,7 +728,6 @@ pub(crate) fn token_tree_to_syntax_node(
731728
db: &dyn ExpandDatabase,
732729
tt: &tt::TopSubtree,
733730
expand_to: ExpandTo,
734-
edition: parser::Edition,
735731
) -> (Parse<SyntaxNode>, ExpansionSpanMap) {
736732
let entry_point = match expand_to {
737733
ExpandTo::Statements => syntax_bridge::TopEntryPoint::MacroStmts,
@@ -740,7 +736,7 @@ pub(crate) fn token_tree_to_syntax_node(
740736
ExpandTo::Type => syntax_bridge::TopEntryPoint::Type,
741737
ExpandTo::Expr => syntax_bridge::TopEntryPoint::Expr,
742738
};
743-
syntax_bridge::token_tree_to_syntax_node(tt, entry_point, &mut |ctx| ctx.edition(db), edition)
739+
syntax_bridge::token_tree_to_syntax_node(tt, entry_point, &mut |ctx| ctx.edition(db))
744740
}
745741

746742
fn check_tt_count(tt: &tt::TopSubtree) -> Result<(), ExpandResult<()>> {

src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ mod tests {
535535
&tt,
536536
syntax_bridge::TopEntryPoint::MacroItems,
537537
&mut |_| parser::Edition::CURRENT,
538-
parser::Edition::CURRENT,
539538
);
540539
assert!(
541540
parse.errors().is_empty(),

src/tools/rust-analyzer/crates/mbe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ pub fn expect_fragment<'t>(
399399
let buffer = tt_iter.remaining();
400400
// FIXME: Pass the correct edition per token. Due to the split between mbe and hir-expand it's complicated.
401401
let parser_input = to_parser_input(buffer, &mut |_ctx| edition);
402-
let tree_traversal = entry_point.parse(&parser_input, edition);
402+
let tree_traversal = entry_point.parse(&parser_input);
403403
let mut cursor = buffer.cursor();
404404
let mut error = false;
405405
for step in tree_traversal.iter() {

src/tools/rust-analyzer/crates/mbe/src/tests.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ fn check_(
6666
if render_debug {
6767
format_to!(expect_res, "{:#?}\n\n", res.value.0);
6868
}
69-
let (node, _) = syntax_bridge::token_tree_to_syntax_node(
70-
&res.value.0,
71-
parse,
72-
&mut |_| def_edition,
73-
def_edition,
74-
);
69+
let (node, _) =
70+
syntax_bridge::token_tree_to_syntax_node(&res.value.0, parse, &mut |_| def_edition);
7571
format_to!(
7672
expect_res,
7773
"{}",

src/tools/rust-analyzer/crates/parser/src/grammar/generic_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn generic_arg(p: &mut Parser<'_>) -> bool {
5959

6060
// test edition_2015_dyn_prefix_inside_generic_arg 2015
6161
// type A = Foo<dyn T>;
62-
T![ident] if !p.edition().at_least_2018() && types::is_dyn_weak(p) => type_arg(p),
62+
T![ident] if !p.current_edition().at_least_2018() && types::is_dyn_weak(p) => type_arg(p),
6363
// test macro_inside_generic_arg
6464
// type A = Foo<syn::Token![_]>;
6565
k if PATH_NAME_REF_KINDS.contains(k) => {

src/tools/rust-analyzer/crates/parser/src/grammar/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ fn type_with_bounds_cond(p: &mut Parser<'_>, allow_bounds: bool) {
5454
T![dyn] => dyn_trait_type(p),
5555
// Some path types are not allowed to have bounds (no plus)
5656
T![<] => path_type_bounds(p, allow_bounds),
57-
T![ident] if !p.edition().at_least_2018() && is_dyn_weak(p) => dyn_trait_type_weak(p),
57+
T![ident] if !p.current_edition().at_least_2018() && is_dyn_weak(p) => {
58+
dyn_trait_type_weak(p)
59+
}
5860
_ if paths::is_path_start(p) => path_or_macro_type(p, allow_bounds),
5961
LIFETIME_IDENT if p.nth_at(1, T![+]) => bare_dyn_trait_type(p),
6062
_ => {

src/tools/rust-analyzer/crates/parser/src/input.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! See [`Input`].
22
3+
use edition::Edition;
4+
35
use crate::SyntaxKind;
46

57
#[allow(non_camel_case_types)]
@@ -16,6 +18,7 @@ pub struct Input {
1618
kind: Vec<SyntaxKind>,
1719
joint: Vec<bits>,
1820
contextual_kind: Vec<SyntaxKind>,
21+
edition: Vec<Edition>,
1922
}
2023

2124
/// `pub` impl used by callers to create `Tokens`.
@@ -26,15 +29,16 @@ impl Input {
2629
kind: Vec::with_capacity(capacity),
2730
joint: Vec::with_capacity(capacity / size_of::<bits>()),
2831
contextual_kind: Vec::with_capacity(capacity),
32+
edition: Vec::with_capacity(capacity),
2933
}
3034
}
3135
#[inline]
32-
pub fn push(&mut self, kind: SyntaxKind) {
33-
self.push_impl(kind, SyntaxKind::EOF)
36+
pub fn push(&mut self, kind: SyntaxKind, edition: Edition) {
37+
self.push_impl(kind, SyntaxKind::EOF, edition)
3438
}
3539
#[inline]
36-
pub fn push_ident(&mut self, contextual_kind: SyntaxKind) {
37-
self.push_impl(SyntaxKind::IDENT, contextual_kind)
40+
pub fn push_ident(&mut self, contextual_kind: SyntaxKind, edition: Edition) {
41+
self.push_impl(SyntaxKind::IDENT, contextual_kind, edition)
3842
}
3943
/// Sets jointness for the last token we've pushed.
4044
///
@@ -59,13 +63,14 @@ impl Input {
5963
self.joint[idx] |= 1 << b_idx;
6064
}
6165
#[inline]
62-
fn push_impl(&mut self, kind: SyntaxKind, contextual_kind: SyntaxKind) {
66+
fn push_impl(&mut self, kind: SyntaxKind, contextual_kind: SyntaxKind, edition: Edition) {
6367
let idx = self.len();
6468
if idx.is_multiple_of(bits::BITS as usize) {
6569
self.joint.push(0);
6670
}
6771
self.kind.push(kind);
6872
self.contextual_kind.push(contextual_kind);
73+
self.edition.push(edition);
6974
}
7075
}
7176

@@ -77,6 +82,9 @@ impl Input {
7782
pub(crate) fn contextual_kind(&self, idx: usize) -> SyntaxKind {
7883
self.contextual_kind.get(idx).copied().unwrap_or(SyntaxKind::EOF)
7984
}
85+
pub(crate) fn edition(&self, idx: usize) -> Edition {
86+
self.edition[idx]
87+
}
8088
pub(crate) fn is_joint(&self, n: usize) -> bool {
8189
let (idx, b_idx) = self.bit_index(n);
8290
self.joint[idx] & (1 << b_idx) != 0

src/tools/rust-analyzer/crates/parser/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub enum TopEntryPoint {
8888
}
8989

9090
impl TopEntryPoint {
91-
pub fn parse(&self, input: &Input, edition: Edition) -> Output {
91+
pub fn parse(&self, input: &Input) -> Output {
9292
let _p = tracing::info_span!("TopEntryPoint::parse", ?self).entered();
9393
let entry_point: fn(&'_ mut parser::Parser<'_>) = match self {
9494
TopEntryPoint::SourceFile => grammar::entry::top::source_file,
@@ -99,7 +99,7 @@ impl TopEntryPoint {
9999
TopEntryPoint::Expr => grammar::entry::top::expr,
100100
TopEntryPoint::MetaItem => grammar::entry::top::meta_item,
101101
};
102-
let mut p = parser::Parser::new(input, edition);
102+
let mut p = parser::Parser::new(input);
103103
entry_point(&mut p);
104104
let events = p.finish();
105105
let res = event::process(events);
@@ -151,7 +151,7 @@ pub enum PrefixEntryPoint {
151151
}
152152

153153
impl PrefixEntryPoint {
154-
pub fn parse(&self, input: &Input, edition: Edition) -> Output {
154+
pub fn parse(&self, input: &Input) -> Output {
155155
let entry_point: fn(&'_ mut parser::Parser<'_>) = match self {
156156
PrefixEntryPoint::Vis => grammar::entry::prefix::vis,
157157
PrefixEntryPoint::Block => grammar::entry::prefix::block,
@@ -164,7 +164,7 @@ impl PrefixEntryPoint {
164164
PrefixEntryPoint::Item => grammar::entry::prefix::item,
165165
PrefixEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
166166
};
167-
let mut p = parser::Parser::new(input, edition);
167+
let mut p = parser::Parser::new(input);
168168
entry_point(&mut p);
169169
let events = p.finish();
170170
event::process(events)
@@ -188,9 +188,9 @@ impl Reparser {
188188
///
189189
/// Tokens must start with `{`, end with `}` and form a valid brace
190190
/// sequence.
191-
pub fn parse(self, tokens: &Input, edition: Edition) -> Output {
191+
pub fn parse(self, tokens: &Input) -> Output {
192192
let Reparser(r) = self;
193-
let mut p = parser::Parser::new(tokens, edition);
193+
let mut p = parser::Parser::new(tokens);
194194
r(&mut p);
195195
let events = p.finish();
196196
event::process(events)

0 commit comments

Comments
 (0)