@@ -13,7 +13,7 @@ use syntax::ast::{AssocItem, AssocItemKind, Item, ItemKind, UseTree, UseTreeKind
13
13
use syntax:: ast:: { Async , Const , Defaultness , IsAuto , PathSegment , Unsafe } ;
14
14
use syntax:: ast:: { BindingMode , Block , FnDecl , FnSig , Mac , MacArgs , MacDelimiter , Param , SelfKind } ;
15
15
use syntax:: ast:: { EnumDef , Generics , StructField , TraitRef , Ty , TyKind , Variant , VariantData } ;
16
- use syntax:: ast:: { FnHeader , ForeignItem , ForeignItemKind , Mutability , Visibility , VisibilityKind } ;
16
+ use syntax:: ast:: { FnHeader , ForeignItem , Mutability , Visibility , VisibilityKind } ;
17
17
use syntax:: ptr:: P ;
18
18
use syntax:: token;
19
19
use syntax:: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
@@ -333,29 +333,19 @@ impl<'a> Parser<'a> {
333
333
self . token . is_keyword ( kw:: Async ) && self . is_keyword_ahead ( 1 , & [ kw:: Fn ] )
334
334
}
335
335
336
- fn missing_assoc_item_kind_err (
337
- & self ,
338
- item_type : & str ,
339
- prev_span : Span ,
340
- ) -> DiagnosticBuilder < ' a > {
341
- let expected_kinds = if item_type == "extern" {
342
- "missing `fn`, `type`, or `static`"
343
- } else {
344
- "missing `fn`, `type`, or `const`"
345
- } ;
346
-
347
- // Given this code `path(`, it seems like this is not
348
- // setting the visibility of a macro invocation, but rather
349
- // a mistyped method declaration.
350
- // Create a diagnostic pointing out that `fn` is missing.
351
- //
352
- // x | pub path(&self) {
353
- // | ^ missing `fn`, `type`, or `const`
354
- // pub path(
355
- // ^^ `sp` below will point to this
336
+ /// Given this code `path(`, it seems like this is not
337
+ /// setting the visibility of a macro invocation,
338
+ /// but rather a mistyped method declaration.
339
+ /// Create a diagnostic pointing out that `fn` is missing.
340
+ ///
341
+ /// ```
342
+ /// x | pub path(&self) {
343
+ /// | ^ missing `fn`, `type`, `const`, or `static`
344
+ /// ```
345
+ fn missing_nested_item_kind_err ( & self , prev_span : Span ) -> DiagnosticBuilder < ' a > {
356
346
let sp = prev_span. between ( self . token . span ) ;
357
- let mut err = self
358
- . struct_span_err ( sp, & format ! ( "{} for {}- item declaration" , expected_kinds, item_type ) ) ;
347
+ let expected_kinds = "missing `fn`, `type`, `const`, or `static`" ;
348
+ let mut err = self . struct_span_err ( sp, & format ! ( "{} for item declaration" , expected_kinds) ) ;
359
349
err. span_label ( sp, expected_kinds) ;
360
350
err
361
351
}
@@ -639,7 +629,7 @@ impl<'a> Parser<'a> {
639
629
fn parse_assoc_item (
640
630
& mut self ,
641
631
at_end : & mut bool ,
642
- req_name : fn ( & token :: Token ) -> bool ,
632
+ req_name : ReqName ,
643
633
) -> PResult < ' a , P < AssocItem > > {
644
634
let attrs = self . parse_outer_attributes ( ) ?;
645
635
let mut unclosed_delims = vec ! [ ] ;
@@ -660,39 +650,47 @@ impl<'a> Parser<'a> {
660
650
& mut self ,
661
651
at_end : & mut bool ,
662
652
mut attrs : Vec < Attribute > ,
663
- req_name : fn ( & token :: Token ) -> bool ,
653
+ req_name : ReqName ,
664
654
) -> PResult < ' a , AssocItem > {
665
655
let lo = self . token . span ;
666
656
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
667
657
let defaultness = self . parse_defaultness ( ) ;
658
+ let ( ident, kind) = self . parse_assoc_item_kind ( at_end, & mut attrs, req_name, & vis) ?;
659
+ let span = lo. to ( self . prev_span ) ;
660
+ let id = DUMMY_NODE_ID ;
661
+ Ok ( AssocItem { id, span, ident, attrs, vis, defaultness, kind, tokens : None } )
662
+ }
668
663
669
- let ( ident, kind) = if self . eat_keyword ( kw:: Type ) {
670
- self . parse_assoc_ty ( ) ?
664
+ fn parse_assoc_item_kind (
665
+ & mut self ,
666
+ at_end : & mut bool ,
667
+ attrs : & mut Vec < Attribute > ,
668
+ req_name : ReqName ,
669
+ vis : & Visibility ,
670
+ ) -> PResult < ' a , ( Ident , AssocItemKind ) > {
671
+ if self . eat_keyword ( kw:: Type ) {
672
+ self . parse_assoc_ty ( )
671
673
} else if self . check_fn_front_matter ( ) {
672
- let ( ident, sig, generics, body) = self . parse_fn ( at_end, & mut attrs, req_name) ?;
673
- ( ident, AssocItemKind :: Fn ( sig, generics, body) )
674
+ let ( ident, sig, generics, body) = self . parse_fn ( at_end, attrs, req_name) ?;
675
+ Ok ( ( ident, AssocItemKind :: Fn ( sig, generics, body) ) )
674
676
} else if self . is_static_global ( ) {
675
677
self . bump ( ) ; // `static`
676
678
let mutbl = self . parse_mutability ( ) ;
677
679
let ( ident, ty, expr) = self . parse_item_const_common ( Some ( mutbl) ) ?;
678
- ( ident, AssocItemKind :: Static ( ty, mutbl, expr) )
680
+ Ok ( ( ident, AssocItemKind :: Static ( ty, mutbl, expr) ) )
679
681
} else if self . eat_keyword ( kw:: Const ) {
680
682
let ( ident, ty, expr) = self . parse_item_const_common ( None ) ?;
681
- ( ident, AssocItemKind :: Const ( ty, expr) )
683
+ Ok ( ( ident, AssocItemKind :: Const ( ty, expr) ) )
682
684
} else if self . isnt_macro_invocation ( ) {
683
- return Err ( self . missing_assoc_item_kind_err ( "associated" , self . prev_span ) ) ;
685
+ Err ( self . missing_nested_item_kind_err ( self . prev_span ) )
684
686
} else if self . token . is_path_start ( ) {
685
687
let mac = self . parse_item_macro ( & vis) ?;
686
688
* at_end = true ;
687
- ( Ident :: invalid ( ) , AssocItemKind :: Macro ( mac) )
689
+ Ok ( ( Ident :: invalid ( ) , AssocItemKind :: Macro ( mac) ) )
688
690
} else {
689
- self . recover_attrs_no_item ( & attrs) ?;
690
- self . unexpected ( ) ?
691
- } ;
692
-
693
- let span = lo. to ( self . prev_span ) ;
694
- let id = DUMMY_NODE_ID ;
695
- Ok ( AssocItem { id, span, ident, attrs, vis, defaultness, kind, tokens : None } )
691
+ self . recover_attrs_no_item ( attrs) ?;
692
+ self . unexpected ( )
693
+ }
696
694
}
697
695
698
696
/// Parses the following grammar:
@@ -869,46 +867,10 @@ impl<'a> Parser<'a> {
869
867
let mut attrs = self . parse_outer_attributes ( ) ?;
870
868
let lo = self . token . span ;
871
869
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
872
-
873
- let ( ident, kind) = if self . eat_keyword ( kw:: Type ) {
874
- // FOREIGN TYPE ITEM
875
- self . parse_item_foreign_type ( ) ?
876
- } else if self . check_fn_front_matter ( ) {
877
- // FOREIGN FUNCTION ITEM
878
- let ( ident, sig, generics, body) = self . parse_fn ( at_end, & mut attrs, |_| true ) ?;
879
- ( ident, ForeignItemKind :: Fn ( sig, generics, body) )
880
- } else if self . is_static_global ( ) {
881
- // FOREIGN STATIC ITEM
882
- self . bump ( ) ; // `static`
883
- let mutbl = self . parse_mutability ( ) ;
884
- let ( ident, ty, expr) = self . parse_item_const_common ( Some ( mutbl) ) ?;
885
- ( ident, ForeignItemKind :: Static ( ty, mutbl, expr) )
886
- } else if self . eat_keyword ( kw:: Const ) {
887
- let ( ident, ty, expr) = self . parse_item_const_common ( None ) ?;
888
- ( ident, ForeignItemKind :: Const ( ty, expr) )
889
- } else if self . isnt_macro_invocation ( ) {
890
- return Err ( self . missing_assoc_item_kind_err ( "extern" , self . prev_span ) ) ;
891
- } else if self . token . is_path_start ( ) {
892
- let mac = self . parse_item_macro ( & vis) ?;
893
- * at_end = true ;
894
- ( Ident :: invalid ( ) , ForeignItemKind :: Macro ( mac) )
895
- } else {
896
- self . recover_attrs_no_item ( & attrs) ?;
897
- self . unexpected ( ) ?
898
- } ;
870
+ let ( ident, kind) = self . parse_assoc_item_kind ( at_end, & mut attrs, |_| true , & vis) ?;
899
871
Ok ( P ( self . mk_item ( lo, ident, kind, vis, attrs) ) )
900
872
}
901
873
902
- /// Parses a type from a foreign module.
903
- fn parse_item_foreign_type ( & mut self ) -> PResult < ' a , ( Ident , ForeignItemKind ) > {
904
- let ( ident, kind) = self . parse_assoc_ty ( ) ?;
905
- let kind = match kind {
906
- AssocItemKind :: TyAlias ( g, b, d) => ForeignItemKind :: TyAlias ( g, b, d) ,
907
- _ => unreachable ! ( ) ,
908
- } ;
909
- Ok ( ( ident, kind) )
910
- }
911
-
912
874
fn is_static_global ( & mut self ) -> bool {
913
875
if self . check_keyword ( kw:: Static ) {
914
876
// Check if this could be a closure.
0 commit comments