1
1
use super :: { Parser , PathStyle , TokenType } ;
2
2
3
- use crate :: errors:: { ExpectedFnPathFoundFnKeyword , FnPtrWithGenerics , FnPtrWithGenericsSugg } ;
3
+ use crate :: errors:: {
4
+ DynAfterMut , ExpectedFnPathFoundFnKeyword , ExpectedMutOrConstInRawPointerType ,
5
+ FnPointerCannotBeAsync , FnPointerCannotBeConst , FnPtrWithGenerics , FnPtrWithGenericsSugg ,
6
+ InvalidDynKeyword , LifetimeAfterMut , NeedPlusAfterTraitObjectLifetime ,
7
+ NegativeBoundsNotSupported , NegativeBoundsNotSupportedSugg , NestedCVariadicType ,
8
+ ReturnTypesUseThinArrow ,
9
+ } ;
4
10
use crate :: { maybe_recover_from_interpolated_ty_qpath, maybe_whole} ;
5
11
6
12
use ast:: DUMMY_NODE_ID ;
@@ -11,7 +17,7 @@ use rustc_ast::{
11
17
self as ast, BareFnTy , FnRetTy , GenericBound , GenericBounds , GenericParam , Generics , Lifetime ,
12
18
MacCall , MutTy , Mutability , PolyTraitRef , TraitBoundModifier , TraitObjectSyntax , Ty , TyKind ,
13
19
} ;
14
- use rustc_errors:: { pluralize , struct_span_err , Applicability , PResult } ;
20
+ use rustc_errors:: { Applicability , PResult } ;
15
21
use rustc_span:: source_map:: Span ;
16
22
use rustc_span:: symbol:: { kw, sym, Ident } ;
17
23
use rustc_span:: Symbol ;
@@ -217,14 +223,7 @@ impl<'a> Parser<'a> {
217
223
// Don't `eat` to prevent `=>` from being added as an expected token which isn't
218
224
// actually expected and could only confuse users
219
225
self . bump ( ) ;
220
- self . struct_span_err ( self . prev_token . span , "return types are denoted using `->`" )
221
- . span_suggestion_short (
222
- self . prev_token . span ,
223
- "use `->` instead" ,
224
- "->" ,
225
- Applicability :: MachineApplicable ,
226
- )
227
- . emit ( ) ;
226
+ self . sess . emit_err ( ReturnTypesUseThinArrow { span : self . prev_token . span } ) ;
228
227
let ty = self . parse_ty_common (
229
228
allow_plus,
230
229
AllowCVariadic :: No ,
@@ -310,7 +309,7 @@ impl<'a> Parser<'a> {
310
309
} else {
311
310
// FIXME(Centril): Should we just allow `...` syntactically
312
311
// anywhere in a type and use semantic restrictions instead?
313
- self . error_illegal_c_varadic_ty ( lo ) ;
312
+ self . sess . emit_err ( NestedCVariadicType { span : lo . to ( self . prev_token . span ) } ) ;
314
313
TyKind :: Err
315
314
}
316
315
} else {
@@ -372,8 +371,7 @@ impl<'a> Parser<'a> {
372
371
let lt_no_plus = self . check_lifetime ( ) && !self . look_ahead ( 1 , |t| t. is_like_plus ( ) ) ;
373
372
let bounds = self . parse_generic_bounds_common ( allow_plus, None ) ?;
374
373
if lt_no_plus {
375
- self . struct_span_err ( lo, "lifetime in trait object type must be followed by `+`" )
376
- . emit ( ) ;
374
+ self . sess . emit_err ( NeedPlusAfterTraitObjectLifetime { span : lo } ) ;
377
375
}
378
376
Ok ( TyKind :: TraitObject ( bounds, TraitObjectSyntax :: None ) )
379
377
}
@@ -407,14 +405,10 @@ impl<'a> Parser<'a> {
407
405
fn parse_ty_ptr ( & mut self ) -> PResult < ' a , TyKind > {
408
406
let mutbl = self . parse_const_or_mut ( ) . unwrap_or_else ( || {
409
407
let span = self . prev_token . span ;
410
- self . struct_span_err ( span, "expected `mut` or `const` keyword in raw pointer type" )
411
- . span_suggestions (
412
- span. shrink_to_hi ( ) ,
413
- "add `mut` or `const` here" ,
414
- [ "mut " . to_string ( ) , "const " . to_string ( ) ] ,
415
- Applicability :: HasPlaceholders ,
416
- )
417
- . emit ( ) ;
408
+ self . sess . emit_err ( ExpectedMutOrConstInRawPointerType {
409
+ span,
410
+ after_asterisk : span. shrink_to_hi ( ) ,
411
+ } ) ;
418
412
Mutability :: Not
419
413
} ) ;
420
414
let ty = self . parse_ty_no_plus ( ) ?;
@@ -469,16 +463,13 @@ impl<'a> Parser<'a> {
469
463
let lifetime_span = self . token . span ;
470
464
let span = and_span. to ( lifetime_span) ;
471
465
472
- let mut err = self . struct_span_err ( span, "lifetime must precede `mut`" ) ;
473
- if let Ok ( lifetime_src) = self . span_to_snippet ( lifetime_span) {
474
- err. span_suggestion (
475
- span,
476
- "place the lifetime before `mut`" ,
477
- format ! ( "&{} mut" , lifetime_src) ,
478
- Applicability :: MaybeIncorrect ,
479
- ) ;
480
- }
481
- err. emit ( ) ;
466
+ let ( suggest_lifetime, snippet) =
467
+ if let Ok ( lifetime_src) = self . span_to_snippet ( lifetime_span) {
468
+ ( Some ( span) , lifetime_src)
469
+ } else {
470
+ ( None , String :: new ( ) )
471
+ } ;
472
+ self . sess . emit_err ( LifetimeAfterMut { span, suggest_lifetime, snippet } ) ;
482
473
483
474
opt_lifetime = Some ( self . expect_lifetime ( ) ) ;
484
475
}
@@ -488,14 +479,7 @@ impl<'a> Parser<'a> {
488
479
{
489
480
// We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
490
481
let span = and_span. to ( self . look_ahead ( 1 , |t| t. span ) ) ;
491
- let mut err = self . struct_span_err ( span, "`mut` must precede `dyn`" ) ;
492
- err. span_suggestion (
493
- span,
494
- "place `mut` before `dyn`" ,
495
- "&mut dyn" ,
496
- Applicability :: MachineApplicable ,
497
- ) ;
498
- err. emit ( ) ;
482
+ self . sess . emit_err ( DynAfterMut { span } ) ;
499
483
500
484
// Recovery
501
485
mutbl = Mutability :: Mut ;
@@ -549,10 +533,10 @@ impl<'a> Parser<'a> {
549
533
// If we ever start to allow `const fn()`, then update
550
534
// feature gating for `#![feature(const_extern_fn)]` to
551
535
// cover it.
552
- self . error_fn_ptr_bad_qualifier ( whole_span, span, "const" ) ;
536
+ self . sess . emit_err ( FnPointerCannotBeConst { span : whole_span, qualifier : span } ) ;
553
537
}
554
538
if let ast:: Async :: Yes { span, .. } = asyncness {
555
- self . error_fn_ptr_bad_qualifier ( whole_span, span, "async" ) ;
539
+ self . sess . emit_err ( FnPointerCannotBeAsync { span : whole_span, qualifier : span } ) ;
556
540
}
557
541
let decl_span = span_start. to ( self . token . span ) ;
558
542
Ok ( TyKind :: BareFn ( P ( BareFnTy { ext, unsafety, generic_params : params, decl, decl_span } ) ) )
@@ -600,19 +584,6 @@ impl<'a> Parser<'a> {
600
584
Ok ( ( ) )
601
585
}
602
586
603
- /// Emit an error for the given bad function pointer qualifier.
604
- fn error_fn_ptr_bad_qualifier ( & self , span : Span , qual_span : Span , qual : & str ) {
605
- self . struct_span_err ( span, & format ! ( "an `fn` pointer type cannot be `{}`" , qual) )
606
- . span_label ( qual_span, format ! ( "`{}` because of this" , qual) )
607
- . span_suggestion_short (
608
- qual_span,
609
- & format ! ( "remove the `{}` qualifier" , qual) ,
610
- "" ,
611
- Applicability :: MaybeIncorrect ,
612
- )
613
- . emit ( ) ;
614
- }
615
-
616
587
/// Parses an `impl B0 + ... + Bn` type.
617
588
fn parse_impl_ty ( & mut self , impl_dyn_multi : & mut bool ) -> PResult < ' a , TyKind > {
618
589
// Always parse bounds greedily for better error recovery.
@@ -699,16 +670,6 @@ impl<'a> Parser<'a> {
699
670
}
700
671
}
701
672
702
- fn error_illegal_c_varadic_ty ( & self , lo : Span ) {
703
- struct_span_err ! (
704
- self . sess. span_diagnostic,
705
- lo. to( self . prev_token. span) ,
706
- E0743 ,
707
- "C-variadic type `...` may not be nested inside another type" ,
708
- )
709
- . emit ( ) ;
710
- }
711
-
712
673
pub ( super ) fn parse_generic_bounds (
713
674
& mut self ,
714
675
colon_span : Option < Span > ,
@@ -736,15 +697,7 @@ impl<'a> Parser<'a> {
736
697
{
737
698
if self . token . is_keyword ( kw:: Dyn ) {
738
699
// Account for `&dyn Trait + dyn Other`.
739
- self . struct_span_err ( self . token . span , "invalid `dyn` keyword" )
740
- . help ( "`dyn` is only needed at the start of a trait `+`-separated list" )
741
- . span_suggestion (
742
- self . token . span ,
743
- "remove this keyword" ,
744
- "" ,
745
- Applicability :: MachineApplicable ,
746
- )
747
- . emit ( ) ;
700
+ self . sess . emit_err ( InvalidDynKeyword { span : self . token . span } ) ;
748
701
self . bump ( ) ;
749
702
}
750
703
match self . parse_generic_bound ( ) ? {
@@ -781,11 +734,7 @@ impl<'a> Parser<'a> {
781
734
bounds : & [ GenericBound ] ,
782
735
negative_bounds : Vec < Span > ,
783
736
) {
784
- let negative_bounds_len = negative_bounds. len ( ) ;
785
- let last_span = * negative_bounds. last ( ) . expect ( "no negative bounds, but still error?" ) ;
786
- let mut err = self . struct_span_err ( negative_bounds, "negative bounds are not supported" ) ;
787
- err. span_label ( last_span, "negative bounds are not supported" ) ;
788
- if let Some ( bound_list) = colon_span {
737
+ let sub = if let Some ( bound_list) = colon_span {
789
738
let bound_list = bound_list. to ( self . prev_token . span ) ;
790
739
let mut new_bound_list = String :: new ( ) ;
791
740
if !bounds. is_empty ( ) {
@@ -796,14 +745,18 @@ impl<'a> Parser<'a> {
796
745
}
797
746
new_bound_list = new_bound_list. replacen ( " +" , ":" , 1 ) ;
798
747
}
799
- err. tool_only_span_suggestion (
748
+
749
+ Some ( NegativeBoundsNotSupportedSugg {
800
750
bound_list,
801
- & format ! ( "remove the bound{}" , pluralize!( negative_bounds_len) ) ,
802
- new_bound_list,
803
- Applicability :: MachineApplicable ,
804
- ) ;
805
- }
806
- err. emit ( ) ;
751
+ num_bounds : negative_bounds. len ( ) ,
752
+ fixed : new_bound_list,
753
+ } )
754
+ } else {
755
+ None
756
+ } ;
757
+
758
+ let last_span = * negative_bounds. last ( ) . expect ( "no negative bounds, but still error?" ) ;
759
+ self . sess . emit_err ( NegativeBoundsNotSupported { negative_bounds, last_span, sub } ) ;
807
760
}
808
761
809
762
/// Parses a bound according to the grammar:
0 commit comments