Skip to content

Commit 9d6aa97

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 9f2d0b3 commit 9d6aa97

File tree

13 files changed

+119
-44
lines changed

13 files changed

+119
-44
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ use rustc_data_structures::sorted_map::SortedMap;
4949
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5050
use rustc_data_structures::sync::Lrc;
5151
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey};
52-
use rustc_hir as hir;
5352
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5453
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
54+
use rustc_hir::{self as hir, ConstArgKind};
5555
use rustc_hir::{
5656
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
5757
};
@@ -1185,7 +1185,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11851185
})
11861186
});
11871187
return GenericArg::Const(ConstArg {
1188-
value: ct,
1188+
kind: ConstArgKind::Anon(ct),
11891189
is_desugared_from_effects: false,
11901190
});
11911191
}
@@ -1196,7 +1196,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11961196
GenericArg::Type(self.lower_ty(ty, itctx))
11971197
}
11981198
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1199-
value: self.lower_anon_const(ct),
1199+
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
12001200
is_desugared_from_effects: false,
12011201
}),
12021202
}
@@ -2654,7 +2654,12 @@ impl<'hir> GenericArgsCtor<'hir> {
26542654

26552655
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
26562656
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2657-
value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body, span }),
2657+
kind: hir::ConstArgKind::Anon(lcx.arena.alloc(hir::AnonConst {
2658+
def_id,
2659+
hir_id,
2660+
body,
2661+
span,
2662+
})),
26582663
is_desugared_from_effects: true,
26592664
}))
26602665
}

compiler/rustc_hir/src/hir.rs

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ pub trait Visitor<'v>: Sized {
344344
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
345345
walk_anon_const(self, c)
346346
}
347+
fn visit_const_arg(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
348+
walk_const_arg(self, c)
349+
}
347350
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
348351
walk_expr(self, ex)
349352
}
@@ -716,6 +719,15 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
716719
visitor.visit_nested_body(constant.body)
717720
}
718721

722+
pub fn walk_const_arg<'v, V: Visitor<'v>>(
723+
visitor: &mut V,
724+
const_arg: &'v ConstArg<'v>,
725+
) -> V::Result {
726+
match const_arg.kind {
727+
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
728+
}
729+
}
730+
719731
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
720732
try_visit!(visitor.visit_id(expression.hir_id));
721733
match expression.kind {
@@ -1203,7 +1215,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(
12031215
match generic_arg {
12041216
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
12051217
GenericArg::Type(ty) => visitor.visit_ty(ty),
1206-
GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
1218+
GenericArg::Const(ct) => visitor.visit_const_arg(ct),
12071219
GenericArg::Infer(inf) => visitor.visit_infer(inf),
12081220
}
12091221
}

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
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

+3-2
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, TyCtxt,
1717
};
@@ -112,7 +112,8 @@ fn generic_arg_mismatch_err(
112112
}
113113
}
114114
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
115-
let body = tcx.hir().body(cnst.value.body);
115+
let ConstArgKind::Anon(anon) = cnst.kind;
116+
let body = tcx.hir().body(anon.body);
116117
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
117118
{
118119
if let Res::Def(DefKind::Fn { .. }, id) = path.res {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
3131
use rustc_errors::{
3232
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, FatalError,
3333
};
34-
use rustc_hir as hir;
3534
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
3635
use rustc_hir::def_id::{DefId, LocalDefId};
3736
use rustc_hir::intravisit::{walk_generics, Visitor as _};
37+
use rustc_hir::{self as hir, ConstArgKind};
3838
use rustc_hir::{GenericArg, GenericArgs, HirId};
3939
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
4040
use rustc_infer::traits::ObligationCause;
@@ -479,11 +479,13 @@ 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)) => {
483-
let did = ct.value.def_id;
484-
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
485-
ty::Const::from_anon_const(tcx, did).into()
486-
}
482+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
483+
ConstArgKind::Anon(anon) => {
484+
let did = anon.def_id;
485+
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
486+
ty::Const::from_anon_const(tcx, did).into()
487+
}
488+
},
487489
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
488490
let ty = tcx
489491
.at(self.span)

compiler/rustc_hir_pretty/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::util::parser::{self, AssocOp, Fixity};
88
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
99
use rustc_ast_pretty::pp::{self, Breaks};
1010
use rustc_ast_pretty::pprust::{Comments, PrintState};
11-
use rustc_hir as hir;
11+
use rustc_hir::{self as hir, ConstArgKind};
1212
use rustc_hir::{
1313
BindingMode, ByRef, GenericArg, GenericBound, GenericParam, GenericParamKind, HirId,
1414
LifetimeParamKind, Node, PatKind, RangeEnd, Term, TraitBoundModifier,
@@ -982,6 +982,12 @@ impl<'a> State<'a> {
982982
self.ann.nested(self, Nested::Body(constant.body))
983983
}
984984

985+
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
986+
match &const_arg.kind {
987+
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
988+
}
989+
}
990+
985991
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
986992
self.popen();
987993
self.commasep_exprs(Inconsistent, args);
@@ -1670,7 +1676,7 @@ impl<'a> State<'a> {
16701676
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
16711677
GenericArg::Lifetime(_) => {}
16721678
GenericArg::Type(ty) => s.print_type(ty),
1673-
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
1679+
GenericArg::Const(ct) => s.print_const_arg(ct),
16741680
GenericArg::Infer(_inf) => s.word("_"),
16751681
}
16761682
});

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+20-12
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::{
@@ -446,16 +446,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
446446
}
447447
}
448448

449-
pub fn lower_const_arg(&self, hir_ct: &hir::AnonConst, param_def_id: DefId) -> ty::Const<'tcx> {
450-
let did = hir_ct.def_id;
451-
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
452-
let ct = ty::Const::from_anon_const(self.tcx, did);
453-
self.register_wf_obligation(
454-
ct.into(),
455-
self.tcx.hir().span(hir_ct.hir_id),
456-
ObligationCauseCode::WellFormed(None),
457-
);
458-
ct
449+
pub fn lower_const_arg(
450+
&self,
451+
const_arg: &ConstArg<'tcx>,
452+
param_def_id: DefId,
453+
) -> ty::Const<'tcx> {
454+
match &const_arg.kind {
455+
ConstArgKind::Anon(anon) => {
456+
let did = anon.def_id;
457+
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
465+
}
466+
}
459467
}
460468

461469
// If the type given by the user has free regions, save it for later, since
@@ -1282,7 +1290,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12821290
self.fcx.lower_ty(ty).raw.into()
12831291
}
12841292
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
1285-
self.fcx.lower_const_arg(&ct.value, param.def_id).into()
1293+
self.fcx.lower_const_arg(ct, param.def_id).into()
12861294
}
12871295
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
12881296
self.fcx.ty_infer(Some(param), inf.span).into()

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
394394
self.cfcx.lower_ty(ty).raw.into()
395395
}
396396
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
397-
self.cfcx.lower_const_arg(&ct.value, param.def_id).into()
397+
self.cfcx.lower_const_arg(ct, param.def_id).into()
398398
}
399399
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
400400
self.cfcx.ty_infer(Some(param), inf.span).into()

compiler/rustc_lint/src/pass_by_value.rs

+1-1
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

+1-1
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

+10-4
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;
@@ -282,10 +282,16 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
282282
}
283283

284284
pub(crate) fn clean_const<'tcx>(
285-
constant: &hir::ConstArg<'_>,
285+
constant: &hir::ConstArg<'tcx>,
286286
cx: &mut DocContext<'tcx>,
287287
) -> Constant {
288-
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
288+
match &constant.kind {
289+
ConstArgKind::Anon(anon) => clean_anon_const(anon, cx),
290+
}
291+
}
292+
293+
fn clean_anon_const<'tcx>(anon: &'tcx hir::AnonConst, cx: &mut DocContext<'tcx>) -> Constant {
294+
let def_id = cx.tcx.hir().body_owner_def_id(anon.body).to_def_id();
289295
Constant {
290296
type_: Box::new(clean_middle_ty(
291297
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
@@ -294,7 +300,7 @@ pub(crate) fn clean_const<'tcx>(
294300
None,
295301
)),
296302
generics: Generics::default(),
297-
kind: ConstantKind::Anonymous { body: constant.value.body },
303+
kind: ConstantKind::Anonymous { body: anon.body },
298304
}
299305
}
300306

0 commit comments

Comments
 (0)