Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 67fccb7

Browse files
committed
Use ConstArg for array lengths
1 parent 8818708 commit 67fccb7

File tree

14 files changed

+77
-45
lines changed

14 files changed

+77
-45
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,10 +2342,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23422342
"using `_` for array lengths is unstable",
23432343
)
23442344
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2345-
hir::ArrayLen::Body(self.lower_anon_const_to_anon_const(c))
2345+
hir::ArrayLen::Body(self.lower_anon_const_to_const_arg(c))
23462346
}
23472347
}
2348-
_ => hir::ArrayLen::Body(self.lower_anon_const_to_anon_const(c)),
2348+
_ => hir::ArrayLen::Body(self.lower_anon_const_to_const_arg(c)),
23492349
}
23502350
}
23512351

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ impl<'hir> ConstArg<'hir> {
242242
}
243243
}
244244

245+
// FIXME: convert to field, where ConstArg has its own HirId
246+
pub fn hir_id(&self) -> HirId {
247+
self.anon_const_hir_id()
248+
}
249+
245250
pub fn anon_const_hir_id(&self) -> HirId {
246251
match self.kind {
247252
ConstArgKind::Anon(anon) => anon.hir_id,
@@ -288,7 +293,7 @@ impl GenericArg<'_> {
288293
match self {
289294
GenericArg::Lifetime(l) => l.hir_id,
290295
GenericArg::Type(t) => t.hir_id,
291-
GenericArg::Const(c) => c.anon_const_hir_id(), // FIXME
296+
GenericArg::Const(c) => c.hir_id(),
292297
GenericArg::Infer(i) => i.hir_id,
293298
}
294299
}
@@ -1617,15 +1622,14 @@ pub type Lit = Spanned<LitKind>;
16171622
#[derive(Copy, Clone, Debug, HashStable_Generic)]
16181623
pub enum ArrayLen<'hir> {
16191624
Infer(InferArg),
1620-
Body(&'hir AnonConst),
1625+
Body(&'hir ConstArg<'hir>),
16211626
}
16221627

16231628
impl ArrayLen<'_> {
16241629
pub fn hir_id(&self) -> HirId {
16251630
match self {
1626-
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
1627-
*hir_id
1628-
}
1631+
ArrayLen::Infer(InferArg { hir_id, .. }) => *hir_id,
1632+
ArrayLen::Body(ct) => ct.hir_id(),
16291633
}
16301634
}
16311635
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen<'v>
711711
match len {
712712
// FIXME: Use `visit_infer` here.
713713
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),
714-
ArrayLen::Body(c) => visitor.visit_anon_const(c),
714+
ArrayLen::Body(c) => visitor.visit_const_arg(c),
715715
}
716716
}
717717

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21402140
let length = match length {
21412141
hir::ArrayLen::Infer(inf) => self.ct_infer(None, inf.span),
21422142
hir::ArrayLen::Body(constant) => {
2143-
ty::Const::from_anon_const(tcx, constant.def_id)
2143+
ty::Const::from_const_arg(tcx, constant, ty::FeedConstTy::No)
21442144
}
21452145
};
21462146

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ impl<'a> State<'a> {
983983
fn print_array_length(&mut self, len: &hir::ArrayLen<'_>) {
984984
match len {
985985
hir::ArrayLen::Infer(..) => self.word("_"),
986-
hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
986+
hir::ArrayLen::Body(ct) => self.print_const_arg(ct),
987987
}
988988
}
989989

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,9 +1439,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14391439
return;
14401440
};
14411441
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
1442-
&& let hir::ArrayLen::Body(&hir::AnonConst { hir_id, .. }) = length
1442+
&& let hir::ArrayLen::Body(ct) = length
14431443
{
1444-
let span = self.tcx.hir().span(hir_id);
1444+
let span = ct.span();
14451445
self.dcx().try_steal_modify_and_emit_err(
14461446
span,
14471447
StashKey::UnderscoreForArrayLengths,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
457457
pub fn lower_array_length(&self, length: &hir::ArrayLen<'tcx>) -> ty::Const<'tcx> {
458458
match length {
459459
hir::ArrayLen::Infer(inf) => self.ct_infer(None, inf.span),
460-
hir::ArrayLen::Body(anon_const) => {
461-
let span = self.tcx.def_span(anon_const.def_id);
462-
let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);
460+
hir::ArrayLen::Body(const_arg) => {
461+
let span = const_arg.span();
462+
let c = ty::Const::from_const_arg(self.tcx, const_arg, ty::FeedConstTy::No);
463463
self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
464464
self.normalize(span, c)
465465
}

compiler/rustc_infer/src/error_reporting/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,9 +1762,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17621762
};
17631763
if let Some(tykind) = tykind
17641764
&& let hir::TyKind::Array(_, length) = tykind
1765-
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
1765+
&& let hir::ArrayLen::Body(ct) = length
17661766
{
1767-
let span = self.tcx.hir().span(*hir_id);
1767+
let span = ct.span();
17681768
Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength { span, length: sz.found })
17691769
} else {
17701770
None

src/librustdoc/clean/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,17 +1822,27 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18221822
TyKind::Array(ty, ref length) => {
18231823
let length = match length {
18241824
hir::ArrayLen::Infer(..) => "_".to_string(),
1825-
hir::ArrayLen::Body(anon_const) => {
1825+
hir::ArrayLen::Body(const_arg) => {
18261826
// NOTE(min_const_generics): We can't use `const_eval_poly` for constants
18271827
// as we currently do not supply the parent generics to anonymous constants
18281828
// but do allow `ConstKind::Param`.
18291829
//
18301830
// `const_eval_poly` tries to first substitute generic parameters which
18311831
// results in an ICE while manually constructing the constant and using `eval`
18321832
// does nothing for `ConstKind::Param`.
1833-
let ct = ty::Const::from_anon_const(cx.tcx, anon_const.def_id);
1834-
let param_env = cx.tcx.param_env(anon_const.def_id);
1835-
print_const(cx, ct.normalize(cx.tcx, param_env))
1833+
let ct = ty::Const::from_const_arg(cx.tcx, const_arg, ty::FeedConstTy::No);
1834+
#[allow(irrefutable_let_patterns)] // FIXME
1835+
let ct = if let hir::ConstArgKind::Anon(hir::AnonConst { def_id, .. }) =
1836+
const_arg.kind
1837+
{
1838+
// Only anon consts can implicitly capture params.
1839+
// FIXME: is this correct behavior?
1840+
let param_env = cx.tcx.param_env(*def_id);
1841+
ct.normalize(cx.tcx, param_env)
1842+
} else {
1843+
ct
1844+
};
1845+
print_const(cx, ct)
18361846
}
18371847
};
18381848

src/tools/clippy/clippy_lints/src/large_stack_arrays.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ fn might_be_expanded<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
106106
///
107107
/// This is a fail-safe to a case where even the `is_from_proc_macro` is unable to determain the
108108
/// correct result.
109-
fn repeat_expr_might_be_expanded<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
110-
let ExprKind::Repeat(_, ArrayLen::Body(anon_const)) = expr.kind else {
109+
fn repeat_expr_might_be_expanded<'tcx>(expr: &Expr<'tcx>) -> bool {
110+
let ExprKind::Repeat(_, ArrayLen::Body(len_ct)) = expr.kind else {
111111
return false;
112112
};
113-
let len_span = cx.tcx.def_span(anon_const.def_id);
114-
!expr.span.contains(len_span)
113+
!expr.span.contains(len_ct.span())
115114
}
116115

117-
expr.span.from_expansion() || is_from_proc_macro(cx, expr) || repeat_expr_might_be_expanded(cx, expr)
116+
expr.span.from_expansion() || is_from_proc_macro(cx, expr) || repeat_expr_might_be_expanded(expr)
118117
}

0 commit comments

Comments
 (0)