@@ -3,10 +3,11 @@ use super::{Parser, TokenType};
33use crate :: maybe_whole;
44use rustc_ast:: ptr:: P ;
55use rustc_ast:: token:: { self , Token } ;
6- use rustc_ast:: { self as ast, AngleBracketedArg , AngleBracketedArgs , ParenthesizedArgs } ;
7- use rustc_ast:: { AnonConst , AssocTyConstraint , AssocTyConstraintKind , BlockCheckMode } ;
8- use rustc_ast:: { GenericArg , GenericArgs } ;
9- use rustc_ast:: { Path , PathSegment , QSelf } ;
6+ use rustc_ast:: {
7+ self as ast, AngleBracketedArg , AngleBracketedArgs , AnonConst , AssocTyConstraint ,
8+ AssocTyConstraintKind , BlockCheckMode , GenericArg , GenericArgs , Generics , ParenthesizedArgs ,
9+ Path , PathSegment , QSelf ,
10+ } ;
1011use rustc_errors:: { pluralize, Applicability , PResult } ;
1112use rustc_span:: source_map:: { BytePos , Span } ;
1213use rustc_span:: symbol:: { kw, sym, Ident } ;
@@ -78,7 +79,7 @@ impl<'a> Parser<'a> {
7879 }
7980
8081 let qself = QSelf { ty, path_span, position : path. segments . len ( ) } ;
81- self . parse_path_segments ( & mut path. segments , style) ?;
82+ self . parse_path_segments ( & mut path. segments , style, None ) ?;
8283
8384 Ok ( (
8485 qself,
@@ -119,6 +120,10 @@ impl<'a> Parser<'a> {
119120 true
120121 }
121122
123+ pub ( super ) fn parse_path ( & mut self , style : PathStyle ) -> PResult < ' a , Path > {
124+ self . parse_path_inner ( style, None )
125+ }
126+
122127 /// Parses simple paths.
123128 ///
124129 /// `path = [::] segment+`
@@ -129,7 +134,11 @@ impl<'a> Parser<'a> {
129134 /// `a::b::C::<D>` (with disambiguator)
130135 /// `Fn(Args)` (without disambiguator)
131136 /// `Fn::(Args)` (with disambiguator)
132- pub ( super ) fn parse_path ( & mut self , style : PathStyle ) -> PResult < ' a , Path > {
137+ pub ( super ) fn parse_path_inner (
138+ & mut self ,
139+ style : PathStyle ,
140+ ty_generics : Option < & Generics > ,
141+ ) -> PResult < ' a , Path > {
133142 maybe_whole ! ( self , NtPath , |path| {
134143 if style == PathStyle :: Mod && path. segments. iter( ) . any( |segment| segment. args. is_some( ) )
135144 {
@@ -152,7 +161,7 @@ impl<'a> Parser<'a> {
152161 if self . eat ( & token:: ModSep ) {
153162 segments. push ( PathSegment :: path_root ( lo. shrink_to_lo ( ) . with_ctxt ( mod_sep_ctxt) ) ) ;
154163 }
155- self . parse_path_segments ( & mut segments, style) ?;
164+ self . parse_path_segments ( & mut segments, style, ty_generics ) ?;
156165
157166 Ok ( Path { segments, span : lo. to ( self . prev_token . span ) , tokens : None } )
158167 }
@@ -161,9 +170,10 @@ impl<'a> Parser<'a> {
161170 & mut self ,
162171 segments : & mut Vec < PathSegment > ,
163172 style : PathStyle ,
173+ ty_generics : Option < & Generics > ,
164174 ) -> PResult < ' a , ( ) > {
165175 loop {
166- let segment = self . parse_path_segment ( style) ?;
176+ let segment = self . parse_path_segment ( style, ty_generics ) ?;
167177 if style == PathStyle :: Expr {
168178 // In order to check for trailing angle brackets, we must have finished
169179 // recursing (`parse_path_segment` can indirectly call this function),
@@ -191,7 +201,11 @@ impl<'a> Parser<'a> {
191201 }
192202 }
193203
194- pub ( super ) fn parse_path_segment ( & mut self , style : PathStyle ) -> PResult < ' a , PathSegment > {
204+ pub ( super ) fn parse_path_segment (
205+ & mut self ,
206+ style : PathStyle ,
207+ ty_generics : Option < & Generics > ,
208+ ) -> PResult < ' a , PathSegment > {
195209 let ident = self . parse_path_segment_ident ( ) ?;
196210 let is_args_start = |token : & Token | {
197211 matches ! (
@@ -229,18 +243,21 @@ impl<'a> Parser<'a> {
229243 let lo = self . token . span ;
230244 let args = if self . eat_lt ( ) {
231245 // `<'a, T, A = U>`
232- let args =
233- self . parse_angle_args_with_leading_angle_bracket_recovery ( style, lo) ?;
246+ let args = self . parse_angle_args_with_leading_angle_bracket_recovery (
247+ style,
248+ lo,
249+ ty_generics,
250+ ) ?;
234251 self . expect_gt ( ) ?;
235252 let span = lo. to ( self . prev_token . span ) ;
236253 AngleBracketedArgs { args, span } . into ( )
237254 } else {
238255 // `(T, U) -> R`
239256 let ( inputs, _) = self . parse_paren_comma_seq ( |p| p. parse_ty ( ) ) ?;
240257 let inputs_span = lo. to ( self . prev_token . span ) ;
241- let span = ident. span . to ( self . prev_token . span ) ;
242258 let output =
243259 self . parse_ret_ty ( AllowPlus :: No , RecoverQPath :: No , RecoverReturnSign :: No ) ?;
260+ let span = ident. span . to ( self . prev_token . span ) ;
244261 ParenthesizedArgs { span, inputs, inputs_span, output } . into ( )
245262 } ;
246263
@@ -275,6 +292,7 @@ impl<'a> Parser<'a> {
275292 & mut self ,
276293 style : PathStyle ,
277294 lo : Span ,
295+ ty_generics : Option < & Generics > ,
278296 ) -> PResult < ' a , Vec < AngleBracketedArg > > {
279297 // We need to detect whether there are extra leading left angle brackets and produce an
280298 // appropriate error and suggestion. This cannot be implemented by looking ahead at
@@ -350,7 +368,7 @@ impl<'a> Parser<'a> {
350368 let snapshot = if is_first_invocation { Some ( self . clone ( ) ) } else { None } ;
351369
352370 debug ! ( "parse_generic_args_with_leading_angle_bracket_recovery: (snapshotting)" ) ;
353- match self . parse_angle_args ( ) {
371+ match self . parse_angle_args ( ty_generics ) {
354372 Ok ( args) => Ok ( args) ,
355373 Err ( mut e) if is_first_invocation && self . unmatched_angle_bracket_count > 0 => {
356374 // Swap `self` with our backup of the parser state before attempting to parse
@@ -403,7 +421,7 @@ impl<'a> Parser<'a> {
403421 . emit ( ) ;
404422
405423 // Try again without unmatched angle bracket characters.
406- self . parse_angle_args ( )
424+ self . parse_angle_args ( ty_generics )
407425 }
408426 }
409427 Err ( e) => Err ( e) ,
@@ -412,9 +430,12 @@ impl<'a> Parser<'a> {
412430
413431 /// Parses (possibly empty) list of generic arguments / associated item constraints,
414432 /// possibly including trailing comma.
415- pub ( super ) fn parse_angle_args ( & mut self ) -> PResult < ' a , Vec < AngleBracketedArg > > {
433+ pub ( super ) fn parse_angle_args (
434+ & mut self ,
435+ ty_generics : Option < & Generics > ,
436+ ) -> PResult < ' a , Vec < AngleBracketedArg > > {
416437 let mut args = Vec :: new ( ) ;
417- while let Some ( arg) = self . parse_angle_arg ( ) ? {
438+ while let Some ( arg) = self . parse_angle_arg ( ty_generics ) ? {
418439 args. push ( arg) ;
419440 if !self . eat ( & token:: Comma ) {
420441 if !self . token . kind . should_end_const_arg ( ) {
@@ -431,9 +452,12 @@ impl<'a> Parser<'a> {
431452 }
432453
433454 /// Parses a single argument in the angle arguments `<...>` of a path segment.
434- fn parse_angle_arg ( & mut self ) -> PResult < ' a , Option < AngleBracketedArg > > {
455+ fn parse_angle_arg (
456+ & mut self ,
457+ ty_generics : Option < & Generics > ,
458+ ) -> PResult < ' a , Option < AngleBracketedArg > > {
435459 let lo = self . token . span ;
436- let arg = self . parse_generic_arg ( ) ?;
460+ let arg = self . parse_generic_arg ( ty_generics ) ?;
437461 match arg {
438462 Some ( arg) => {
439463 if self . check ( & token:: Colon ) | self . check ( & token:: Eq ) {
@@ -476,7 +500,7 @@ impl<'a> Parser<'a> {
476500 /// That is, parse `<term>` in `Item = <term>`.
477501 /// Right now, this only admits types in `<term>`.
478502 fn parse_assoc_equality_term ( & mut self , ident : Ident , eq : Span ) -> PResult < ' a , P < ast:: Ty > > {
479- let arg = self . parse_generic_arg ( ) ?;
503+ let arg = self . parse_generic_arg ( None ) ?;
480504 let span = ident. span . to ( self . prev_token . span ) ;
481505 match arg {
482506 Some ( GenericArg :: Type ( ty) ) => return Ok ( ty) ,
@@ -563,7 +587,10 @@ impl<'a> Parser<'a> {
563587
564588 /// Parse a generic argument in a path segment.
565589 /// This does not include constraints, e.g., `Item = u8`, which is handled in `parse_angle_arg`.
566- pub ( super ) fn parse_generic_arg ( & mut self ) -> PResult < ' a , Option < GenericArg > > {
590+ pub ( super ) fn parse_generic_arg (
591+ & mut self ,
592+ ty_generics : Option < & Generics > ,
593+ ) -> PResult < ' a , Option < GenericArg > > {
567594 let start = self . token . span ;
568595 let arg = if self . check_lifetime ( ) && self . look_ahead ( 1 , |t| !t. is_like_plus ( ) ) {
569596 // Parse lifetime argument.
@@ -580,25 +607,8 @@ impl<'a> Parser<'a> {
580607 return self . recover_const_arg ( start, err) . map ( Some ) ;
581608 }
582609 }
583- } else if self . eat_keyword_noexpect ( kw:: Const ) {
584- // Detect and recover from the old, pre-RFC2000 syntax for const generics.
585- let mut err = self . struct_span_err (
586- start,
587- "expected lifetime, type, or constant, found keyword `const`" ,
588- ) ;
589- if self . check_const_arg ( ) {
590- err. span_suggestion_verbose (
591- start. until ( self . token . span ) ,
592- "the `const` keyword is only needed in the definition of the type" ,
593- String :: new ( ) ,
594- Applicability :: MaybeIncorrect ,
595- ) ;
596- err. emit ( ) ;
597- GenericArg :: Const ( self . parse_const_arg ( ) ?)
598- } else {
599- let after_kw_const = self . token . span ;
600- return self . recover_const_arg ( after_kw_const, err) . map ( Some ) ;
601- }
610+ } else if self . token . is_keyword ( kw:: Const ) {
611+ return self . recover_const_param_declaration ( ty_generics) ;
602612 } else {
603613 return Ok ( None ) ;
604614 } ;
0 commit comments