@@ -4,7 +4,7 @@ use super::{
4
4
AttrWrapper , FollowedByType , ForceCollect , Parser , PathStyle , Recovered , Trailing ,
5
5
TrailingToken ,
6
6
} ;
7
- use crate :: errors:: { self , MacroExpandsToAdtField } ;
7
+ use crate :: errors:: { self , MacroExpandsToAdtField , UnexpectedExpressionInPatternConstSugg } ;
8
8
use crate :: fluent_generated as fluent;
9
9
use crate :: maybe_whole;
10
10
use ast:: token:: IdentIsRaw ;
@@ -13,6 +13,7 @@ use rustc_ast::ptr::P;
13
13
use rustc_ast:: token:: { self , Delimiter , TokenKind } ;
14
14
use rustc_ast:: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
15
15
use rustc_ast:: util:: case:: Case ;
16
+ use rustc_ast:: visit:: { walk_pat, walk_stmt, Visitor } ;
16
17
use rustc_ast:: { self as ast} ;
17
18
use rustc_ast_pretty:: pprust;
18
19
use rustc_errors:: { codes:: * , struct_span_code_err, Applicability , PResult , StashKey } ;
@@ -2362,6 +2363,52 @@ impl<'a> Parser<'a> {
2362
2363
}
2363
2364
( AttrVec :: new ( ) , None )
2364
2365
} ;
2366
+
2367
+ if let Some ( body) = & body {
2368
+ struct PatVisitor < ' a > {
2369
+ parser : & ' a Parser < ' a > ,
2370
+ stmt : Option < & ' a Stmt > ,
2371
+ }
2372
+
2373
+ impl < ' a > Visitor < ' a > for PatVisitor < ' a > {
2374
+ fn visit_stmt ( & mut self , s : & ' a Stmt ) -> Self :: Result {
2375
+ self . stmt = Some ( s) ;
2376
+
2377
+ walk_stmt ( self , s)
2378
+ }
2379
+
2380
+ fn visit_pat ( & mut self , pat : & ' a Pat ) -> Self :: Result {
2381
+ if matches ! ( pat. kind, PatKind :: Err ( _) ) {
2382
+ let sm = self . parser . psess . source_map ( ) ;
2383
+
2384
+ self . parser . dcx ( ) . try_steal_modify_and_emit_err (
2385
+ pat. span ,
2386
+ StashKey :: ExprInPat ,
2387
+ |err| {
2388
+ err. subdiagnostic (
2389
+ & self . parser . dcx ( ) ,
2390
+ UnexpectedExpressionInPatternConstSugg {
2391
+ const_span : sm
2392
+ . span_extend_to_line ( self . stmt . unwrap ( ) . span )
2393
+ . shrink_to_lo ( ) ,
2394
+ pat_span : pat. span ,
2395
+ expr : self . parser . span_to_snippet ( pat. span ) . unwrap ( ) ,
2396
+ indentation : sm
2397
+ . indentation_before ( self . stmt . unwrap ( ) . span )
2398
+ . unwrap_or_default ( ) ,
2399
+ } ,
2400
+ ) ;
2401
+ } ,
2402
+ ) ;
2403
+ }
2404
+
2405
+ walk_pat ( self , pat)
2406
+ }
2407
+ }
2408
+
2409
+ PatVisitor { parser : self , stmt : None } . visit_block ( body) ;
2410
+ }
2411
+
2365
2412
attrs. extend ( inner_attrs) ;
2366
2413
Ok ( body)
2367
2414
}
0 commit comments