Skip to content

Conversation

@Shinonn23
Copy link
Contributor

@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, allowing invalid HIR to reach type checking and trigger 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 is now handled consistently with other invalid pattern expressions.

A new UI test is included to ensure the compiler reports a proper error 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

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from dd08ea6 to b713916 Compare December 5, 2025 05:38
@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 2 times, most recently from 85231c0 to 37c37ed Compare December 5, 2025 11:12
@rust-log-analyzer

This comment has been minimized.

Comment on lines -680 to -682
hir::PatExprKind::ConstBlock(anon_const) => {
self.lower_inline_const(anon_const, expr.hir_id, expr.span)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good point. There's a handful of other references to inline const patterns in comments as well, e.g. in pattern lowering and checking code, as well as on the definition of thir::PatKind::ExpandedConstant.

Comment on lines -643 to -657
infcx.canonicalize_user_type_annotation(ty::UserType::new(ty::UserTypeKind::TypeOf(
def_id.to_def_id(),
ty::UserArgs { args, user_self_ty: None },
)))
};
let annotation =
CanonicalUserTypeAnnotation { user_ty: Box::new(annotation), span, inferred_ty: ty };
PatKind::AscribeUserType {
subpattern: pattern,
ascription: Ascription {
annotation,
// Note that we use `Contravariant` here. See the `variance` field documentation
// for details.
variance: ty::Contravariant,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

We might also be able to remove the code that uses the ascriptions this applied. I noticed a mention of inline const patterns in a span_bug! in the MIR type checker:

span_bug!(
span,
"bad inline const pattern: ({:?} = {:?}) {:?}",
const_ty,
inferred_ty,
terr
);

and after doing a little digging, I think the surrounding code is what uses those. It was added in #120390. Of course, I'd want someone more familiar with borrowck internals than myself to look at that. It might make more sense for a follow-up PR.

Copy link
Member

Choose a reason for hiding this comment

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

It might make more sense for a follow-up PR.

Yeah, I think it's fine to leave further cleanup to a later PR, to avoid scope-creeping this one.

@dianne
Copy link
Contributor

dianne commented Dec 21, 2025

@Shinonn23 The commit description still seems to be out of date. Could you fix it? I know this is nit-picky, but I'd like to to be correct before merging ^^

The patch itself looks good though! I think further cleanup and comment fixes can be left for followup work.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from e99a9e6 to fcdad41 Compare December 26, 2025 07:46
@rustbot

This comment has been minimized.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from fcdad41 to 1d08e9f Compare December 26, 2025 07:47
@rustbot

This comment has been minimized.

@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from 1d08e9f to f910a1d Compare December 26, 2025 07:48
This fixes the ICE reported by rejecting `const` blocks in
pattern position during AST lowering.

Previously, `ExprKind::ConstBlock` could reach HIR as `PatExprKind::ConstBlock`,
allowing invalid patterns to be type-checked and triggering an ICE.
This patch removes the lowering path for const blocks in patterns
and emits a proper diagnostic instead.

A new UI test is added to ensure the compiler reports a regular error
and to prevent regressions.
@Shinonn23 Shinonn23 force-pushed the fix-ice-constblock-148138 branch from f910a1d to a9442b4 Compare December 26, 2025 11:16
@dianne dianne removed the T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. label Dec 28, 2025
@dianne
Copy link
Contributor

dianne commented Dec 28, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 28, 2025

📌 Commit a9442b4 has been approved by dianne

It is now in the queue for this repository.

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 28, 2025
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Dec 28, 2025
…8, r=dianne

Fix ICE by rejecting const blocks in patterns during AST lowering (closes rust-lang#148138)

This PR fixes the ICE reported in rust-lang#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`, allowing invalid HIR to reach type checking and trigger 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 is now handled consistently with other invalid pattern expressions.

A new UI test is included to ensure the compiler reports a proper error and to prevent regressions.

Closes rust-lang#148138.
bors added a commit that referenced this pull request Dec 28, 2025
…uwer

Rollup of 8 pull requests

Successful merges:

 - #148321 (parser/lexer: bump to Unicode 17, use faster unicode-ident)
 - #149540 (std: sys: fs: uefi: Implement readdir)
 - #149582 (Implement `Duration::div_duration_{floor,ceil}`)
 - #149663 (Optimized implementation for uN::{gather,scatter}_bits)
 - #149667 (Fix ICE by rejecting const blocks in patterns during AST lowering (closes #148138))
 - #149947 (add several older crashtests)
 - #150011 (Add more `unbounded_sh[lr]` examples)
 - #150411 (refactor `destructure_const`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ef835a8 into rust-lang:main Dec 29, 2025
11 checks passed
@rustbot rustbot added this to the 1.94.0 milestone Dec 29, 2025
rust-timer added a commit that referenced this pull request Dec 29, 2025
Rollup merge of #149667 - Shinonn23:fix-ice-constblock-148138, r=dianne

Fix ICE by rejecting const blocks in patterns during AST lowering (closes #148138)

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`, allowing invalid HIR to reach type checking and trigger 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 is now handled consistently with other invalid pattern expressions.

A new UI test is included to ensure the compiler reports a proper error and to prevent regressions.

Closes #148138.
@Shinonn23 Shinonn23 deleted the fix-ice-constblock-148138 branch December 29, 2025 18:54
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jan 8, 2026
…8, r=dianne

Fix ICE by rejecting const blocks in patterns during AST lowering (closes rust-lang#148138)

This PR fixes the ICE reported in rust-lang#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`, allowing invalid HIR to reach type checking and trigger 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 is now handled consistently with other invalid pattern expressions.

A new UI test is included to ensure the compiler reports a proper error and to prevent regressions.

Closes rust-lang#148138.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 14, 2026
… r=lcnr

cleanup: remove borrowck handling for inline const patterns

rust-lang#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of rust-lang#138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of rust-lang#149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 14, 2026
… r=lcnr

cleanup: remove borrowck handling for inline const patterns

rust-lang#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of rust-lang#138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of rust-lang#149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 14, 2026
… r=lcnr

cleanup: remove borrowck handling for inline const patterns

rust-lang#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of rust-lang#138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of rust-lang#149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 14, 2026
… r=lcnr

cleanup: remove borrowck handling for inline const patterns

rust-lang#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of rust-lang#138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of rust-lang#149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
rust-timer added a commit that referenced this pull request Jan 15, 2026
Rollup merge of #150817 - cleanup-inline-const-pat-borrowck, r=lcnr

cleanup: remove borrowck handling for inline const patterns

#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of #138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of #149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jan 15, 2026
cleanup: remove borrowck handling for inline const patterns

rust-lang/rust#120390 added borrow-checking for inline const patterns; a type annotation was added to inline const patterns in the THIR to remember the `DefId` and args of the constants so they could be checked and constraints could be propagated to their parents. As of rust-lang/rust#138499 though, inline const patterns can't be borrow-checked due to a query cycle, and as of rust-lang/rust#149667, the type/`DefId`/args annotations on inline const patterns have been removed, so the borrowck code for them seems unused; this PR removes it.

In a hypothetical future where borrowck doesn't depend on exhaustiveness checking so `inline_const_pat` can be reinstated, I imagine we also won't be evaluating inline const patterns before borrowck. As such, we might be able to reuse the existing code for normal unevaluated inline const operands in [`TypeChecker::visit_operand`](https://github.com/rust-lang/rust/blob/32fe406b5e71afbb0d8b95280e50e67d1549224c/compiler/rustc_borrowck/src/type_check/mod.rs#L1720-L1749) (or at least we shouldn't need to encode inline const patterns' `DefId` and args in user type annotations if they appear directly in the MIR).

r? lcnr
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-clippy Relevant to the Clippy team. 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 adjust mode unimplemented for ConstBlock

10 participants