Skip to content

Commit caba872

Browse files
committed
fix: Don't create empty path nodes
1 parent b6fc9c1 commit caba872

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

crates/parser/src/grammar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
242242
// struct MyStruct(pub ());
243243
if !(in_tuple_field && matches!(p.nth(1), T![ident] | T![')'])) {
244244
p.bump(T!['(']);
245-
paths::use_path(p);
245+
paths::vis_path(p);
246246
p.expect(T![')']);
247247
}
248248
}
@@ -252,7 +252,7 @@ fn opt_visibility(p: &mut Parser<'_>, in_tuple_field: bool) -> bool {
252252
T![in] => {
253253
p.bump(T!['(']);
254254
p.bump(T![in]);
255-
paths::use_path(p);
255+
paths::vis_path(p);
256256
p.expect(T![')']);
257257
}
258258
_ => {}

crates/parser/src/grammar/generic_args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ pub(super) fn const_arg_expr(p: &mut Parser<'_>) {
168168
expressions::literal(p);
169169
lm.complete(p, PREFIX_EXPR);
170170
}
171-
_ if paths::is_use_path_start(p) => {
171+
_ if paths::is_path_start(p) => {
172172
// This shouldn't be hit by `const_arg`
173173
let lm = p.start();
174-
paths::use_path(p);
174+
paths::expr_path(p);
175175
lm.complete(p, PATH_EXPR);
176176
}
177177
_ => {

crates/parser/src/grammar/paths.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pub(super) fn use_path(p: &mut Parser<'_>) {
1919
path(p, Mode::Use);
2020
}
2121

22+
pub(super) fn vis_path(p: &mut Parser<'_>) {
23+
path(p, Mode::Vis);
24+
}
25+
2226
pub(super) fn attr_path(p: &mut Parser<'_>) {
2327
path(p, Mode::Attr);
2428
}
@@ -44,13 +48,17 @@ enum Mode {
4448
Attr,
4549
Type,
4650
Expr,
51+
Vis,
4752
}
4853

49-
fn path(p: &mut Parser<'_>, mode: Mode) {
54+
fn path(p: &mut Parser<'_>, mode: Mode) -> Option<CompletedMarker> {
5055
let path = p.start();
51-
path_segment(p, mode, true);
56+
if path_segment(p, mode, true).is_none() {
57+
path.abandon(p);
58+
return None;
59+
}
5260
let qual = path.complete(p, PATH);
53-
path_for_qualifier(p, mode, qual);
61+
Some(path_for_qualifier(p, mode, qual))
5462
}
5563

5664
fn path_for_qualifier(
@@ -76,7 +84,7 @@ const EXPR_PATH_SEGMENT_RECOVERY_SET: TokenSet =
7684
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')'], T![,], T![let]]));
7785
const TYPE_PATH_SEGMENT_RECOVERY_SET: TokenSet = types::TYPE_RECOVERY_SET;
7886

79-
fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
87+
fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<CompletedMarker> {
8088
let m = p.start();
8189
// test qual_paths
8290
// type X = <A as B>::Output;
@@ -117,6 +125,7 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
117125
Mode::Attr => {
118126
items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![']'], T![=], T![#]]))
119127
}
128+
Mode::Vis => items::ITEM_RECOVERY_SET.union(TokenSet::new(&[T![')']])),
120129
Mode::Type => TYPE_PATH_SEGMENT_RECOVERY_SET,
121130
Mode::Expr => EXPR_PATH_SEGMENT_RECOVERY_SET,
122131
};
@@ -125,17 +134,17 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) {
125134
// test_err empty_segment
126135
// use crate::;
127136
m.abandon(p);
128-
return;
137+
return None;
129138
}
130139
}
131140
};
132141
}
133-
m.complete(p, PATH_SEGMENT);
142+
Some(m.complete(p, PATH_SEGMENT))
134143
}
135144

136145
fn opt_path_type_args(p: &mut Parser<'_>, mode: Mode) {
137146
match mode {
138-
Mode::Use | Mode::Attr => {}
147+
Mode::Use | Mode::Attr | Mode::Vis => {}
139148
Mode::Type => {
140149
// test typepathfn_with_coloncolon
141150
// type F = Start::(Middle) -> (Middle)::End;

crates/parser/src/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ impl Marker {
327327
self.bomb.defuse();
328328
let idx = self.pos as usize;
329329
if idx == p.events.len() - 1 {
330-
match p.events.pop() {
331-
Some(Event::Start { kind: TOMBSTONE, forward_parent: None }) => (),
332-
_ => unreachable!(),
333-
}
330+
assert!(matches!(
331+
p.events.pop(),
332+
Some(Event::Start { kind: TOMBSTONE, forward_parent: None })
333+
));
334334
}
335335
}
336336
}

crates/parser/test_data/parser/inline/err/crate_visibility_empty_recover.rast

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ SOURCE_FILE
33
VISIBILITY
44
PUB_KW "pub"
55
L_PAREN "("
6-
PATH
7-
PATH_SEGMENT
8-
ERROR
9-
R_PAREN ")"
6+
R_PAREN ")"
107
WHITESPACE " "
118
STRUCT_KW "struct"
129
WHITESPACE " "
@@ -15,4 +12,3 @@ SOURCE_FILE
1512
SEMICOLON ";"
1613
WHITESPACE "\n"
1714
error 4: expected identifier
18-
error 5: expected R_PAREN

crates/parser/test_data/parser/inline/err/meta_recovery.rast

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ SOURCE_FILE
44
BANG "!"
55
L_BRACK "["
66
META
7-
PATH
87
R_BRACK "]"
98
WHITESPACE "\n"
109
ATTR
@@ -55,7 +54,6 @@ SOURCE_FILE
5554
L_BRACK "["
5655
META
5756
UNSAFE_KW "unsafe"
58-
PATH
5957
R_BRACK "]"
6058
WHITESPACE "\n"
6159
ATTR
@@ -65,7 +63,6 @@ SOURCE_FILE
6563
META
6664
UNSAFE_KW "unsafe"
6765
WHITESPACE " "
68-
PATH
6966
EQ "="
7067
R_BRACK "]"
7168
WHITESPACE "\n"
@@ -80,7 +77,7 @@ error 41: expected L_PAREN
8077
error 41: expected identifier
8178
error 41: expected R_PAREN
8279
error 52: expected L_PAREN
83-
error 53: expected identifier
80+
error 52: expected identifier
8481
error 54: expected expression
8582
error 54: expected expression
8683
error 54: expected R_PAREN

0 commit comments

Comments
 (0)