Skip to content

Commit a948229

Browse files
Simplify some cfging, fix error msg and comments
1 parent edcd005 commit a948229

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

compiler/rustc_middle/src/ty/consts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl<'tcx> Const<'tcx> {
135135
uv: ty::UnevaluatedConst<'tcx>,
136136
ty: Ty<'tcx>,
137137
) -> Const<'tcx> {
138+
tcx.debug_assert_args_compatible(uv.def, uv.args);
138139
Const::new(tcx, ty::ConstKind::Unevaluated(uv), ty)
139140
}
140141

compiler/rustc_middle/src/ty/context.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -2009,34 +2009,50 @@ impl<'tcx> TyCtxt<'tcx> {
20092009
true
20102010
}
20112011

2012-
pub fn assert_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) {
2013-
if !self.check_args_compatible(def_id, args) {
2014-
if let DefKind::AssocTy = self.def_kind(def_id)
2015-
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
2016-
{
2017-
bug!()
2018-
} else {
2019-
bug!(
2020-
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
2021-
self.def_path_str(def_id),
2022-
args,
2023-
ty::GenericArgs::identity_for_item(self, def_id)
2024-
);
2012+
/// With `cfg(debug_assertions)`, assert that args are compatible with their generics,
2013+
/// and print out the args if not.
2014+
#[cfg_attr(not(debug_assertions), allow(unused_variables))]
2015+
pub fn debug_assert_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) {
2016+
#[cfg(debug_assertions)]
2017+
{
2018+
if !self.check_args_compatible(def_id, args) {
2019+
if let DefKind::AssocTy = self.def_kind(def_id)
2020+
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
2021+
{
2022+
bug!(
2023+
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
2024+
self.def_path_str(def_id),
2025+
args,
2026+
// Make `[Self, GAT_ARGS...]` (this could be simplified)
2027+
self.mk_args_from_iter(
2028+
[self.types.self_param.into()].into_iter().chain(
2029+
self.generics_of(def_id)
2030+
.own_args(ty::GenericArgs::identity_for_item(self, def_id))
2031+
.iter()
2032+
.copied()
2033+
)
2034+
)
2035+
);
2036+
} else {
2037+
bug!(
2038+
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
2039+
self.def_path_str(def_id),
2040+
args,
2041+
ty::GenericArgs::identity_for_item(self, def_id)
2042+
);
2043+
}
20252044
}
20262045
}
20272046
}
20282047

20292048
#[inline(always)]
20302049
pub(crate) fn check_and_mk_args(
20312050
self,
2032-
_def_id: DefId,
2051+
def_id: DefId,
20332052
args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
20342053
) -> GenericArgsRef<'tcx> {
20352054
let args = self.mk_args_from_iter(args.into_iter().map(Into::into));
2036-
#[cfg(debug_assertions)]
2037-
{
2038-
self.assert_args_compatible(_def_id, args);
2039-
}
2055+
self.debug_assert_args_compatible(def_id, args);
20402056
args
20412057
}
20422058

compiler/rustc_middle/src/ty/sty.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1624,9 +1624,7 @@ impl<'tcx> Ty<'tcx> {
16241624

16251625
#[inline]
16261626
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
1627-
if cfg!(debug_assertions) {
1628-
tcx.assert_args_compatible(def.did(), args);
1629-
}
1627+
tcx.debug_assert_args_compatible(def.did(), args);
16301628
Ty::new(tcx, Adt(def, args))
16311629
}
16321630

@@ -1707,9 +1705,7 @@ impl<'tcx> Ty<'tcx> {
17071705
def_id: DefId,
17081706
closure_args: GenericArgsRef<'tcx>,
17091707
) -> Ty<'tcx> {
1710-
if cfg!(debug_assertions) {
1711-
tcx.assert_args_compatible(def_id, closure_args);
1712-
}
1708+
tcx.debug_assert_args_compatible(def_id, closure_args);
17131709
Ty::new(tcx, Closure(def_id, closure_args))
17141710
}
17151711

@@ -1719,9 +1715,7 @@ impl<'tcx> Ty<'tcx> {
17191715
def_id: DefId,
17201716
closure_args: GenericArgsRef<'tcx>,
17211717
) -> Ty<'tcx> {
1722-
if cfg!(debug_assertions) {
1723-
tcx.assert_args_compatible(def_id, closure_args);
1724-
}
1718+
tcx.debug_assert_args_compatible(def_id, closure_args);
17251719
Ty::new(tcx, CoroutineClosure(def_id, closure_args))
17261720
}
17271721

@@ -1731,9 +1725,7 @@ impl<'tcx> Ty<'tcx> {
17311725
def_id: DefId,
17321726
coroutine_args: GenericArgsRef<'tcx>,
17331727
) -> Ty<'tcx> {
1734-
if cfg!(debug_assertions) {
1735-
tcx.assert_args_compatible(def_id, coroutine_args);
1736-
}
1728+
tcx.debug_assert_args_compatible(def_id, coroutine_args);
17371729
Ty::new(tcx, Coroutine(def_id, coroutine_args))
17381730
}
17391731

0 commit comments

Comments
 (0)