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 ;
@@ -12,7 +18,7 @@ use rustc_ast::{
12
18
MacCall , MutTy , Mutability , PolyTraitRef , TraitBoundModifier , TraitObjectSyntax , Ty , TyKind ,
13
19
} ;
14
20
use rustc_ast_pretty:: pprust;
15
- use rustc_errors:: { pluralize , struct_span_err , Applicability , PResult } ;
21
+ use rustc_errors:: { Applicability , PResult } ;
16
22
use rustc_span:: source_map:: Span ;
17
23
use rustc_span:: symbol:: { kw, sym, Ident } ;
18
24
use rustc_span:: Symbol ;
@@ -233,14 +239,7 @@ impl<'a> Parser<'a> {
233
239
// Don't `eat` to prevent `=>` from being added as an expected token which isn't
234
240
// actually expected and could only confuse users
235
241
self . bump ( ) ;
236
- self . struct_span_err ( self . prev_token . span , "return types are denoted using `->`" )
237
- . span_suggestion_short (
238
- self . prev_token . span ,
239
- "use `->` instead" ,
240
- "->" ,
241
- Applicability :: MachineApplicable ,
242
- )
243
- . emit ( ) ;
242
+ self . sess . emit_err ( ReturnTypesUseThinArrow { span : self . prev_token . span } ) ;
244
243
let ty = self . parse_ty_common (
245
244
allow_plus,
246
245
AllowCVariadic :: No ,
@@ -328,7 +327,7 @@ impl<'a> Parser<'a> {
328
327
AllowCVariadic :: No => {
329
328
// FIXME(Centril): Should we just allow `...` syntactically
330
329
// anywhere in a type and use semantic restrictions instead?
331
- self . error_illegal_c_varadic_ty ( lo ) ;
330
+ self . sess . emit_err ( NestedCVariadicType { span : lo . to ( self . prev_token . span ) } ) ;
332
331
TyKind :: Err
333
332
}
334
333
}
@@ -431,8 +430,7 @@ impl<'a> Parser<'a> {
431
430
let lt_no_plus = self . check_lifetime ( ) && !self . look_ahead ( 1 , |t| t. is_like_plus ( ) ) ;
432
431
let bounds = self . parse_generic_bounds_common ( allow_plus, None ) ?;
433
432
if lt_no_plus {
434
- self . struct_span_err ( lo, "lifetime in trait object type must be followed by `+`" )
435
- . emit ( ) ;
433
+ self . sess . emit_err ( NeedPlusAfterTraitObjectLifetime { span : lo } ) ;
436
434
}
437
435
Ok ( TyKind :: TraitObject ( bounds, TraitObjectSyntax :: None ) )
438
436
}
@@ -466,14 +464,10 @@ impl<'a> Parser<'a> {
466
464
fn parse_ty_ptr ( & mut self ) -> PResult < ' a , TyKind > {
467
465
let mutbl = self . parse_const_or_mut ( ) . unwrap_or_else ( || {
468
466
let span = self . prev_token . span ;
469
- self . struct_span_err ( span, "expected `mut` or `const` keyword in raw pointer type" )
470
- . span_suggestions (
471
- span. shrink_to_hi ( ) ,
472
- "add `mut` or `const` here" ,
473
- [ "mut " . to_string ( ) , "const " . to_string ( ) ] ,
474
- Applicability :: HasPlaceholders ,
475
- )
476
- . emit ( ) ;
467
+ self . sess . emit_err ( ExpectedMutOrConstInRawPointerType {
468
+ span,
469
+ after_asterisk : span. shrink_to_hi ( ) ,
470
+ } ) ;
477
471
Mutability :: Not
478
472
} ) ;
479
473
let ty = self . parse_ty_no_plus ( ) ?;
@@ -528,16 +522,13 @@ impl<'a> Parser<'a> {
528
522
let lifetime_span = self . token . span ;
529
523
let span = and_span. to ( lifetime_span) ;
530
524
531
- let mut err = self . struct_span_err ( span, "lifetime must precede `mut`" ) ;
532
- if let Ok ( lifetime_src) = self . span_to_snippet ( lifetime_span) {
533
- err. span_suggestion (
534
- span,
535
- "place the lifetime before `mut`" ,
536
- format ! ( "&{} mut" , lifetime_src) ,
537
- Applicability :: MaybeIncorrect ,
538
- ) ;
539
- }
540
- err. emit ( ) ;
525
+ let ( suggest_lifetime, snippet) =
526
+ if let Ok ( lifetime_src) = self . span_to_snippet ( lifetime_span) {
527
+ ( Some ( span) , lifetime_src)
528
+ } else {
529
+ ( None , String :: new ( ) )
530
+ } ;
531
+ self . sess . emit_err ( LifetimeAfterMut { span, suggest_lifetime, snippet } ) ;
541
532
542
533
opt_lifetime = Some ( self . expect_lifetime ( ) ) ;
543
534
}
@@ -547,14 +538,7 @@ impl<'a> Parser<'a> {
547
538
{
548
539
// We have `&dyn mut ...`, which is invalid and should be `&mut dyn ...`.
549
540
let span = and_span. to ( self . look_ahead ( 1 , |t| t. span ) ) ;
550
- let mut err = self . struct_span_err ( span, "`mut` must precede `dyn`" ) ;
551
- err. span_suggestion (
552
- span,
553
- "place `mut` before `dyn`" ,
554
- "&mut dyn" ,
555
- Applicability :: MachineApplicable ,
556
- ) ;
557
- err. emit ( ) ;
541
+ self . sess . emit_err ( DynAfterMut { span } ) ;
558
542
559
543
// Recovery
560
544
mutbl = Mutability :: Mut ;
@@ -608,10 +592,10 @@ impl<'a> Parser<'a> {
608
592
// If we ever start to allow `const fn()`, then update
609
593
// feature gating for `#![feature(const_extern_fn)]` to
610
594
// cover it.
611
- self . error_fn_ptr_bad_qualifier ( whole_span, span, "const" ) ;
595
+ self . sess . emit_err ( FnPointerCannotBeConst { span : whole_span, qualifier : span } ) ;
612
596
}
613
597
if let ast:: Async :: Yes { span, .. } = asyncness {
614
- self . error_fn_ptr_bad_qualifier ( whole_span, span, "async" ) ;
598
+ self . sess . emit_err ( FnPointerCannotBeAsync { span : whole_span, qualifier : span } ) ;
615
599
}
616
600
let decl_span = span_start. to ( self . token . span ) ;
617
601
Ok ( TyKind :: BareFn ( P ( BareFnTy { ext, unsafety, generic_params : params, decl, decl_span } ) ) )
@@ -659,19 +643,6 @@ impl<'a> Parser<'a> {
659
643
Ok ( ( ) )
660
644
}
661
645
662
- /// Emit an error for the given bad function pointer qualifier.
663
- fn error_fn_ptr_bad_qualifier ( & self , span : Span , qual_span : Span , qual : & str ) {
664
- self . struct_span_err ( span, & format ! ( "an `fn` pointer type cannot be `{}`" , qual) )
665
- . span_label ( qual_span, format ! ( "`{}` because of this" , qual) )
666
- . span_suggestion_short (
667
- qual_span,
668
- & format ! ( "remove the `{}` qualifier" , qual) ,
669
- "" ,
670
- Applicability :: MaybeIncorrect ,
671
- )
672
- . emit ( ) ;
673
- }
674
-
675
646
/// Parses an `impl B0 + ... + Bn` type.
676
647
fn parse_impl_ty ( & mut self , impl_dyn_multi : & mut bool ) -> PResult < ' a , TyKind > {
677
648
// Always parse bounds greedily for better error recovery.
@@ -758,16 +729,6 @@ impl<'a> Parser<'a> {
758
729
}
759
730
}
760
731
761
- fn error_illegal_c_varadic_ty ( & self , lo : Span ) {
762
- struct_span_err ! (
763
- self . sess. span_diagnostic,
764
- lo. to( self . prev_token. span) ,
765
- E0743 ,
766
- "C-variadic type `...` may not be nested inside another type" ,
767
- )
768
- . emit ( ) ;
769
- }
770
-
771
732
pub ( super ) fn parse_generic_bounds (
772
733
& mut self ,
773
734
colon_span : Option < Span > ,
@@ -797,15 +758,7 @@ impl<'a> Parser<'a> {
797
758
{
798
759
if self . token . is_keyword ( kw:: Dyn ) {
799
760
// Account for `&dyn Trait + dyn Other`.
800
- self . struct_span_err ( self . token . span , "invalid `dyn` keyword" )
801
- . help ( "`dyn` is only needed at the start of a trait `+`-separated list" )
802
- . span_suggestion (
803
- self . token . span ,
804
- "remove this keyword" ,
805
- "" ,
806
- Applicability :: MachineApplicable ,
807
- )
808
- . emit ( ) ;
761
+ self . sess . emit_err ( InvalidDynKeyword { span : self . token . span } ) ;
809
762
self . bump ( ) ;
810
763
}
811
764
match self . parse_generic_bound ( ) ? {
@@ -842,11 +795,7 @@ impl<'a> Parser<'a> {
842
795
bounds : & [ GenericBound ] ,
843
796
negative_bounds : Vec < Span > ,
844
797
) {
845
- let negative_bounds_len = negative_bounds. len ( ) ;
846
- let last_span = * negative_bounds. last ( ) . expect ( "no negative bounds, but still error?" ) ;
847
- let mut err = self . struct_span_err ( negative_bounds, "negative bounds are not supported" ) ;
848
- err. span_label ( last_span, "negative bounds are not supported" ) ;
849
- if let Some ( bound_list) = colon_span {
798
+ let sub = if let Some ( bound_list) = colon_span {
850
799
let bound_list = bound_list. to ( self . prev_token . span ) ;
851
800
let mut new_bound_list = String :: new ( ) ;
852
801
if !bounds. is_empty ( ) {
@@ -857,14 +806,18 @@ impl<'a> Parser<'a> {
857
806
}
858
807
new_bound_list = new_bound_list. replacen ( " +" , ":" , 1 ) ;
859
808
}
860
- err. tool_only_span_suggestion (
809
+
810
+ Some ( NegativeBoundsNotSupportedSugg {
861
811
bound_list,
862
- & format ! ( "remove the bound{}" , pluralize!( negative_bounds_len) ) ,
863
- new_bound_list,
864
- Applicability :: MachineApplicable ,
865
- ) ;
866
- }
867
- err. emit ( ) ;
812
+ num_bounds : negative_bounds. len ( ) ,
813
+ fixed : new_bound_list,
814
+ } )
815
+ } else {
816
+ None
817
+ } ;
818
+
819
+ let last_span = * negative_bounds. last ( ) . expect ( "no negative bounds, but still error?" ) ;
820
+ self . sess . emit_err ( NegativeBoundsNotSupported { negative_bounds, last_span, sub } ) ;
868
821
}
869
822
870
823
/// Parses a bound according to the grammar:
0 commit comments