Skip to content

Commit 3458211

Browse files
committed
Auto merge of rust-lang#125076 - compiler-errors:alias-term, r=lcnr
Split out `ty::AliasTerm` from `ty::AliasTy` Splitting out `AliasTerm` (for use in project and normalizes goals) and `AliasTy` (for use in `ty::Alias`) r? lcnr
2 parents ab14f94 + fa84018 commit 3458211

File tree

73 files changed

+695
-458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+695
-458
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
10101010
clauses.iter().any(|pred| {
10111011
match pred.kind().skip_binder() {
10121012
ty::ClauseKind::Trait(data) if data.self_ty() == ty => {}
1013-
ty::ClauseKind::Projection(data) if data.projection_ty.self_ty() == ty => {}
1013+
ty::ClauseKind::Projection(data)
1014+
if data.projection_term.self_ty() == ty => {}
10141015
_ => return false,
10151016
}
10161017
tcx.any_free_region_meets(pred, |r| *r == ty::ReEarlyParam(region))

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ fn param_env_with_gat_bounds<'tcx>(
22062206
_ => predicates.push(
22072207
ty::Binder::bind_with_vars(
22082208
ty::ProjectionPredicate {
2209-
projection_ty: ty::AliasTy::new(tcx, trait_ty.def_id, rebased_args),
2209+
projection_term: ty::AliasTerm::new(tcx, trait_ty.def_id, rebased_args),
22102210
term: normalize_impl_ty.into(),
22112211
},
22122212
bound_vars,

Diff for: compiler/rustc_hir_analysis/src/check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ fn bounds_from_generic_predicates<'tcx>(
344344
let mut projections_str = vec![];
345345
for projection in &projections {
346346
let p = projection.skip_binder();
347-
let alias_ty = p.projection_ty;
348-
if bound == tcx.parent(alias_ty.def_id) && alias_ty.self_ty() == ty {
349-
let name = tcx.item_name(alias_ty.def_id);
347+
if bound == tcx.parent(p.projection_term.def_id)
348+
&& p.projection_term.self_ty() == ty
349+
{
350+
let name = tcx.item_name(p.projection_term.def_id);
350351
projections_str.push(format!("{} = {}", name, p.term));
351352
}
352353
}

Diff for: compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn associated_type_bounds<'tcx>(
4040
let bounds_from_parent = trait_predicates.predicates.iter().copied().filter(|(pred, _)| {
4141
match pred.kind().skip_binder() {
4242
ty::ClauseKind::Trait(tr) => tr.self_ty() == item_ty,
43-
ty::ClauseKind::Projection(proj) => proj.projection_ty.self_ty() == item_ty,
43+
ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty() == item_ty,
4444
ty::ClauseKind::TypeOutlives(outlives) => outlives.0 == item_ty,
4545
_ => false,
4646
}

Diff for: compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ pub(super) fn explicit_predicates_of<'tcx>(
446446
.copied()
447447
.filter(|(pred, _)| match pred.kind().skip_binder() {
448448
ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
449-
ty::ClauseKind::Projection(proj) => !is_assoc_item_ty(proj.projection_ty.self_ty()),
449+
ty::ClauseKind::Projection(proj) => {
450+
!is_assoc_item_ty(proj.projection_term.self_ty())
451+
}
450452
ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
451453
_ => true,
452454
})

Diff for: compiler/rustc_hir_analysis/src/constrained_generic_params.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn setup_constraining_predicates<'tcx>(
198198
// Special case: watch out for some kind of sneaky attempt
199199
// to project out an associated type defined by this very
200200
// trait.
201-
let unbound_trait_ref = projection.projection_ty.trait_ref(tcx);
201+
let unbound_trait_ref = projection.projection_term.trait_ref(tcx);
202202
if Some(unbound_trait_ref) == impl_trait_ref {
203203
continue;
204204
}
@@ -208,7 +208,7 @@ pub fn setup_constraining_predicates<'tcx>(
208208
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
209209
// Then the projection only applies if `T` is known, but it still
210210
// does not determine `U`.
211-
let inputs = parameters_for(tcx, projection.projection_ty, true);
211+
let inputs = parameters_for(tcx, projection.projection_term, true);
212212
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p));
213213
if !relies_only_on_inputs {
214214
continue;

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
327327
})
328328
.or_insert(binding.span);
329329

330-
let projection_ty = if let ty::AssocKind::Fn = assoc_kind {
330+
let projection_term = if let ty::AssocKind::Fn = assoc_kind {
331331
let mut emitted_bad_param_err = None;
332332
// If we have an method return type bound, then we need to instantiate
333333
// the method's early bound params with suitable late-bound params.
@@ -381,7 +381,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
381381
let output = if let ty::Alias(ty::Projection, alias_ty) = *output.skip_binder().kind()
382382
&& tcx.is_impl_trait_in_trait(alias_ty.def_id)
383383
{
384-
alias_ty
384+
alias_ty.into()
385385
} else {
386386
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit {
387387
span: binding.span,
@@ -422,10 +422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
422422
);
423423
debug!(?alias_args);
424424

425-
// Note that we're indeed also using `AliasTy` (alias *type*) for associated
426-
// *constants* to represent *const projections*. Alias *term* would be a more
427-
// appropriate name but alas.
428-
ty::AliasTy::new(tcx, assoc_item.def_id, alias_args)
425+
ty::AliasTerm::new(tcx, assoc_item.def_id, alias_args)
429426
});
430427

431428
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
@@ -462,7 +459,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
462459
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
463460
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
464461
let late_bound_in_projection_ty =
465-
tcx.collect_constrained_late_bound_regions(projection_ty);
462+
tcx.collect_constrained_late_bound_regions(projection_term);
466463
let late_bound_in_term =
467464
tcx.collect_referenced_late_bound_regions(trait_ref.rebind(term));
468465
debug!(?late_bound_in_projection_ty);
@@ -491,8 +488,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
491488

492489
bounds.push_projection_bound(
493490
tcx,
494-
projection_ty
495-
.map_bound(|projection_ty| ty::ProjectionPredicate { projection_ty, term }),
491+
projection_term.map_bound(|projection_term| ty::ProjectionPredicate {
492+
projection_term,
493+
term,
494+
}),
496495
binding.span,
497496
);
498497
}
@@ -502,6 +501,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
502501
// NOTE: If `only_self_bounds` is true, do NOT expand this associated type bound into
503502
// a trait predicate, since we only want to add predicates for the `Self` type.
504503
if !only_self_bounds.0 {
504+
let projection_ty = projection_term
505+
.map_bound(|projection_term| projection_term.expect_ty(self.tcx()));
505506
// Calling `skip_binder` is okay, because `lower_bounds` expects the `param_ty`
506507
// parameter to have a skipped binder.
507508
let param_ty = Ty::new_alias(tcx, ty::Projection, projection_ty.skip_binder());

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -626,25 +626,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
626626
let bound_predicate = pred.kind();
627627
match bound_predicate.skip_binder() {
628628
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
629-
let pred = bound_predicate.rebind(pred);
630629
// `<Foo as Iterator>::Item = String`.
631-
let projection_ty = pred.skip_binder().projection_ty;
630+
let projection_term = pred.projection_term;
631+
let quiet_projection_term =
632+
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
632633

633-
let args_with_infer_self = tcx.mk_args_from_iter(
634-
std::iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
635-
.chain(projection_ty.args.iter().skip(1)),
636-
);
637-
638-
let quiet_projection_ty =
639-
ty::AliasTy::new(tcx, projection_ty.def_id, args_with_infer_self);
640-
641-
let term = pred.skip_binder().term;
642-
643-
let obligation = format!("{projection_ty} = {term}");
644-
let quiet = format!("{quiet_projection_ty} = {term}");
634+
let term = pred.term;
635+
let obligation = format!("{projection_term} = {term}");
636+
let quiet = format!("{quiet_projection_term} = {term}");
645637

646-
bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
647-
Some((obligation, projection_ty.self_ty()))
638+
bound_span_label(projection_term.self_ty(), &obligation, &quiet);
639+
Some((obligation, projection_term.self_ty()))
648640
}
649641
ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
650642
let p = poly_trait_ref.trait_ref;

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
281281

282282
let existential_projections = projection_bounds.iter().map(|(bound, _)| {
283283
bound.map_bound(|mut b| {
284-
assert_eq!(b.projection_ty.self_ty(), dummy_self);
284+
assert_eq!(b.projection_term.self_ty(), dummy_self);
285285

286286
// Like for trait refs, verify that `dummy_self` did not leak inside default type
287287
// parameters.
288-
let references_self = b.projection_ty.args.iter().skip(1).any(|arg| {
288+
let references_self = b.projection_term.args.iter().skip(1).any(|arg| {
289289
if arg.walk().any(|arg| arg == dummy_self.into()) {
290290
return true;
291291
}
@@ -295,7 +295,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
295295
let guar = tcx
296296
.dcx()
297297
.span_delayed_bug(span, "trait object projection bounds reference `Self`");
298-
b.projection_ty = replace_dummy_self_with_error(tcx, b.projection_ty, guar);
298+
b.projection_term = replace_dummy_self_with_error(tcx, b.projection_term, guar);
299299
}
300300

301301
ty::ExistentialProjection::erase_self_ty(tcx, b)

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,20 @@ fn unconstrained_parent_impl_args<'tcx>(
258258
// unconstrained parameters.
259259
for (clause, _) in impl_generic_predicates.predicates.iter() {
260260
if let ty::ClauseKind::Projection(proj) = clause.kind().skip_binder() {
261-
let projection_ty = proj.projection_ty;
262-
let projected_ty = proj.term;
263-
264-
let unbound_trait_ref = projection_ty.trait_ref(tcx);
261+
let unbound_trait_ref = proj.projection_term.trait_ref(tcx);
265262
if Some(unbound_trait_ref) == impl_trait_ref {
266263
continue;
267264
}
268265

269-
unconstrained_parameters.extend(cgp::parameters_for(tcx, projection_ty, true));
266+
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.projection_term, true));
270267

271-
for param in cgp::parameters_for(tcx, projected_ty, false) {
268+
for param in cgp::parameters_for(tcx, proj.term, false) {
272269
if !unconstrained_parameters.contains(&param) {
273270
constrained_params.insert(param.0);
274271
}
275272
}
276273

277-
unconstrained_parameters.extend(cgp::parameters_for(tcx, projected_ty, true));
274+
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.term, true));
278275
}
279276
}
280277

@@ -495,11 +492,11 @@ fn check_specialization_on<'tcx>(
495492
.emit())
496493
}
497494
}
498-
ty::ClauseKind::Projection(ty::ProjectionPredicate { projection_ty, term }) => Err(tcx
495+
ty::ClauseKind::Projection(ty::ProjectionPredicate { projection_term, term }) => Err(tcx
499496
.dcx()
500497
.struct_span_err(
501498
span,
502-
format!("cannot specialize on associated type `{projection_ty} == {term}`",),
499+
format!("cannot specialize on associated type `{projection_term} == {term}`",),
503500
)
504501
.emit()),
505502
ty::ClauseKind::ConstArgHasType(..) => {

Diff for: compiler/rustc_hir_analysis/src/variance/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
167167
}
168168
}
169169
ty::ClauseKind::Projection(ty::ProjectionPredicate {
170-
projection_ty: ty::AliasTy { args, .. },
170+
projection_term: ty::AliasTerm { args, .. },
171171
term,
172172
}) => {
173173
for arg in &args[1..] {

Diff for: compiler/rustc_hir_typeck/src/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
575575
self.misc(span),
576576
self.param_env,
577577
ty::ProjectionPredicate {
578-
projection_ty: ty::AliasTy::new(
578+
projection_term: ty::AliasTerm::new(
579579
self.tcx,
580580
fn_once_output_def_id,
581581
[arg_ty.into(), fn_sig.inputs()[0].into(), const_param],

Diff for: compiler/rustc_hir_typeck/src/closure.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
415415
// many viable options, so pick the most restrictive.
416416
let trait_def_id = match bound_predicate.skip_binder() {
417417
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
418-
Some(data.projection_ty.trait_def_id(self.tcx))
418+
Some(data.projection_term.trait_def_id(self.tcx))
419419
}
420420
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => Some(data.def_id()),
421421
_ => None,
@@ -476,7 +476,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
476476
_ => return None,
477477
}
478478

479-
let arg_param_ty = projection.skip_binder().projection_ty.args.type_at(1);
479+
let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
480480
let arg_param_ty = self.resolve_vars_if_possible(arg_param_ty);
481481
debug!(?arg_param_ty);
482482

@@ -931,7 +931,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
931931
};
932932

933933
// Check that this is a projection from the `Future` trait.
934-
let trait_def_id = predicate.projection_ty.trait_def_id(self.tcx);
934+
let trait_def_id = predicate.projection_term.trait_def_id(self.tcx);
935935
let future_trait = self.tcx.require_lang_item(LangItem::Future, Some(cause_span));
936936
if trait_def_id != future_trait {
937937
debug!("deduce_future_output_from_projection: not a future");
@@ -941,11 +941,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
941941
// The `Future` trait has only one associated item, `Output`,
942942
// so check that this is what we see.
943943
let output_assoc_item = self.tcx.associated_item_def_ids(future_trait)[0];
944-
if output_assoc_item != predicate.projection_ty.def_id {
944+
if output_assoc_item != predicate.projection_term.def_id {
945945
span_bug!(
946946
cause_span,
947947
"projecting associated item `{:?}` from future, which is not Output `{:?}`",
948-
predicate.projection_ty.def_id,
948+
predicate.projection_term.def_id,
949949
output_assoc_item,
950950
);
951951
}

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3737
ty::ClauseKind::Trait(pred) => {
3838
(pred.trait_ref.args.to_vec(), Some(pred.self_ty().into()))
3939
}
40-
ty::ClauseKind::Projection(pred) => (pred.projection_ty.args.to_vec(), None),
40+
ty::ClauseKind::Projection(pred) => (pred.projection_term.args.to_vec(), None),
4141
ty::ClauseKind::ConstArgHasType(arg, ty) => (vec![ty.into(), arg.into()], None),
4242
ty::ClauseKind::ConstEvaluatable(e) => (vec![e.into()], None),
4343
_ => return false,

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3838
self.type_matches_expected_vid(expected_vid, data.self_ty())
3939
}
4040
ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
41-
self.type_matches_expected_vid(expected_vid, data.projection_ty.self_ty())
41+
self.type_matches_expected_vid(expected_vid, data.projection_term.self_ty())
4242
}
4343
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
4444
| ty::PredicateKind::Subtype(..)

Diff for: compiler/rustc_hir_typeck/src/method/suggest.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use std::borrow::Cow;
4747
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
4848
use super::{CandidateSource, MethodError, NoMatchData};
4949
use rustc_hir::intravisit::Visitor;
50-
use std::iter;
5150

5251
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5352
fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
@@ -173,7 +172,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
173172
}
174173
}
175174
}
176-
ty::Slice(..) | ty::Adt(..) | ty::Alias(ty::AliasKind::Opaque, _) => {
175+
ty::Slice(..) | ty::Adt(..) | ty::Alias(ty::Opaque, _) => {
177176
for unsatisfied in unsatisfied_predicates.iter() {
178177
if is_iterator_predicate(unsatisfied.0, self.tcx) {
179178
return true;
@@ -788,26 +787,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
788787
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
789788
let pred = bound_predicate.rebind(pred);
790789
// `<Foo as Iterator>::Item = String`.
791-
let projection_ty = pred.skip_binder().projection_ty;
792-
793-
let args_with_infer_self = tcx.mk_args_from_iter(
794-
iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
795-
.chain(projection_ty.args.iter().skip(1)),
796-
);
797-
798-
let quiet_projection_ty =
799-
ty::AliasTy::new(tcx, projection_ty.def_id, args_with_infer_self);
790+
let projection_term = pred.skip_binder().projection_term;
791+
let quiet_projection_term =
792+
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
800793

801794
let term = pred.skip_binder().term;
802795

803-
let obligation = format!("{projection_ty} = {term}");
796+
let obligation = format!("{projection_term} = {term}");
804797
let quiet = with_forced_trimmed_paths!(format!(
805798
"{} = {}",
806-
quiet_projection_ty, term
799+
quiet_projection_term, term
807800
));
808801

809-
bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
810-
Some((obligation, projection_ty.self_ty()))
802+
bound_span_label(projection_term.self_ty(), &obligation, &quiet);
803+
Some((obligation, projection_term.self_ty()))
811804
}
812805
ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
813806
let p = poly_trait_ref.trait_ref;

Diff for: compiler/rustc_infer/src/infer/at.rs

+14
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,20 @@ impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
431431
}
432432

433433
impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
434+
fn to_trace(
435+
cause: &ObligationCause<'tcx>,
436+
a_is_expected: bool,
437+
a: Self,
438+
b: Self,
439+
) -> TypeTrace<'tcx> {
440+
TypeTrace {
441+
cause: cause.clone(),
442+
values: Aliases(ExpectedFound::new(a_is_expected, a.into(), b.into())),
443+
}
444+
}
445+
}
446+
447+
impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
434448
fn to_trace(
435449
cause: &ObligationCause<'tcx>,
436450
a_is_expected: bool,

0 commit comments

Comments
 (0)