Skip to content

Commit 55c9d66

Browse files
committed
implement ~const Tr bounds under new desugaring
1 parent 1dd00fc commit 55c9d66

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
235235
speculative: bool,
236236
dup_bindings: &mut FxHashMap<DefId, Span>,
237237
path_span: Span,
238+
// TODO remove unused
238239
constness: ty::BoundConstness,
239240
only_self_bounds: OnlySelfBounds,
240241
polarity: ty::ImplPolarity,

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
734734
args,
735735
trait_segment.infer_args,
736736
Some(self_ty),
737-
constness,
737+
// RIP APART THIS!!!
738+
ty::BoundConstness::NotConst,
738739
);
739740

740741
let tcx = self.tcx();
@@ -749,7 +750,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
749750
);
750751

751752
debug!(?poly_trait_ref, ?assoc_bindings);
752-
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
753+
bounds.push_trait_bound(tcx, self.item_def_id(), poly_trait_ref, span, polarity, constness);
753754

754755
let mut dup_bindings = FxHashMap::default();
755756
for binding in &assoc_bindings {

compiler/rustc_hir_analysis/src/bounds.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustc_hir::LangItem;
55
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
6-
use rustc_span::Span;
6+
use rustc_span::{def_id::DefId, Span};
77

88
/// Collects together a list of type bounds. These lists of bounds occur in many places
99
/// in Rust's syntax:
@@ -40,19 +40,11 @@ impl<'tcx> Bounds<'tcx> {
4040
pub fn push_trait_bound(
4141
&mut self,
4242
tcx: TyCtxt<'tcx>,
43+
defining_def_id: DefId,
4344
trait_ref: ty::PolyTraitRef<'tcx>,
4445
span: Span,
4546
polarity: ty::ImplPolarity,
46-
) {
47-
self.push_trait_bound_inner(tcx, trait_ref, span, polarity);
48-
}
49-
50-
fn push_trait_bound_inner(
51-
&mut self,
52-
tcx: TyCtxt<'tcx>,
53-
trait_ref: ty::PolyTraitRef<'tcx>,
54-
span: Span,
55-
polarity: ty::ImplPolarity,
47+
constness: ty::BoundConstness,
5648
) {
5749
self.clauses.push((
5850
trait_ref
@@ -62,6 +54,28 @@ impl<'tcx> Bounds<'tcx> {
6254
.to_predicate(tcx),
6355
span,
6456
));
57+
// For `T: ~const Tr` or `T: const Tr`, we need to add an additional bound on the
58+
// associated type of `<T as Tr>` and make sure that the effect is compatible.
59+
if let Some(compat_val) = match constness {
60+
// TODO: do we need `T: const Trait` anymore?
61+
ty::BoundConstness::Const => Some(tcx.consts.false_),
62+
ty::BoundConstness::ConstIfConst => {
63+
Some(tcx.expected_host_effect_param_for_body(defining_def_id))
64+
}
65+
ty::BoundConstness::NotConst => None,
66+
} {
67+
// create a new projection type `<T as Tr>::Effects`
68+
let assoc = tcx.associated_type_for_effects(trait_ref.def_id()).unwrap();
69+
let self_ty = Ty::new_projection(tcx, assoc, trait_ref.skip_binder().args);
70+
// make `<T as Tr>::Effects: Compat<runtime>`
71+
let new_trait_ref = ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::EffectsCompat, Some(span)), [ty::GenericArg::from(self_ty), compat_val.into()]);
72+
self.clauses.push((
73+
trait_ref
74+
.rebind(new_trait_ref)
75+
.to_predicate(tcx),
76+
span,
77+
));
78+
}
6579
}
6680

6781
pub fn push_projection_bound(

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
116116
let parent = tcx.local_parent(def_id);
117117

118118
let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
119-
predicates
120-
.extend(tcx.explicit_predicates_of(parent).instantiate_own(tcx, identity_args));
119+
predicates.extend(tcx.explicit_predicates_of(parent).instantiate_own(tcx, identity_args));
121120
return ty::GenericPredicates {
122121
parent: Some(parent.to_def_id()),
123122
predicates: tcx.arena.alloc_from_iter(predicates),

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12881288
}
12891289

12901290
fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem {
1291-
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some() || self.root.tables.is_effects_desugaring.get(self, id) {
1291+
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some()
1292+
|| self.root.tables.is_effects_desugaring.get(self, id)
1293+
{
12921294
kw::Empty
12931295
} else {
12941296
self.item_name(id)

0 commit comments

Comments
 (0)