Skip to content

Commit

Permalink
Simplify some cfging, fix error msg and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Mar 30, 2024
1 parent edcd005 commit a948229
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<'tcx> Const<'tcx> {
uv: ty::UnevaluatedConst<'tcx>,
ty: Ty<'tcx>,
) -> Const<'tcx> {
tcx.debug_assert_args_compatible(uv.def, uv.args);
Const::new(tcx, ty::ConstKind::Unevaluated(uv), ty)
}

Expand Down
52 changes: 34 additions & 18 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,34 +2009,50 @@ impl<'tcx> TyCtxt<'tcx> {
true
}

pub fn assert_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) {
if !self.check_args_compatible(def_id, args) {
if let DefKind::AssocTy = self.def_kind(def_id)
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
{
bug!()
} else {
bug!(
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
self.def_path_str(def_id),
args,
ty::GenericArgs::identity_for_item(self, def_id)
);
/// With `cfg(debug_assertions)`, assert that args are compatible with their generics,
/// and print out the args if not.
#[cfg_attr(not(debug_assertions), allow(unused_variables))]
pub fn debug_assert_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) {
#[cfg(debug_assertions)]
{
if !self.check_args_compatible(def_id, args) {
if let DefKind::AssocTy = self.def_kind(def_id)
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
{
bug!(
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
self.def_path_str(def_id),
args,
// Make `[Self, GAT_ARGS...]` (this could be simplified)
self.mk_args_from_iter(
[self.types.self_param.into()].into_iter().chain(
self.generics_of(def_id)
.own_args(ty::GenericArgs::identity_for_item(self, def_id))
.iter()
.copied()
)
)
);
} else {
bug!(
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
self.def_path_str(def_id),
args,
ty::GenericArgs::identity_for_item(self, def_id)
);
}
}
}
}

#[inline(always)]
pub(crate) fn check_and_mk_args(
self,
_def_id: DefId,
def_id: DefId,
args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
) -> GenericArgsRef<'tcx> {
let args = self.mk_args_from_iter(args.into_iter().map(Into::into));
#[cfg(debug_assertions)]
{
self.assert_args_compatible(_def_id, args);
}
self.debug_assert_args_compatible(def_id, args);
args
}

Expand Down
16 changes: 4 additions & 12 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,9 +1624,7 @@ impl<'tcx> Ty<'tcx> {

#[inline]
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
if cfg!(debug_assertions) {
tcx.assert_args_compatible(def.did(), args);
}
tcx.debug_assert_args_compatible(def.did(), args);
Ty::new(tcx, Adt(def, args))
}

Expand Down Expand Up @@ -1707,9 +1705,7 @@ impl<'tcx> Ty<'tcx> {
def_id: DefId,
closure_args: GenericArgsRef<'tcx>,
) -> Ty<'tcx> {
if cfg!(debug_assertions) {
tcx.assert_args_compatible(def_id, closure_args);
}
tcx.debug_assert_args_compatible(def_id, closure_args);
Ty::new(tcx, Closure(def_id, closure_args))
}

Expand All @@ -1719,9 +1715,7 @@ impl<'tcx> Ty<'tcx> {
def_id: DefId,
closure_args: GenericArgsRef<'tcx>,
) -> Ty<'tcx> {
if cfg!(debug_assertions) {
tcx.assert_args_compatible(def_id, closure_args);
}
tcx.debug_assert_args_compatible(def_id, closure_args);
Ty::new(tcx, CoroutineClosure(def_id, closure_args))
}

Expand All @@ -1731,9 +1725,7 @@ impl<'tcx> Ty<'tcx> {
def_id: DefId,
coroutine_args: GenericArgsRef<'tcx>,
) -> Ty<'tcx> {
if cfg!(debug_assertions) {
tcx.assert_args_compatible(def_id, coroutine_args);
}
tcx.debug_assert_args_compatible(def_id, coroutine_args);
Ty::new(tcx, Coroutine(def_id, coroutine_args))
}

Expand Down

0 comments on commit a948229

Please sign in to comment.