Skip to content

Commit 92f4c59

Browse files
committed
lower impl const to bind to host effect param
1 parent 4f7bb98 commit 92f4c59

File tree

24 files changed

+237
-138
lines changed

24 files changed

+237
-138
lines changed

compiler/rustc_ast/src/ast.rs

+10
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ pub enum TraitBoundModifier {
313313
MaybeConstMaybe,
314314
}
315315

316+
impl TraitBoundModifier {
317+
pub fn to_constness(self) -> Const {
318+
match self {
319+
// FIXME(effects) span
320+
Self::MaybeConst => Const::Yes(DUMMY_SP),
321+
_ => Const::No,
322+
}
323+
}
324+
}
325+
316326
/// The AST represents all type param bounds as types.
317327
/// `typeck::collect::compute_bounds` matches these against
318328
/// the "special" built-in traits (see `middle::lang_items`) and

compiler/rustc_ast_lowering/src/asm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
207207
&sym.path,
208208
ParamMode::Optional,
209209
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
210+
None,
210211
);
211212
hir::InlineAsmOperand::SymStatic { path, def_id }
212213
} else {

compiler/rustc_ast_lowering/src/expr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
100100
ParamMode::Optional,
101101
ParenthesizedGenericArgs::Err,
102102
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
103+
None,
103104
));
104105
let receiver = self.lower_expr(receiver);
105106
let args =
@@ -260,6 +261,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
260261
path,
261262
ParamMode::Optional,
262263
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
264+
None,
263265
);
264266
hir::ExprKind::Path(qpath)
265267
}
@@ -307,6 +309,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
307309
&se.path,
308310
ParamMode::Optional,
309311
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
312+
None,
310313
)),
311314
self.arena
312315
.alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
@@ -1179,6 +1182,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11791182
path,
11801183
ParamMode::Optional,
11811184
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1185+
None,
11821186
);
11831187
// Destructure like a tuple struct.
11841188
let tuple_struct_pat = hir::PatKind::TupleStruct(
@@ -1198,6 +1202,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11981202
path,
11991203
ParamMode::Optional,
12001204
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1205+
None,
12011206
);
12021207
// Destructure like a unit struct.
12031208
let unit_struct_pat = hir::PatKind::Path(qpath);
@@ -1222,6 +1227,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12221227
&se.path,
12231228
ParamMode::Optional,
12241229
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1230+
None,
12251231
);
12261232
let fields_omitted = match &se.rest {
12271233
StructRest::Base(e) => {

compiler/rustc_ast_lowering/src/item.rs

+59-25
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8585
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
8686
allow_gen_future: Some([sym::gen_future, sym::closure_track_caller][..].into()),
8787
generics_def_id_map: Default::default(),
88+
host_param_id: None,
8889
};
8990
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
9091

@@ -139,8 +140,24 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
139140
// This is used to track which lifetimes have already been defined,
140141
// and which need to be replicated when lowering an async fn.
141142

142-
if let hir::ItemKind::Impl(impl_) = parent_hir.node().expect_item().kind {
143-
lctx.is_in_trait_impl = impl_.of_trait.is_some();
143+
match parent_hir.node().expect_item().kind {
144+
hir::ItemKind::Impl(impl_) => {
145+
lctx.is_in_trait_impl = impl_.of_trait.is_some();
146+
}
147+
hir::ItemKind::Trait(_, _, generics, _, _) if lctx.tcx.features().effects => {
148+
lctx.host_param_id = generics
149+
.params
150+
.iter()
151+
.find(|param| {
152+
parent_hir
153+
.attrs
154+
.get(param.hir_id.local_id)
155+
.iter()
156+
.any(|attr| attr.has_name(sym::rustc_host))
157+
})
158+
.map(|param| param.def_id);
159+
}
160+
_ => {}
144161
}
145162

146163
match ctxt {
@@ -384,6 +401,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
384401
self.lower_generics(ast_generics, *constness, id, &itctx, |this| {
385402
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
386403
this.lower_trait_ref(
404+
*constness,
387405
trait_ref,
388406
&ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
389407
)
@@ -414,7 +432,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
414432
polarity,
415433
defaultness,
416434
defaultness_span,
417-
constness: self.lower_constness(*constness),
418435
generics,
419436
of_trait: trait_ref,
420437
self_ty: lowered_ty,
@@ -1358,6 +1375,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
13581375
}
13591376
}
13601377

1378+
// Desugar `~const` bound in generics into an additional `const host: bool` param
1379+
// if the effects feature is enabled. This needs to be done before we lower where
1380+
// clauses since where clauses need to bind to the DefId of the host param
1381+
let host_param_parts = if let Const::Yes(span) = constness && self.tcx.features().effects {
1382+
if let Some(param) = generics.params.iter().find(|x| {
1383+
x.attrs.iter().any(|x| x.has_name(sym::rustc_host))
1384+
}) {
1385+
// user has manually specified a `rustc_host` param, in this case, we set
1386+
// the param id so that lowering logic can use that. But we don't create
1387+
// another host param, so this gives `None`.
1388+
self.host_param_id = Some(self.local_def_id(param.id));
1389+
None
1390+
} else {
1391+
let param_node_id = self.next_node_id();
1392+
let hir_id = self.next_id();
1393+
let def_id = self.create_def(self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), span);
1394+
self.host_param_id = Some(def_id);
1395+
Some((span, hir_id, def_id))
1396+
}
1397+
} else {
1398+
None
1399+
};
1400+
13611401
let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
13621402
predicates.extend(generics.params.iter().filter_map(|param| {
13631403
self.lower_generic_bound_predicate(
@@ -1405,22 +1445,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
14051445
let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
14061446
predicates.extend(impl_trait_bounds.into_iter());
14071447

1408-
// Desugar `~const` bound in generics into an additional `const host: bool` param
1409-
// if the effects feature is enabled.
1410-
if let Const::Yes(span) = constness && self.tcx.features().effects
1411-
// Do not add host param if it already has it (manually specified)
1412-
&& !params.iter().any(|x| {
1413-
self.attrs.get(&x.hir_id.local_id).map_or(false, |attrs| {
1414-
attrs.iter().any(|x| x.has_name(sym::rustc_host))
1415-
})
1416-
})
1417-
{
1418-
let param_node_id = self.next_node_id();
1448+
if let Some((span, hir_id, def_id)) = host_param_parts {
14191449
let const_node_id = self.next_node_id();
1420-
let def_id = self.create_def(self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), span);
1421-
let anon_const: LocalDefId = self.create_def(def_id, const_node_id, DefPathData::AnonConst, span);
1450+
let anon_const: LocalDefId =
1451+
self.create_def(def_id, const_node_id, DefPathData::AnonConst, span);
14221452

1423-
let hir_id = self.next_id();
14241453
let const_id = self.next_id();
14251454
let const_expr_id = self.next_id();
14261455
let bool_id = self.next_id();
@@ -1430,14 +1459,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
14301459

14311460
let attr_id = self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
14321461

1433-
let attrs = self.arena.alloc_from_iter([
1434-
Attribute {
1435-
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(sym::rustc_host, span)))),
1462+
let attrs = self.arena.alloc_from_iter([Attribute {
1463+
kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(
1464+
sym::rustc_host,
14361465
span,
1437-
id: attr_id,
1438-
style: AttrStyle::Outer,
1439-
},
1440-
]);
1466+
)))),
1467+
span,
1468+
id: attr_id,
1469+
style: AttrStyle::Outer,
1470+
}]);
14411471
self.attrs.insert(hir_id.local_id, attrs);
14421472

14431473
let const_body = self.lower_body(|this| {
@@ -1476,7 +1506,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
14761506
}),
14771507
)),
14781508
)),
1479-
default: Some(hir::AnonConst { def_id: anon_const, hir_id: const_id, body: const_body }),
1509+
default: Some(hir::AnonConst {
1510+
def_id: anon_const,
1511+
hir_id: const_id,
1512+
body: const_body,
1513+
}),
14801514
},
14811515
colon_span: None,
14821516
pure_wrt_drop: false,

compiler/rustc_ast_lowering/src/lib.rs

+77-8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ struct LoweringContext<'a, 'hir> {
142142
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
143143
/// field from the original parameter 'a to the new parameter 'a1.
144144
generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
145+
146+
host_param_id: Option<LocalDefId>,
145147
}
146148

147149
trait ResolverAstLoweringExt {
@@ -1267,6 +1269,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12671269
span: t.span
12681270
},
12691271
itctx,
1272+
ast::Const::No,
12701273
);
12711274
let bounds = this.arena.alloc_from_iter([bound]);
12721275
let lifetime_bound = this.elided_dyn_bound(t.span);
@@ -1277,7 +1280,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12771280
}
12781281

12791282
let id = self.lower_node_id(t.id);
1280-
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
1283+
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx, None);
12811284
self.ty_path(id, t.span, qpath)
12821285
}
12831286

@@ -1361,10 +1364,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13611364
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
13621365
GenericBound::Trait(
13631366
ty,
1364-
TraitBoundModifier::None
1367+
modifier @ (TraitBoundModifier::None
13651368
| TraitBoundModifier::MaybeConst
1366-
| TraitBoundModifier::Negative,
1367-
) => Some(this.lower_poly_trait_ref(ty, itctx)),
1369+
| TraitBoundModifier::Negative),
1370+
) => {
1371+
Some(this.lower_poly_trait_ref(ty, itctx, modifier.to_constness()))
1372+
}
13681373
// `~const ?Bound` will cause an error during AST validation
13691374
// anyways, so treat it like `?Bound` as compilation proceeds.
13701375
GenericBound::Trait(
@@ -2189,7 +2194,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21892194
) -> hir::GenericBound<'hir> {
21902195
match tpb {
21912196
GenericBound::Trait(p, modifier) => hir::GenericBound::Trait(
2192-
self.lower_poly_trait_ref(p, itctx),
2197+
self.lower_poly_trait_ref(p, itctx, modifier.to_constness()),
21932198
self.lower_trait_bound_modifier(*modifier),
21942199
),
21952200
GenericBound::Outlives(lifetime) => {
@@ -2332,8 +2337,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23322337
}
23332338
}
23342339

2335-
fn lower_trait_ref(&mut self, p: &TraitRef, itctx: &ImplTraitContext) -> hir::TraitRef<'hir> {
2336-
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
2340+
fn lower_trait_ref(
2341+
&mut self,
2342+
constness: ast::Const,
2343+
p: &TraitRef,
2344+
itctx: &ImplTraitContext,
2345+
) -> hir::TraitRef<'hir> {
2346+
let path = match self.lower_qpath(
2347+
p.ref_id,
2348+
&None,
2349+
&p.path,
2350+
ParamMode::Explicit,
2351+
itctx,
2352+
Some(constness),
2353+
) {
23372354
hir::QPath::Resolved(None, path) => path,
23382355
qpath => panic!("lower_trait_ref: unexpected QPath `{qpath:?}`"),
23392356
};
@@ -2345,10 +2362,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23452362
&mut self,
23462363
p: &PolyTraitRef,
23472364
itctx: &ImplTraitContext,
2365+
constness: ast::Const,
23482366
) -> hir::PolyTraitRef<'hir> {
23492367
let bound_generic_params =
23502368
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
2351-
let trait_ref = self.lower_trait_ref(&p.trait_ref, itctx);
2369+
let trait_ref = self.lower_trait_ref(constness, &p.trait_ref, itctx);
23522370
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
23532371
}
23542372

@@ -2702,6 +2720,57 @@ struct GenericArgsCtor<'hir> {
27022720
}
27032721

27042722
impl<'hir> GenericArgsCtor<'hir> {
2723+
fn push_constness(&mut self, lcx: &mut LoweringContext<'_, 'hir>, constness: ast::Const) {
2724+
if !lcx.tcx.features().effects {
2725+
return;
2726+
}
2727+
2728+
// if bound is non-const, don't add host effect param
2729+
let ast::Const::Yes(span) = constness else { return };
2730+
2731+
let span = lcx.lower_span(span);
2732+
2733+
let id = lcx.next_node_id();
2734+
let hir_id = lcx.next_id();
2735+
let body = lcx.lower_body(|lcx| {
2736+
(
2737+
&[],
2738+
match constness {
2739+
ast::Const::Yes(_) => {
2740+
let hir_id = lcx.next_id();
2741+
let res =
2742+
Res::Def(DefKind::ConstParam, lcx.host_param_id.unwrap().to_def_id());
2743+
let expr_kind = hir::ExprKind::Path(hir::QPath::Resolved(
2744+
None,
2745+
lcx.arena.alloc(hir::Path {
2746+
span,
2747+
res,
2748+
segments: arena_vec![lcx; hir::PathSegment::new(Ident {
2749+
name: sym::host,
2750+
span,
2751+
}, hir_id, res)],
2752+
}),
2753+
));
2754+
lcx.expr(span, expr_kind)
2755+
}
2756+
ast::Const::No => lcx.expr(
2757+
span,
2758+
hir::ExprKind::Lit(
2759+
lcx.arena.alloc(hir::Lit { span, node: ast::LitKind::Bool(true) }),
2760+
),
2761+
),
2762+
},
2763+
)
2764+
});
2765+
let def_id =
2766+
lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span);
2767+
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2768+
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2769+
value: hir::AnonConst { def_id, hir_id, body },
2770+
span,
2771+
}))
2772+
}
2773+
27052774
fn is_empty(&self) -> bool {
27062775
self.args.is_empty()
27072776
&& self.bindings.is_empty()

compiler/rustc_ast_lowering/src/pat.rs

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3838
path,
3939
ParamMode::Optional,
4040
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
41+
None,
4142
);
4243
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
4344
break hir::PatKind::TupleStruct(qpath, pats, ddpos);
@@ -54,6 +55,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5455
path,
5556
ParamMode::Optional,
5657
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
58+
None,
5759
);
5860
break hir::PatKind::Path(qpath);
5961
}
@@ -64,6 +66,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6466
path,
6567
ParamMode::Optional,
6668
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
69+
None,
6770
);
6871

6972
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {

0 commit comments

Comments
 (0)