Skip to content

Commit 5870e28

Browse files
authored
Rollup merge of rust-lang#109151 - compiler-errors:debug-assert-alias, r=WaffleLapkin
Assert def-kind is correct for alias types Make sure we're not constructing alias types for the wrong def-kind, at least for debug cases 😅
2 parents 49f1623 + cf6424e commit 5870e28

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

compiler/rustc_infer/src/infer/at.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ impl<'tcx> InferCtxt<'tcx> {
8484

8585
pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
8686
fn to_trace(
87-
tcx: TyCtxt<'tcx>,
8887
cause: &ObligationCause<'tcx>,
8988
a_is_expected: bool,
9089
a: Self,
@@ -233,7 +232,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
233232
where
234233
T: ToTrace<'tcx>,
235234
{
236-
let trace = ToTrace::to_trace(self.infcx.tcx, self.cause, a_is_expected, a, b);
235+
let trace = ToTrace::to_trace(self.cause, a_is_expected, a, b);
237236
Trace { at: self, trace, a_is_expected }
238237
}
239238
}
@@ -306,18 +305,17 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
306305

307306
impl<'tcx> ToTrace<'tcx> for ImplSubject<'tcx> {
308307
fn to_trace(
309-
tcx: TyCtxt<'tcx>,
310308
cause: &ObligationCause<'tcx>,
311309
a_is_expected: bool,
312310
a: Self,
313311
b: Self,
314312
) -> TypeTrace<'tcx> {
315313
match (a, b) {
316314
(ImplSubject::Trait(trait_ref_a), ImplSubject::Trait(trait_ref_b)) => {
317-
ToTrace::to_trace(tcx, cause, a_is_expected, trait_ref_a, trait_ref_b)
315+
ToTrace::to_trace(cause, a_is_expected, trait_ref_a, trait_ref_b)
318316
}
319317
(ImplSubject::Inherent(ty_a), ImplSubject::Inherent(ty_b)) => {
320-
ToTrace::to_trace(tcx, cause, a_is_expected, ty_a, ty_b)
318+
ToTrace::to_trace(cause, a_is_expected, ty_a, ty_b)
321319
}
322320
(ImplSubject::Trait(_), ImplSubject::Inherent(_))
323321
| (ImplSubject::Inherent(_), ImplSubject::Trait(_)) => {
@@ -329,7 +327,6 @@ impl<'tcx> ToTrace<'tcx> for ImplSubject<'tcx> {
329327

330328
impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
331329
fn to_trace(
332-
_: TyCtxt<'tcx>,
333330
cause: &ObligationCause<'tcx>,
334331
a_is_expected: bool,
335332
a: Self,
@@ -344,7 +341,6 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
344341

345342
impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
346343
fn to_trace(
347-
_: TyCtxt<'tcx>,
348344
cause: &ObligationCause<'tcx>,
349345
a_is_expected: bool,
350346
a: Self,
@@ -356,7 +352,6 @@ impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
356352

357353
impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
358354
fn to_trace(
359-
_: TyCtxt<'tcx>,
360355
cause: &ObligationCause<'tcx>,
361356
a_is_expected: bool,
362357
a: Self,
@@ -371,7 +366,6 @@ impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
371366

372367
impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
373368
fn to_trace(
374-
_: TyCtxt<'tcx>,
375369
cause: &ObligationCause<'tcx>,
376370
a_is_expected: bool,
377371
a: Self,
@@ -399,7 +393,6 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
399393

400394
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
401395
fn to_trace(
402-
_: TyCtxt<'tcx>,
403396
cause: &ObligationCause<'tcx>,
404397
a_is_expected: bool,
405398
a: Self,
@@ -411,7 +404,6 @@ impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
411404

412405
impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
413406
fn to_trace(
414-
_: TyCtxt<'tcx>,
415407
cause: &ObligationCause<'tcx>,
416408
a_is_expected: bool,
417409
a: Self,
@@ -426,7 +418,6 @@ impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
426418

427419
impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
428420
fn to_trace(
429-
_: TyCtxt<'tcx>,
430421
cause: &ObligationCause<'tcx>,
431422
a_is_expected: bool,
432423
a: Self,
@@ -441,24 +432,17 @@ impl<'tcx> ToTrace<'tcx> for ty::PolyTraitRef<'tcx> {
441432

442433
impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
443434
fn to_trace(
444-
tcx: TyCtxt<'tcx>,
445435
cause: &ObligationCause<'tcx>,
446436
a_is_expected: bool,
447437
a: Self,
448438
b: Self,
449439
) -> TypeTrace<'tcx> {
450-
let a_ty = tcx.mk_projection(a.def_id, a.substs);
451-
let b_ty = tcx.mk_projection(b.def_id, b.substs);
452-
TypeTrace {
453-
cause: cause.clone(),
454-
values: Terms(ExpectedFound::new(a_is_expected, a_ty.into(), b_ty.into())),
455-
}
440+
TypeTrace { cause: cause.clone(), values: Aliases(ExpectedFound::new(a_is_expected, a, b)) }
456441
}
457442
}
458443

459444
impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
460445
fn to_trace(
461-
_: TyCtxt<'tcx>,
462446
cause: &ObligationCause<'tcx>,
463447
a_is_expected: bool,
464448
a: Self,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
15681568
ValuePairs::TraitRefs(_) | ValuePairs::PolyTraitRefs(_) => {
15691569
(false, Mismatch::Fixed("trait"))
15701570
}
1571+
ValuePairs::Aliases(infer::ExpectedFound { expected, .. }) => {
1572+
(false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id)))
1573+
}
15711574
ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
15721575
};
15731576
let Some(vals) = self.values_str(values) else {
@@ -2124,6 +2127,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21242127
match values {
21252128
infer::Regions(exp_found) => self.expected_found_str(exp_found),
21262129
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
2130+
infer::Aliases(exp_found) => self.expected_found_str(exp_found),
21272131
infer::TraitRefs(exp_found) => {
21282132
let pretty_exp_found = ty::error::ExpectedFound {
21292133
expected: exp_found.expected.print_only_trait_path(),

compiler/rustc_infer/src/infer/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ pub struct InferCtxt<'tcx> {
338338
pub enum ValuePairs<'tcx> {
339339
Regions(ExpectedFound<ty::Region<'tcx>>),
340340
Terms(ExpectedFound<ty::Term<'tcx>>),
341+
Aliases(ExpectedFound<ty::AliasTy<'tcx>>),
341342
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
342343
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
343344
Sigs(ExpectedFound<ty::FnSig<'tcx>>),

compiler/rustc_middle/src/ty/context.rs

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use rustc_type_ir::WithCachedTypeInfo;
7171
use rustc_type_ir::{CollectAndApply, DynKind, Interner, TypeFlags};
7272

7373
use std::any::Any;
74+
use std::assert_matches::debug_assert_matches;
7475
use std::borrow::Borrow;
7576
use std::cmp::Ordering;
7677
use std::fmt;
@@ -2049,6 +2050,12 @@ impl<'tcx> TyCtxt<'tcx> {
20492050

20502051
#[inline]
20512052
pub fn mk_alias(self, kind: ty::AliasKind, alias_ty: ty::AliasTy<'tcx>) -> Ty<'tcx> {
2053+
debug_assert_matches!(
2054+
(kind, self.def_kind(alias_ty.def_id)),
2055+
(ty::Opaque, DefKind::OpaqueTy)
2056+
| (ty::Projection, DefKind::AssocTy)
2057+
| (ty::Opaque | ty::Projection, DefKind::ImplTraitPlaceholder)
2058+
);
20522059
self.mk_ty_from_kind(Alias(kind, alias_ty))
20532060
}
20542061

0 commit comments

Comments
 (0)