Skip to content

Conversation

@Shinonn23
Copy link

@Shinonn23 Shinonn23 commented Dec 5, 2025

This PR fixes the ICE reported in #148138

The root cause is that const blocks aren’t allowed in pattern position, but the AST lowering logic still attempted to create PatExprKind::ConstBlock. That allowed invalid HIR to reach type checking, which then hit a span_bug!.

Following the discussion in the issue, this patch removes the ConstBlock lowering path from lower_expr_within_pat.
Any ExprKind::ConstBlock inside a pattern now lowers to PatKind::Err, consistent with how other invalid pattern expressions are handled.

A new UI test is included to ensure the compiler emits a proper error for const blocks in patterns and to prevent regressions.

Closes #148138

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 5, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 5, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment on lines 8 to 9
#![feature(deref_patterns)]
//~^ WARN the feature `deref_patterns` is incomplete
Copy link
Contributor

Choose a reason for hiding this comment

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

If you use #![expect(...)], the WARN will not occur.

Suggested change
#![feature(deref_patterns)]
//~^ WARN the feature `deref_patterns` is incomplete
#![feature(deref_patterns)]
#![expect(incomplete_features)]

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, @reddevilmidzy! I've updated the test file to use #![expect(incomplete_features)] instead of //~^ WARN and force-pushed the changes.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from dd08ea6 to b713916 Compare December 5, 2025 05:38
ExprKind::Lit(lit) => {
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: false }
}
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Since they're caught here now, maybe it would make sense to remove the special-casing of const blocks in patterns from the parser?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks! I've removed PatExprKind::ConstBlock and cleaned up the parser, HIR, and tooling (including rust-analyzer) as suggested. Ready for review.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd actually not recommend changing rust-analyzer here. Could you remove the rust-analyzer changes from this PR and open a separate cleanup PR to the rust-analyzer repository instead (rust-lang/rust-analyzer)? rust-analyzer uses its own representations of Rust code, so it shouldn't be necessary to change it here, I don't think.

Copy link
Author

Choose a reason for hiding this comment

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

I've reverted the rust-analyzer changes in the latest push

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also not seeing the change to the parser you said you did. Are you writing this using an LLM? I'd strongly recommend writing contributions by hand and reviewing your own code and github interactions. If I'm effectively writing this PR by proxy, I can't approve it; it'd at least need another reviewer to sign off on it. I'd like to help you contribute if it's your own contribution, but machine-generated PRs and replies place extra stress on reviewers; we rely on contributors to self-review and understand the changes they're making. See rust-lang/compiler-team#893 for more information. Apologies if I've jumped to conclusions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for your patience as well, and sorry for any alarm I might have caused. ^^
It's perfectly fine if you need translation assistance. I would just recommend extra care in that case since LLMs do have significant shortcomings when it comes to communication.

Copy link
Contributor

Choose a reason for hiding this comment

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

And of course, if anything is unclear, I'd be happy to clarify or explain.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much, it my first time to contribute :) So, I have done clean up the parser

@dianne
Copy link
Contributor

dianne commented Dec 5, 2025

r? dianne

@rustbot rustbot assigned dianne and unassigned SparrowLii Dec 5, 2025
@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from b713916 to 8a0d87b Compare December 5, 2025 07:22
@rustbot
Copy link
Collaborator

rustbot commented Dec 5, 2025

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in match checking

cc @Nadrieril

@rustbot rustbot added T-clippy Relevant to the Clippy team. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Dec 5, 2025
@rustbot

This comment has been minimized.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from 8a0d87b to e412043 Compare December 5, 2025 07:25
@rustbot

This comment has been minimized.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from e412043 to 3d5438d Compare December 5, 2025 07:26
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

This should not need to touch rust-analyzer source

View changes since this review

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch 2 times, most recently from 5260fe6 to d217779 Compare December 5, 2025 07:54
@rust-log-analyzer

This comment has been minimized.

@jieyouxu jieyouxu closed this Dec 5, 2025
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 5, 2025
@jieyouxu jieyouxu reopened this Dec 5, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 5, 2025
@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from d217779 to 85231c0 Compare December 5, 2025 11:09
… HIR

This fixes the reported ICE by explicitly rejecting `const` blocks
in patterns during AST lowering.

Additionally, performs complete cleanup:
- Removed `PatExprKind::ConstBlock` from HIR.
- Removed `Pat::ConstBlockPat` from Rust Analyzer.
- Cleaned up Clippy and other tools.
@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from 85231c0 to 37c37ed Compare December 5, 2025 11:12
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
4 LL |         const { 1 + 7 } => {}
-    |               ^^^^^^^^^
+    |         ^^^^^ expected identifier, found keyword
6    |
-    = help: use a named `const`-item or an `if`-guard (`x if x == const { ... }`) instead
+ help: escape `const` to use it as an identifier
+    |
+ LL |         r#const { 1 + 7 } => {}
+    |         ++
8 
- error: aborting due to 1 previous error
+ error: expected `,`
+   --> $DIR/in-pat-recovery.rs:6:19
+    |
+ LL |         const { 1 + 7 } => {}
+    |         -----     ^
+    |         |
+    |         while parsing the fields for this pattern
10 
+ error[E0422]: cannot find struct, variant or union type `r#const` in this scope
+   --> $DIR/in-pat-recovery.rs:6:9
+    |
+ LL |         const { 1 + 7 } => {}
+    |         ^^^^^ not found in this scope
+ 
+ error: aborting due to 3 previous errors
+ 
+ For more information about this error, try `rustc --explain E0422`.
---
+   --> $DIR/in-pat-recovery.rs:6:9
+    |         ^^^^^ expected identifier, found keyword
+ help: escape `const` to use it as an identifier
+    |
+ LL |         r#const { 1 + 7 } => {}
+    |         ++
+ error: expected `,`
+   --> $DIR/in-pat-recovery.rs:6:19
+    |
+ LL |         const { 1 + 7 } => {}
+    |         -----     ^
+    |         |
+    |         while parsing the fields for this pattern
+ error[E0422]: cannot find struct, variant or union type `r#const` in this scope
+   --> $DIR/in-pat-recovery.rs:6:9
+    |
+ LL |         const { 1 + 7 } => {}
+    |         ^^^^^ not found in this scope
+ 
+ error: aborting due to 3 previous errors
+ 
+ For more information about this error, try `rustc --explain E0422`.
---
To only update this specific test, also pass `--test-args inline-const/in-pat-recovery.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/inline-const/in-pat-recovery.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/inline-const/in-pat-recovery" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: expected identifier, found keyword `const`
##[error]  --> /checkout/tests/ui/inline-const/in-pat-recovery.rs:6:9
   |
LL |         const { 1 + 7 } => {}
   |         ^^^^^ expected identifier, found keyword
   |
help: escape `const` to use it as an identifier
   |
LL |         r#const { 1 + 7 } => {}
   |         ++

error: expected `,`
##[error]  --> /checkout/tests/ui/inline-const/in-pat-recovery.rs:6:19
   |

Comment on lines -787 to -795
} else if self.check_inline_const(0) {
// Parse `const pat`
let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;

if let Some(re) = self.parse_range_end() {
self.parse_pat_range_begin_with(const_expr, re)?
} else {
PatKind::Expr(const_expr)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We still need to parse inline const blocks for error recovery purposes. Without this, we'll try parsing the const as an identifier. However, the cleanup we can do is removing the check in parse_const_block that the const block isn't from a pattern, since we'll reach an error in that case later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE adjust mode unimplemented for ConstBlock

8 participants