Skip to content

Commit 51b627a

Browse files
committed
implement ~const Tr bounds under new desugaring
1 parent 7e1eb87 commit 51b627a

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
675675
args,
676676
trait_segment.infer_args,
677677
Some(self_ty),
678-
constness,
678+
// TODO: remove this!!!
679+
ty::BoundConstness::NotConst,
679680
);
680681

681682
let tcx = self.tcx();
@@ -688,7 +689,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
688689
);
689690

690691
debug!(?poly_trait_ref);
691-
bounds.push_trait_bound(tcx, poly_trait_ref, span, polarity);
692+
bounds.push_trait_bound(tcx, self.item_def_id(), poly_trait_ref, span, polarity, constness);
692693

693694
let mut dup_bindings = FxIndexMap::default();
694695
for binding in args.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
@@ -1293,7 +1293,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12931293
}
12941294

12951295
fn get_associated_item(self, id: DefIndex, sess: &'a Session) -> ty::AssocItem {
1296-
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some() || self.root.tables.is_effects_desugaring.get(self, id) {
1296+
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some()
1297+
|| self.root.tables.is_effects_desugaring.get(self, id)
1298+
{
12971299
kw::Empty
12981300
} else {
12991301
self.item_name(id)

0 commit comments

Comments
 (0)