Skip to content

Commit c90a4ed

Browse files
committed
Introduce ConstArgKind
1 parent 2a8221d commit c90a4ed

File tree

12 files changed

+110
-38
lines changed

12 files changed

+110
-38
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern crate tracing;
4242

4343
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait, TraitFnAsync};
4444

45+
use hir::ConstArgKind;
4546
use rustc_ast::ptr::P;
4647
use rustc_ast::visit;
4748
use rustc_ast::{self as ast, *};
@@ -1231,18 +1232,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12311232
hir_id: this.lower_node_id(node_id),
12321233
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
12331234
});
1234-
return GenericArg::Const(ConstArg { value: ct, span });
1235+
// FIXME(const_arg_kind)
1236+
return GenericArg::Const(
1237+
self.arena.alloc(ConstArg {
1238+
kind: ConstArgKind::AnonConst(span, ct),
1239+
}),
1240+
);
12351241
}
12361242
}
12371243
}
12381244
_ => {}
12391245
}
12401246
GenericArg::Type(self.lower_ty(&ty, itctx))
12411247
}
1242-
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1243-
value: self.lower_anon_const(&ct),
1244-
span: self.lower_span(ct.value.span),
1245-
}),
1248+
ast::GenericArg::Const(ct) => GenericArg::Const(self.arena.alloc(ConstArg {
1249+
// FIXME(const_arg_kind)
1250+
kind: ConstArgKind::AnonConst(
1251+
self.lower_span(ct.value.span),
1252+
self.lower_anon_const(&ct),
1253+
),
1254+
})),
12461255
}
12471256
}
12481257

compiler/rustc_hir/src/hir.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,23 @@ impl<'hir> PathSegment<'hir> {
242242
}
243243

244244
#[derive(Clone, Copy, Debug, HashStable_Generic)]
245-
pub struct ConstArg {
246-
pub value: AnonConst,
247-
pub span: Span,
245+
pub struct ConstArg<'hir> {
246+
pub kind: ConstArgKind<'hir>,
247+
}
248+
249+
#[derive(Clone, Copy, Debug, HashStable_Generic)]
250+
pub enum ConstArgKind<'hir> {
251+
AnonConst(Span, AnonConst),
252+
Param(HirId, QPath<'hir>),
253+
}
254+
255+
impl<'hir> ConstArg<'hir> {
256+
pub fn span(&self) -> Span {
257+
match self.kind {
258+
ConstArgKind::AnonConst(span, _) => span,
259+
ConstArgKind::Param(_, qpath) => qpath.span(),
260+
}
261+
}
248262
}
249263

250264
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -263,7 +277,7 @@ impl InferArg {
263277
pub enum GenericArg<'hir> {
264278
Lifetime(&'hir Lifetime),
265279
Type(&'hir Ty<'hir>),
266-
Const(ConstArg),
280+
Const(&'hir ConstArg<'hir>),
267281
Infer(InferArg),
268282
}
269283

@@ -272,7 +286,7 @@ impl GenericArg<'_> {
272286
match self {
273287
GenericArg::Lifetime(l) => l.ident.span,
274288
GenericArg::Type(t) => t.span,
275-
GenericArg::Const(c) => c.span,
289+
GenericArg::Const(c) => c.span(),
276290
GenericArg::Infer(i) => i.span,
277291
}
278292
}
@@ -281,7 +295,10 @@ impl GenericArg<'_> {
281295
match self {
282296
GenericArg::Lifetime(l) => l.hir_id,
283297
GenericArg::Type(t) => t.hir_id,
284-
GenericArg::Const(c) => c.value.hir_id,
298+
GenericArg::Const(c) => match c.kind {
299+
ConstArgKind::AnonConst(_, ct) => ct.hir_id,
300+
ConstArgKind::Param(id, _) => id,
301+
},
285302
GenericArg::Infer(i) => i.hir_id,
286303
}
287304
}
@@ -4011,7 +4028,7 @@ mod size_asserts {
40114028
static_assert_size!(FnDecl<'_>, 40);
40124029
static_assert_size!(ForeignItem<'_>, 72);
40134030
static_assert_size!(ForeignItemKind<'_>, 40);
4014-
static_assert_size!(GenericArg<'_>, 32);
4031+
static_assert_size!(GenericArg<'_>, 16);
40154032
static_assert_size!(GenericBound<'_>, 48);
40164033
static_assert_size!(Generics<'_>, 56);
40174034
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ pub trait Visitor<'v>: Sized {
347347
fn visit_ty(&mut self, t: &'v Ty<'v>) {
348348
walk_ty(self, t)
349349
}
350+
fn visit_const_arg(&mut self, c: &'v ConstArg<'v>) {
351+
walk_const_arg(self, c)
352+
}
350353
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
351354
walk_generic_param(self, p)
352355
}
@@ -851,6 +854,18 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
851854
}
852855
}
853856

857+
pub fn walk_const_arg<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v ConstArg<'v>) {
858+
match &ct.kind {
859+
ConstArgKind::AnonConst(_, ct) => {
860+
visitor.visit_anon_const(ct);
861+
}
862+
ConstArgKind::Param(hir_id, qpath) => {
863+
visitor.visit_id(*hir_id);
864+
visitor.visit_qpath(qpath, *hir_id, qpath.span())
865+
}
866+
}
867+
}
868+
854869
pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam<'v>) {
855870
visitor.visit_id(param.hir_id);
856871
match param.name {
@@ -1116,7 +1131,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(visitor: &mut V, generic_arg: &'v Ge
11161131
match generic_arg {
11171132
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
11181133
GenericArg::Type(ty) => visitor.visit_ty(ty),
1119-
GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
1134+
GenericArg::Const(ct) => visitor.visit_const_arg(ct),
11201135
GenericArg::Infer(inf) => visitor.visit_infer(inf),
11211136
}
11221137
}

compiler/rustc_hir_analysis/src/astconv/generics.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,15 @@ fn generic_arg_mismatch_err(
107107
);
108108
}
109109
}
110-
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
111-
let body = tcx.hir().body(cnst.value.body);
110+
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. })
111+
if matches!(cnst.kind, hir::ConstArgKind::AnonConst(_, _)) =>
112+
{
113+
// FIXME(const_arg_kind)
114+
let body = match cnst.kind {
115+
hir::ConstArgKind::AnonConst(_, cnst) => tcx.hir().body(cnst.body),
116+
_ => unreachable!(),
117+
};
118+
112119
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
113120
{
114121
if let Res::Def(DefKind::Fn { .. }, id) = path.res {

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
449449
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
450450
handle_ty_args(has_default, &inf.to_ty())
451451
}
452-
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
453-
let did = ct.value.def_id;
454-
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
455-
ty::Const::from_anon_const(tcx, did).into()
456-
}
452+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match ct.kind {
453+
// FIXME(const_arg_kind)
454+
hir::ConstArgKind::AnonConst(_, ct) => {
455+
let did = ct.def_id;
456+
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
457+
ty::Const::from_anon_const(tcx, did).into()
458+
}
459+
hir::ConstArgKind::Param(_, _) => todo!(),
460+
},
457461
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
458462
let ty = tcx
459463
.at(self.span)

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15881588
i += 1;
15891589
}
15901590
GenericArg::Const(ct) => {
1591-
self.visit_anon_const(&ct.value);
1591+
self.visit_const_arg(ct);
15921592
i += 1;
15931593
}
15941594
GenericArg::Infer(inf) => {

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ impl<'a> State<'a> {
370370
self.end()
371371
}
372372

373+
pub fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
374+
self.maybe_print_comment(const_arg.span().lo());
375+
self.ibox(0);
376+
match &const_arg.kind {
377+
hir::ConstArgKind::AnonConst(_, ct) => self.print_anon_const(ct),
378+
hir::ConstArgKind::Param(_, path) => self.print_qpath(path, false),
379+
}
380+
self.end();
381+
}
382+
373383
pub fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
374384
self.hardbreak_if_not_bol();
375385
self.maybe_print_comment(item.span.lo());
@@ -1708,7 +1718,7 @@ impl<'a> State<'a> {
17081718
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
17091719
GenericArg::Lifetime(_) => {}
17101720
GenericArg::Type(ty) => s.print_type(ty),
1711-
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
1721+
GenericArg::Const(ct) => s.print_const_arg(ct),
17121722
GenericArg::Infer(_inf) => s.word("_"),
17131723
}
17141724
});

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
417417

418418
pub fn const_arg_to_const(
419419
&self,
420-
ast_c: &hir::AnonConst,
420+
ast_c: &hir::ConstArg<'_>,
421421
param_def_id: DefId,
422422
) -> ty::Const<'tcx> {
423-
let did = ast_c.def_id;
424-
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
425-
let c = ty::Const::from_anon_const(self.tcx, did);
426-
self.register_wf_obligation(
427-
c.into(),
428-
self.tcx.hir().span(ast_c.hir_id),
429-
ObligationCauseCode::WellFormed(None),
430-
);
431-
c
423+
// FIXME(const_arg_kind)
424+
match ast_c.kind {
425+
hir::ConstArgKind::AnonConst(_, ast_c) => {
426+
let did = ast_c.def_id;
427+
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
428+
let c = ty::Const::from_anon_const(self.tcx, did);
429+
self.register_wf_obligation(
430+
c.into(),
431+
self.tcx.hir().span(ast_c.hir_id),
432+
ObligationCauseCode::WellFormed(None),
433+
);
434+
c
435+
}
436+
hir::ConstArgKind::Param(_, _) => todo!(),
437+
}
432438
}
433439

434440
// If the type given by the user has free regions, save it for later, since
@@ -1271,7 +1277,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12711277
self.fcx.to_ty(ty).raw.into()
12721278
}
12731279
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
1274-
self.fcx.const_arg_to_const(&ct.value, param.def_id).into()
1280+
self.fcx.const_arg_to_const(ct, param.def_id).into()
12751281
}
12761282
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
12771283
self.fcx.ty_infer(Some(param), inf.span).into()

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
377377
self.cfcx.to_ty(ty).raw.into()
378378
}
379379
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
380-
self.cfcx.const_arg_to_const(&ct.value, param.def_id).into()
380+
self.cfcx.const_arg_to_const(ct, param.def_id).into()
381381
}
382382
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
383383
self.cfcx.ty_infer(Some(param), inf.span).into()

compiler/rustc_lint/src/pass_by_value.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
7373
GenericArg::Type(ty) => {
7474
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_else(|_| "_".into())
7575
}
76-
GenericArg::Const(c) => {
77-
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_else(|_| "_".into())
78-
}
76+
GenericArg::Const(c) => cx
77+
.tcx
78+
.sess
79+
.source_map()
80+
.span_to_snippet(c.span())
81+
.unwrap_or_else(|_| "_".into()),
7982
GenericArg::Infer(_) => String::from("_"),
8083
})
8184
.collect::<Vec<_>>();

0 commit comments

Comments
 (0)