Unnamed repository; edit this file 'description' to name the repository.
fix: Don't create empty path nodes
Lukas Wirth 2024-12-04
parent b6fc9c1 · commit caba872
-rw-r--r--crates/parser/src/grammar.rs4
-rw-r--r--crates/parser/src/grammar/generic_args.rs4
-rw-r--r--crates/parser/src/grammar/paths.rs23
-rw-r--r--crates/parser/src/parser.rs8
-rw-r--r--crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast6
-rw-r--r--crates/parser/test_data/parser/inline/err/meta_recovery.rast5
6 files changed, 26 insertions, 24 deletions
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index a50a2182a7..c402c49855 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -242,7 +242,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
// struct MyStruct(pub ());
if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) {
p.bump(T!['(']);
- paths::use_path(p);
+ paths::vis_path(p);
p.expect(T![')']);
}
}
@@ -252,7 +252,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
T![in] => {
p.bump(T!['(']);
p.bump(T![in]);
- paths::use_path(p);
+ paths::vis_path(p);
p.expect(T![')']);
}
_ => {}
diff --git a/crates/parser/src/grammar/generic_args.rs b/crates/parser/src/grammar/generic_args.rs
index c62c8a9d3f..77379ef147 100644
--- a/crates/parser/src/grammar/generic_args.rs
+++ b/crates/parser/src/grammar/generic_args.rs
@@ -168,10 +168,10 @@ pub(super) fn const_arg_expr(p: &mut Parser<'_>) {
expressions::literal(p);
lm.complete(p, PREFIX_EXPR);
}
- _ if paths::is_use_path_start(p) => {
+ _ if paths::is_path_start(p) => {
// This shouldn't be hit by `const_arg`
let lm = p.start();
- paths::use_path(p);
+ paths::expr_path(p);
lm.complete(p, PATH_EXPR);
}
_ => {
diff --git a/crates/parser/src/grammar/paths.rs b/crates/parser/src/grammar/paths.rs
index 057a691ef0..15b3529642 100644
--- a/crates/parser/src/grammar/paths.rs
+++ b/crates/parser/src/grammar/paths.rs
@@ -19,6 +19,10 @@ pub(super) fn use_path(p: &mut Parser<'_>) {
path(p, Mode::Use);
}
+pub(super) fn vis_path(p: &mut Parser<'_>) {
+ path(p, Mode::Vis);
+}
+
pub(super) fn attr_path(p: &mut Parser<'_>) {
path(p, Mode::Attr);
}
@@ -44,13 +48,17 @@ enum Mode {
Attr,
Type,
Expr,
+ Vis,
}
-fn path(p: &mut Parser<'_>, mode: Mode) {
+fn path(p: &mut Parser<'_>, mode: Mode) -> Option<CompletedMarker> {
let path = p.start();
- path_segment(p, mode, true);
+ if path_segment(p, mode, true).is_none() {
+ path.abandon(p);
+ return None;
+ }
let qual = path.complete(p, PATH);
- path_for_qualifier(p, mode, qual);
+ Some(path_for_qualifier(p, mode, qual))
}
fn path_for_qualifier(
@@ -76,7 +84,7 @@ const EXPR_PATH_SEGMENT_RECOVERY_SET: TokenSet =
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')'], T![,], T![let]]));
const TYPE_PATH_SEGMENT_RECOVERY_SET: TokenSet = types::TYPE_RECOVERY_SET;
-fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
+fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<CompletedMarker> {
let m = p.start();
// test qual_paths
// type X = <A as B>::Output;
@@ -117,6 +125,7 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
Mode::Attr => {
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![']'], T![=], T![#]]))
}
+ Mode::Vis => items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')']])),
Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
};
@@ -125,17 +134,17 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
// test_err empty_segment
// use crate::;
m.abandon(p);
- return;
+ return None;
}
}
};
}
- m.complete(p, PATH_SEGMENT);
+ Some(m.complete(p, PATH_SEGMENT))
}
fn opt_path_type_args(p: &mut Parser<'_>, mode: Mode) {
match mode {
- Mode::Use | Mode::Attr => {}
+ Mode::Use | Mode::Attr | Mode::Vis => {}
Mode::Type => {
// test typepathfn_with_coloncolon
// type F = Start::(Middle) -> (Middle)::End;
diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs
index 8078532e0b..75a75f601c 100644
--- a/crates/parser/src/parser.rs
+++ b/crates/parser/src/parser.rs
@@ -327,10 +327,10 @@ impl Marker {
self.bomb.defuse();
let idx = self.pos as usize;
if idx == p.events.len() - 1 {
- match p.events.pop() {
- Some(Event::Start { kind: TOMBSTONE, forward_parent: None }) => (),
- _ => unreachable!(),
- }
+ assert!(matches!(
+ p.events.pop(),
+ Some(Event::Start { kind: TOMBSTONE, forward_parent: None })
+ ));
}
}
}
diff --git a/crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast b/crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast
index 0fe4ca42d7..681ca6b6e0 100644
--- a/crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast
+++ b/crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast
@@ -3,10 +3,7 @@ SOURCE_FILE
VISIBILITY
PUB_KW "pub"
L_PAREN "("
- PATH
- PATH_SEGMENT
- ERROR
- R_PAREN ")"
+ R_PAREN ")"
WHITESPACE " "
STRUCT_KW "struct"
WHITESPACE " "
@@ -15,4 +12,3 @@ SOURCE_FILE
SEMICOLON ";"
WHITESPACE "\n"
error 4: expected identifier
-error 5: expected R_PAREN
diff --git a/crates/parser/test_data/parser/inline/err/meta_recovery.rast b/crates/parser/test_data/parser/inline/err/meta_recovery.rast
index c4bec849e7..926dd50fc8 100644
--- a/crates/parser/test_data/parser/inline/err/meta_recovery.rast
+++ b/crates/parser/test_data/parser/inline/err/meta_recovery.rast
@@ -4,7 +4,6 @@ SOURCE_FILE
BANG "!"
L_BRACK "["
META
- PATH
R_BRACK "]"
WHITESPACE "\n"
ATTR
@@ -55,7 +54,6 @@ SOURCE_FILE
L_BRACK "["
META
UNSAFE_KW "unsafe"
- PATH
R_BRACK "]"
WHITESPACE "\n"
ATTR
@@ -65,7 +63,6 @@ SOURCE_FILE
META
UNSAFE_KW "unsafe"
WHITESPACE " "
- PATH
EQ "="
R_BRACK "]"
WHITESPACE "\n"
@@ -80,7 +77,7 @@ error 41: expected L_PAREN
error 41: expected identifier
error 41: expected R_PAREN
error 52: expected L_PAREN
-error 53: expected identifier
+error 52: expected identifier
error 54: expected expression
error 54: expected expression
error 54: expected R_PAREN