Skip to content

Commit 7e1eb87

Browse files
committed
Add new effects desugaring
1 parent 0ecbd06 commit 7e1eb87

File tree

20 files changed

+271
-22
lines changed

20 files changed

+271
-22
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
338338
// lifetime to be added, but rather a reference to a
339339
// parent lifetime.
340340
let itctx = ImplTraitContext::Universal;
341+
// TODO we need to rip apart this infrastructure
341342
let (generics, (trait_ref, lowered_ty)) =
342-
self.lower_generics(ast_generics, *constness, id, itctx, |this| {
343+
self.lower_generics(ast_generics, Const::No, id, itctx, |this| {
343344
let modifiers = TraitBoundModifiers {
344-
constness: match *constness {
345-
Const::Yes(span) => BoundConstness::Maybe(span),
346-
Const::No => BoundConstness::Never,
347-
},
345+
constness: BoundConstness::Never,
348346
asyncness: BoundAsyncness::Normal,
349347
// we don't use this in bound lowering
350348
polarity: BoundPolarity::Positive,
@@ -379,6 +377,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
379377
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
380378
};
381379
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
380+
constness: self.lower_constness(*constness),
382381
unsafety: self.lower_unsafety(*unsafety),
383382
polarity,
384383
defaultness,
@@ -390,15 +389,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
390389
}))
391390
}
392391
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
393-
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
394-
let constness = attrs
395-
.unwrap_or(&[])
396-
.iter()
397-
.find(|x| x.has_name(sym::const_trait))
398-
.map_or(Const::No, |x| Const::Yes(x.span));
399392
let (generics, (unsafety, items, bounds)) = self.lower_generics(
400393
generics,
401-
constness,
394+
Const::No,
402395
id,
403396
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
404397
|this| {

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,7 @@ pub enum ItemKind<'hir> {
31683168

31693169
#[derive(Debug, Clone, Copy, HashStable_Generic)]
31703170
pub struct Impl<'hir> {
3171+
pub constness: Constness,
31713172
pub unsafety: Unsafety,
31723173
pub polarity: ImplPolarity,
31733174
pub defaultness: Defaultness,

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
533533
try_visit!(visitor.visit_enum_def(enum_definition, item.hir_id()));
534534
}
535535
ItemKind::Impl(Impl {
536+
constness: _,
536537
unsafety: _,
537538
defaultness: _,
538539
polarity: _,

compiler/rustc_hir/src/lang_items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ language_item_table! {
342342

343343
String, sym::String, string, Target::Struct, GenericRequirement::None;
344344
CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
345+
346+
EffectsRuntime, sym::EffectsRuntime, effects_runtime, Target::Struct, GenericRequirement::None;
347+
EffectsNoRuntime, sym::EffectsNoRuntime, effects_no_runtime, Target::Struct, GenericRequirement::None;
348+
EffectsMaybe, sym::EffectsMaybe, effects_maybe, Target::Struct, GenericRequirement::None;
349+
EffectsCompat, sym::EffectsCompat, effects_compat, Target::Trait, GenericRequirement::Exact(1);
345350
}
346351

347352
pub enum GenericRequirement {

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,10 +1987,10 @@ pub(super) fn check_type_bounds<'tcx>(
19871987
let infcx = tcx.infer_ctxt().build();
19881988
let ocx = ObligationCtxt::new(&infcx);
19891989

1990-
// A synthetic impl Trait for RPITIT desugaring has no HIR, which we currently use to get the
1991-
// span for an impl's associated type. Instead, for these, use the def_span for the synthesized
1992-
// associated type.
1993-
let impl_ty_span = if impl_ty.is_impl_trait_in_trait() {
1990+
// A synthetic impl Trait for RPITIT desugaring or assoc type for effects desugaring has no HIR,
1991+
// which we currently use to get the span for an impl's associated type. Instead, for these,
1992+
// use the def_span for the synthesized associated type.
1993+
let impl_ty_span = if impl_ty.is_impl_trait_in_trait() || impl_ty.is_effects_desugaring {
19941994
tcx.def_span(impl_ty_def_id)
19951995
} else {
19961996
match tcx.hir_node_by_def_id(impl_ty_def_id) {

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ pub(super) fn explicit_item_bounds(
104104
None => {}
105105
}
106106

107+
// effects desugared associated types have no bounds.
108+
if tcx.is_effects_desugared_assoc_ty(def_id.to_def_id()) {
109+
return ty::EarlyBinder::bind(&[]);
110+
}
111+
107112
let bounds = match tcx.hir_node_by_def_id(def_id) {
108113
hir::Node::TraitItem(hir::TraitItem {
109114
kind: hir::TraitItemKind::Type(bounds, _),

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
108108
None => {}
109109
}
110110

111+
if tcx.is_effects_desugared_assoc_ty(def_id.to_def_id()) {
112+
let mut predicates = Vec::new();
113+
114+
// Inherit predicates of parent (impl or trait)
115+
// TODO do we need to distinguish impl or trait?
116+
let parent = tcx.local_parent(def_id);
117+
118+
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));
121+
return ty::GenericPredicates {
122+
parent: Some(parent.to_def_id()),
123+
predicates: tcx.arena.alloc_from_iter(predicates),
124+
};
125+
}
126+
111127
let hir_id = tcx.local_def_id_to_hir_id(def_id);
112128
let node = tcx.hir_node(hir_id);
113129

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ impl<'a> State<'a> {
574574
self.print_struct(struct_def, generics, item.ident.name, item.span, true);
575575
}
576576
hir::ItemKind::Impl(&hir::Impl {
577+
constness,
577578
unsafety,
578579
polarity,
579580
defaultness,
@@ -588,6 +589,10 @@ impl<'a> State<'a> {
588589
self.print_unsafety(unsafety);
589590
self.word_nbsp("impl");
590591

592+
if let hir::Constness::Const = constness {
593+
self.word_nbsp("const");
594+
}
595+
591596
if !generics.params.is_empty() {
592597
self.print_generic_params(generics.params);
593598
self.space();

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,7 @@ impl<'tcx> Pick<'tcx> {
13591359
trait_item_def_id: _,
13601360
fn_has_self_parameter: _,
13611361
opt_rpitit_info: _,
1362+
is_effects_desugaring: _,
13621363
},
13631364
kind: _,
13641365
import_ids: _,

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ 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() {
1296+
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some() || self.root.tables.is_effects_desugaring.get(self, id) {
12971297
kw::Empty
12981298
} else {
12991299
self.item_name(id)
@@ -1316,6 +1316,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13161316
container,
13171317
fn_has_self_parameter: has_self,
13181318
opt_rpitit_info,
1319+
is_effects_desugaring: self.root.tables.is_effects_desugaring.get(self, id),
13191320
}
13201321
}
13211322

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ provide! { tcx, def_id, other, cdata,
288288
})
289289
}
290290

291+
associated_type_for_effects => {
292+
table
293+
}
291294
associated_types_for_impl_traits_in_associated_fn => { table_defaulted_array }
292295

293296
visibility => { cdata.get_visibility(def_id.index) }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14411441
for &def_id in associated_item_def_ids {
14421442
self.encode_info_for_assoc_item(def_id);
14431443
}
1444+
if let Some(assoc_def_id) = self.tcx.associated_type_for_effects(def_id) {
1445+
record!(self.tables.associated_type_for_effects[def_id] <- assoc_def_id);
1446+
}
14441447
}
14451448
if def_kind == DefKind::Closure
14461449
&& let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id)
@@ -1613,6 +1616,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16131616
);
16141617
}
16151618
}
1619+
if item.is_effects_desugaring {
1620+
self.tables.is_effects_desugaring.set(def_id.index, true);
1621+
}
16161622
}
16171623

16181624
fn encode_mir(&mut self) {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ define_tables! {
390390
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
391391
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
392392
associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
393+
associated_type_for_effects: Table<DefIndex, Option<LazyValue<DefId>>>,
393394
opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
395+
is_effects_desugaring: Table<DefIndex, bool>,
394396
unused_generic_params: Table<DefIndex, UnusedGenericParams>,
395397
// Reexported names are not associated with individual `DefId`s,
396398
// e.g. a glob import can introduce a lot of names, all with the same `DefId`.

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,12 @@ rustc_queries! {
840840
separate_provide_extern
841841
}
842842

843+
query associated_type_for_effects(def_id: DefId) -> Option<DefId> {
844+
desc { |tcx| "creating associated items for effects in `{}`", tcx.def_path_str(def_id) }
845+
cache_on_disk_if { def_id.is_local() }
846+
separate_provide_extern
847+
}
848+
843849
/// Given an impl trait in trait `opaque_ty_def_id`, create and return the corresponding
844850
/// associated item.
845851
query associated_type_for_impl_trait_in_trait(opaque_ty_def_id: LocalDefId) -> LocalDefId {

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub struct AssocItem {
3333
/// return-position `impl Trait` in trait desugaring. The `ImplTraitInTraitData`
3434
/// provides additional information about its source.
3535
pub opt_rpitit_info: Option<ty::ImplTraitInTraitData>,
36+
37+
// TODO: is this necessary
38+
pub is_effects_desugaring: bool,
3639
}
3740

3841
impl AssocItem {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,9 +2288,9 @@ impl<'tcx> TyCtxt<'tcx> {
22882288
matches!(
22892289
node,
22902290
hir::Node::Item(hir::Item {
2291-
kind: hir::ItemKind::Impl(hir::Impl { generics, .. }),
2291+
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
22922292
..
2293-
}) if generics.params.iter().any(|p| matches!(p.kind, hir::GenericParamKind::Const { is_host_effect: true, .. }))
2293+
}) if matches!(constness, hir::Constness::Const)
22942294
)
22952295
}
22962296

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ impl<'tcx> TyCtxt<'tcx> {
15951595
}
15961596
}
15971597

1598-
/// If the def-id is an associated type that was desugared from a
1598+
/// If the `def_id`` is an associated type that was desugared from a
15991599
/// return-position `impl Trait` from a trait, then provide the source info
16001600
/// about where that RPITIT came from.
16011601
pub fn opt_rpitit_info(self, def_id: DefId) -> Option<ImplTraitInTraitData> {
@@ -1606,6 +1606,16 @@ impl<'tcx> TyCtxt<'tcx> {
16061606
}
16071607
}
16081608

1609+
/// Whether the `def_id` is an associated type that was desugared from a
1610+
/// `#[const_trait]` or `impl_const`.
1611+
pub fn is_effects_desugared_assoc_ty(self, def_id: DefId) -> bool {
1612+
if let DefKind::AssocTy = self.def_kind(def_id) {
1613+
self.associated_item(def_id).is_effects_desugaring
1614+
} else {
1615+
false
1616+
}
1617+
}
1618+
16091619
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<FieldIdx> {
16101620
variant.fields.iter_enumerated().find_map(|(i, field)| {
16111621
self.hygienic_eq(ident, field.ident(self), variant.def_id).then_some(i)

compiler/rustc_span/src/symbol.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ symbols! {
192192
Display,
193193
DoubleEndedIterator,
194194
Duration,
195+
EffectsCompat,
196+
EffectsMaybe,
197+
EffectsNoRuntime,
198+
EffectsRuntime,
199+
Effects__,
195200
Encodable,
196201
Encoder,
197202
Eq,

0 commit comments

Comments
 (0)