Skip to content

Commit 0ee891d

Browse files
committed
Use ConstArg for array lengths
1 parent 65f16da commit 0ee891d

File tree

14 files changed

+62
-38
lines changed

14 files changed

+62
-38
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,10 +2340,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23402340
"using `_` for array lengths is unstable",
23412341
)
23422342
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2343-
hir::ArrayLen::Body(self.lower_anon_const(c))
2343+
hir::ArrayLen::Body(self.lower_anon_const_as_const_arg(c))
23442344
}
23452345
}
2346-
_ => hir::ArrayLen::Body(self.lower_anon_const(c)),
2346+
_ => hir::ArrayLen::Body(self.lower_anon_const_as_const_arg(c)),
23472347
}
23482348
}
23492349

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,13 +1611,13 @@ pub type Lit = Spanned<LitKind>;
16111611
#[derive(Copy, Clone, Debug, HashStable_Generic)]
16121612
pub enum ArrayLen<'hir> {
16131613
Infer(InferArg),
1614-
Body(&'hir AnonConst),
1614+
Body(&'hir ConstArg<'hir>),
16151615
}
16161616

16171617
impl ArrayLen<'_> {
16181618
pub fn hir_id(&self) -> HirId {
16191619
match self {
1620-
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
1620+
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(ConstArg { hir_id, .. }) => {
16211621
*hir_id
16221622
}
16231623
}

compiler/rustc_hir/src/intravisit.rs

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

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21312131
let length = match length {
21322132
hir::ArrayLen::Infer(inf) => self.ct_infer(None, inf.span),
21332133
hir::ArrayLen::Body(constant) => {
2134-
ty::Const::from_anon_const(tcx, constant.def_id)
2134+
ty::Const::from_const_arg_without_feeding(tcx, constant)
21352135
}
21362136
};
21372137

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ impl<'a> State<'a> {
977977
fn print_array_length(&mut self, len: &hir::ArrayLen<'_>) {
978978
match len {
979979
hir::ArrayLen::Infer(..) => self.word("_"),
980-
hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
980+
hir::ArrayLen::Body(ct) => self.print_const_arg(ct),
981981
}
982982
}
983983

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14401440
return;
14411441
};
14421442
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
1443-
&& let hir::ArrayLen::Body(&hir::AnonConst { hir_id, .. }) = length
1443+
&& let hir::ArrayLen::Body(&hir::ConstArg { hir_id, .. }) = length
14441444
{
14451445
let span = self.tcx.hir().span(hir_id);
14461446
self.dcx().try_steal_modify_and_emit_err(

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_without_feeding(self.tcx, const_arg);
463463
self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None));
464464
self.normalize(span, c)
465465
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21902190
};
21912191
if let Some(tykind) = tykind
21922192
&& let hir::TyKind::Array(_, length) = tykind
2193-
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
2193+
&& let hir::ArrayLen::Body(hir::ConstArg { hir_id, .. }) = length
21942194
{
21952195
let span = self.tcx.hir().span(*hir_id);
21962196
Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength { span, length: sz.found })

src/librustdoc/clean/mod.rs

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

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)