Skip to content

Commit e95644e

Browse files
committed
Auto merge of rust-lang#15212 - lowr:patch/recover-from-incomplete-assoc-ty, r=HKalbasi
Recover from missing associated items and generic const defaults Fixes rust-lang#15129
2 parents e219999 + 49b039f commit e95644e

6 files changed

+113
-3
lines changed

crates/parser/src/grammar/generic_args.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const GENERIC_ARG_FIRST: TokenSet = TokenSet::new(&[
3232
])
3333
.union(types::TYPE_FIRST);
3434

35+
// Despite its name, it can also be used for generic param list.
36+
const GENERIC_ARG_RECOVERY_SET: TokenSet = TokenSet::new(&[T![>], T![,]]);
37+
3538
// test generic_arg
3639
// type T = S<i32>;
3740
fn generic_arg(p: &mut Parser<'_>) -> bool {
@@ -55,6 +58,15 @@ fn generic_arg(p: &mut Parser<'_>) -> bool {
5558
// test assoc_type_eq
5659
// type T = StreamingIterator<Item<'a> = &'a T>;
5760
types::type_(p);
61+
} else if p.at_ts(GENERIC_ARG_RECOVERY_SET) {
62+
// Although `const_arg()` recovers as expected, we want to
63+
// handle those here to give the following message because
64+
// we don't know whether this associated item is a type or
65+
// const at this point.
66+
67+
// test_err recover_from_missing_assoc_item_binding
68+
// fn f() -> impl Iterator<Item = , Item = > {}
69+
p.error("missing associated item binding");
5870
} else {
5971
// test assoc_const_eq
6072
// fn foo<F: Foo<N=3>>() {}
@@ -141,12 +153,17 @@ pub(super) fn const_arg_expr(p: &mut Parser<'_>) {
141153
expressions::literal(p);
142154
lm.complete(p, PREFIX_EXPR);
143155
}
144-
_ => {
156+
_ if paths::is_use_path_start(p) => {
145157
// This shouldn't be hit by `const_arg`
146158
let lm = p.start();
147159
paths::use_path(p);
148160
lm.complete(p, PATH_EXPR);
149161
}
162+
_ => {
163+
// test_err recover_from_missing_const_default
164+
// struct A<const N: i32 = , const M: i32 =>;
165+
p.err_recover("expected a generic const argument", GENERIC_ARG_RECOVERY_SET);
166+
}
150167
}
151168
}
152169

crates/parser/src/grammar/generic_params.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ fn const_param(p: &mut Parser<'_>, m: Marker) {
7979
p.error("missing type for const parameter");
8080
}
8181

82-
if p.at(T![=]) {
82+
if p.eat(T![=]) {
8383
// test const_param_default_literal
8484
// struct A<const N: i32 = -1>;
85-
p.bump(T![=]);
8685

8786
// test const_param_default_expression
8887
// struct A<const N: i32 = { 1 }>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "f"
7+
PARAM_LIST
8+
L_PAREN "("
9+
R_PAREN ")"
10+
WHITESPACE " "
11+
RET_TYPE
12+
THIN_ARROW "->"
13+
WHITESPACE " "
14+
IMPL_TRAIT_TYPE
15+
IMPL_KW "impl"
16+
WHITESPACE " "
17+
TYPE_BOUND_LIST
18+
TYPE_BOUND
19+
PATH_TYPE
20+
PATH
21+
PATH_SEGMENT
22+
NAME_REF
23+
IDENT "Iterator"
24+
GENERIC_ARG_LIST
25+
L_ANGLE "<"
26+
ASSOC_TYPE_ARG
27+
NAME_REF
28+
IDENT "Item"
29+
WHITESPACE " "
30+
EQ "="
31+
WHITESPACE " "
32+
COMMA ","
33+
WHITESPACE " "
34+
ASSOC_TYPE_ARG
35+
NAME_REF
36+
IDENT "Item"
37+
WHITESPACE " "
38+
EQ "="
39+
WHITESPACE " "
40+
R_ANGLE ">"
41+
WHITESPACE " "
42+
BLOCK_EXPR
43+
STMT_LIST
44+
L_CURLY "{"
45+
R_CURLY "}"
46+
WHITESPACE "\n"
47+
error 30: missing associated item binding
48+
error 39: missing associated item binding
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn f() -> impl Iterator<Item = , Item = > {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
SOURCE_FILE
2+
STRUCT
3+
STRUCT_KW "struct"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "A"
7+
GENERIC_PARAM_LIST
8+
L_ANGLE "<"
9+
CONST_PARAM
10+
CONST_KW "const"
11+
WHITESPACE " "
12+
NAME
13+
IDENT "N"
14+
COLON ":"
15+
WHITESPACE " "
16+
PATH_TYPE
17+
PATH
18+
PATH_SEGMENT
19+
NAME_REF
20+
IDENT "i32"
21+
WHITESPACE " "
22+
EQ "="
23+
WHITESPACE " "
24+
COMMA ","
25+
WHITESPACE " "
26+
CONST_PARAM
27+
CONST_KW "const"
28+
WHITESPACE " "
29+
NAME
30+
IDENT "M"
31+
COLON ":"
32+
WHITESPACE " "
33+
PATH_TYPE
34+
PATH
35+
PATH_SEGMENT
36+
NAME_REF
37+
IDENT "i32"
38+
WHITESPACE " "
39+
EQ "="
40+
R_ANGLE ">"
41+
SEMICOLON ";"
42+
WHITESPACE "\n"
43+
error 23: expected a generic const argument
44+
error 40: expected a generic const argument
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct A<const N: i32 = , const M: i32 =>;

0 commit comments

Comments
 (0)