Skip to content

Commit 13ff467

Browse files
committed
initial work on lowering impl const to bind to host effect param
1 parent 76eff8e commit 13ff467

File tree

12 files changed

+80
-20
lines changed

12 files changed

+80
-20
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+
// TODO 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

+2-1
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

@@ -378,6 +379,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
378379
self.lower_generics(ast_generics, *constness, id, &itctx, |this| {
379380
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
380381
this.lower_trait_ref(
382+
*constness,
381383
trait_ref,
382384
&ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
383385
)
@@ -408,7 +410,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
408410
polarity,
409411
defaultness,
410412
defaultness_span,
411-
constness: self.lower_constness(*constness),
412413
generics,
413414
of_trait: trait_ref,
414415
self_ty: lowered_ty,

compiler/rustc_ast_lowering/src/lib.rs

+35-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<hir::HirId>,
145147
}
146148

147149
trait ResolverAstLoweringExt {
@@ -1267,6 +1269,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12671269
span: t.span
12681270
},
12691271
itctx,
1272+
// TODO?
1273+
ast::Const::No,
12701274
);
12711275
let bounds = this.arena.alloc_from_iter([bound]);
12721276
let lifetime_bound = this.elided_dyn_bound(t.span);
@@ -1277,7 +1281,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12771281
}
12781282

12791283
let id = self.lower_node_id(t.id);
1280-
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
1284+
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx, None);
12811285
self.ty_path(id, t.span, qpath)
12821286
}
12831287

@@ -1361,10 +1365,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13611365
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
13621366
GenericBound::Trait(
13631367
ty,
1364-
TraitBoundModifier::None
1368+
modifier @ (TraitBoundModifier::None
13651369
| TraitBoundModifier::MaybeConst
1366-
| TraitBoundModifier::Negative,
1367-
) => Some(this.lower_poly_trait_ref(ty, itctx)),
1370+
| TraitBoundModifier::Negative),
1371+
) => Some(this.lower_poly_trait_ref(ty, itctx, modifier.to_constness())),
13681372
// `~const ?Bound` will cause an error during AST validation
13691373
// anyways, so treat it like `?Bound` as compilation proceeds.
13701374
GenericBound::Trait(
@@ -2189,7 +2193,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21892193
) -> hir::GenericBound<'hir> {
21902194
match tpb {
21912195
GenericBound::Trait(p, modifier) => hir::GenericBound::Trait(
2192-
self.lower_poly_trait_ref(p, itctx),
2196+
self.lower_poly_trait_ref(p, itctx, modifier.to_constness()),
21932197
self.lower_trait_bound_modifier(*modifier),
21942198
),
21952199
GenericBound::Outlives(lifetime) => {
@@ -2332,8 +2336,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23322336
}
23332337
}
23342338

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) {
2339+
fn lower_trait_ref(&mut self, constness: ast::Const, p: &TraitRef, itctx: &ImplTraitContext) -> hir::TraitRef<'hir> {
2340+
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx, Some(constness)) {
23372341
hir::QPath::Resolved(None, path) => path,
23382342
qpath => panic!("lower_trait_ref: unexpected QPath `{qpath:?}`"),
23392343
};
@@ -2345,10 +2349,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23452349
&mut self,
23462350
p: &PolyTraitRef,
23472351
itctx: &ImplTraitContext,
2352+
constness: ast::Const,
23482353
) -> hir::PolyTraitRef<'hir> {
23492354
let bound_generic_params =
23502355
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);
2356+
let trait_ref = self.lower_trait_ref(constness, &p.trait_ref, itctx,);
23522357
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
23532358
}
23542359

@@ -2702,6 +2707,28 @@ struct GenericArgsCtor<'hir> {
27022707
}
27032708

27042709
impl<'hir> GenericArgsCtor<'hir> {
2710+
fn push_constness(&mut self, lcx: &mut LoweringContext<'_, 'hir>, constness: ast::Const) {
2711+
let span = if let ast::Const::Yes(sp) = constness {
2712+
sp
2713+
} else {
2714+
DUMMY_SP
2715+
};
2716+
let id = lcx.next_node_id();
2717+
lcx.lower_body(|lcx| {
2718+
(&[], match constness {
2719+
ast::Const::Yes(_) => lcx.expr_ident_mut(span, Ident { name: sym::host, span }, binding),
2720+
ast::Const::No => lcx.expr(span, hir::ExprKind::Lit(lcx.arena.alloc(hir::Lit { span, node: ast::LitKind::Bool(true) }))),
2721+
})
2722+
});
2723+
let def = lcx.create_def(lcx.current_hir_id_owner.def_id, id, DefPathData::AnonConst, span);
2724+
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2725+
value: hir::AnonConst {
2726+
def_id,
2727+
},
2728+
span,
2729+
}))
2730+
}
2731+
27052732
fn is_empty(&self) -> bool {
27062733
self.args.is_empty()
27072734
&& 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| {

compiler/rustc_ast_lowering/src/path.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2323
p: &Path,
2424
param_mode: ParamMode,
2525
itctx: &ImplTraitContext,
26+
// constness of the impl/bound if this is a trait path
27+
constness: Option<ast::Const>,
2628
) -> hir::QPath<'hir> {
2729
let qself_position = qself.as_ref().map(|q| q.position);
2830
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));
@@ -73,6 +75,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7375
param_mode,
7476
parenthesized_generic_args,
7577
itctx,
78+
// if this is the last segment, add constness to the trait path
79+
if i == proj_start - 1 {
80+
constness
81+
} else {
82+
None
83+
}
7684
)
7785
},
7886
)),
@@ -119,6 +127,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
119127
param_mode,
120128
ParenthesizedGenericArgs::Err,
121129
itctx,
130+
None,
122131
));
123132
let qpath = hir::QPath::TypeRelative(ty, hir_segment);
124133

@@ -159,6 +168,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
159168
param_mode,
160169
ParenthesizedGenericArgs::Err,
161170
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
171+
None,
162172
)
163173
})),
164174
span: self.lower_span(p.span),
@@ -172,8 +182,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
172182
param_mode: ParamMode,
173183
parenthesized_generic_args: ParenthesizedGenericArgs,
174184
itctx: &ImplTraitContext,
185+
constness: Option<ast::Const>,
175186
) -> hir::PathSegment<'hir> {
176-
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment,);
187+
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment);
177188
let (mut generic_args, infer_args) = if let Some(generic_args) = segment.args.as_deref() {
178189
match generic_args {
179190
GenericArgs::AngleBracketed(data) => {
@@ -231,6 +242,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
231242
)
232243
};
233244

245+
if let Some(constness) = constness {
246+
generic_args.push_constness(self, constness);
247+
}
248+
234249
let has_lifetimes =
235250
generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
236251

compiler/rustc_hir/src/hir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3357,7 +3357,8 @@ pub struct Impl<'hir> {
33573357
// We do not put a `Span` in `Defaultness` because it breaks foreign crate metadata
33583358
// decoding as `Span`s cannot be decoded when a `Session` is not available.
33593359
pub defaultness_span: Option<Span>,
3360-
pub constness: Constness,
3360+
// TODO remove this comment
3361+
// pub constness: Constness,
33613362
pub generics: &'hir Generics<'hir>,
33623363

33633364
/// The trait being implemented, if any.

compiler/rustc_hir/src/intravisit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
516516
unsafety: _,
517517
defaultness: _,
518518
polarity: _,
519-
constness: _,
520519
defaultness_span: _,
521520
ref generics,
522521
ref of_trait,

compiler/rustc_hir_pretty/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ impl<'a> State<'a> {
622622
unsafety,
623623
polarity,
624624
defaultness,
625-
constness,
626625
defaultness_span: _,
627626
generics,
628627
ref of_trait,
@@ -639,10 +638,6 @@ impl<'a> State<'a> {
639638
self.space();
640639
}
641640

642-
if constness == hir::Constness::Const {
643-
self.word_nbsp("const");
644-
}
645-
646641
if let hir::ImplPolarity::Negative(_) = polarity {
647642
self.word("!");
648643
}

compiler/rustc_middle/src/ty/context.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1987,13 +1987,15 @@ impl<'tcx> TyCtxt<'tcx> {
19871987
let hir_id = self.local_def_id_to_hir_id(local_def_id);
19881988
let node = self.hir().get(hir_id);
19891989

1990-
matches!(
1990+
// TODO
1991+
false
1992+
/*matches!(
19911993
node,
19921994
hir::Node::Item(hir::Item {
19931995
kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
19941996
..
19951997
})
1996-
)
1998+
)*/
19971999
}
19982000

19992001
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {

tests/ui/rfcs/rfc-2632-const-trait-impl/const-drop-fail-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// known-bug: #110395
2-
#![feature(const_trait_impl)]
2+
#![feature(const_trait_impl, effects)]
33
#![feature(const_mut_refs)]
44
#![cfg_attr(precise, feature(const_precise_live_drops))]
55

0 commit comments

Comments
 (0)