Skip to content

Commit c861ab1

Browse files
committed
lower methods with host effect param instead
1 parent 51b627a commit c861ab1

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
@@ -100,7 +100,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
100100
ParamMode::Optional,
101101
ParenthesizedGenericArgs::Err,
102102
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
103-
None,
104103
// Method calls can't have bound modifiers
105104
None,
106105
));

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ 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
342341
let (generics, (trait_ref, lowered_ty)) =
343342
self.lower_generics(ast_generics, Const::No, id, itctx, |this| {
344343
let modifiers = TraitBoundModifiers {
@@ -592,30 +591,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
592591
// This is used to track which lifetimes have already been defined,
593592
// and which need to be replicated when lowering an async fn.
594593

595-
let generics = match parent_hir.node().expect_item().kind {
594+
let parent_item = parent_hir.node().expect_item();
595+
let constness = match parent_item.kind {
596596
hir::ItemKind::Impl(impl_) => {
597597
self.is_in_trait_impl = impl_.of_trait.is_some();
598-
&impl_.generics
598+
match impl_.constness {
599+
// TODO bad span
600+
hir::Constness::Const => Const::Yes(impl_.self_ty.span),
601+
hir::Constness::NotConst => Const::No,
602+
}
599603
}
600-
hir::ItemKind::Trait(_, _, generics, _, _) => generics,
604+
hir::ItemKind::Trait(_, _, _, _, _) => {
605+
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))
606+
},
601607
kind => {
602608
span_bug!(item.span, "assoc item has unexpected kind of parent: {}", kind.descr())
603609
}
604610
};
605611

606-
if self.tcx.features().effects {
607-
self.host_param_id = generics
608-
.params
609-
.iter()
610-
.find(|param| {
611-
matches!(param.kind, hir::GenericParamKind::Const { is_host_effect: true, .. })
612-
})
613-
.map(|param| param.def_id);
614-
}
615-
616612
match ctxt {
617-
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
618-
AssocCtxt::Impl => hir::OwnerNode::ImplItem(self.lower_impl_item(item)),
613+
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item, constness)),
614+
AssocCtxt::Impl => hir::OwnerNode::ImplItem(self.lower_impl_item(item, constness)),
619615
}
620616
}
621617

@@ -745,7 +741,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
745741
}
746742
}
747743

748-
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
744+
fn lower_trait_item(&mut self, i: &AssocItem, trait_constness: Const) -> &'hir hir::TraitItem<'hir> {
749745
let hir_id = self.lower_node_id(i.id);
750746
self.lower_attrs(hir_id, &i.attrs);
751747
let trait_item_def_id = hir_id.expect_owner();
@@ -775,6 +771,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
775771
i.id,
776772
FnDeclKind::Trait,
777773
sig.header.coroutine_kind,
774+
trait_constness,
778775
);
779776
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)), false)
780777
}
@@ -792,6 +789,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
792789
i.id,
793790
FnDeclKind::Trait,
794791
sig.header.coroutine_kind,
792+
trait_constness,
795793
);
796794
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true)
797795
}
@@ -869,7 +867,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
869867
self.expr(span, hir::ExprKind::Err(guar))
870868
}
871869

872-
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
870+
fn lower_impl_item(&mut self, i: &AssocItem, impl_constness: Const) -> &'hir hir::ImplItem<'hir> {
873871
// Since `default impl` is not yet implemented, this is always true in impls.
874872
let has_value = true;
875873
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
@@ -904,6 +902,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
904902
i.id,
905903
if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
906904
sig.header.coroutine_kind,
905+
impl_constness,
907906
);
908907

909908
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1316,11 +1315,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13161315
id: NodeId,
13171316
kind: FnDeclKind,
13181317
coroutine_kind: Option<CoroutineKind>,
1318+
parent_constness: Const,
13191319
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
13201320
let header = self.lower_fn_header(sig.header);
13211321
// Don't pass along the user-provided constness of trait associated functions; we don't want to
13221322
// synthesize a host effect param for them. We reject `const` on them during AST validation.
1323-
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { Const::No };
1323+
let constness = if kind == FnDeclKind::Inherent { sig.header.constness } else { parent_constness };
13241324
let itctx = ImplTraitContext::Universal;
13251325
let (generics, decl) = self.lower_generics(generics, constness, id, itctx, |this| {
13261326
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
@@ -2545,79 +2545,6 @@ struct GenericArgsCtor<'hir> {
25452545
}
25462546

25472547
impl<'hir> GenericArgsCtor<'hir> {
2548-
fn push_constness(
2549-
&mut self,
2550-
lcx: &mut LoweringContext<'_, 'hir>,
2551-
constness: ast::BoundConstness,
2552-
) {
2553-
if !lcx.tcx.features().effects {
2554-
return;
2555-
}
2556-
2557-
let (span, body) = match constness {
2558-
BoundConstness::Never => return,
2559-
BoundConstness::Always(span) => {
2560-
let span = lcx.lower_span(span);
2561-
2562-
let body = hir::ExprKind::Lit(
2563-
lcx.arena.alloc(hir::Lit { node: LitKind::Bool(false), span }),
2564-
);
2565-
2566-
(span, body)
2567-
}
2568-
BoundConstness::Maybe(span) => {
2569-
let span = lcx.lower_span(span);
2570-
2571-
let Some(host_param_id) = lcx.host_param_id else {
2572-
lcx.dcx().span_delayed_bug(
2573-
span,
2574-
"no host param id for call in const yet no errors reported",
2575-
);
2576-
return;
2577-
};
2578-
2579-
let hir_id = lcx.next_id();
2580-
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2581-
let body = hir::ExprKind::Path(hir::QPath::Resolved(
2582-
None,
2583-
lcx.arena.alloc(hir::Path {
2584-
span,
2585-
res,
2586-
segments: arena_vec![
2587-
lcx;
2588-
hir::PathSegment::new(
2589-
Ident { name: sym::host, span },
2590-
hir_id,
2591-
res
2592-
)
2593-
],
2594-
}),
2595-
));
2596-
2597-
(span, body)
2598-
}
2599-
};
2600-
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
2601-
2602-
let id = lcx.next_node_id();
2603-
let hir_id = lcx.next_id();
2604-
2605-
let def_id = lcx.create_def(
2606-
lcx.current_hir_id_owner.def_id,
2607-
id,
2608-
kw::Empty,
2609-
DefKind::AnonConst,
2610-
span,
2611-
);
2612-
2613-
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2614-
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2615-
value: hir::AnonConst { def_id, hir_id, body },
2616-
span,
2617-
is_desugared_from_effects: true,
2618-
}))
2619-
}
2620-
26212548
fn is_empty(&self) -> bool {
26222549
self.args.is_empty()
26232550
&& 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
@@ -106,8 +106,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
106106
param_mode,
107107
parenthesized_generic_args,
108108
itctx,
109-
// if this is the last segment, add constness to the trait path
110-
if i == proj_start - 1 { modifiers.map(|m| m.constness) } else { None },
111109
bound_modifier_allowed_features.clone(),
112110
)
113111
},
@@ -164,7 +162,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
164162
ParenthesizedGenericArgs::Err,
165163
itctx,
166164
None,
167-
None,
168165
));
169166
let qpath = hir::QPath::TypeRelative(ty, hir_segment);
170167

@@ -207,7 +204,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
207204
ParenthesizedGenericArgs::Err,
208205
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
209206
None,
210-
None,
211207
)
212208
})),
213209
span: self.lower_span(p.span),
@@ -221,7 +217,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
221217
param_mode: ParamMode,
222218
parenthesized_generic_args: ParenthesizedGenericArgs,
223219
itctx: ImplTraitContext,
224-
constness: Option<ast::BoundConstness>,
225220
// Additional features ungated with a bound modifier like `async`.
226221
// This is passed down to the implicit associated type binding in
227222
// parenthesized bounds.
@@ -288,10 +283,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
288283
)
289284
};
290285

291-
if let Some(constness) = constness {
292-
generic_args.push_constness(self, constness);
293-
}
294-
295286
let has_lifetimes =
296287
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
297288

0 commit comments

Comments
 (0)