@@ -4,7 +4,7 @@ use super::{
44 AttrWrapper , FollowedByType , ForceCollect , Parser , PathStyle , Recovered , Trailing ,
55 TrailingToken ,
66} ;
7- use crate :: errors:: { self , MacroExpandsToAdtField } ;
7+ use crate :: errors:: { self , MacroExpandsToAdtField , UnexpectedExpressionInPatternConstSugg } ;
88use crate :: fluent_generated as fluent;
99use crate :: maybe_whole;
1010use ast:: token:: IdentIsRaw ;
@@ -13,6 +13,7 @@ use rustc_ast::ptr::P;
1313use rustc_ast:: token:: { self , Delimiter , TokenKind } ;
1414use rustc_ast:: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
1515use rustc_ast:: util:: case:: Case ;
16+ use rustc_ast:: visit:: { walk_pat, walk_stmt, Visitor } ;
1617use rustc_ast:: { self as ast} ;
1718use rustc_ast_pretty:: pprust;
1819use rustc_errors:: { codes:: * , struct_span_code_err, Applicability , PResult , StashKey } ;
@@ -2362,6 +2363,52 @@ impl<'a> Parser<'a> {
23622363 }
23632364 ( AttrVec :: new ( ) , None )
23642365 } ;
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+
23652412 attrs. extend ( inner_attrs) ;
23662413 Ok ( body)
23672414 }
0 commit comments