Skip to content

Commit 0932f89

Browse files
committed
Auto merge of #16588 - compiler-errors:async-and-const-bounds, r=Veykril
internal: Parse (nightly) `const` and `async` trait bounds Both of these bound modifiers were added recently: * `const` trait bounds: #119099 * `async` trait bounds: #120392 The latter will certainly will not do the right thing; namely, `async Fn` needs to be mapped to the `AsyncFn` trait. IDK how to do that, so advice would be appreciated, though perhaps we could land this first so the parser isn't complaining about these bounds?
2 parents 25d1267 + 36020bb commit 0932f89

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

crates/parser/src/grammar/generic_params.rs

+10
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ fn type_bound(p: &mut Parser<'_>) -> bool {
157157
p.bump_any();
158158
p.expect(T![const]);
159159
}
160+
// test const_trait_bound
161+
// const fn foo(_: impl const Trait) {}
162+
T![const] => {
163+
p.bump_any();
164+
}
165+
// test async_trait_bound
166+
// fn async_foo(_: impl async Fn(&i32)) {}
167+
T![async] => {
168+
p.bump_any();
169+
}
160170
_ => (),
161171
}
162172
if paths::is_use_path_start(p) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
SOURCE_FILE
2+
FN
3+
FN_KW "fn"
4+
WHITESPACE " "
5+
NAME
6+
IDENT "async_foo"
7+
PARAM_LIST
8+
L_PAREN "("
9+
PARAM
10+
WILDCARD_PAT
11+
UNDERSCORE "_"
12+
COLON ":"
13+
WHITESPACE " "
14+
IMPL_TRAIT_TYPE
15+
IMPL_KW "impl"
16+
WHITESPACE " "
17+
TYPE_BOUND_LIST
18+
TYPE_BOUND
19+
ASYNC_KW "async"
20+
WHITESPACE " "
21+
PATH_TYPE
22+
PATH
23+
PATH_SEGMENT
24+
NAME_REF
25+
IDENT "Fn"
26+
PARAM_LIST
27+
L_PAREN "("
28+
PARAM
29+
REF_TYPE
30+
AMP "&"
31+
PATH_TYPE
32+
PATH
33+
PATH_SEGMENT
34+
NAME_REF
35+
IDENT "i32"
36+
R_PAREN ")"
37+
R_PAREN ")"
38+
WHITESPACE " "
39+
BLOCK_EXPR
40+
STMT_LIST
41+
L_CURLY "{"
42+
R_CURLY "}"
43+
WHITESPACE "\n"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn async_foo(_: impl async Fn(&i32)) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
SOURCE_FILE
2+
FN
3+
CONST_KW "const"
4+
WHITESPACE " "
5+
FN_KW "fn"
6+
WHITESPACE " "
7+
NAME
8+
IDENT "foo"
9+
PARAM_LIST
10+
L_PAREN "("
11+
PARAM
12+
WILDCARD_PAT
13+
UNDERSCORE "_"
14+
COLON ":"
15+
WHITESPACE " "
16+
IMPL_TRAIT_TYPE
17+
IMPL_KW "impl"
18+
WHITESPACE " "
19+
TYPE_BOUND_LIST
20+
TYPE_BOUND
21+
CONST_KW "const"
22+
WHITESPACE " "
23+
PATH_TYPE
24+
PATH
25+
PATH_SEGMENT
26+
NAME_REF
27+
IDENT "Trait"
28+
R_PAREN ")"
29+
WHITESPACE " "
30+
BLOCK_EXPR
31+
STMT_LIST
32+
L_CURLY "{"
33+
R_CURLY "}"
34+
WHITESPACE "\n"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const fn foo(_: impl const Trait) {}

crates/syntax/rust.ungram

+1-1
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ TypeBoundList =
614614

615615
TypeBound =
616616
Lifetime
617-
| ('?' | '~' 'const')? Type
617+
| ('~' 'const' | 'const')? 'async'? '?'? Type
618618

619619
//************************//
620620
// Patterns //

crates/syntax/src/ast/generated/nodes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1410,9 +1410,10 @@ pub struct TypeBound {
14101410
}
14111411
impl TypeBound {
14121412
pub fn lifetime(&self) -> Option<Lifetime> { support::child(&self.syntax) }
1413-
pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) }
14141413
pub fn tilde_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![~]) }
14151414
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
1415+
pub fn async_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![async]) }
1416+
pub fn question_mark_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![?]) }
14161417
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
14171418
}
14181419

0 commit comments

Comments
 (0)