Skip to content

Commit c4fb31e

Browse files
committed
wip: Lower const params to new variant ConstArgKind::Path
1 parent 79ce092 commit c4fb31e

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
@@ -1147,40 +1147,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11471147
ty,
11481148
);
11491149

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

@@ -2591,16 +2597,34 @@ impl<'hir> GenericArgsCtor<'hir> {
25912597
return;
25922598
}
25932599

2594-
let (span, body) = match constness {
2600+
let id = lcx.next_node_id();
2601+
let hir_id = lcx.next_id();
2602+
2603+
let const_arg_kind = match constness {
25952604
BoundConstness::Never => return,
25962605
BoundConstness::Always(span) => {
25972606
let span = lcx.lower_span(span);
25982607

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

2603-
(span, body)
2621+
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2622+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2623+
def_id,
2624+
hir_id,
2625+
body,
2626+
span,
2627+
}))
26042628
}
26052629
BoundConstness::Maybe(span) => {
26062630
let span = lcx.lower_span(span);
@@ -2613,48 +2637,26 @@ impl<'hir> GenericArgsCtor<'hir> {
26132637
return;
26142638
};
26152639

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

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

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
}
@@ -3937,7 +3935,7 @@ mod size_asserts {
39373935
static_assert_size!(FnDecl<'_>, 40);
39383936
static_assert_size!(ForeignItem<'_>, 72);
39393937
static_assert_size!(ForeignItemKind<'_>, 40);
3940-
static_assert_size!(GenericArg<'_>, 24);
3938+
static_assert_size!(GenericArg<'_>, 40);
39413939
static_assert_size!(GenericBound<'_>, 48);
39423940
static_assert_size!(Generics<'_>, 56);
39433941
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
@@ -986,6 +986,7 @@ impl<'a> State<'a> {
986986

987987
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
988988
match &const_arg.kind {
989+
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
989990
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
990991
}
991992
}

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)