Skip to content

Commit 1dd00fc

Browse files
committed
Add new effects desugaring
1 parent b11fbfb commit 1dd00fc

File tree

20 files changed

+271
-24
lines changed

20 files changed

+271
-24
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
341341
// lifetime to be added, but rather a reference to a
342342
// parent lifetime.
343343
let itctx = ImplTraitContext::Universal;
344+
// TODO we need to rip apart this infrastructure
344345
let (generics, (trait_ref, lowered_ty)) =
345-
self.lower_generics(ast_generics, *constness, id, &itctx, |this| {
346-
let constness = match *constness {
347-
Const::Yes(span) => BoundConstness::Maybe(span),
348-
Const::No => BoundConstness::Never,
349-
};
350-
346+
self.lower_generics(ast_generics, Const::No, id, &itctx, |this| {
351347
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
352348
this.lower_trait_ref(
353-
constness,
349+
BoundConstness::Never,
354350
trait_ref,
355351
&ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
356352
)
@@ -377,6 +373,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
377373
ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
378374
};
379375
hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
376+
constness: self.lower_constness(*constness),
380377
unsafety: self.lower_unsafety(*unsafety),
381378
polarity,
382379
defaultness,
@@ -388,15 +385,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
388385
}))
389386
}
390387
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
391-
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
392-
let constness = attrs
393-
.unwrap_or(&[])
394-
.iter()
395-
.find(|x| x.has_name(sym::const_trait))
396-
.map_or(Const::No, |x| Const::Yes(x.span));
397388
let (generics, (unsafety, items, bounds)) = self.lower_generics(
398389
generics,
399-
constness,
390+
Const::No,
400391
id,
401392
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
402393
|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
@@ -516,6 +516,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
516516
visitor.visit_enum_def(enum_definition, item.hir_id())
517517
}
518518
ItemKind::Impl(Impl {
519+
constness: _,
519520
unsafety: _,
520521
defaultness: _,
521522
polarity: _,

compiler/rustc_hir/src/lang_items.rs

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

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

346351
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
@@ -2001,10 +2001,10 @@ pub(super) fn check_type_bounds<'tcx>(
20012001
let infcx = tcx.infer_ctxt().build();
20022002
let ocx = ObligationCtxt::new(&infcx);
20032003

2004-
// A synthetic impl Trait for RPITIT desugaring has no HIR, which we currently use to get the
2005-
// span for an impl's associated type. Instead, for these, use the def_span for the synthesized
2006-
// associated type.
2007-
let impl_ty_span = if impl_ty.is_impl_trait_in_trait() {
2004+
// A synthetic impl Trait for RPITIT desugaring or assoc type for effects desugaring has no HIR,
2005+
// which we currently use to get the span for an impl's associated type. Instead, for these,
2006+
// use the def_span for the synthesized associated type.
2007+
let impl_ty_span = if impl_ty.is_impl_trait_in_trait() || impl_ty.is_effects_desugaring {
20082008
tcx.def_span(impl_ty_def_id)
20092009
} else {
20102010
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
@@ -568,6 +568,7 @@ impl<'a> State<'a> {
568568
self.print_struct(struct_def, generics, item.ident.name, item.span, true);
569569
}
570570
hir::ItemKind::Impl(&hir::Impl {
571+
constness,
571572
unsafety,
572573
polarity,
573574
defaultness,
@@ -582,6 +583,10 @@ impl<'a> State<'a> {
582583
self.print_unsafety(unsafety);
583584
self.word_nbsp("impl");
584585

586+
if let hir::Constness::Const = constness {
587+
self.word_nbsp("const");
588+
}
589+
585590
if !generics.params.is_empty() {
586591
self.print_generic_params(generics.params);
587592
self.space();

compiler/rustc_hir_typeck/src/method/probe.rs

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

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ 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() {
1291+
let name = if self.root.tables.opt_rpitit_info.get(self, id).is_some() || self.root.tables.is_effects_desugaring.get(self, id) {
12921292
kw::Empty
12931293
} else {
12941294
self.item_name(id)
@@ -1311,6 +1311,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13111311
container,
13121312
fn_has_self_parameter: has_self,
13131313
opt_rpitit_info,
1314+
is_effects_desugaring: self.root.tables.is_effects_desugaring.get(self, id),
13141315
}
13151316
}
13161317

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ provide! { tcx, def_id, other, cdata,
278278
})
279279
}
280280

281+
associated_type_for_effects => {
282+
table
283+
}
281284
associated_types_for_impl_traits_in_associated_fn => { table_defaulted_array }
282285

283286
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
@@ -1447,6 +1447,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14471447
for &def_id in associated_item_def_ids {
14481448
self.encode_info_for_assoc_item(def_id);
14491449
}
1450+
if let Some(assoc_def_id) = self.tcx.associated_type_for_effects(def_id) {
1451+
record!(self.tables.associated_type_for_effects[def_id] <- assoc_def_id);
1452+
}
14501453
}
14511454
if def_kind == DefKind::Closure
14521455
&& let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id)
@@ -1606,6 +1609,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16061609
);
16071610
}
16081611
}
1612+
if item.is_effects_desugaring {
1613+
self.tables.is_effects_desugaring.set(def_id.index, true);
1614+
}
16091615
}
16101616

16111617
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
@@ -834,6 +834,12 @@ rustc_queries! {
834834
separate_provide_extern
835835
}
836836

837+
query associated_type_for_effects(def_id: DefId) -> Option<DefId> {
838+
desc { |tcx| "creating associated items for effects in `{}`", tcx.def_path_str(def_id) }
839+
cache_on_disk_if { def_id.is_local() }
840+
separate_provide_extern
841+
}
842+
837843
/// Given an impl trait in trait `opaque_ty_def_id`, create and return the corresponding
838844
/// associated item.
839845
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
@@ -2273,9 +2273,9 @@ impl<'tcx> TyCtxt<'tcx> {
22732273
matches!(
22742274
node,
22752275
hir::Node::Item(hir::Item {
2276-
kind: hir::ItemKind::Impl(hir::Impl { generics, .. }),
2276+
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
22772277
..
2278-
}) if generics.params.iter().any(|p| matches!(p.kind, hir::GenericParamKind::Const { is_host_effect: true, .. }))
2278+
}) if matches!(constness, hir::Constness::Const)
22792279
)
22802280
}
22812281

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ impl<'tcx> TyCtxt<'tcx> {
22322232
}
22332233
}
22342234

2235-
/// If the def-id is an associated type that was desugared from a
2235+
/// If the `def_id`` is an associated type that was desugared from a
22362236
/// return-position `impl Trait` from a trait, then provide the source info
22372237
/// about where that RPITIT came from.
22382238
pub fn opt_rpitit_info(self, def_id: DefId) -> Option<ImplTraitInTraitData> {
@@ -2243,6 +2243,16 @@ impl<'tcx> TyCtxt<'tcx> {
22432243
}
22442244
}
22452245

2246+
/// Whether the `def_id` is an associated type that was desugared from a
2247+
/// `#[const_trait]` or `impl_const`.
2248+
pub fn is_effects_desugared_assoc_ty(self, def_id: DefId) -> bool {
2249+
if let DefKind::AssocTy = self.def_kind(def_id) {
2250+
self.associated_item(def_id).is_effects_desugaring
2251+
} else {
2252+
false
2253+
}
2254+
}
2255+
22462256
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<FieldIdx> {
22472257
variant.fields.iter_enumerated().find_map(|(i, field)| {
22482258
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
@@ -189,6 +189,11 @@ symbols! {
189189
Display,
190190
DoubleEndedIterator,
191191
Duration,
192+
EffectsCompat,
193+
EffectsMaybe,
194+
EffectsNoRuntime,
195+
EffectsRuntime,
196+
Effects__,
192197
Encodable,
193198
Encoder,
194199
Eq,

0 commit comments

Comments
 (0)