prefer assert_not_erased over matching+unreachable

This commit is contained in:
Jana Dönszelmann
2026-05-04 15:37:16 +02:00
parent 8ca4e431ef
commit f0c1798a3a
9 changed files with 23 additions and 29 deletions
@@ -8,7 +8,7 @@
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::layout::{HasTypingEnv, TyAndLayout};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, MayBeErased, Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{bug, throw_inval};
use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
@@ -374,7 +374,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));
if cfg!(debug_assertions) {
match key.typing_env.typing_mode() {
match key.typing_env.typing_mode().assert_not_erased() {
ty::TypingMode::PostAnalysis => {}
ty::TypingMode::Coherence
| ty::TypingMode::Analysis { .. }
@@ -384,7 +384,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
)
}
ty::TypingMode::ErasedNotCoherence(MayBeErased) => todo!(),
}
// Make sure we format the instance even if we do not print it.
@@ -3,7 +3,7 @@
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId, ValTreeCreationError};
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::layout::{LayoutCx, TyAndLayout};
use rustc_middle::ty::{self, MayBeErased, Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::{bug, mir};
use rustc_span::DUMMY_SP;
use tracing::{debug, instrument, trace};
@@ -237,7 +237,7 @@ pub(crate) fn eval_to_valtree<'tcx>(
cid: GlobalId<'tcx>,
) -> EvalToValTreeResult<'tcx> {
if cfg!(debug_assertions) {
match typing_env.typing_mode() {
match typing_env.typing_mode().assert_not_erased() {
ty::TypingMode::PostAnalysis => {}
ty::TypingMode::Coherence
| ty::TypingMode::Analysis { .. }
@@ -247,7 +247,6 @@ pub(crate) fn eval_to_valtree<'tcx>(
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
)
}
ty::TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
}
let const_alloc = tcx.eval_to_allocation_raw(typing_env.as_query_input(cid))?;
@@ -9,8 +9,8 @@
LayoutOfHelpers, TyAndLayout,
};
use rustc_middle::ty::{
self, GenericArgsRef, MayBeErased, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv,
TypingMode, Variance,
self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingEnv, TypingMode,
Variance,
};
use rustc_middle::{bug, mir, span_bug};
use rustc_span::Span;
@@ -243,7 +243,7 @@ pub fn new(
// types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
// already been revealed, so we'd be able to at least partially observe the hidden types anyways.
if cfg!(debug_assertions) {
match typing_env.typing_mode() {
match typing_env.typing_mode().assert_not_erased() {
TypingMode::PostAnalysis => {}
TypingMode::Coherence
| TypingMode::Analysis { .. }
@@ -251,7 +251,6 @@ pub fn new(
| TypingMode::PostBorrowckAnalysis { .. } => {
bug!("Const eval should always happens in PostAnalysis mode.");
}
TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
}
@@ -7,7 +7,7 @@
use rustc_middle::mir::*;
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::util::IntTypeExt;
use rustc_middle::ty::{self, GenericArg, GenericArgsRef, MayBeErased, Ty, TyCtxt, Unnormalized};
use rustc_middle::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt, Unnormalized};
use rustc_middle::{bug, span_bug, traits};
use rustc_span::{DUMMY_SP, Spanned, dummy_spanned};
use tracing::{debug, instrument};
@@ -547,7 +547,7 @@ fn move_paths_for_fields(
let subpath = self.elaborator.field_subpath(variant_path, field_idx);
let tcx = self.tcx();
match self.elaborator.typing_env().typing_mode() {
match self.elaborator.typing_env().typing_mode().assert_not_erased() {
ty::TypingMode::PostAnalysis => {}
ty::TypingMode::Coherence
| ty::TypingMode::Analysis { .. }
@@ -555,7 +555,6 @@ fn move_paths_for_fields(
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
bug!()
}
ty::TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
let field_ty = field.ty(tcx, args);
@@ -24,7 +24,7 @@
use derive_where::derive_where;
use rustc_type_ir::inherent::*;
pub use rustc_type_ir::solve::*;
use rustc_type_ir::{self as ty, Interner, MayBeErased, TyVid, TypingMode};
use rustc_type_ir::{self as ty, Interner, TyVid, TypingMode};
use tracing::instrument;
pub use self::eval_ctxt::{
@@ -358,7 +358,11 @@ fn structurally_normalize_term(
}
fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
match self.typing_mode() {
match self
.typing_mode()
// Caller should handle erased mode
.assert_not_erased()
{
// Opaques are never rigid outside of analysis mode.
TypingMode::Coherence | TypingMode::PostAnalysis => false,
// During analysis, opaques are rigid unless they may be defined by
@@ -368,8 +372,6 @@ fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: non_rigid_opaques } => {
!def_id.as_local().is_some_and(|def_id| non_rigid_opaques.contains(&def_id))
}
// Caller should handle this variant
TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
}
}
@@ -8,8 +8,7 @@
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::solve::{FetchEligibleAssocItemResponse, RerunReason};
use rustc_type_ir::{
self as ty, FieldInfo, Interner, MayBeErased, NormalizesTo, PredicateKind, Unnormalized,
Upcast as _,
self as ty, FieldInfo, Interner, NormalizesTo, PredicateKind, Unnormalized, Upcast as _,
};
use tracing::instrument;
@@ -82,7 +82,7 @@ pub(super) fn normalize_opaque_type(
// During HIR typeck, opaque types start out as unconstrained
// inference variables. In borrowck we instead use the type
// computed in HIR typeck as the initial value.
match self.typing_mode() {
match self.typing_mode().assert_not_erased() {
TypingMode::Analysis { .. } => {}
TypingMode::Borrowck { .. } => {
let actual = cx
@@ -98,7 +98,6 @@ pub(super) fn normalize_opaque_type(
TypingMode::Coherence
| TypingMode::PostBorrowckAnalysis { .. }
| TypingMode::PostAnalysis => unreachable!(),
TypingMode::ErasedNotCoherence(MayBeErased) => todo!(),
}
}
@@ -12,8 +12,8 @@
use rustc_middle::span_bug;
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
use rustc_middle::ty::{
self, AliasTerm, MayBeErased, Term, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
TypeVisitable, TypeVisitableExt, TypingMode, Unnormalized,
self, AliasTerm, Term, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
TypeVisitableExt, TypingMode, Unnormalized,
};
use tracing::{debug, instrument};
@@ -138,14 +138,13 @@ pub(super) fn needs_normalization<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
// Opaques are treated as rigid outside of `TypingMode::PostAnalysis`,
// so we can ignore those.
match infcx.typing_mode_raw() {
match infcx.typing_mode_raw().assert_not_erased() {
// FIXME(#132279): We likely want to reveal opaques during post borrowck analysis
TypingMode::Coherence
| TypingMode::Analysis { .. }
| TypingMode::Borrowck { .. }
| TypingMode::PostBorrowckAnalysis { .. } => flags.remove(ty::TypeFlags::HAS_TY_OPAQUE),
TypingMode::PostAnalysis => {}
TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
value.has_type_flags(flags)
+3 -4
View File
@@ -6,8 +6,8 @@
use rustc_middle::query::Providers;
use rustc_middle::traits::{BuiltinImplSource, CodegenObligationError};
use rustc_middle::ty::{
self, ClosureKind, GenericArgsRef, Instance, MayBeErased, PseudoCanonicalInput, TyCtxt,
TypeVisitableExt, Unnormalized,
self, ClosureKind, GenericArgsRef, Instance, PseudoCanonicalInput, TyCtxt, TypeVisitableExt,
Unnormalized,
};
use rustc_span::sym;
use rustc_trait_selection::traits;
@@ -155,13 +155,12 @@ fn resolve_associated_item<'tcx>(
// and the obligation is monomorphic, otherwise passes such as
// transmute checking and polymorphic MIR optimizations could
// get a result which isn't correct for all monomorphizations.
match typing_env.typing_mode() {
match typing_env.typing_mode().assert_not_erased() {
ty::TypingMode::Coherence
| ty::TypingMode::Analysis { .. }
| ty::TypingMode::Borrowck { .. }
| ty::TypingMode::PostBorrowckAnalysis { .. } => false,
ty::TypingMode::PostAnalysis => !trait_ref.still_further_specializable(),
ty::TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
}
};
if !eligible {