Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1c1ce2f

Browse files
committed
Add term to ExistentialProjection
Also prevent ICE when adding a const in associated const equality.
1 parent f396888 commit 1c1ce2f

File tree

35 files changed

+213
-71
lines changed

35 files changed

+213
-71
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
724724
gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
725725
gate_all!(inline_const, "inline-const is experimental");
726726
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
727+
gate_all!(associated_const_equality, "associated const equality is incomplete");
727728

728729
// All uses of `gate_all!` below this point were added in #65742,
729730
// and subsequently disabled (with the non-early gating readded).

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ fn push_debuginfo_type_name<'tcx>(
203203
let projection_bounds: SmallVec<[_; 4]> = trait_data
204204
.projection_bounds()
205205
.map(|bound| {
206-
let ExistentialProjection { item_def_id, ty, .. } = bound.skip_binder();
207-
(item_def_id, ty)
206+
let ExistentialProjection { item_def_id, term, .. } = bound.skip_binder();
207+
// FIXME(associated_const_equality): allow for consts here
208+
(item_def_id, term.ty().unwrap())
208209
})
209210
.collect();
210211

compiler/rustc_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ declare_features! (
288288
(active, asm_sym, "1.58.0", Some(72016), None),
289289
/// Allows the `may_unwind` option in inline assembly.
290290
(active, asm_unwind, "1.58.0", Some(72016), None),
291+
/// Allows users to enforce equality of associated constants `TraitImpl<AssocConst=3>`.
292+
(active, associated_const_equality, "1.58.0", Some(92827), None),
291293
/// Allows the user of associated type bounds.
292294
(active, associated_type_bounds, "1.34.0", Some(52662), None),
293295
/// Allows associated type defaults.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use rustc_middle::ty::error::TypeError;
6969
use rustc_middle::ty::{
7070
self,
7171
subst::{GenericArgKind, Subst, SubstsRef},
72-
Region, Term, Ty, TyCtxt, TypeFoldable,
72+
Region, Ty, TyCtxt, TypeFoldable,
7373
};
7474
use rustc_span::{sym, BytePos, DesugaringKind, MultiSpan, Pos, Span};
7575
use rustc_target::spec::abi;
@@ -1780,11 +1780,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17801780
{
17811781
if projection_predicate.projection_ty.item_def_id == item_def_id {
17821782
// We don't account for multiple `Future::Output = Ty` contraints.
1783-
match projection_predicate.term {
1784-
Term::Ty(ty) => return Some(ty),
1785-
// Can return None, but not sure if that makes sense?
1786-
Term::Const(_c) => todo!(),
1787-
}
1783+
return projection_predicate.term.ty();
17881784
}
17891785
}
17901786
}

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind};
44
use crate::ty::TyKind::*;
55
use crate::ty::{
66
ConstKind, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, InferTy,
7-
ProjectionTy, TyCtxt, TyS, TypeAndMut,
7+
ProjectionTy, Term, TyCtxt, TyS, TypeAndMut,
88
};
99

1010
use rustc_errors::{Applicability, DiagnosticBuilder};
@@ -105,8 +105,14 @@ impl<'tcx> TyS<'tcx> {
105105
ExistentialPredicate::Trait(ExistentialTraitRef { substs, .. }) => {
106106
substs.iter().all(generic_arg_is_suggestible)
107107
}
108-
ExistentialPredicate::Projection(ExistentialProjection { substs, ty, .. }) => {
109-
ty.is_suggestable() && substs.iter().all(generic_arg_is_suggestible)
108+
ExistentialPredicate::Projection(ExistentialProjection {
109+
substs, term, ..
110+
}) => {
111+
let term_is_suggestable = match term {
112+
Term::Ty(ty) => ty.is_suggestable(),
113+
Term::Const(c) => const_is_suggestable(c.val),
114+
};
115+
term_is_suggestable && substs.iter().all(generic_arg_is_suggestible)
110116
}
111117
_ => true,
112118
}),

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,10 @@ impl FlagComputation {
320320

321321
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
322322
self.add_substs(projection.substs);
323-
self.add_ty(projection.ty);
323+
match projection.term {
324+
ty::Term::Ty(ty) => self.add_ty(ty),
325+
ty::Term::Const(ct) => self.add_const(ct),
326+
}
324327
}
325328

326329
fn add_projection_ty(&mut self, projection_ty: ty::ProjectionTy<'_>) {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ pub struct CoercePredicate<'tcx> {
795795
}
796796
pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
797797

798-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
798+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, TyEncodable, TyDecodable)]
799799
#[derive(HashStable, TypeFoldable)]
800800
pub enum Term<'tcx> {
801801
Ty(Ty<'tcx>),

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -905,29 +905,27 @@ pub trait PrettyPrinter<'tcx>:
905905
}
906906

907907
for (assoc_item_def_id, term) in assoc_items {
908-
let ty = match term.skip_binder() {
909-
Term::Ty(ty) => ty,
910-
Term::Const(c) => {
911-
p!(print(c));
912-
continue;
913-
}
914-
};
915908
if !first {
916909
p!(", ");
917910
}
918911
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).ident));
919912

920-
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks
921-
match ty.kind() {
922-
ty::Projection(ty::ProjectionTy { item_def_id, .. })
923-
if Some(*item_def_id) == self.tcx().lang_items().generator_return() =>
924-
{
925-
p!("[async output]")
913+
match term.skip_binder() {
914+
Term::Ty(ty) => {
915+
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks
916+
if matches!(
917+
ty.kind(), ty::Projection(ty::ProjectionTy { item_def_id, .. })
918+
if Some(*item_def_id) == self.tcx().lang_items().generator_return()
919+
) {
920+
p!("[async output]")
921+
} else {
922+
p!(print(ty))
923+
}
926924
}
927-
_ => {
928-
p!(print(ty))
925+
Term::Const(c) => {
926+
p!(print(c));
929927
}
930-
}
928+
};
931929

932930
first = false;
933931
}
@@ -1031,7 +1029,11 @@ pub trait PrettyPrinter<'tcx>:
10311029
let mut projections = predicates.projection_bounds();
10321030
if let (Some(proj), None) = (projections.next(), projections.next()) {
10331031
let tys: Vec<_> = args.iter().map(|k| k.expect_ty()).collect();
1034-
p!(pretty_fn_sig(&tys, false, proj.skip_binder().ty));
1032+
p!(pretty_fn_sig(
1033+
&tys,
1034+
false,
1035+
proj.skip_binder().term.ty().expect("Return type was a const")
1036+
));
10351037
resugared = true;
10361038
}
10371039
}
@@ -2454,7 +2456,7 @@ define_print_and_forward_display! {
24542456

24552457
ty::ExistentialProjection<'tcx> {
24562458
let name = cx.tcx().associated_item(self.item_def_id).ident;
2457-
p!(write("{} = ", name), print(self.ty))
2459+
p!(write("{} = ", name), print(self.term))
24582460
}
24592461

24602462
ty::ExistentialPredicate<'tcx> {

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,19 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
291291
b.item_def_id,
292292
)))
293293
} else {
294-
let ty = relation.relate_with_variance(
294+
let term = relation.relate_with_variance(
295295
ty::Invariant,
296296
ty::VarianceDiagInfo::default(),
297-
a.ty,
298-
b.ty,
297+
a.term,
298+
b.term,
299299
)?;
300300
let substs = relation.relate_with_variance(
301301
ty::Invariant,
302302
ty::VarianceDiagInfo::default(),
303303
a.substs,
304304
b.substs,
305305
)?;
306-
Ok(ty::ExistentialProjection { item_def_id: a.item_def_id, substs, ty })
306+
Ok(ty::ExistentialProjection { item_def_id: a.item_def_id, substs, term })
307307
}
308308
}
309309
}

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> {
423423
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
424424
tcx.lift(self.substs).map(|substs| ty::ExistentialProjection {
425425
substs,
426-
ty: tcx.lift(self.ty).expect("type must lift when substs do"),
426+
term: tcx.lift(self.term).expect("type must lift when substs do"),
427427
item_def_id: self.item_def_id,
428428
})
429429
}

0 commit comments

Comments
 (0)