Skip to content

parser: fix parsing of trait bound polarity and for-binders #20417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions crates/parser/src/grammar/generic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ fn type_bound(p: &mut Parser<'_>) -> bool {
);
m.complete(p, USE_BOUND_GENERIC_ARGS);
}
T![?] if p.nth_at(1, T![for]) => {
// test question_for_type_trait_bound
// fn f<T>() where T: ?for<> Sized {}
p.bump_any();
types::for_type(p, false)
}
_ => {
if path_type_bound(p).is_err() {
m.abandon(p);
Expand Down Expand Up @@ -219,8 +213,13 @@ fn path_type_bound(p: &mut Parser<'_>) -> Result<(), ()> {
// test async_trait_bound
// fn async_foo(_: impl async Fn(&i32)) {}
p.eat(T![async]);
// test question_for_type_trait_bound
// fn f<T>() where T: for<> ?Sized {}
p.eat(T![?]);

// test_err invalid_question_for_type_trait_bound
// fn f<T>() where T: ?for<> Sized {}

if paths::is_use_path_start(p) {
types::path_type_bounds(p, false);
// test_err type_bounds_macro_call_recovery
Expand Down
6 changes: 6 additions & 0 deletions crates/parser/test_data/generated/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ mod err {
#[test]
fn impl_type() { run_and_expect_errors("test_data/parser/inline/err/impl_type.rs"); }
#[test]
fn invalid_question_for_type_trait_bound() {
run_and_expect_errors(
"test_data/parser/inline/err/invalid_question_for_type_trait_bound.rs",
);
}
#[test]
fn let_else_right_curly_brace() {
run_and_expect_errors("test_data/parser/inline/err/let_else_right_curly_brace.rs");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "f"
GENERIC_PARAM_LIST
L_ANGLE "<"
TYPE_PARAM
NAME
IDENT "T"
R_ANGLE ">"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
WHERE_CLAUSE
WHERE_KW "where"
WHITESPACE " "
WHERE_PRED
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "T"
COLON ":"
WHITESPACE " "
TYPE_BOUND_LIST
QUESTION "?"
WHERE_PRED
FOR_BINDER
FOR_KW "for"
GENERIC_PARAM_LIST
L_ANGLE "<"
R_ANGLE ">"
WHITESPACE " "
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "Sized"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
R_CURLY "}"
WHITESPACE "\n"
error 20: expected comma
error 31: expected colon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn f<T>() where T: ?for<> Sized {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ SOURCE_FILE
WHITESPACE " "
TYPE_BOUND_LIST
TYPE_BOUND
FOR_BINDER
FOR_KW "for"
GENERIC_PARAM_LIST
L_ANGLE "<"
R_ANGLE ">"
WHITESPACE " "
QUESTION "?"
FOR_TYPE
FOR_BINDER
FOR_KW "for"
GENERIC_PARAM_LIST
L_ANGLE "<"
R_ANGLE ">"
WHITESPACE " "
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "Sized"
PATH_TYPE
PATH
PATH_SEGMENT
NAME_REF
IDENT "Sized"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fn f<T>() where T: ?for<> Sized {}
fn f<T>() where T: for<> ?Sized {}
Loading