|
3 | 3 |
|
4 | 4 | use rustc_hir::{def::DefKind, LangItem}; |
5 | 5 | use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt}; |
| 6 | +use rustc_span::sym; |
6 | 7 | use rustc_span::{def_id::DefId, Span}; |
7 | 8 |
|
8 | 9 | /// Collects together a list of type bounds. These lists of bounds occur in many places |
@@ -62,17 +63,34 @@ impl<'tcx> Bounds<'tcx> { |
62 | 63 | Some(tcx.expected_host_effect_param_for_body(defining_def_id)) |
63 | 64 | } |
64 | 65 |
|
65 | | - (_, ty::BoundConstness::NotConst) => None, |
| 66 | + (_, ty::BoundConstness::NotConst) => { |
| 67 | + tcx.has_attr(trait_ref.def_id(), sym::const_trait).then_some(tcx.consts.true_) |
| 68 | + } |
66 | 69 |
|
67 | 70 | // if the defining_def_id is a trait, we wire it differently than others by equating the effects. |
68 | 71 | ( |
69 | | - kind @ (DefKind::Trait | DefKind::Impl { of_trait: true }), |
| 72 | + kind @ (DefKind::Trait | DefKind::Impl { of_trait: true } | DefKind::AssocTy), |
70 | 73 | ty::BoundConstness::ConstIfConst, |
71 | 74 | ) => { |
72 | | - let trait_we_are_in = if let DefKind::Trait = kind { |
73 | | - ty::TraitRef::identity(tcx, defining_def_id) |
| 75 | + let parent_def_id = if kind == DefKind::AssocTy { |
| 76 | + let did = tcx.parent(defining_def_id); |
| 77 | + if !matches!( |
| 78 | + tcx.def_kind(did), |
| 79 | + DefKind::Trait | DefKind::Impl { of_trait: true } |
| 80 | + ) { |
| 81 | + tcx.dcx().span_delayed_bug(span, "invalid `~const` encountered"); |
| 82 | + return; |
| 83 | + } |
| 84 | + did |
74 | 85 | } else { |
75 | | - tcx.impl_trait_ref(defining_def_id).unwrap().instantiate_identity() |
| 86 | + defining_def_id |
| 87 | + }; |
| 88 | + let trait_we_are_in = match tcx.def_kind(parent_def_id) { |
| 89 | + DefKind::Trait => ty::TraitRef::identity(tcx, parent_def_id), |
| 90 | + DefKind::Impl { of_trait: true } => { |
| 91 | + tcx.impl_trait_ref(parent_def_id).unwrap().instantiate_identity() |
| 92 | + } |
| 93 | + _ => unreachable!(), |
76 | 94 | }; |
77 | 95 | // create a new projection type `<T as TraitForBound>::Effects` |
78 | 96 | let Some(assoc) = tcx.associated_type_for_effects(trait_ref.def_id()) else { |
|
0 commit comments