@@ -1789,6 +1789,35 @@ impl<'a> Parser<'a> {
17891789 self . look_ahead ( offset + 1 , |t| t == & token:: Colon )
17901790 }
17911791
1792+ /// Skip unexpected attributes and doc comments in this position and emit an appropriate error.
1793+ fn eat_incorrect_doc_comment ( & mut self , applied_to : & str ) {
1794+ if let token:: DocComment ( _) = self . token {
1795+ let mut err = self . diagnostic ( ) . struct_span_err (
1796+ self . span ,
1797+ & format ! ( "documentation comments cannot be applied to {}" , applied_to) ,
1798+ ) ;
1799+ err. span_label ( self . span , "doc comments are not allowed here" ) ;
1800+ err. emit ( ) ;
1801+ self . bump ( ) ;
1802+ } else if self . token == token:: Pound && self . look_ahead ( 1 , |t| {
1803+ * t == token:: OpenDelim ( token:: Bracket )
1804+ } ) {
1805+ let lo = self . span ;
1806+ // Skip every token until next possible arg.
1807+ while self . token != token:: CloseDelim ( token:: Bracket ) {
1808+ self . bump ( ) ;
1809+ }
1810+ let sp = lo. to ( self . span ) ;
1811+ self . bump ( ) ;
1812+ let mut err = self . diagnostic ( ) . struct_span_err (
1813+ sp,
1814+ & format ! ( "attributes cannot be applied to {}" , applied_to) ,
1815+ ) ;
1816+ err. span_label ( sp, "attributes are not allowed here" ) ;
1817+ err. emit ( ) ;
1818+ }
1819+ }
1820+
17921821 /// This version of parse arg doesn't necessarily require
17931822 /// identifier names.
17941823 fn parse_arg_general ( & mut self , require_name : bool ) -> PResult < ' a , Arg > {
@@ -1797,7 +1826,11 @@ impl<'a> Parser<'a> {
17971826 let ( pat, ty) = if require_name || self . is_named_argument ( ) {
17981827 debug ! ( "parse_arg_general parse_pat (require_name:{})" ,
17991828 require_name) ;
1800- let pat = self . parse_pat ( ) ?;
1829+ self . eat_incorrect_doc_comment ( "method arguments" ) ;
1830+ let pat = self . parse_pat ( ) . map_err ( |mut err| {
1831+ err. span_label ( self . span , "expected argument name" ) ;
1832+ err
1833+ } ) ?;
18011834
18021835 if let Err ( mut err) = self . expect ( & token:: Colon ) {
18031836 // If we find a pattern followed by an identifier, it could be an (incorrect)
@@ -1819,10 +1852,12 @@ impl<'a> Parser<'a> {
18191852 return Err ( err) ;
18201853 }
18211854
1855+ self . eat_incorrect_doc_comment ( "a method argument's type" ) ;
18221856 ( pat, self . parse_ty ( ) ?)
18231857 } else {
18241858 debug ! ( "parse_arg_general ident_to_pat" ) ;
18251859 let parser_snapshot_before_ty = self . clone ( ) ;
1860+ self . eat_incorrect_doc_comment ( "a method argument's type" ) ;
18261861 let mut ty = self . parse_ty ( ) ;
18271862 if ty. is_ok ( ) && self . token == token:: Colon {
18281863 // This wasn't actually a type, but a pattern looking like a type,
0 commit comments