Skip to content

fix ICE when a forced keyword follows a parameter pattern - #165906

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
programskillforverification:forced-kw-param-without-type
Nov 20, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
programskillforverification:forced-kw-param-without-type

Conversation

@programskillforverification

@programskillforverification programskillforverification commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

This is a resubmission of #165700 by lazerg.

Fixes #165690

r? fmease

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 13, 2026
@programskillforverification

Copy link
Copy Markdown
Contributor Author

@fmease I apply your suggestion in #165700, the below 4 tests has failed. It looks like that non_reserved_ident() exclude all keywords.

tests/ui/parser/macro/kw-in-item-pos-recovery-149692.stderr
--- a/tests/ui/parser/macro/kw-in-item-pos-recovery-149692.stderr
+++ b/tests/ui/parser/macro/kw-in-item-pos-recovery-149692.stderr
@@ -1,57 +1,29 @@
-error: expected identifier, found keyword `Self`
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:10:4
+error: expected one of `!` or `::`, found `(`
+  --> $DIR/kw-in-item-pos-recovery-149692.rs:10:8
    |
-LL | m!(Self());
-   |    ^^^^ expected identifier, found keyword
-
-error: missing `fn` or `struct` for function or struct definition
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:10:4
-   |
 LL |     ($id:item()) => {}
    |      -------- while parsing argument for this `item` macro fragment
 ...
 LL | m!(Self());
-   |    ^^^^
-   |
-help: if you meant to call a macro, try
-   |
-LL | m!(Self!());
-   |        +
+   |        ^ expected one of `!` or `::`
 
-error: expected identifier, found keyword `Self`
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:14:4
+error: expected one of `!` or `::`, found `{`
+  --> $DIR/kw-in-item-pos-recovery-149692.rs:14:8
    |
-LL | m!(Self{});
-   |    ^^^^ expected identifier, found keyword
-
-error: missing `enum` or `struct` for enum or struct definition
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:14:4
-   |
 LL |     ($id:item()) => {}
    |      -------- while parsing argument for this `item` macro fragment
 ...
 LL | m!(Self{});
-   |    ^^^^
+   |        ^ expected one of `!` or `::`
 
-error: expected identifier, found keyword `crate`
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:18:4
+error: expected one of `!` or `::`, found `(`
+  --> $DIR/kw-in-item-pos-recovery-149692.rs:18:9
    |
-LL | m!(crate());
-   |    ^^^^^ expected identifier, found keyword
-
-error: missing `fn` or `struct` for function or struct definition
-  --> $DIR/kw-in-item-pos-recovery-149692.rs:18:4
-   |
 LL |     ($id:item()) => {}
    |      -------- while parsing argument for this `item` macro fragment
 ...
 LL | m!(crate());
-   |    ^^^^^
-   |
-help: if you meant to call a macro, try
-   |
-LL | m!(crate!());
-   |         +
+   |         ^ expected one of `!` or `::`
 
-error: aborting due to 6 previous errors
+error: aborting due to 3 previous errors
 
tests/ui/parser/macro/kw-in-const-item-pos-recovery-149692.stderr
--- a/tests/ui/parser/macro/kw-in-const-item-pos-recovery-149692.stderr
+++ b/tests/ui/parser/macro/kw-in-const-item-pos-recovery-149692.stderr
@@ -1,22 +1,11 @@
-error: expected identifier, found keyword `Self`
-  --> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:10
+error: expected one of `!` or `::`, found `(`
+  --> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:14
    |
-LL | m!(const Self());
-   |          ^^^^ expected identifier, found keyword
-
-error: missing `fn` or `struct` for function or struct definition
-  --> $DIR/kw-in-const-item-pos-recovery-149692.rs:8:10
-   |
 LL |     (const $id:item()) => {}
    |            -------- while parsing argument for this `item` macro fragment
 ...
 LL | m!(const Self());
-   |          ^^^^
-   |
-help: if you meant to call a macro, try
-   |
-LL | m!(const Self!());
-   |              +
+   |              ^ expected one of `!` or `::`
 
-error: aborting due to 2 previous errors
+error: aborting due to 1 previous error
 
tests/ui/parser/macro/kw-in-item-pos-recovery-151238.stderr
--- a/tests/ui/parser/macro/kw-in-item-pos-recovery-151238.stderr
+++ b/tests/ui/parser/macro/kw-in-item-pos-recovery-151238.stderr
@@ -1,20 +1,8 @@
-error: expected identifier, found keyword `self`
-  --> $DIR/kw-in-item-pos-recovery-151238.rs:7:28
-   |
-LL |   trait MyTrait { fn bar(c self) }
-   |                            ^^^^ expected identifier, found keyword
-
 error: expected one of `:`, `@`, or `|`, found keyword `self`
   --> $DIR/kw-in-item-pos-recovery-151238.rs:7:28
    |
 LL |   trait MyTrait { fn bar(c self) }
    |                            ^^^^ expected one of `:`, `@`, or `|`
-   |
-help: declare the type after the parameter binding
-   |
-LL -   trait MyTrait { fn bar(c self) }
-LL +   trait MyTrait { fn bar(<identifier>: <type>) }
-   |
 
 error: expected one of `->`, `;`, `where`, or `{`, found `}`
   --> $DIR/kw-in-item-pos-recovery-151238.rs:7:34
@@ -24,5 +12,5 @@
    |                      |
    |                      while parsing this `fn`
 
-error: aborting due to 3 previous errors
+error: aborting due to 2 previous errors
 
tests/ui/parser/issues/issue-70549-resolve-after-recovered-self-ctor.stderr
--- a/tests/ui/parser/issues/issue-70549-resolve-after-recovered-self-ctor.stderr
+++ b/tests/ui/parser/issues/issue-70549-resolve-after-recovered-self-ctor.stderr
@@ -1,9 +1,3 @@
-error: expected identifier, found keyword `Self`
-  --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:4:17
-   |
-LL |     fn foo(&mur Self) {}
-   |                 ^^^^ expected identifier, found keyword
-
 error: expected one of `:`, `@`, or `|`, found keyword `Self`
   --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:4:17
    |
@@ -28,12 +22,6 @@
 LL +     fn bar(&mur Self) {}
    |
 
-error: expected identifier, found keyword `Self`
-  --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:8:25
-   |
-LL |     fn bar(&'static mur Self) {}
-   |                         ^^^^ expected identifier, found keyword
-
 error: expected one of `:`, `@`, or `|`, found keyword `Self`
   --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:8:25
    |
@@ -58,18 +46,5 @@
 LL +     fn baz(&mut Self @ _) {}
    |
 
-error[E0533]: expected unit struct, found self constructor `Self`
-  --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:4:17
-   |
-LL |     fn foo(&mur Self) {}
-   |                 ^^^^ not a unit struct
+error: aborting due to 4 previous errors
 
-error[E0533]: expected unit struct, found self constructor `Self`
-  --> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:8:25
-   |
-LL |     fn bar(&'static mur Self) {}
-   |                         ^^^^ not a unit struct
-
-error: aborting due to 8 previous errors
-
-For more information about this error, try `rustc --explain E0533`.

@rust-log-analyzer

This comment has been minimized.

@fmease fmease left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at the test failures and almost all of the diagnostic changes look fine or even like an improvement to me.

To put things into context, recover_missing_kw_before_item is meant to trigger in cases where the user wanted to write a struct, enum, fn, etc. but forgot to prefix the item with keyword struct, enum, fn, etc. respectively.

Well, you generally wouldn't call your struct/enum/fn a keyword since that's not allowed without an r#, right? Note that we do recover from that which is absolutely desired in cases like fn if() {} (here suggest adding r#). However, how likely is it that somebody forgot struct/enum/fn and tried to name the item a keyword (e.g., pub if {})? Incredibly unlikely. So I'm happy about us stopping to trigger in cases like const Self(); and item!(Self {});. You can't call a struct Self and you can't rewrite it to r#Self either.

PR #150590 actually made us stop recovering there but for some reason it was brought back in PR #151249.

Regarding parameter_without_type basically the thing applies: You wouldn't try to write a C-style parameter and name it a keyword ... except self (as found in one failing test), that I do consider legitimate.


So, simply remove the relevant //..~ ERROR annotations from the tests (most if not all tests are just regression tests for former ICEs btw) and re--bless the test expectations.

View changes since this review

@programskillforverification

programskillforverification commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author
tests/ui/parser/forced-keywords/param-without-type.stderr
--- a/tests/ui/parser/forced-keywords/param-without-type.stderr
+++ b/tests/ui/parser/forced-keywords/param-without-type.stderr
@@ -3,6 +3,12 @@
    |
 LL | fn foo(S k#self) {}
    |          ^^^^^^ expected one of `:`, `@`, or `|`
+   |
+help: declare the type after the parameter binding
+   |
+LL - fn foo(S k#self) {}
+LL + fn foo(<identifier>: <type>) {}
+   |
 
 error: expected one of `:`, `@`, or `|`, found `k#not_a_keyword`
   --> $DIR/param-without-type.rs:9:10
@@ -10,5 +16,13 @@
 LL | fn bar(S k#not_a_keyword) {}
    |          ^^^^^^^^^^^^^^^ expected one of `:`, `@`, or `|`
 
-error: aborting due to 2 previous errors
+error: `self` parameter is only allowed in associated functions
+  --> $DIR/param-without-type.rs:6:8
+   |
+LL | fn foo(S k#self) {}
+   |        ^ not semantically valid as function parameter
+   |
+   = note: associated functions are those in `impl` or `trait` definitions
 
+error: aborting due to 3 previous errors
+

k#self's ident.name is also self, so it hits the ident.name == kw::SelfLower branch too, which gives two extra outputs: the "declare the type" suggestion, and a "self parameter is only allowed in associated functions" error. @fmease We accept this or narrow the self exception to IdentKind::Normal?

@rust-log-analyzer

This comment has been minimized.

@fmease fmease left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k#self's ident.name is also self, so it hits the ident.name == kw::SelfLower branch too, which gives two extra outputs: the "declare the type" suggestion, and a "self parameter is only allowed in associated functions" error. @fmease We accept this or narrow the self exception to IdentKind::Normal?

Apologies for the delayed response. Since both self and k#self denote keyword self, it's correct to trigger the "declare the type" suggestion. After all, you can define a method like fn f(k#self); or fn f(k#self: Type);.

Regarding the extra "self parameter is only allowed in associated functions", that would be evidence for the fact that we've successfully recovered from fn f(Type k#self); interpreting it as fn f(k#self: Type);. If you don't want it in the test, consider wrapping the function in question in a trait def or impl block (unless it alters the original test intention).

View changes since this review

@programskillforverification
programskillforverification marked this pull request as ready for review November 17, 2026 13:52
@rustbot

rustbot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 17, 2026
@rust-log-analyzer

This comment has been minimized.

@programskillforverification

Copy link
Copy Markdown
Contributor Author

@bors squash

@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

🔨 4 commits were squashed into cbaa0d2.

@rust-bors
rust-bors Bot force-pushed the forced-kw-param-without-type branch from d6fe4aa to cbaa0d2 Compare November 17, 2026 14:37
@rust-log-analyzer

This comment has been minimized.

@programskillforverification

Copy link
Copy Markdown
Contributor Author

@bors squash

@rust-bors

This comment has been minimized.

* fix ICE when a forced keyword follows a parameter pattern
* Update compiler/rustc_parse/src/parser/diagnostics.rs

* fix the errors
* fix format error
* update stderr

Co-authored-by: Joseph Zhao <65984904+programskillforverification@users.noreply.github.com>
Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
@rust-bors

rust-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

🔨 5 commits were squashed into 421b1b2.

@rust-bors
rust-bors Bot force-pushed the forced-kw-param-without-type branch from 070afe1 to 421b1b2 Compare November 18, 2026 02:40

@fmease fmease left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! :) @bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 421b1b2 has been approved by fmease

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 19, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 20, 2026
…-kw-param-without-type, r=fmease

fix ICE when a forced keyword follows a parameter pattern

This is a resubmission of rust-lang#165700 by lazerg.

Fixes rust-lang#165690

r? fmease
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup of 21 pull requests

Successful merges:

 - #165707 (bootstrap: Make low priority the default)
 - #165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - #165934 (Avoid using Terminator:None in simplify_cfg)
 - #166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - #166069 (bootstrap: fix keep-stage-std)
 - #166117 (Remove ebadf handling from stdio)
 - #166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - #165094 (reflection: doc attributes from adts, fields, and variants)
 - #165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - #165694 (view-types: make sure view types are !Sized)
 - #165802 (Add CI workflow to publish backtrace after a push to main)
 - #165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - #165906 (fix ICE when a forced keyword follows a parameter pattern)
 - #166064 (Add const generic transmute min support)
 - #166136 (Move ub_checks() inside the const_eval_select branch.)
 - #166158 (bump some tools' `toml` dependency)
 - #166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - #166176 (compiletest: Move all code out of the crate root)
 - #166185 (simd-test: Fix cfg syntax in the test macro)
 - #166194 (std: Unstably constify `std::process::abort`)
 - #166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - #166150 (Reflection: refactor Type and TypeKind)
 - #166160 (refactor CTFE-specific intrinsics to different file)
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup of 20 pull requests

Successful merges:

 - #165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - #165934 (Avoid using Terminator:None in simplify_cfg)
 - #166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - #166069 (bootstrap: fix keep-stage-std)
 - #166117 (Remove ebadf handling from stdio)
 - #166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - #165094 (reflection: doc attributes from adts, fields, and variants)
 - #165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - #165694 (view-types: make sure view types are !Sized)
 - #165802 (Add CI workflow to publish backtrace after a push to main)
 - #165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - #165906 (fix ICE when a forced keyword follows a parameter pattern)
 - #166064 (Add const generic transmute min support)
 - #166136 (Move ub_checks() inside the const_eval_select branch.)
 - #166158 (bump some tools' `toml` dependency)
 - #166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - #166176 (compiletest: Move all code out of the crate root)
 - #166185 (simd-test: Fix cfg syntax in the test macro)
 - #166194 (std: Unstably constify `std::process::abort`)
 - #166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - #166150 (Reflection: refactor Type and TypeKind)
 - #166160 (refactor CTFE-specific intrinsics to different file)
@rust-bors
rust-bors Bot merged commit 944870b into rust-lang:main Nov 20, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 20, 2026
Rollup merge of #165906 - programskillforverification:forced-kw-param-without-type, r=fmease

fix ICE when a forced keyword follows a parameter pattern

This is a resubmission of #165700 by lazerg.

Fixes #165690

r? fmease
folkertdev pushed a commit to folkertdev/stdarch that referenced this pull request Nov 22, 2026
Rollup of 20 pull requests

Successful merges:

 - rust-lang/rust#165716 (Refactor split_block in check_pointers to avoid Terminator: None)
 - rust-lang/rust#165934 (Avoid using Terminator:None in simplify_cfg)
 - rust-lang/rust#166050 (Use attribute parser for lang attributes in `LateResolutionVisitor`)
 - rust-lang/rust#166069 (bootstrap: fix keep-stage-std)
 - rust-lang/rust#166117 (Remove ebadf handling from stdio)
 - rust-lang/rust#166119 (Avoid an ICE on re-exports of private imports without a DefId)
 - rust-lang/rust#165094 (reflection: doc attributes from adts, fields, and variants)
 - rust-lang/rust#165216 (Clearer diagnostics when `&FnOnce` was provided when `FnOnce` was expected)
 - rust-lang/rust#165694 (view-types: make sure view types are !Sized)
 - rust-lang/rust#165802 (Add CI workflow to publish backtrace after a push to main)
 - rust-lang/rust#165883 (Report a recursion limit error instead of delaying a bug in RPITIT collector)
 - rust-lang/rust#165906 (fix ICE when a forced keyword follows a parameter pattern)
 - rust-lang/rust#166064 (Add const generic transmute min support)
 - rust-lang/rust#166136 (Move ub_checks() inside the const_eval_select branch.)
 - rust-lang/rust#166158 (bump some tools' `toml` dependency)
 - rust-lang/rust#166166 (Don't recover if not allowed in `parse_seq_to_before_tokens`)
 - rust-lang/rust#166176 (compiletest: Move all code out of the crate root)
 - rust-lang/rust#166185 (simd-test: Fix cfg syntax in the test macro)
 - rust-lang/rust#166194 (std: Unstably constify `std::process::abort`)
 - rust-lang/rust#166210 (Avoid terminator: None in insert_direct_enum_check)

Failed merges:

 - rust-lang/rust#166150 (Reflection: refactor Type and TypeKind)
 - rust-lang/rust#166160 (refactor CTFE-specific intrinsics to different file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: called Result::unwrap() on an Err value, in parser/diagnostics.rs's parameter_without_type

4 participants