Skip to content

Commit 0a07bbd

Browse files
committed
lower methods with host effect param instead
1 parent 55c9d66 commit 0a07bbd

File tree

4 files changed

+19
-102
lines changed

4 files changed

+19
-102
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
9999
ParamMode::Optional,
100100
ParenthesizedGenericArgs::Err,
101101
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
102-
None,
103102
));
104103
let receiver = self.lower_expr(receiver);
105104
let args =

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ 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
345344
let (generics, (trait_ref, lowered_ty)) =
346345
self.lower_generics(ast_generics, Const::No, id, &itctx, |this| {
347346
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
@@ -576,30 +575,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
576575
// This is used to track which lifetimes have already been defined,
577576
// and which need to be replicated when lowering an async fn.
578577

579-
let generics = match parent_hir.node().expect_item().kind {
578+
let parent_item = parent_hir.node().expect_item();
579+
let constness = match parent_item.kind {
580580
hir::ItemKind::Impl(impl_) => {
581581
self.is_in_trait_impl = impl_.of_trait.is_some();
582-
&impl_.generics
582+
match impl_.constness {
583+
// TODO bad span
584+
hir::Constness::Const => Const::Yes(impl_.self_ty.span),
585+
hir::Constness::NotConst => Const::No,
586+
}
583587
}
584-
hir::ItemKind::Trait(_, _, generics, _, _) => generics,
588+
hir::ItemKind::Trait(_, _, _, _, _) => {
589+
parent_hir.attrs.get(parent_item.hir_id().local_id).iter().find(|attr| attr.has_name(sym::const_trait)).map_or(Const::No, |attr| Const::Yes(attr.span))
590+
},
585591
kind => {
586592
span_bug!(item.span, "assoc item has unexpected kind of parent: {}", kind.descr())
587593
}
588594
};
589595

590-
if self.tcx.features().effects {
591-
self.host_param_id = generics
592-
.params
593-
.iter()
594-
.find(|param| {
595-
matches!(param.kind, hir::GenericParamKind::Const { is_host_effect: true, .. })
596-
})
597-
.map(|param| param.def_id);
598-
}
599-
600596
match ctxt {
601-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
602-
AssocCtxt::Impl => hir::OwnerNode::ImplItem(self.lower_impl_item(item)),
597+
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item, constness)),
598+
AssocCtxt::Impl => hir::OwnerNode::ImplItem(self.lower_impl_item(item, constness)),
603599
}
604600
}
605601

@@ -726,7 +722,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
726722
}
727723
}
728724

729-
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
725+
fn lower_trait_item(&mut self, i: &AssocItem, trait_constness: Const) -> &'hir hir::TraitItem<'hir> {
730726
let hir_id = self.lower_node_id(i.id);
731727
self.lower_attrs(hir_id, &i.attrs);
732728
let trait_item_def_id = hir_id.expect_owner();
@@ -758,6 +754,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
758754
i.id,
759755
FnDeclKind::Trait,
760756
sig.header.coroutine_kind,
757+
trait_constness,
761758
);
762759
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)), false)
763760
}
@@ -775,6 +772,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
775772
i.id,
776773
FnDeclKind::Trait,
777774
sig.header.coroutine_kind,
775+
trait_constness,
778776
);
779777
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true)
780778
}
@@ -852,7 +850,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
852850
self.expr(span, hir::ExprKind::Err(guar))
853851
}
854852

855-
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
853+
fn lower_impl_item(&mut self, i: &AssocItem, impl_constness: Const) -> &'hir hir::ImplItem<'hir> {
856854
// Since `default impl` is not yet implemented, this is always true in impls.
857855
let has_value = true;
858856
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
@@ -887,6 +885,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
887885
i.id,
888886
if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
889887
sig.header.coroutine_kind,
888+
impl_constness,
890889
);
891890

892891
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1300,11 +1299,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13001299
id: NodeId,
13011300
kind: FnDeclKind,
13021301
coroutine_kind: Option<CoroutineKind>,
1302+
parent_constness: Const,
13031303
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
13041304
let header = self.lower_fn_header(sig.header);
13051305
// Don't pass along the user-provided constness of trait associated functions; we don't want to
13061306
// synthesize a host effect param for them. We reject `const` on them during AST validation.
1307-
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { Const::No };
1307+
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { parent_constness };
13081308
let itctx = ImplTraitContext::Universal;
13091309
let (generics, decl) = self.lower_generics(generics, constness, id, &itctx, |this| {
13101310
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,79 +2574,6 @@ struct GenericArgsCtor<'hir> {
25742574
}
25752575

25762576
impl<'hir> GenericArgsCtor<'hir> {
2577-
fn push_constness(
2578-
&mut self,
2579-
lcx: &mut LoweringContext<'_, 'hir>,
2580-
constness: ast::BoundConstness,
2581-
) {
2582-
if !lcx.tcx.features().effects {
2583-
return;
2584-
}
2585-
2586-
let (span, body) = match constness {
2587-
BoundConstness::Never => return,
2588-
BoundConstness::Always(span) => {
2589-
let span = lcx.lower_span(span);
2590-
2591-
let body = hir::ExprKind::Lit(
2592-
lcx.arena.alloc(hir::Lit { node: LitKind::Bool(false), span }),
2593-
);
2594-
2595-
(span, body)
2596-
}
2597-
BoundConstness::Maybe(span) => {
2598-
let span = lcx.lower_span(span);
2599-
2600-
let Some(host_param_id) = lcx.host_param_id else {
2601-
lcx.dcx().span_delayed_bug(
2602-
span,
2603-
"no host param id for call in const yet no errors reported",
2604-
);
2605-
return;
2606-
};
2607-
2608-
let hir_id = lcx.next_id();
2609-
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2610-
let body = hir::ExprKind::Path(hir::QPath::Resolved(
2611-
None,
2612-
lcx.arena.alloc(hir::Path {
2613-
span,
2614-
res,
2615-
segments: arena_vec![
2616-
lcx;
2617-
hir::PathSegment::new(
2618-
Ident { name: sym::host, span },
2619-
hir_id,
2620-
res
2621-
)
2622-
],
2623-
}),
2624-
));
2625-
2626-
(span, body)
2627-
}
2628-
};
2629-
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
2630-
2631-
let id = lcx.next_node_id();
2632-
let hir_id = lcx.next_id();
2633-
2634-
let def_id = lcx.create_def(
2635-
lcx.current_hir_id_owner.def_id,
2636-
id,
2637-
kw::Empty,
2638-
DefKind::AnonConst,
2639-
span,
2640-
);
2641-
2642-
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2643-
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2644-
value: hir::AnonConst { def_id, hir_id, body },
2645-
span,
2646-
is_desugared_from_effects: true,
2647-
}))
2648-
}
2649-
26502577
fn is_empty(&self) -> bool {
26512578
self.args.is_empty()
26522579
&& self.bindings.is_empty()

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7676
param_mode,
7777
parenthesized_generic_args,
7878
itctx,
79-
// if this is the last segment, add constness to the trait path
80-
if i == proj_start - 1 { constness } else { None },
8179
)
8280
},
8381
)),
@@ -124,7 +122,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
124122
param_mode,
125123
ParenthesizedGenericArgs::Err,
126124
itctx,
127-
None,
128125
));
129126
let qpath = hir::QPath::TypeRelative(ty, hir_segment);
130127

@@ -165,7 +162,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
165162
param_mode,
166163
ParenthesizedGenericArgs::Err,
167164
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
168-
None,
169165
)
170166
})),
171167
span: self.lower_span(p.span),
@@ -179,7 +175,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
179175
param_mode: ParamMode,
180176
parenthesized_generic_args: ParenthesizedGenericArgs,
181177
itctx: &ImplTraitContext,
182-
constness: Option<ast::BoundConstness>,
183178
) -> hir::PathSegment<'hir> {
184179
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment);
185180
let (mut generic_args, infer_args) = if let Some(generic_args) = segment.args.as_deref() {
@@ -239,10 +234,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
239234
)
240235
};
241236

242-
if let Some(constness) = constness {
243-
generic_args.push_constness(self, constness);
244-
}
245-
246237
let has_lifetimes =
247238
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
248239

0 commit comments

Comments
 (0)