@@ -28,7 +28,7 @@ use rustc_data_structures::fx::FxHashMap;
2828use rustc_data_structures:: stack:: ensure_sufficient_stack;
2929use rustc_errors:: {
3030 pluralize, struct_span_err, Applicability , Diagnostic , DiagnosticBuilder , DiagnosticId ,
31- ErrorGuaranteed ,
31+ ErrorGuaranteed , StashKey ,
3232} ;
3333use rustc_hir as hir;
3434use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
@@ -1307,7 +1307,39 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13071307 span : expr. span ,
13081308 } )
13091309 } ;
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+ }
13111343 }
13121344
13131345 fn check_expr_const_block (
@@ -1333,10 +1365,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13331365 element : & ' tcx hir:: Expr < ' tcx > ,
13341366 count : & ' tcx hir:: ArrayLen ,
13351367 expected : Expectation < ' tcx > ,
1336- _expr : & ' tcx hir:: Expr < ' tcx > ,
1368+ expr : & ' tcx hir:: Expr < ' tcx > ,
13371369 ) -> Ty < ' tcx > {
13381370 let tcx = self . tcx ;
13391371 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+ }
13401375
13411376 let uty = match expected {
13421377 ExpectHasType ( uty) => match * uty. kind ( ) {
0 commit comments