Skip to content

Commit f6b0f0a

Browse files
committed
wip: Lower const params to new variant ConstArgKind::Path
1 parent 85accee commit f6b0f0a

File tree

11 files changed

+164
-139
lines changed

11 files changed

+164
-139
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,40 +1149,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11491149
ty,
11501150
);
11511151

1152-
// Construct an AnonConst where the expr is the "ty"'s path.
1153-
1154-
let parent_def_id = self.current_hir_id_owner;
1155-
let node_id = self.next_node_id();
1156-
let span = self.lower_span(ty.span);
1157-
1158-
// Add a definition for the in-band const def.
1159-
let def_id = self.create_def(
1160-
parent_def_id.def_id,
1161-
node_id,
1162-
kw::Empty,
1163-
DefKind::AnonConst,
1164-
span,
1152+
let qpath = self.lower_qpath(
1153+
ty.id,
1154+
&None,
1155+
path,
1156+
ParamMode::Optional,
1157+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1158+
None,
11651159
);
1166-
1167-
let path_expr = Expr {
1168-
id: ty.id,
1169-
kind: ExprKind::Path(None, path.clone()),
1170-
span,
1171-
attrs: AttrVec::new(),
1172-
tokens: None,
1173-
};
1174-
1175-
let ct = self.with_new_scopes(span, |this| {
1176-
self.arena.alloc(hir::AnonConst {
1177-
def_id,
1178-
hir_id: this.lower_node_id(node_id),
1179-
body: this
1180-
.lower_const_body(path_expr.span, Some(&path_expr)),
1181-
span,
1182-
})
1183-
});
11841160
return GenericArg::Const(ConstArg {
1185-
kind: ConstArgKind::Anon(ct),
1161+
hir_id: self.lower_node_id(ty.id),
1162+
kind: ConstArgKind::Path(qpath),
11861163
is_desugared_from_effects: false,
11871164
});
11881165
}
@@ -1192,10 +1169,39 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11921169
}
11931170
GenericArg::Type(self.lower_ty(ty, itctx))
11941171
}
1195-
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1196-
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
1197-
is_desugared_from_effects: false,
1198-
}),
1172+
ast::GenericArg::Const(ct) => GenericArg::Const(self.lower_anon_const_as_const_arg(ct)),
1173+
}
1174+
}
1175+
1176+
fn lower_anon_const_as_const_arg(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
1177+
if let ExprKind::Path(qself, path) = &anon.value.kind {
1178+
let qpath = self.lower_qpath(
1179+
anon.id,
1180+
qself,
1181+
path,
1182+
ParamMode::Optional,
1183+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
1184+
None,
1185+
);
1186+
// FIXME(min_generic_const_exprs): for now we only lower params to ConstArgKind::Path
1187+
if let hir::QPath::Resolved(
1188+
_,
1189+
&hir::Path { res: Res::Def(DefKind::ConstParam, _), .. },
1190+
) = qpath
1191+
{
1192+
return ConstArg {
1193+
hir_id: self.lower_node_id(anon.id),
1194+
kind: ConstArgKind::Path(qpath),
1195+
is_desugared_from_effects: false,
1196+
};
1197+
}
1198+
}
1199+
1200+
let lowered_anon = self.lower_anon_const(anon);
1201+
ConstArg {
1202+
hir_id: lowered_anon.hir_id,
1203+
kind: ConstArgKind::Anon(lowered_anon),
1204+
is_desugared_from_effects: false,
11991205
}
12001206
}
12011207

@@ -2593,16 +2599,34 @@ impl<'hir> GenericArgsCtor<'hir> {
25932599
return;
25942600
}
25952601

2596-
let (span, body) = match constness {
2602+
let id = lcx.next_node_id();
2603+
let hir_id = lcx.next_id();
2604+
2605+
let const_arg_kind = match constness {
25972606
BoundConstness::Never => return,
25982607
BoundConstness::Always(span) => {
25992608
let span = lcx.lower_span(span);
26002609

26012610
let body = hir::ExprKind::Lit(
26022611
lcx.arena.alloc(hir::Lit { node: LitKind::Bool(false), span }),
26032612
);
2613+
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
2614+
2615+
let def_id = lcx.create_def(
2616+
lcx.current_hir_id_owner.def_id,
2617+
id,
2618+
kw::Empty,
2619+
DefKind::AnonConst,
2620+
span,
2621+
);
26042622

2605-
(span, body)
2623+
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2624+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2625+
def_id,
2626+
hir_id,
2627+
body,
2628+
span,
2629+
}))
26062630
}
26072631
BoundConstness::Maybe(span) => {
26082632
let span = lcx.lower_span(span);
@@ -2615,48 +2639,26 @@ impl<'hir> GenericArgsCtor<'hir> {
26152639
return;
26162640
};
26172641

2618-
let hir_id = lcx.next_id();
26192642
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
2620-
let body = hir::ExprKind::Path(hir::QPath::Resolved(
2621-
None,
2622-
lcx.arena.alloc(hir::Path {
2623-
span,
2624-
res,
2625-
segments: arena_vec![
2626-
lcx;
2627-
hir::PathSegment::new(
2628-
Ident { name: sym::host, span },
2629-
hir_id,
2630-
res
2631-
)
2632-
],
2633-
}),
2634-
));
2635-
2636-
(span, body)
2643+
let path = lcx.arena.alloc(hir::Path {
2644+
span,
2645+
res,
2646+
segments: arena_vec![
2647+
lcx;
2648+
hir::PathSegment::new(
2649+
Ident { name: sym::host, span },
2650+
hir_id,
2651+
res
2652+
)
2653+
],
2654+
});
2655+
hir::ConstArgKind::Path(hir::QPath::Resolved(None, path))
26372656
}
26382657
};
2639-
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
2640-
2641-
let id = lcx.next_node_id();
2642-
let hir_id = lcx.next_id();
2643-
2644-
let def_id = lcx.create_def(
2645-
lcx.current_hir_id_owner.def_id,
2646-
id,
2647-
kw::Empty,
2648-
DefKind::AnonConst,
2649-
span,
2650-
);
26512658

2652-
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26532659
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2654-
kind: hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2655-
def_id,
2656-
hir_id,
2657-
body,
2658-
span,
2659-
})),
2660+
hir_id,
2661+
kind: const_arg_kind,
26602662
is_desugared_from_effects: true,
26612663
}))
26622664
}

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ impl<'hir> PathSegment<'hir> {
230230

231231
#[derive(Clone, Copy, Debug, HashStable_Generic)]
232232
pub struct ConstArg<'hir> {
233+
#[stable_hasher(ignore)]
234+
pub hir_id: HirId,
233235
pub kind: ConstArgKind<'hir>,
234236
/// Indicates whether this comes from a `~const` desugaring.
235237
pub is_desugared_from_effects: bool,
@@ -238,19 +240,15 @@ pub struct ConstArg<'hir> {
238240
impl<'hir> ConstArg<'hir> {
239241
pub fn span(&self) -> Span {
240242
match self.kind {
243+
ConstArgKind::Path(path) => path.span(),
241244
ConstArgKind::Anon(anon) => anon.span,
242245
}
243246
}
244-
245-
pub fn hir_id(&self) -> HirId {
246-
match self.kind {
247-
ConstArgKind::Anon(anon) => anon.hir_id,
248-
}
249-
}
250247
}
251248

252249
#[derive(Clone, Copy, Debug, HashStable_Generic)]
253250
pub enum ConstArgKind<'hir> {
251+
Path(QPath<'hir>),
254252
Anon(&'hir AnonConst),
255253
}
256254

@@ -288,7 +286,7 @@ impl GenericArg<'_> {
288286
match self {
289287
GenericArg::Lifetime(l) => l.hir_id,
290288
GenericArg::Type(t) => t.hir_id,
291-
GenericArg::Const(c) => c.hir_id(),
289+
GenericArg::Const(c) => c.hir_id,
292290
GenericArg::Infer(i) => i.hir_id,
293291
}
294292
}
@@ -3952,7 +3950,7 @@ mod size_asserts {
39523950
static_assert_size!(FnDecl<'_>, 40);
39533951
static_assert_size!(ForeignItem<'_>, 72);
39543952
static_assert_size!(ForeignItemKind<'_>, 40);
3955-
static_assert_size!(GenericArg<'_>, 24);
3953+
static_assert_size!(GenericArg<'_>, 40);
39563954
static_assert_size!(GenericBound<'_>, 48);
39573955
static_assert_size!(Generics<'_>, 56);
39583956
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,10 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
736736
visitor: &mut V,
737737
const_arg: &'v ConstArg<'v>,
738738
) -> V::Result {
739-
match const_arg.kind {
740-
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
739+
try_visit!(visitor.visit_id(const_arg.hir_id));
740+
match &const_arg.kind {
741+
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, const_arg.hir_id, qpath.span()),
742+
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
741743
}
742744
}
743745

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_errors::{
1010
};
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::DefId;
13-
use rustc_hir::GenericArg;
14-
use rustc_hir::{self as hir, ConstArgKind};
13+
use rustc_hir::{self as hir};
14+
use rustc_hir::{ConstArgKind, GenericArg};
1515
use rustc_middle::ty::{
1616
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
1717
};
@@ -112,10 +112,7 @@ fn generic_arg_mismatch_err(
112112
}
113113
}
114114
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
115-
let ConstArgKind::Anon(anon) = cnst.kind;
116-
let body = tcx.hir().body(anon.body);
117-
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
118-
{
115+
if let ConstArgKind::Path(hir::QPath::Resolved(_, path)) = cnst.kind {
119116
if let Res::Def(DefKind::Fn { .. }, id) = path.res {
120117
err.help(format!("`{}` is a function item, not a type", tcx.item_name(id)));
121118
err.help("function item types cannot be named directly");

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
476476
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
477477
handle_ty_args(has_default, &inf.to_ty())
478478
}
479-
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
479+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match ct.kind {
480+
hir::ConstArgKind::Path(qpath) => {
481+
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
482+
ty::Const::from_param(tcx, qpath, ct.hir_id).into()
483+
}
480484
hir::ConstArgKind::Anon(anon) => {
481485
let did = anon.def_id;
482486
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ impl<'a> State<'a> {
988988

989989
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
990990
match &const_arg.kind {
991+
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
991992
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
992993
}
993994
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
471471
const_arg: &ConstArg<'tcx>,
472472
param_def_id: DefId,
473473
) -> ty::Const<'tcx> {
474-
match &const_arg.kind {
474+
let ct = match const_arg.kind {
475+
ConstArgKind::Path(qpath) => {
476+
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
477+
ty::Const::from_param(self.tcx, qpath, const_arg.hir_id)
478+
}
475479
ConstArgKind::Anon(anon) => {
476480
let did = anon.def_id;
477481
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
478-
let ct = ty::Const::from_anon_const(self.tcx, did);
479-
self.register_wf_obligation(
480-
ct.into(),
481-
self.tcx.hir().span(anon.hir_id),
482-
ObligationCauseCode::WellFormed(None),
483-
);
484-
ct
482+
ty::Const::from_anon_const(self.tcx, did)
485483
}
486-
}
484+
};
485+
self.register_wf_obligation(
486+
ct.into(),
487+
self.tcx.hir().span(const_arg.hir_id),
488+
ObligationCauseCode::WellFormed(None),
489+
);
490+
ct
487491
}
488492

489493
// If the type given by the user has free regions, save it for later, since

0 commit comments

Comments
 (0)