Skip to content

Commit 11b144a

Browse files
committed
hir: Create hir::ConstArgKind enum
This will allow lowering const params to a dedicated enum variant, rather than to an `AnonConst` that is later examined during `ty` lowering.
1 parent 71f8aed commit 11b144a

File tree

13 files changed

+102
-40
lines changed

13 files changed

+102
-40
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_data_structures::sync::Lrc;
5353
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5454
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
56-
use rustc_hir::{self as hir};
56+
use rustc_hir::{self as hir, ConstArgKind};
5757
use rustc_hir::{
5858
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
5959
};
@@ -1203,7 +1203,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12031203
})
12041204
});
12051205
return GenericArg::Const(ConstArg {
1206-
value: ct,
1206+
kind: ConstArgKind::Anon(ct),
12071207
is_desugared_from_effects: false,
12081208
});
12091209
}
@@ -1214,7 +1214,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12141214
GenericArg::Type(self.lower_ty(ty, itctx))
12151215
}
12161216
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1217-
value: self.lower_anon_const(ct),
1217+
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
12181218
is_desugared_from_effects: false,
12191219
}),
12201220
}

compiler/rustc_hir/src/hir.rs

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

231231
#[derive(Clone, Copy, Debug, HashStable_Generic)]
232232
pub struct ConstArg<'hir> {
233-
pub value: &'hir AnonConst,
233+
pub kind: ConstArgKind<'hir>,
234234
/// Indicates whether this comes from a `~const` desugaring.
235235
pub is_desugared_from_effects: bool,
236236
}
237237

238+
impl<'hir> ConstArg<'hir> {
239+
pub fn span(&self) -> Span {
240+
match self.kind {
241+
ConstArgKind::Anon(anon) => anon.span,
242+
}
243+
}
244+
245+
pub fn hir_id(&self) -> HirId {
246+
match self.kind {
247+
ConstArgKind::Anon(anon) => anon.hir_id,
248+
}
249+
}
250+
}
251+
252+
#[derive(Clone, Copy, Debug, HashStable_Generic)]
253+
pub enum ConstArgKind<'hir> {
254+
Anon(&'hir AnonConst),
255+
}
256+
238257
#[derive(Clone, Copy, Debug, HashStable_Generic)]
239258
pub struct InferArg {
240259
pub hir_id: HirId,
@@ -260,7 +279,7 @@ impl GenericArg<'_> {
260279
match self {
261280
GenericArg::Lifetime(l) => l.ident.span,
262281
GenericArg::Type(t) => t.span,
263-
GenericArg::Const(c) => c.value.span,
282+
GenericArg::Const(c) => c.span(),
264283
GenericArg::Infer(i) => i.span,
265284
}
266285
}
@@ -269,7 +288,7 @@ impl GenericArg<'_> {
269288
match self {
270289
GenericArg::Lifetime(l) => l.hir_id,
271290
GenericArg::Type(t) => t.hir_id,
272-
GenericArg::Const(c) => c.value.hir_id,
291+
GenericArg::Const(c) => c.hir_id(),
273292
GenericArg::Infer(i) => i.hir_id,
274293
}
275294
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ pub trait Visitor<'v>: Sized {
347347
fn visit_inline_const(&mut self, c: &'v ConstBlock) -> Self::Result {
348348
walk_inline_const(self, c)
349349
}
350+
fn visit_const_arg(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
351+
walk_const_arg(self, c)
352+
}
350353
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
351354
walk_expr(self, ex)
352355
}
@@ -725,6 +728,15 @@ pub fn walk_inline_const<'v, V: Visitor<'v>>(
725728
visitor.visit_nested_body(constant.body)
726729
}
727730

731+
pub fn walk_const_arg<'v, V: Visitor<'v>>(
732+
visitor: &mut V,
733+
const_arg: &'v ConstArg<'v>,
734+
) -> V::Result {
735+
match const_arg.kind {
736+
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
737+
}
738+
}
739+
728740
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
729741
try_visit!(visitor.visit_id(expression.hir_id));
730742
match expression.kind {
@@ -1216,7 +1228,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(
12161228
match generic_arg {
12171229
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
12181230
GenericArg::Type(ty) => visitor.visit_ty(ty),
1219-
GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
1231+
GenericArg::Const(ct) => visitor.visit_const_arg(ct),
12201232
GenericArg::Infer(inf) => visitor.visit_infer(inf),
12211233
}
12221234
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
15941594
i += 1;
15951595
}
15961596
GenericArg::Const(ct) => {
1597-
self.visit_anon_const(&ct.value);
1597+
self.visit_const_arg(ct);
15981598
i += 1;
15991599
}
16001600
GenericArg::Infer(inf) => {

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use rustc_ast::ast::ParamKindOrd;
88
use rustc_errors::{
99
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
1010
};
11-
use rustc_hir as hir;
1211
use rustc_hir::def::{DefKind, Res};
1312
use rustc_hir::def_id::DefId;
1413
use rustc_hir::GenericArg;
14+
use rustc_hir::{self as hir, ConstArgKind};
1515
use rustc_middle::ty::{
1616
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty,
1717
};
@@ -113,7 +113,8 @@ fn generic_arg_mismatch_err(
113113
}
114114
}
115115
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
116-
let body = tcx.hir().body(cnst.value.body);
116+
let ConstArgKind::Anon(anon) = cnst.kind;
117+
let body = tcx.hir().body(anon.body);
117118
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
118119
{
119120
if let Res::Def(DefKind::Fn { .. }, id) = path.res {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
470470
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
471471
handle_ty_args(has_default, &inf.to_ty())
472472
}
473-
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
474-
let did = ct.value.def_id;
475-
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
476-
ty::Const::from_anon_const(tcx, did).into()
477-
}
473+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
474+
hir::ConstArgKind::Anon(anon) => {
475+
let did = anon.def_id;
476+
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
477+
ty::Const::from_anon_const(tcx, did).into()
478+
}
479+
},
478480
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
479481
self.lowerer.ct_infer(Some(param), inf.span).into()
480482
}

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::util::parser::{self, AssocOp, Fixity};
1010
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
1111
use rustc_ast_pretty::pp::{self, Breaks};
1212
use rustc_ast_pretty::pprust::{Comments, PrintState};
13-
use rustc_hir as hir;
13+
use rustc_hir::{self as hir, ConstArgKind};
1414
use rustc_hir::{
1515
BindingMode, ByRef, GenericArg, GenericBound, GenericParam, GenericParamKind, HirId,
1616
LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term, TraitBoundModifier,
@@ -991,6 +991,12 @@ impl<'a> State<'a> {
991991
self.ann.nested(self, Nested::Body(constant.body))
992992
}
993993

994+
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
995+
match &const_arg.kind {
996+
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
997+
}
998+
}
999+
9941000
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
9951001
self.popen();
9961002
self.commasep_exprs(Inconsistent, args);
@@ -1679,7 +1685,7 @@ impl<'a> State<'a> {
16791685
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
16801686
GenericArg::Lifetime(_) => {}
16811687
GenericArg::Type(ty) => s.print_type(ty),
1682-
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
1688+
GenericArg::Const(ct) => s.print_const_arg(ct),
16831689
GenericArg::Infer(_inf) => s.word("_"),
16841690
}
16851691
});

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::rvalue_scopes;
55
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey};
8-
use rustc_hir as hir;
98
use rustc_hir::def::{CtorOf, DefKind, Res};
109
use rustc_hir::def_id::DefId;
1110
use rustc_hir::lang_items::LangItem;
11+
use rustc_hir::{self as hir, ConstArg, ConstArgKind};
1212
use rustc_hir::{ExprKind, GenericArg, HirId, Node, QPath};
1313
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
1414
use rustc_hir_analysis::hir_ty_lowering::generics::{
@@ -466,16 +466,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
466466
}
467467
}
468468

469-
pub fn lower_const_arg(&self, hir_ct: &hir::AnonConst, param_def_id: DefId) -> ty::Const<'tcx> {
470-
let did = hir_ct.def_id;
471-
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
472-
let ct = ty::Const::from_anon_const(self.tcx, did);
473-
self.register_wf_obligation(
474-
ct.into(),
475-
self.tcx.hir().span(hir_ct.hir_id),
476-
ObligationCauseCode::WellFormed(None),
477-
);
478-
ct
469+
pub fn lower_const_arg(
470+
&self,
471+
const_arg: &ConstArg<'tcx>,
472+
param_def_id: DefId,
473+
) -> ty::Const<'tcx> {
474+
match &const_arg.kind {
475+
ConstArgKind::Anon(anon) => {
476+
let did = anon.def_id;
477+
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
485+
}
486+
}
479487
}
480488

481489
// If the type given by the user has free regions, save it for later, since
@@ -1298,7 +1306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12981306
self.fcx.lower_ty(ty).raw.into()
12991307
}
13001308
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
1301-
self.fcx.lower_const_arg(&ct.value, param.def_id).into()
1309+
self.fcx.lower_const_arg(ct, param.def_id).into()
13021310
}
13031311
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
13041312
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
@@ -400,7 +400,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
400400
self.cfcx.lower_ty(ty).raw.into()
401401
}
402402
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
403-
self.cfcx.lower_const_arg(&ct.value, param.def_id).into()
403+
self.cfcx.lower_const_arg(ct, param.def_id).into()
404404
}
405405
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
406406
self.cfcx.ty_infer(Some(param), inf.span).into()

compiler/rustc_lint/src/pass_by_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
7878
.tcx
7979
.sess
8080
.source_map()
81-
.span_to_snippet(c.value.span)
81+
.span_to_snippet(c.span())
8282
.unwrap_or_else(|_| "_".into()),
8383
GenericArg::Infer(_) => String::from("_"),
8484
})

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
452452
match ga {
453453
hir::GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
454454
hir::GenericArg::Type(ty) => self.visit_ty(ty),
455-
hir::GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
455+
hir::GenericArg::Const(ct) => self.visit_const_arg(ct),
456456
hir::GenericArg::Infer(inf) => self.visit_infer(inf),
457457
}
458458
}

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
3636
use rustc_attr as attr;
3737
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
3838
use rustc_errors::{codes::*, struct_span_code_err, FatalError};
39-
use rustc_hir as hir;
4039
use rustc_hir::def::{CtorKind, DefKind, Res};
4140
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
4241
use rustc_hir::PredicateOrigin;
42+
use rustc_hir::{self as hir, ConstArgKind};
4343
use rustc_hir_analysis::lower_ty;
4444
use rustc_middle::metadata::Reexport;
4545
use rustc_middle::middle::resolve_bound_vars as rbv;
@@ -285,10 +285,12 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
285285
}
286286

287287
pub(crate) fn clean_const<'tcx>(
288-
constant: &hir::ConstArg<'_>,
288+
constant: &hir::ConstArg<'tcx>,
289289
_cx: &mut DocContext<'tcx>,
290290
) -> Constant {
291-
Constant { kind: ConstantKind::Anonymous { body: constant.value.body } }
291+
match &constant.kind {
292+
ConstArgKind::Anon(anon) => Constant { kind: ConstantKind::Anonymous { body: anon.body } },
293+
}
292294
}
293295

294296
pub(crate) fn clean_middle_const<'tcx>(

src/tools/clippy/clippy_utils/src/hir_utils.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use rustc_data_structures::fx::FxHasher;
77
use rustc_hir::def::Res;
88
use rustc_hir::MatchSource::TryDesugar;
99
use rustc_hir::{
10-
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy,
11-
GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, LifetimeName, Pat, PatField,
12-
PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind,
10+
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
11+
ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime,
12+
LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind,
1313
};
1414
use rustc_lexer::{tokenize, TokenKind};
1515
use rustc_lint::LateContext;
@@ -411,14 +411,20 @@ impl HirEqInterExpr<'_, '_, '_> {
411411

412412
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
413413
match (left, right) {
414-
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
414+
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_const_arg(l, r),
415415
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
416416
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
417417
(GenericArg::Infer(l_inf), GenericArg::Infer(r_inf)) => self.eq_ty(&l_inf.to_ty(), &r_inf.to_ty()),
418418
_ => false,
419419
}
420420
}
421421

422+
fn eq_const_arg(&mut self, left: &ConstArg<'_>, right: &ConstArg<'_>) -> bool {
423+
match (&left.kind, &right.kind) {
424+
(ConstArgKind::Anon(l_an), ConstArgKind::Anon(r_an)) => self.eq_body(l_an.body, r_an.body),
425+
}
426+
}
427+
422428
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
423429
left.res == right.res
424430
}
@@ -1134,12 +1140,18 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
11341140
self.maybe_typeck_results = old_maybe_typeck_results;
11351141
}
11361142

1143+
fn hash_const_arg(&mut self, const_arg: &ConstArg<'_>) {
1144+
match &const_arg.kind {
1145+
ConstArgKind::Anon(anon) => self.hash_body(anon.body),
1146+
}
1147+
}
1148+
11371149
fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
11381150
for arg in arg_list {
11391151
match *arg {
11401152
GenericArg::Lifetime(l) => self.hash_lifetime(l),
11411153
GenericArg::Type(ty) => self.hash_ty(ty),
1142-
GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
1154+
GenericArg::Const(ref ca) => self.hash_const_arg(ca),
11431155
GenericArg::Infer(ref inf) => self.hash_ty(&inf.to_ty()),
11441156
}
11451157
}

0 commit comments

Comments
 (0)