@@ -28,7 +28,7 @@ use rustc_data_structures::fx::FxHashMap;
28
28
use rustc_data_structures:: stack:: ensure_sufficient_stack;
29
29
use rustc_errors:: {
30
30
pluralize, struct_span_err, Applicability , Diagnostic , DiagnosticBuilder , DiagnosticId ,
31
- ErrorGuaranteed ,
31
+ ErrorGuaranteed , StashKey ,
32
32
} ;
33
33
use rustc_hir as hir;
34
34
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -1307,7 +1307,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1307
1307
span : expr. span ,
1308
1308
} )
1309
1309
} ;
1310
- self . tcx . mk_array ( element_ty, args. len ( ) as u64 )
1310
+ let array_len = args. len ( ) as u64 ;
1311
+ self . suggest_array_len ( expr, array_len) ;
1312
+ self . tcx . mk_array ( element_ty, array_len)
1313
+ }
1314
+
1315
+ fn suggest_array_len ( & self , expr : & ' tcx hir:: Expr < ' tcx > , array_len : u64 ) {
1316
+ if let Some ( parent_hir_id) = self . tcx . hir ( ) . find_parent_node ( expr. hir_id ) {
1317
+ let ty = match self . tcx . hir ( ) . find ( parent_hir_id) {
1318
+ Some (
1319
+ hir:: Node :: Local ( hir:: Local { ty : Some ( ty) , .. } )
1320
+ | hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Const ( ty, _) , .. } ) ,
1321
+ ) => Some ( ty) ,
1322
+ _ => None ,
1323
+ } ;
1324
+ if let Some ( ty) = ty
1325
+ && let hir:: TyKind :: Array ( _, length) = ty. kind
1326
+ && let hir:: ArrayLen :: Body ( hir:: AnonConst { hir_id, .. } ) = length
1327
+ && let Some ( span) = self . tcx . hir ( ) . opt_span ( hir_id)
1328
+ {
1329
+ match self . tcx . sess . diagnostic ( ) . steal_diagnostic ( span, StashKey :: UnderscoreForArrayLengths ) {
1330
+ Some ( mut err) => {
1331
+ err. span_suggestion (
1332
+ span,
1333
+ "consider specifying the array length" ,
1334
+ array_len,
1335
+ Applicability :: MaybeIncorrect ,
1336
+ ) ;
1337
+ err. emit ( ) ;
1338
+ }
1339
+ None => ( )
1340
+ }
1341
+ }
1342
+ }
1311
1343
}
1312
1344
1313
1345
fn check_expr_const_block (
@@ -1333,10 +1365,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1333
1365
element : & ' tcx hir:: Expr < ' tcx > ,
1334
1366
count : & ' tcx hir:: ArrayLen ,
1335
1367
expected : Expectation < ' tcx > ,
1336
- _expr : & ' tcx hir:: Expr < ' tcx > ,
1368
+ expr : & ' tcx hir:: Expr < ' tcx > ,
1337
1369
) -> Ty < ' tcx > {
1338
1370
let tcx = self . tcx ;
1339
1371
let count = self . array_length_to_const ( count) ;
1372
+ if let Some ( count) = count. try_eval_usize ( tcx, self . param_env ) {
1373
+ self . suggest_array_len ( expr, count) ;
1374
+ }
1340
1375
1341
1376
let uty = match expected {
1342
1377
ExpectHasType ( uty) => match * uty. kind ( ) {
0 commit comments