@@ -1789,6 +1789,35 @@ impl<'a> Parser<'a> {
1789
1789
self . look_ahead ( offset + 1 , |t| t == & token:: Colon )
1790
1790
}
1791
1791
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
+
1792
1821
/// This version of parse arg doesn't necessarily require
1793
1822
/// identifier names.
1794
1823
fn parse_arg_general ( & mut self , require_name : bool ) -> PResult < ' a , Arg > {
@@ -1797,7 +1826,11 @@ impl<'a> Parser<'a> {
1797
1826
let ( pat, ty) = if require_name || self . is_named_argument ( ) {
1798
1827
debug ! ( "parse_arg_general parse_pat (require_name:{})" ,
1799
1828
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
+ } ) ?;
1801
1834
1802
1835
if let Err ( mut err) = self . expect ( & token:: Colon ) {
1803
1836
// If we find a pattern followed by an identifier, it could be an (incorrect)
@@ -1819,10 +1852,12 @@ impl<'a> Parser<'a> {
1819
1852
return Err ( err) ;
1820
1853
}
1821
1854
1855
+ self . eat_incorrect_doc_comment ( "a method argument's type" ) ;
1822
1856
( pat, self . parse_ty ( ) ?)
1823
1857
} else {
1824
1858
debug ! ( "parse_arg_general ident_to_pat" ) ;
1825
1859
let parser_snapshot_before_ty = self . clone ( ) ;
1860
+ self . eat_incorrect_doc_comment ( "a method argument's type" ) ;
1826
1861
let mut ty = self . parse_ty ( ) ;
1827
1862
if ty. is_ok ( ) && self . token == token:: Colon {
1828
1863
// This wasn't actually a type, but a pattern looking like a type,
0 commit comments