11use std:: assert_matches:: assert_matches;
2+ use std:: ops:: ControlFlow ;
23
34use rustc_ast:: ptr:: P as AstP ;
45use rustc_ast:: * ;
@@ -12,6 +13,7 @@ use rustc_span::source_map::{respan, Spanned};
1213use rustc_span:: symbol:: { kw, sym, Ident , Symbol } ;
1314use rustc_span:: { DesugaringKind , Span , DUMMY_SP } ;
1415use thin_vec:: { thin_vec, ThinVec } ;
16+ use visit:: Visitor ;
1517
1618use super :: errors:: {
1719 AsyncCoroutinesNotSupported , AwaitOnlyInAsyncFnAndBlocks , BaseExpressionDoubleDot ,
@@ -22,9 +24,28 @@ use super::errors::{
2224use super :: {
2325 GenericArgsMode , ImplTraitContext , LoweringContext , ParamMode , ResolverAstLoweringExt ,
2426} ;
25- use crate :: errors:: YieldInClosure ;
27+ use crate :: errors:: { InvalidLegacyConstGenericArg , YieldInClosure } ;
2628use crate :: { fluent_generated, FnDeclKind , ImplTraitPosition } ;
2729
30+ struct WillCreateDefIdsVisitor { }
31+
32+ impl < ' v > rustc_ast:: visit:: Visitor < ' v > for WillCreateDefIdsVisitor {
33+ type Result = ControlFlow < ( ) > ;
34+
35+ fn visit_item ( & mut self , _: & ' v Item ) -> Self :: Result {
36+ ControlFlow :: Break ( ( ) )
37+ }
38+
39+ fn visit_expr ( & mut self , ex : & ' v Expr ) -> Self :: Result {
40+ match ex. kind {
41+ ExprKind :: Gen ( ..) | ExprKind :: ConstBlock ( ..) | ExprKind :: Closure ( ..) => {
42+ ControlFlow :: Break ( ( ) )
43+ }
44+ _ => ControlFlow :: Continue ( ( ) ) ,
45+ }
46+ }
47+ }
48+
2849impl < ' hir > LoweringContext < ' _ , ' hir > {
2950 fn lower_exprs ( & mut self , exprs : & [ AstP < Expr > ] ) -> & ' hir [ hir:: Expr < ' hir > ] {
3051 self . arena . alloc_from_iter ( exprs. iter ( ) . map ( |x| self . lower_expr_mut ( x) ) )
@@ -392,7 +413,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
392413 self . create_def ( parent_def_id, node_id, kw:: Empty , DefKind :: AnonConst , f. span ) ;
393414 }
394415
395- let anon_const = AnonConst { id : node_id, value : arg } ;
416+ let mut visitor = WillCreateDefIdsVisitor { } ;
417+ let const_value = if visitor. visit_expr ( & arg) . is_break ( ) {
418+ AstP ( Expr {
419+ id : self . next_node_id ( ) ,
420+ kind : ExprKind :: Err (
421+ self . tcx
422+ . dcx ( )
423+ . emit_err ( InvalidLegacyConstGenericArg { span : arg. span } ) ,
424+ ) ,
425+ span : f. span ,
426+ attrs : [ ] . into ( ) ,
427+ tokens : None ,
428+ } )
429+ } else {
430+ arg
431+ } ;
432+
433+ let anon_const = AnonConst { id : node_id, value : const_value } ;
396434 generic_args. push ( AngleBracketedArg :: Arg ( GenericArg :: Const ( anon_const) ) ) ;
397435 } else {
398436 real_args. push ( arg) ;
0 commit comments