Skip to content

Commit e412043

Browse files
committed
Fix ICE: Disallow const blocks in patterns and remove ConstBlock from HIR
This fixes the ICE reported in #148138 by explicitly rejecting `const` blocks in patterns during AST lowering. Additionally, this commit performs a complete cleanup as suggested by reviewers: - Removed `PatExprKind::ConstBlock` from HIR as it is no longer reachable. - Removed `Pat::ConstBlockPat` and related logic from Rust Analyzer. - Removed special-casing for const blocks in patterns from the parser. - Removed dead code in `rustc_mir_build` and fixed Clippy adaptors.
1 parent 29e035e commit e412043

File tree

17 files changed

+754
-91
lines changed

17 files changed

+754
-91
lines changed

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
399399
ExprKind::Lit(lit) => {
400400
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: false }
401401
}
402-
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
403402
ExprKind::IncludedBytes(byte_sym) => hir::PatExprKind::Lit {
404403
lit: respan(span, LitKind::ByteStr(*byte_sym, StrStyle::Cooked)),
405404
negated: false,

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,6 @@ pub enum PatExprKind<'hir> {
19211921
// once instead of matching on unop neg expressions everywhere.
19221922
negated: bool,
19231923
},
1924-
ConstBlock(ConstBlock),
19251924
/// A path pattern for a unit struct/variant or a (maybe-associated) constant.
19261925
Path(QPath<'hir>),
19271926
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>)
789789
try_visit!(visitor.visit_id(*hir_id));
790790
match kind {
791791
PatExprKind::Lit { lit, negated } => visitor.visit_lit(*hir_id, *lit, *negated),
792-
PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
793792
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, *span),
794793
}
795794
}

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,6 @@ impl<'a> State<'a> {
18761876
}
18771877
self.print_literal(lit);
18781878
}
1879-
hir::PatExprKind::ConstBlock(c) => self.print_inline_const(c),
18801879
hir::PatExprKind::Path(qpath) => self.print_qpath(qpath, true),
18811880
}
18821881
}

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
925925
}
926926
ty
927927
}
928-
rustc_hir::PatExprKind::ConstBlock(c) => {
929-
self.check_expr_const_block(c, Expectation::NoExpectation)
930-
}
931928
rustc_hir::PatExprKind::Path(qpath) => {
932929
let (res, opt_ty, segments) =
933930
self.resolve_ty_and_res_fully_qualified_call(qpath, lt.hir_id, lt.span);

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
1313
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1414
use rustc_hir::{self as hir, ByRef, LangItem, Mutability, Pinnedness, RangeEnd};
1515
use rustc_index::Idx;
16-
use rustc_infer::infer::TyCtxtInferExt;
1716
use rustc_middle::mir::interpret::LitToConstInput;
1817
use rustc_middle::thir::{
1918
Ascription, FieldPat, LocalVarId, Pat, PatKind, PatRange, PatRangeBoundary,
2019
};
2120
use rustc_middle::ty::adjustment::{PatAdjust, PatAdjustment};
2221
use rustc_middle::ty::layout::IntegerExt;
23-
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypingMode};
22+
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt};
2423
use rustc_middle::{bug, span_bug};
2524
use rustc_span::def_id::DefId;
2625
use rustc_span::{ErrorGuaranteed, Span};
@@ -621,51 +620,6 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
621620
pattern
622621
}
623622

624-
/// Lowers an inline const block (e.g. `const { 1 + 1 }`) to a pattern.
625-
fn lower_inline_const(
626-
&mut self,
627-
block: &'tcx hir::ConstBlock,
628-
id: hir::HirId,
629-
span: Span,
630-
) -> PatKind<'tcx> {
631-
let tcx = self.tcx;
632-
let def_id = block.def_id;
633-
let ty = tcx.typeck(def_id).node_type(block.hir_id);
634-
635-
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id());
636-
let parent_args = ty::GenericArgs::identity_for_item(tcx, typeck_root_def_id);
637-
let args = ty::InlineConstArgs::new(tcx, ty::InlineConstArgsParts { parent_args, ty }).args;
638-
639-
let ct = ty::UnevaluatedConst { def: def_id.to_def_id(), args };
640-
let c = ty::Const::new_unevaluated(self.tcx, ct);
641-
let pattern = self.const_to_pat(c, ty, id, span);
642-
643-
// Apply a type ascription for the inline constant.
644-
let annotation = {
645-
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
646-
let args = ty::InlineConstArgs::new(
647-
tcx,
648-
ty::InlineConstArgsParts { parent_args, ty: infcx.next_ty_var(span) },
649-
)
650-
.args;
651-
infcx.canonicalize_user_type_annotation(ty::UserType::new(ty::UserTypeKind::TypeOf(
652-
def_id.to_def_id(),
653-
ty::UserArgs { args, user_self_ty: None },
654-
)))
655-
};
656-
let annotation =
657-
CanonicalUserTypeAnnotation { user_ty: Box::new(annotation), span, inferred_ty: ty };
658-
PatKind::AscribeUserType {
659-
subpattern: pattern,
660-
ascription: Ascription {
661-
annotation,
662-
// Note that we use `Contravariant` here. See the `variance` field documentation
663-
// for details.
664-
variance: ty::Contravariant,
665-
},
666-
}
667-
}
668-
669623
/// Lowers the kinds of "expression" that can appear in a HIR pattern:
670624
/// - Paths (e.g. `FOO`, `foo::BAR`, `Option::None`)
671625
/// - Inline const blocks (e.g. `const { 1 + 1 }`)
@@ -677,9 +631,6 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
677631
) -> PatKind<'tcx> {
678632
match &expr.kind {
679633
hir::PatExprKind::Path(qpath) => self.lower_path(qpath, expr.hir_id, expr.span).kind,
680-
hir::PatExprKind::ConstBlock(anon_const) => {
681-
self.lower_inline_const(anon_const, expr.hir_id, expr.span)
682-
}
683634
hir::PatExprKind::Lit { lit, negated } => {
684635
// We handle byte string literal patterns by using the pattern's type instead of the
685636
// literal's type in `const_to_pat`: if the literal `b"..."` matches on a slice reference,

0 commit comments

Comments
 (0)