diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 506199b60adb6..51bc53395a8e8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -5802,20 +5802,14 @@ impl<'a> Parser<'a> { } fn complain_if_pub_macro(&mut self, vis: &VisibilityKind, sp: Span) { - if let Err(mut err) = self.complain_if_pub_macro_diag(vis, sp) { - err.emit(); - } - } - - fn complain_if_pub_macro_diag(&mut self, vis: &VisibilityKind, sp: Span) -> PResult<'a, ()> { match *vis { - VisibilityKind::Inherited => Ok(()), + VisibilityKind::Inherited => {} _ => { let is_macro_rules: bool = match self.token { token::Ident(sid, _) => sid.name == Symbol::intern("macro_rules"), _ => false, }; - if is_macro_rules { + let mut err = if is_macro_rules { let mut err = self.diagnostic() .struct_span_err(sp, "can't qualify macro_rules invocation with `pub`"); err.span_suggestion_with_applicability( @@ -5824,13 +5818,14 @@ impl<'a> Parser<'a> { "#[macro_export]".to_owned(), Applicability::MaybeIncorrect // speculative ); - Err(err) + err } else { let mut err = self.diagnostic() .struct_span_err(sp, "can't qualify macro invocation with `pub`"); err.help("try adjusting the macro to put `pub` inside the invocation"); - Err(err) - } + err + }; + err.emit(); } } } @@ -6139,9 +6134,6 @@ impl<'a> Parser<'a> { fn consume_block(&mut self, delim: token::DelimToken) { let mut brace_depth = 0; - if !self.eat(&token::OpenDelim(delim)) { - return; - } loop { if self.eat(&token::OpenDelim(delim)) { brace_depth += 1; @@ -6152,7 +6144,7 @@ impl<'a> Parser<'a> { brace_depth -= 1; continue; } - } else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) { + } else if self.token == token::Eof || self.eat(&token::CloseDelim(token::NoDelim)) { return; } else { self.bump(); @@ -7402,17 +7394,27 @@ impl<'a> Parser<'a> { return Err(err); } else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) { let ident = self.parse_ident().unwrap(); + self.bump(); // `(` + let kw_name = if let Ok(Some(_)) = self.parse_self_arg() { + "method" + } else { + "function" + }; self.consume_block(token::Paren); - let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) || - self.check(&token::OpenDelim(token::Brace)) - { - ("fn", "method", false) + let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) { + self.eat_to_tokens(&[&token::OpenDelim(token::Brace)]); + self.bump(); // `{` + ("fn", kw_name, false) + } else if self.check(&token::OpenDelim(token::Brace)) { + self.bump(); // `{` + ("fn", kw_name, false) } else if self.check(&token::Colon) { let kw = "struct"; (kw, kw, false) } else { - ("fn` or `struct", "method or struct", true) + ("fn` or `struct", "function or struct", true) }; + self.consume_block(token::Brace); let msg = format!("missing `{}` for {} definition", kw, kw_name); let mut err = self.diagnostic().struct_span_err(sp, &msg); @@ -7439,6 +7441,32 @@ impl<'a> Parser<'a> { } } return Err(err); + } else if self.look_ahead(1, |t| *t == token::Lt) { + let ident = self.parse_ident().unwrap(); + self.eat_to_tokens(&[&token::Gt]); + self.bump(); // `>` + let (kw, kw_name, ambiguous) = if self.eat(&token::OpenDelim(token::Paren)) { + if let Ok(Some(_)) = self.parse_self_arg() { + ("fn", "method", false) + } else { + ("fn", "function", false) + } + } else if self.check(&token::OpenDelim(token::Brace)) { + ("struct", "struct", false) + } else { + ("fn` or `struct", "function or struct", true) + }; + let msg = format!("missing `{}` for {} definition", kw, kw_name); + let mut err = self.diagnostic().struct_span_err(sp, &msg); + if !ambiguous { + err.span_suggestion_short_with_applicability( + sp, + &format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name), + format!(" {} ", kw), + Applicability::MachineApplicable, + ); + } + return Err(err); } } self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility) diff --git a/src/test/ui/pub/pub-ident-fn-2.rs b/src/test/ui/pub/pub-ident-fn-2.rs index 44884bfcdfdce..648b1067add0d 100644 --- a/src/test/ui/pub/pub-ident-fn-2.rs +++ b/src/test/ui/pub/pub-ident-fn-2.rs @@ -9,7 +9,7 @@ // except according to those terms. pub foo(s: usize) { bar() } -//~^ ERROR missing `fn` for method definition +//~^ ERROR missing `fn` for function definition fn main() { foo(2); diff --git a/src/test/ui/pub/pub-ident-fn-2.stderr b/src/test/ui/pub/pub-ident-fn-2.stderr index bbbb3df876986..ed7430fd39419 100644 --- a/src/test/ui/pub/pub-ident-fn-2.stderr +++ b/src/test/ui/pub/pub-ident-fn-2.stderr @@ -1,9 +1,9 @@ -error: missing `fn` for method definition +error: missing `fn` for function definition --> $DIR/pub-ident-fn-2.rs:11:4 | LL | pub foo(s: usize) { bar() } | ^ -help: add `fn` here to parse `foo` as a public method +help: add `fn` here to parse `foo` as a public function | LL | pub fn foo(s: usize) { bar() } | ^^ diff --git a/src/test/ui/pub/pub-ident-fn-or-struct-2.rs b/src/test/ui/pub/pub-ident-fn-or-struct-2.rs index 1ccadc8a40b72..e528e86fbc6e9 100644 --- a/src/test/ui/pub/pub-ident-fn-or-struct-2.rs +++ b/src/test/ui/pub/pub-ident-fn-or-struct-2.rs @@ -9,6 +9,6 @@ // except according to those terms. pub S(); -//~^ ERROR missing `fn` or `struct` for method or struct definition +//~^ ERROR missing `fn` or `struct` for function or struct definition fn main() {} diff --git a/src/test/ui/pub/pub-ident-fn-or-struct-2.stderr b/src/test/ui/pub/pub-ident-fn-or-struct-2.stderr index e492a8c47567e..d423b838f0514 100644 --- a/src/test/ui/pub/pub-ident-fn-or-struct-2.stderr +++ b/src/test/ui/pub/pub-ident-fn-or-struct-2.stderr @@ -1,4 +1,4 @@ -error: missing `fn` or `struct` for method or struct definition +error: missing `fn` or `struct` for function or struct definition --> $DIR/pub-ident-fn-or-struct-2.rs:11:4 | LL | pub S(); diff --git a/src/test/ui/pub/pub-ident-fn-or-struct.rs b/src/test/ui/pub/pub-ident-fn-or-struct.rs index 0664918945b43..d5254bf2066c2 100644 --- a/src/test/ui/pub/pub-ident-fn-or-struct.rs +++ b/src/test/ui/pub/pub-ident-fn-or-struct.rs @@ -9,6 +9,6 @@ // except according to those terms. pub S (foo) bar -//~^ ERROR missing `fn` or `struct` for method or struct definition +//~^ ERROR missing `fn` or `struct` for function or struct definition fn main() {} diff --git a/src/test/ui/pub/pub-ident-fn-or-struct.stderr b/src/test/ui/pub/pub-ident-fn-or-struct.stderr index c1bff34cec3e6..b8b5e8a4bbf74 100644 --- a/src/test/ui/pub/pub-ident-fn-or-struct.stderr +++ b/src/test/ui/pub/pub-ident-fn-or-struct.stderr @@ -1,4 +1,4 @@ -error: missing `fn` or `struct` for method or struct definition +error: missing `fn` or `struct` for function or struct definition --> $DIR/pub-ident-fn-or-struct.rs:11:4 | LL | pub S (foo) bar diff --git a/src/test/ui/pub/pub-ident-fn-with-lifetime-2.rs b/src/test/ui/pub/pub-ident-fn-with-lifetime-2.rs new file mode 100644 index 0000000000000..1ee8c84f13bd3 --- /dev/null +++ b/src/test/ui/pub/pub-ident-fn-with-lifetime-2.rs @@ -0,0 +1,6 @@ +pub bar<'a>(&self, _s: &'a usize) -> bool { true } +//~^ ERROR missing `fn` for method definition + +fn main() { + bar(2); +} diff --git a/src/test/ui/pub/pub-ident-fn-with-lifetime-2.stderr b/src/test/ui/pub/pub-ident-fn-with-lifetime-2.stderr new file mode 100644 index 0000000000000..c403774df8ede --- /dev/null +++ b/src/test/ui/pub/pub-ident-fn-with-lifetime-2.stderr @@ -0,0 +1,12 @@ +error: missing `fn` for method definition + --> $DIR/pub-ident-fn-with-lifetime-2.rs:1:4 + | +LL | pub bar<'a>(&self, _s: &'a usize) -> bool { true } + | ^^^ +help: add `fn` here to parse `bar` as a public method + | +LL | pub fn bar<'a>(&self, _s: &'a usize) -> bool { true } + | ^^ + +error: aborting due to previous error + diff --git a/src/test/ui/pub/pub-ident-fn-with-lifetime.rs b/src/test/ui/pub/pub-ident-fn-with-lifetime.rs new file mode 100644 index 0000000000000..84f7bdc1fb147 --- /dev/null +++ b/src/test/ui/pub/pub-ident-fn-with-lifetime.rs @@ -0,0 +1,6 @@ +pub foo<'a>(_s: &'a usize) -> bool { true } +//~^ ERROR missing `fn` for function definition + +fn main() { + foo(2); +} diff --git a/src/test/ui/pub/pub-ident-fn-with-lifetime.stderr b/src/test/ui/pub/pub-ident-fn-with-lifetime.stderr new file mode 100644 index 0000000000000..858238096408f --- /dev/null +++ b/src/test/ui/pub/pub-ident-fn-with-lifetime.stderr @@ -0,0 +1,12 @@ +error: missing `fn` for function definition + --> $DIR/pub-ident-fn-with-lifetime.rs:1:4 + | +LL | pub foo<'a>(_s: &'a usize) -> bool { true } + | ^^^ +help: add `fn` here to parse `foo` as a public function + | +LL | pub fn foo<'a>(_s: &'a usize) -> bool { true } + | ^^ + +error: aborting due to previous error + diff --git a/src/test/ui/pub/pub-ident-fn.fixed b/src/test/ui/pub/pub-ident-fn.fixed index f2d0c6c3e1d3f..667e02114a1a8 100644 --- a/src/test/ui/pub/pub-ident-fn.fixed +++ b/src/test/ui/pub/pub-ident-fn.fixed @@ -11,7 +11,7 @@ // run-rustfix pub fn foo(_s: usize) -> bool { true } -//~^ ERROR missing `fn` for method definition +//~^ ERROR missing `fn` for function definition fn main() { foo(2); diff --git a/src/test/ui/pub/pub-ident-fn.rs b/src/test/ui/pub/pub-ident-fn.rs index 82c32f57eeae0..91bbf58fe77e8 100644 --- a/src/test/ui/pub/pub-ident-fn.rs +++ b/src/test/ui/pub/pub-ident-fn.rs @@ -11,7 +11,7 @@ // run-rustfix pub foo(_s: usize) -> bool { true } -//~^ ERROR missing `fn` for method definition +//~^ ERROR missing `fn` for function definition fn main() { foo(2); diff --git a/src/test/ui/pub/pub-ident-fn.stderr b/src/test/ui/pub/pub-ident-fn.stderr index f7c96b8b9f4bb..1df6dd85ffec5 100644 --- a/src/test/ui/pub/pub-ident-fn.stderr +++ b/src/test/ui/pub/pub-ident-fn.stderr @@ -1,9 +1,9 @@ -error: missing `fn` for method definition +error: missing `fn` for function definition --> $DIR/pub-ident-fn.rs:13:4 | LL | pub foo(_s: usize) -> bool { true } | ^^^ -help: add `fn` here to parse `foo` as a public method +help: add `fn` here to parse `foo` as a public function | LL | pub fn foo(_s: usize) -> bool { true } | ^^ diff --git a/src/test/ui/pub/pub-ident-struct-with-lifetime.rs b/src/test/ui/pub/pub-ident-struct-with-lifetime.rs new file mode 100644 index 0000000000000..2feb0266070d5 --- /dev/null +++ b/src/test/ui/pub/pub-ident-struct-with-lifetime.rs @@ -0,0 +1,4 @@ +pub S<'a> { +//~^ ERROR missing `struct` for struct definition +} +fn main() {} diff --git a/src/test/ui/pub/pub-ident-struct-with-lifetime.stderr b/src/test/ui/pub/pub-ident-struct-with-lifetime.stderr new file mode 100644 index 0000000000000..2bbcf5dfff010 --- /dev/null +++ b/src/test/ui/pub/pub-ident-struct-with-lifetime.stderr @@ -0,0 +1,12 @@ +error: missing `struct` for struct definition + --> $DIR/pub-ident-struct-with-lifetime.rs:1:4 + | +LL | pub S<'a> { + | ^ +help: add `struct` here to parse `S` as a public struct + | +LL | pub struct S<'a> { + | ^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/pub/pub-ident-with-lifetime-incomplete.rs b/src/test/ui/pub/pub-ident-with-lifetime-incomplete.rs new file mode 100644 index 0000000000000..c86a9f2fdd655 --- /dev/null +++ b/src/test/ui/pub/pub-ident-with-lifetime-incomplete.rs @@ -0,0 +1,5 @@ +fn main() { +} + +pub foo<'a> +//~^ ERROR missing `fn` or `struct` for function or struct definition diff --git a/src/test/ui/pub/pub-ident-with-lifetime-incomplete.stderr b/src/test/ui/pub/pub-ident-with-lifetime-incomplete.stderr new file mode 100644 index 0000000000000..0e0b127054d1d --- /dev/null +++ b/src/test/ui/pub/pub-ident-with-lifetime-incomplete.stderr @@ -0,0 +1,8 @@ +error: missing `fn` or `struct` for function or struct definition + --> $DIR/pub-ident-with-lifetime-incomplete.rs:4:4 + | +LL | pub foo<'a> + | ^^^ + +error: aborting due to previous error +