Skip to content

Commit ac7a867

Browse files
authored
Rollup merge of rust-lang#91907 - lcnr:const-arg-infer, r=BoxyUwU
Allow `_` as the length of array types and repeat expressions r? `@BoxyUwU` cc `@varkor`
2 parents d49c692 + a02bf76 commit ac7a867

File tree

28 files changed

+268
-85
lines changed

28 files changed

+268
-85
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
3434
}
3535
ExprKind::Repeat(ref expr, ref count) => {
3636
let expr = self.lower_expr(expr);
37-
let count = self.lower_anon_const(count);
37+
let count = self.lower_array_length(count);
3838
hir::ExprKind::Repeat(expr, count)
3939
}
4040
ExprKind::Tup(ref elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),

compiler/rustc_ast_lowering/src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use rustc_hir::{ConstArg, GenericArg, ParamName};
5656
use rustc_index::vec::{Idx, IndexVec};
5757
use rustc_query_system::ich::StableHashingContext;
5858
use rustc_session::lint::LintBuffer;
59+
use rustc_session::parse::feature_err;
5960
use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
6061
use rustc_session::Session;
6162
use rustc_span::hygiene::ExpnId;
@@ -1248,7 +1249,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12481249
))
12491250
}
12501251
TyKind::Array(ref ty, ref length) => {
1251-
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_anon_const(length))
1252+
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_array_length(length))
12521253
}
12531254
TyKind::Typeof(ref expr) => hir::TyKind::Typeof(self.lower_anon_const(expr)),
12541255
TyKind::TraitObject(ref bounds, kind) => {
@@ -2039,6 +2040,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20392040
self.expr_block(block, AttrVec::new())
20402041
}
20412042

2043+
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
2044+
match c.value.kind {
2045+
ExprKind::Underscore => {
2046+
if self.sess.features_untracked().generic_arg_infer {
2047+
hir::ArrayLen::Infer(self.lower_node_id(c.id), c.value.span)
2048+
} else {
2049+
feature_err(
2050+
&self.sess.parse_sess,
2051+
sym::generic_arg_infer,
2052+
c.value.span,
2053+
"using `_` for array lengths is unstable",
2054+
)
2055+
.emit();
2056+
hir::ArrayLen::Body(self.lower_anon_const(c))
2057+
}
2058+
}
2059+
_ => hir::ArrayLen::Body(self.lower_anon_const(c)),
2060+
}
2061+
}
2062+
20422063
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
20432064
self.with_new_scopes(|this| hir::AnonConst {
20442065
hir_id: this.lower_node_id(c.id),

compiler/rustc_hir/src/hir.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,20 @@ impl fmt::Display for ConstContext {
14071407
/// A literal.
14081408
pub type Lit = Spanned<LitKind>;
14091409

1410+
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)]
1411+
pub enum ArrayLen {
1412+
Infer(HirId, Span),
1413+
Body(AnonConst),
1414+
}
1415+
1416+
impl ArrayLen {
1417+
pub fn hir_id(&self) -> HirId {
1418+
match self {
1419+
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, body: _ }) => hir_id,
1420+
}
1421+
}
1422+
}
1423+
14101424
/// A constant (expression) that's not an item or associated item,
14111425
/// but needs its own `DefId` for type-checking, const-eval, etc.
14121426
/// These are usually found nested inside types (e.g., array lengths)
@@ -1756,7 +1770,7 @@ pub enum ExprKind<'hir> {
17561770
///
17571771
/// E.g., `[1; 5]`. The first expression is the element
17581772
/// to be repeated; the second is the number of times to repeat it.
1759-
Repeat(&'hir Expr<'hir>, AnonConst),
1773+
Repeat(&'hir Expr<'hir>, ArrayLen),
17601774

17611775
/// A suspension point for generators (i.e., `yield <expr>`).
17621776
Yield(&'hir Expr<'hir>, YieldSource),
@@ -2266,7 +2280,7 @@ pub enum TyKind<'hir> {
22662280
/// A variable length slice (i.e., `[T]`).
22672281
Slice(&'hir Ty<'hir>),
22682282
/// A fixed length array (i.e., `[T; n]`).
2269-
Array(&'hir Ty<'hir>, AnonConst),
2283+
Array(&'hir Ty<'hir>, ArrayLen),
22702284
/// A raw pointer (i.e., `*const T` or `*mut T`).
22712285
Ptr(MutTy<'hir>),
22722286
/// A reference (i.e., `&'a T` or `&'a mut T`).

compiler/rustc_hir/src/intravisit.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ pub trait Visitor<'v>: Sized {
383383
fn visit_pat(&mut self, p: &'v Pat<'v>) {
384384
walk_pat(self, p)
385385
}
386+
fn visit_array_length(&mut self, len: &'v ArrayLen) {
387+
walk_array_len(self, len)
388+
}
386389
fn visit_anon_const(&mut self, c: &'v AnonConst) {
387390
walk_anon_const(self, c)
388391
}
@@ -753,7 +756,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
753756
}
754757
TyKind::Array(ref ty, ref length) => {
755758
visitor.visit_ty(ty);
756-
visitor.visit_anon_const(length)
759+
visitor.visit_array_length(length)
757760
}
758761
TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
759762
for bound in bounds {
@@ -1124,6 +1127,13 @@ pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt<'v>) {
11241127
}
11251128
}
11261129

1130+
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) {
1131+
match len {
1132+
&ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id),
1133+
ArrayLen::Body(c) => visitor.visit_anon_const(c),
1134+
}
1135+
}
1136+
11271137
pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
11281138
visitor.visit_id(constant.hir_id);
11291139
visitor.visit_nested_body(constant.body);
@@ -1147,7 +1157,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
11471157
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
11481158
ExprKind::Repeat(ref element, ref count) => {
11491159
visitor.visit_expr(element);
1150-
visitor.visit_anon_const(count)
1160+
visitor.visit_array_length(count)
11511161
}
11521162
ExprKind::Struct(ref qpath, fields, ref optional_base) => {
11531163
visitor.visit_qpath(qpath, expression.hir_id, expression.span);

compiler/rustc_hir_pretty/src/lib.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'a> State<'a> {
358358
self.word("[");
359359
self.print_type(&ty);
360360
self.word("; ");
361-
self.print_anon_const(length);
361+
self.print_array_length(length);
362362
self.word("]");
363363
}
364364
hir::TyKind::Typeof(ref e) => {
@@ -1065,6 +1065,13 @@ impl<'a> State<'a> {
10651065
self.print_else(elseopt)
10661066
}
10671067

1068+
pub fn print_array_length(&mut self, len: &hir::ArrayLen) {
1069+
match len {
1070+
hir::ArrayLen::Infer(_, _) => self.word("_"),
1071+
hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
1072+
}
1073+
}
1074+
10681075
pub fn print_anon_const(&mut self, constant: &hir::AnonConst) {
10691076
self.ann.nested(self, Nested::Body(constant.body))
10701077
}
@@ -1140,12 +1147,12 @@ impl<'a> State<'a> {
11401147
self.end()
11411148
}
11421149

1143-
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::AnonConst) {
1150+
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen) {
11441151
self.ibox(INDENT_UNIT);
11451152
self.word("[");
11461153
self.print_expr(element);
11471154
self.word_space(";");
1148-
self.print_anon_const(count);
1155+
self.print_array_length(count);
11491156
self.word("]");
11501157
self.end()
11511158
}

compiler/rustc_mir_build/src/thir/cx/expr.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,12 @@ impl<'tcx> Cx<'tcx> {
583583
ExprKind::ConstBlock { value }
584584
}
585585
// Now comes the rote stuff:
586-
hir::ExprKind::Repeat(ref v, ref count) => {
587-
let count_def_id = self.tcx.hir().local_def_id(count.hir_id);
588-
let count = ty::Const::from_anon_const(self.tcx, count_def_id);
586+
hir::ExprKind::Repeat(ref v, _) => {
587+
let ty = self.typeck_results().expr_ty(expr);
588+
let count = match ty.kind() {
589+
ty::Array(_, ct) => ct,
590+
_ => span_bug!(expr.span, "unexpected repeat expr ty: {:?}", ty),
591+
};
589592

590593
ExprKind::Repeat { value: self.mirror_expr(v), count }
591594
}

compiler/rustc_save_analysis/src/dump_visitor.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -1326,12 +1326,18 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
13261326
}
13271327
intravisit::walk_qpath(self, path, t.hir_id, t.span);
13281328
}
1329-
hir::TyKind::Array(ref ty, ref anon_const) => {
1329+
hir::TyKind::Array(ref ty, ref length) => {
13301330
self.visit_ty(ty);
13311331
let map = self.tcx.hir();
1332-
self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1333-
v.visit_expr(&map.body(anon_const.body).value)
1334-
});
1332+
match length {
1333+
// FIXME(generic_arg_infer): We probably want to
1334+
// output the inferred type here? :shrug:
1335+
hir::ArrayLen::Infer(..) => {}
1336+
hir::ArrayLen::Body(anon_const) => self
1337+
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1338+
v.visit_expr(&map.body(anon_const.body).value)
1339+
}),
1340+
}
13351341
}
13361342
hir::TyKind::OpaqueDef(item_id, _) => {
13371343
let item = self.tcx.hir().item(item_id);
@@ -1390,12 +1396,18 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
13901396
v.visit_expr(&body.value)
13911397
});
13921398
}
1393-
hir::ExprKind::Repeat(ref expr, ref anon_const) => {
1399+
hir::ExprKind::Repeat(ref expr, ref length) => {
13941400
self.visit_expr(expr);
13951401
let map = self.tcx.hir();
1396-
self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1397-
v.visit_expr(&map.body(anon_const.body).value)
1398-
});
1402+
match length {
1403+
// FIXME(generic_arg_infer): We probably want to
1404+
// output the inferred type here? :shrug:
1405+
hir::ArrayLen::Infer(..) => {}
1406+
hir::ArrayLen::Body(anon_const) => self
1407+
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1408+
v.visit_expr(&map.body(anon_const.body).value)
1409+
}),
1410+
}
13991411
}
14001412
// In particular, we take this branch for call and path expressions,
14011413
// where we'll index the idents involved just by continuing to walk.

compiler/rustc_save_analysis/src/sig.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@ impl<'hir> Sig for hir::Ty<'hir> {
310310
let nested = bounds_to_string(&bounds);
311311
Ok(text_sig(nested))
312312
}
313-
hir::TyKind::Array(ref ty, ref anon_const) => {
313+
hir::TyKind::Array(ref ty, ref length) => {
314314
let nested_ty = ty.make(offset + 1, id, scx)?;
315-
let expr = id_to_string(&scx.tcx.hir(), anon_const.body.hir_id).replace('\n', " ");
315+
let expr = id_to_string(&scx.tcx.hir(), length.hir_id()).replace('\n', " ");
316316
let text = format!("[{}; {}]", nested_ty.text, expr);
317317
Ok(replace_text(nested_ty, text))
318318
}

compiler/rustc_typeck/src/astconv/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
104104
GenericArg::Type(hir::Ty { kind: hir::TyKind::Array(_, len), .. }),
105105
GenericParamDefKind::Const { .. },
106106
) if tcx.type_of(param.def_id) == tcx.types.usize => {
107-
let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id));
107+
let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id()));
108108
if let Ok(snippet) = snippet {
109109
err.span_suggestion(
110110
arg.span(),

compiler/rustc_typeck/src/astconv/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2363,8 +2363,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23632363
self.normalize_ty(span, tcx.at(span).type_of(def_id).subst(tcx, substs))
23642364
}
23652365
hir::TyKind::Array(ref ty, ref length) => {
2366-
let length_def_id = tcx.hir().local_def_id(length.hir_id);
2367-
let length = ty::Const::from_anon_const(tcx, length_def_id);
2366+
let length = match length {
2367+
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
2368+
hir::ArrayLen::Body(constant) => {
2369+
let length_def_id = tcx.hir().local_def_id(constant.hir_id);
2370+
ty::Const::from_anon_const(tcx, length_def_id)
2371+
}
2372+
};
2373+
23682374
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(ty), length));
23692375
self.normalize_ty(ast_ty.span, array_ty)
23702376
}

compiler/rustc_typeck/src/check/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1238,12 +1238,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12381238
fn check_expr_repeat(
12391239
&self,
12401240
element: &'tcx hir::Expr<'tcx>,
1241-
count: &'tcx hir::AnonConst,
1241+
count: &'tcx hir::ArrayLen,
12421242
expected: Expectation<'tcx>,
12431243
_expr: &'tcx hir::Expr<'tcx>,
12441244
) -> Ty<'tcx> {
12451245
let tcx = self.tcx;
1246-
let count = self.to_const(count);
1246+
let count = self.array_length_to_const(count);
12471247

12481248
let uty = match expected {
12491249
ExpectHasType(uty) => match *uty.kind() {

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
498498
ty
499499
}
500500

501+
pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> &'tcx ty::Const<'tcx> {
502+
match length {
503+
&hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
504+
hir::ArrayLen::Body(anon_const) => self.to_const(anon_const),
505+
}
506+
}
507+
501508
pub fn to_const(&self, ast_c: &hir::AnonConst) -> &'tcx ty::Const<'tcx> {
502509
let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id);
503510
let c = ty::Const::from_anon_const(self.tcx, const_def_id);

compiler/rustc_typeck/src/collect.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ crate fn placeholder_type_error<'tcx>(
182182
sugg.push((span, format!(", {}", type_name)));
183183
}
184184

185-
let mut err = bad_placeholder_type(tcx, placeholder_types, kind);
185+
let mut err = bad_placeholder(tcx, "type", placeholder_types, kind);
186186

187187
// Suggest, but only if it is not a function in const or static
188188
if suggest {
@@ -314,8 +314,9 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
314314
///////////////////////////////////////////////////////////////////////////
315315
// Utility types and common code for the above passes.
316316

317-
fn bad_placeholder_type<'tcx>(
317+
fn bad_placeholder<'tcx>(
318318
tcx: TyCtxt<'tcx>,
319+
placeholder_kind: &'static str,
319320
mut spans: Vec<Span>,
320321
kind: &'static str,
321322
) -> rustc_errors::DiagnosticBuilder<'tcx> {
@@ -326,7 +327,8 @@ fn bad_placeholder_type<'tcx>(
326327
tcx.sess,
327328
spans.clone(),
328329
E0121,
329-
"the type placeholder `_` is not allowed within types on item signatures for {}",
330+
"the {} placeholder `_` is not allowed within types on item signatures for {}",
331+
placeholder_kind,
330332
kind
331333
);
332334
for span in spans {
@@ -393,7 +395,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
393395
_: Option<&ty::GenericParamDef>,
394396
span: Span,
395397
) -> &'tcx Const<'tcx> {
396-
bad_placeholder_type(self.tcx(), vec![span], "generic").emit();
398+
bad_placeholder(self.tcx(), "const", vec![span], "generic").emit();
397399
// Typeck doesn't expect erased regions to be returned from `type_of`.
398400
let ty = self.tcx.fold_regions(ty, &mut false, |r, _| match r {
399401
ty::ReErased => self.tcx.lifetimes.re_static,
@@ -1482,7 +1484,11 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
14821484
// `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
14831485
// as they shouldn't be able to cause query cycle errors.
14841486
Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
1485-
| Node::Variant(Variant { disr_expr: Some(ref constant), .. })
1487+
if constant.hir_id() == hir_id =>
1488+
{
1489+
Some(parent_def_id.to_def_id())
1490+
}
1491+
Node::Variant(Variant { disr_expr: Some(ref constant), .. })
14861492
if constant.hir_id == hir_id =>
14871493
{
14881494
Some(parent_def_id.to_def_id())
@@ -1788,7 +1794,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17881794

17891795
let mut visitor = PlaceholderHirTyCollector::default();
17901796
visitor.visit_ty(ty);
1791-
let mut diag = bad_placeholder_type(tcx, visitor.0, "return type");
1797+
let mut diag = bad_placeholder(tcx, "type", visitor.0, "return type");
17921798
let ret_ty = fn_sig.skip_binder().output();
17931799
if !ret_ty.references_error() {
17941800
if !ret_ty.is_closure() {

compiler/rustc_typeck/src/collect/type_of.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_span::symbol::Ident;
1313
use rustc_span::{Span, DUMMY_SP};
1414

1515
use super::ItemCtxt;
16-
use super::{bad_placeholder_type, is_suggestable_infer_ty};
16+
use super::{bad_placeholder, is_suggestable_infer_ty};
1717

1818
/// Computes the relevant generic parameter for a potential generic const argument.
1919
///
@@ -490,7 +490,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
490490
match parent_node {
491491
Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
492492
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
493-
if constant.hir_id == hir_id =>
493+
if constant.hir_id() == hir_id =>
494494
{
495495
tcx.types.usize
496496
}
@@ -788,7 +788,7 @@ fn infer_placeholder_type<'a>(
788788
err.emit();
789789
}
790790
None => {
791-
let mut diag = bad_placeholder_type(tcx, vec![span], kind);
791+
let mut diag = bad_placeholder(tcx, "type", vec![span], kind);
792792

793793
if !ty.references_error() {
794794
let mut mk_nameable = MakeNameable::new(tcx);

0 commit comments

Comments
 (0)