Skip to content

Commit afc3c1d

Browse files
committed
wip: Lower const params to new variant ConstArgKind::Path
1 parent 9d6aa97 commit afc3c1d

File tree

11 files changed

+184
-150
lines changed

11 files changed

+184
-150
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+77-75
Original file line numberDiff line numberDiff line change
@@ -1152,40 +1152,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11521152
ty,
11531153
);
11541154

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

@@ -2596,16 +2602,34 @@ impl<'hir> GenericArgsCtor<'hir> {
25962602
return;
25972603
}
25982604

2599-
let (span, body) = match constness {
2605+
let id = lcx.next_node_id();
2606+
let hir_id = lcx.next_id();
2607+
2608+
let const_arg_kind = match constness {
26002609
BoundConstness::Never => return,
26012610
BoundConstness::Always(span) => {
26022611
let span = lcx.lower_span(span);
26032612

26042613
let body = hir::ExprKind::Lit(
26052614
lcx.arena.alloc(hir::Lit { node: LitKind::Bool(false), span }),
26062615
);
2616+
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
2617+
2618+
let def_id = lcx.create_def(
2619+
lcx.current_hir_id_owner.def_id,
2620+
id,
2621+
kw::Empty,
2622+
DefKind::AnonConst,
2623+
span,
2624+
);
26072625

2608-
(span, body)
2626+
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
2627+
hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2628+
def_id,
2629+
hir_id,
2630+
body,
2631+
span,
2632+
}))
26092633
}
26102634
BoundConstness::Maybe(span) => {
26112635
let span = lcx.lower_span(span);
@@ -2618,48 +2642,26 @@ impl<'hir> GenericArgsCtor<'hir> {
26182642
return;
26192643
};
26202644

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

2655-
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26562662
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2657-
kind: hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2658-
def_id,
2659-
hir_id,
2660-
body,
2661-
span,
2662-
})),
2663+
hir_id,
2664+
kind: const_arg_kind,
26632665
is_desugared_from_effects: true,
26642666
}))
26652667
}

compiler/rustc_hir/src/hir.rs

+6-8
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
}
@@ -3922,7 +3920,7 @@ mod size_asserts {
39223920
static_assert_size!(FnDecl<'_>, 40);
39233921
static_assert_size!(ForeignItem<'_>, 72);
39243922
static_assert_size!(ForeignItemKind<'_>, 40);
3925-
static_assert_size!(GenericArg<'_>, 24);
3923+
static_assert_size!(GenericArg<'_>, 40);
39263924
static_assert_size!(GenericBound<'_>, 48);
39273925
static_assert_size!(Generics<'_>, 56);
39283926
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,10 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
723723
visitor: &mut V,
724724
const_arg: &'v ConstArg<'v>,
725725
) -> V::Result {
726-
match const_arg.kind {
727-
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
726+
try_visit!(visitor.visit_id(const_arg.hir_id));
727+
match &const_arg.kind {
728+
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, const_arg.hir_id, qpath.span()),
729+
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
728730
}
729731
}
730732

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+3-6
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
479479
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
480480
handle_ty_args(has_default, &inf.to_ty())
481481
}
482-
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
482+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match ct.kind {
483+
ConstArgKind::Path(qpath) => {
484+
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
485+
ty::Const::from_param(tcx, qpath, ct.hir_id).into()
486+
}
483487
ConstArgKind::Anon(anon) => {
484488
let did = anon.def_id;
485489
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));

compiler/rustc_hir_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ impl<'a> State<'a> {
984984

985985
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
986986
match &const_arg.kind {
987+
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
987988
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
988989
}
989990
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -451,19 +451,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
451451
const_arg: &ConstArg<'tcx>,
452452
param_def_id: DefId,
453453
) -> ty::Const<'tcx> {
454-
match &const_arg.kind {
454+
let ct = match const_arg.kind {
455+
ConstArgKind::Path(qpath) => {
456+
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
457+
ty::Const::from_param(self.tcx, qpath, const_arg.hir_id)
458+
}
455459
ConstArgKind::Anon(anon) => {
456460
let did = anon.def_id;
457461
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
458-
let ct = ty::Const::from_anon_const(self.tcx, did);
459-
self.register_wf_obligation(
460-
ct.into(),
461-
self.tcx.hir().span(anon.hir_id),
462-
ObligationCauseCode::WellFormed(None),
463-
);
464-
ct
462+
ty::Const::from_anon_const(self.tcx, did)
465463
}
466-
}
464+
};
465+
self.register_wf_obligation(
466+
ct.into(),
467+
self.tcx.hir().span(const_arg.hir_id),
468+
ObligationCauseCode::WellFormed(None),
469+
);
470+
ct
467471
}
468472

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

0 commit comments

Comments
 (0)