Skip to content

Commit 8b53dd0

Browse files
Use per-token edition in the parser
1 parent bcf9756 commit 8b53dd0

File tree

15 files changed

+26
-41
lines changed

15 files changed

+26
-41
lines changed

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
373373
subtree,
374374
syntax_bridge::TopEntryPoint::MacroItems,
375375
&mut |_| span::Edition::CURRENT,
376-
span::Edition::CURRENT,
377376
);
378377
if parse.errors().is_empty() {
379378
Ok(subtree.clone())

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 =

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

@@ -733,7 +730,6 @@ pub(crate) fn token_tree_to_syntax_node(
733730
db: &dyn ExpandDatabase,
734731
tt: &tt::TopSubtree,
735732
expand_to: ExpandTo,
736-
edition: parser::Edition,
737733
) -> (Parse<SyntaxNode>, ExpansionSpanMap) {
738734
let entry_point = match expand_to {
739735
ExpandTo::Statements => syntax_bridge::TopEntryPoint::MacroStmts,
@@ -742,7 +738,7 @@ pub(crate) fn token_tree_to_syntax_node(
742738
ExpandTo::Type => syntax_bridge::TopEntryPoint::Type,
743739
ExpandTo::Expr => syntax_bridge::TopEntryPoint::Expr,
744740
};
745-
syntax_bridge::token_tree_to_syntax_node(tt, entry_point, &mut |ctx| ctx.edition(db), edition)
741+
syntax_bridge::token_tree_to_syntax_node(tt, entry_point, &mut |ctx| ctx.edition(db))
746742
}
747743

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

crates/hir-expand/src/fixup.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ mod tests {
537537
&tt,
538538
syntax_bridge::TopEntryPoint::MacroItems,
539539
&mut |_| parser::Edition::CURRENT,
540-
parser::Edition::CURRENT,
541540
);
542541
assert!(
543542
parse.errors().is_empty(),

crates/mbe/src/lib.rs

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

crates/mbe/src/tests.rs

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

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) => {

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
_ => {

crates/parser/src/lib.rs

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

8989
impl TopEntryPoint {
90-
pub fn parse(&self, input: &Input, edition: Edition) -> Output {
90+
pub fn parse(&self, input: &Input) -> Output {
9191
let _p = tracing::info_span!("TopEntryPoint::parse", ?self).entered();
9292
let entry_point: fn(&'_ mut parser::Parser<'_>) = match self {
9393
TopEntryPoint::SourceFile => grammar::entry::top::source_file,
@@ -98,7 +98,7 @@ impl TopEntryPoint {
9898
TopEntryPoint::Expr => grammar::entry::top::expr,
9999
TopEntryPoint::MetaItem => grammar::entry::top::meta_item,
100100
};
101-
let mut p = parser::Parser::new(input, edition);
101+
let mut p = parser::Parser::new(input);
102102
entry_point(&mut p);
103103
let events = p.finish();
104104
let res = event::process(events);
@@ -150,7 +150,7 @@ pub enum PrefixEntryPoint {
150150
}
151151

152152
impl PrefixEntryPoint {
153-
pub fn parse(&self, input: &Input, edition: Edition) -> Output {
153+
pub fn parse(&self, input: &Input) -> Output {
154154
let entry_point: fn(&'_ mut parser::Parser<'_>) = match self {
155155
PrefixEntryPoint::Vis => grammar::entry::prefix::vis,
156156
PrefixEntryPoint::Block => grammar::entry::prefix::block,
@@ -163,7 +163,7 @@ impl PrefixEntryPoint {
163163
PrefixEntryPoint::Item => grammar::entry::prefix::item,
164164
PrefixEntryPoint::MetaItem => grammar::entry::prefix::meta_item,
165165
};
166-
let mut p = parser::Parser::new(input, edition);
166+
let mut p = parser::Parser::new(input);
167167
entry_point(&mut p);
168168
let events = p.finish();
169169
event::process(events)
@@ -187,9 +187,9 @@ impl Reparser {
187187
///
188188
/// Tokens must start with `{`, end with `}` and form a valid brace
189189
/// sequence.
190-
pub fn parse(self, tokens: &Input, edition: Edition) -> Output {
190+
pub fn parse(self, tokens: &Input) -> Output {
191191
let Reparser(r) = self;
192-
let mut p = parser::Parser::new(tokens, edition);
192+
let mut p = parser::Parser::new(tokens);
193193
r(&mut p);
194194
let events = p.finish();
195195
event::process(events)

crates/parser/src/parser.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ pub(crate) struct Parser<'t> {
2626
pos: usize,
2727
events: Vec<Event>,
2828
steps: Cell<u32>,
29-
edition: Edition,
3029
}
3130

3231
const PARSER_STEP_LIMIT: usize = 15_000_000;
3332

3433
impl<'t> Parser<'t> {
35-
pub(super) fn new(inp: &'t Input, edition: Edition) -> Parser<'t> {
36-
Parser { inp, pos: 0, events: Vec::new(), steps: Cell::new(0), edition }
34+
pub(super) fn new(inp: &'t Input) -> Parser<'t> {
35+
Parser { inp, pos: 0, events: Vec::new(), steps: Cell::new(0) }
3736
}
3837

3938
pub(crate) fn finish(self) -> Vec<Event> {
@@ -288,8 +287,8 @@ impl<'t> Parser<'t> {
288287
self.events.push(event);
289288
}
290289

291-
pub(crate) fn edition(&self) -> Edition {
292-
self.edition
290+
pub(crate) fn current_edition(&self) -> Edition {
291+
self.inp.edition(self.pos)
293292
}
294293
}
295294

0 commit comments

Comments
 (0)