Skip to content

Commit 91b6b4e

Browse files
committed
Harden Ty constructors a bit in debug mode
`Ty::new` wasn't used anywhere outside this module `Ty::new_adt` shouldn't ever be used for anything but adts. This hasn't caught any bugs, but seems good to check anyway
1 parent 65d7296 commit 91b6b4e

File tree

1 file changed

+37
-2
lines changed
  • compiler/rustc_middle/src/ty

1 file changed

+37
-2
lines changed

compiler/rustc_middle/src/ty/sty.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'tcx> Ty<'tcx> {
401401
/// The more specific methods will often optimize their creation.
402402
#[allow(rustc::usage_of_ty_tykind)]
403403
#[inline]
404-
pub fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
404+
fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
405405
tcx.mk_ty_from_kind(st)
406406
}
407407

@@ -613,6 +613,41 @@ impl<'tcx> Ty<'tcx> {
613613
#[inline]
614614
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
615615
tcx.debug_assert_args_compatible(def.did(), args);
616+
if cfg!(debug_assertions) {
617+
match tcx.def_kind(def.did()) {
618+
DefKind::Struct | DefKind::Union | DefKind::Enum => {}
619+
DefKind::Mod
620+
| DefKind::Variant
621+
| DefKind::Trait
622+
| DefKind::TyAlias
623+
| DefKind::ForeignTy
624+
| DefKind::TraitAlias
625+
| DefKind::AssocTy
626+
| DefKind::TyParam
627+
| DefKind::Fn
628+
| DefKind::Const
629+
| DefKind::ConstParam
630+
| DefKind::Static { .. }
631+
| DefKind::Ctor(..)
632+
| DefKind::AssocFn
633+
| DefKind::AssocConst
634+
| DefKind::Macro(..)
635+
| DefKind::ExternCrate
636+
| DefKind::Use
637+
| DefKind::ForeignMod
638+
| DefKind::AnonConst
639+
| DefKind::InlineConst
640+
| DefKind::OpaqueTy
641+
| DefKind::Field
642+
| DefKind::LifetimeParam
643+
| DefKind::GlobalAsm
644+
| DefKind::Impl { .. }
645+
| DefKind::Closure
646+
| DefKind::SyntheticCoroutineBody => {
647+
bug!("not an adt: {def:?} ({:?})", tcx.def_kind(def.did()))
648+
}
649+
}
650+
}
616651
Ty::new(tcx, Adt(def, args))
617652
}
618653

@@ -772,7 +807,7 @@ impl<'tcx> Ty<'tcx> {
772807
}
773808
}
774809
});
775-
Ty::new(tcx, Adt(adt_def, args))
810+
Ty::new_adt(tcx, adt_def, args)
776811
}
777812

778813
#[inline]

0 commit comments

Comments
 (0)