Auto merge of #45997 - estebank:pub-ident, r=nikomatsakis
Account for missing keyword in fn/struct definition Fix #38911.
This commit is contained in:
commit
d1364a65c0
24 changed files with 268 additions and 22 deletions
|
@ -1890,7 +1890,9 @@ impl<'a> LoweringContext<'a> {
|
|||
bounds,
|
||||
items)
|
||||
}
|
||||
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
|
||||
ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
|
||||
panic!("Shouldn't still be around")
|
||||
}
|
||||
}
|
||||
|
||||
// [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to
|
||||
|
|
|
@ -1156,7 +1156,7 @@ impl EmitterWriter {
|
|||
let start = parts[0].snippet.len() - parts[0].snippet.trim_left().len();
|
||||
let sub_len = parts[0].snippet.trim().len();
|
||||
let underline_start = span_start_pos.col.0 + start;
|
||||
let underline_end = span_start_pos.col.0 + sub_len;
|
||||
let underline_end = span_start_pos.col.0 + start + sub_len;
|
||||
for p in underline_start..underline_end {
|
||||
buffer.putc(row_num,
|
||||
max_line_num_len + 3 + p,
|
||||
|
|
|
@ -1945,7 +1945,7 @@ impl<'a> Resolver<'a> {
|
|||
self.resolve_use_tree(item, use_tree, &path);
|
||||
}
|
||||
|
||||
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_)=> {
|
||||
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_) => {
|
||||
// do nothing, these are just around to be encoded
|
||||
}
|
||||
|
||||
|
|
|
@ -1156,6 +1156,7 @@ impl<'a> Parser<'a> {
|
|||
None => token::CloseDelim(self.token_cursor.frame.delim),
|
||||
})
|
||||
}
|
||||
|
||||
fn look_ahead_span(&self, dist: usize) -> Span {
|
||||
if dist == 0 {
|
||||
return self.span
|
||||
|
@ -4268,7 +4269,16 @@ impl<'a> Parser<'a> {
|
|||
let mut stmts = vec![];
|
||||
|
||||
while !self.eat(&token::CloseDelim(token::Brace)) {
|
||||
if let Some(stmt) = self.parse_full_stmt(false)? {
|
||||
let stmt = match self.parse_full_stmt(false) {
|
||||
Err(mut err) => {
|
||||
err.emit();
|
||||
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Break);
|
||||
self.eat(&token::CloseDelim(token::Brace));
|
||||
break;
|
||||
}
|
||||
Ok(stmt) => stmt,
|
||||
};
|
||||
if let Some(stmt) = stmt {
|
||||
stmts.push(stmt);
|
||||
} else if self.token == token::Eof {
|
||||
break;
|
||||
|
@ -4277,7 +4287,6 @@ impl<'a> Parser<'a> {
|
|||
continue;
|
||||
};
|
||||
}
|
||||
|
||||
Ok(P(ast::Block {
|
||||
stmts,
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
|
@ -5325,18 +5334,45 @@ impl<'a> Parser<'a> {
|
|||
Ok((class_name, ItemKind::Union(vdata, generics), None))
|
||||
}
|
||||
|
||||
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;
|
||||
} else if self.eat(&token::CloseDelim(delim)) {
|
||||
if brace_depth == 0 {
|
||||
return;
|
||||
} else {
|
||||
brace_depth -= 1;
|
||||
continue;
|
||||
}
|
||||
} else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) {
|
||||
return;
|
||||
} else {
|
||||
self.bump();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
||||
let mut fields = Vec::new();
|
||||
if self.eat(&token::OpenDelim(token::Brace)) {
|
||||
while self.token != token::CloseDelim(token::Brace) {
|
||||
fields.push(self.parse_struct_decl_field().map_err(|e| {
|
||||
let field = self.parse_struct_decl_field().map_err(|e| {
|
||||
self.recover_stmt();
|
||||
self.eat(&token::CloseDelim(token::Brace));
|
||||
e
|
||||
})?);
|
||||
});
|
||||
match field {
|
||||
Ok(field) => fields.push(field),
|
||||
Err(mut err) => {
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.bump();
|
||||
self.eat(&token::CloseDelim(token::Brace));
|
||||
} else {
|
||||
let token_str = self.this_token_to_string();
|
||||
return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
|
||||
|
@ -5384,8 +5420,15 @@ impl<'a> Parser<'a> {
|
|||
self.bump();
|
||||
}
|
||||
token::CloseDelim(token::Brace) => {}
|
||||
token::DocComment(_) => return Err(self.span_fatal_err(self.span,
|
||||
Error::UselessDocComment)),
|
||||
token::DocComment(_) => {
|
||||
let mut err = self.span_fatal_err(self.span, Error::UselessDocComment);
|
||||
self.bump(); // consume the doc comment
|
||||
if self.eat(&token::Comma) || self.token == token::CloseDelim(token::Brace) {
|
||||
err.emit();
|
||||
} else {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
_ => return Err(self.span_fatal_help(self.span,
|
||||
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
|
||||
"struct fields should be separated by commas")),
|
||||
|
@ -6241,7 +6284,65 @@ impl<'a> Parser<'a> {
|
|||
return Ok(Some(macro_def));
|
||||
}
|
||||
|
||||
self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
|
||||
// Verify wether we have encountered a struct or method definition where the user forgot to
|
||||
// add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
|
||||
if visibility == Visibility::Public &&
|
||||
self.check_ident() &&
|
||||
self.look_ahead(1, |t| *t != token::Not)
|
||||
{
|
||||
// Space between `pub` keyword and the identifier
|
||||
//
|
||||
// pub S {}
|
||||
// ^^^ `sp` points here
|
||||
let sp = self.prev_span.between(self.span);
|
||||
let full_sp = self.prev_span.to(self.span);
|
||||
let ident_sp = self.span;
|
||||
if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
|
||||
// possible public struct definition where `struct` was forgotten
|
||||
let ident = self.parse_ident().unwrap();
|
||||
let msg = format!("add `struct` here to parse `{}` as a public struct",
|
||||
ident);
|
||||
let mut err = self.diagnostic()
|
||||
.struct_span_err(sp, "missing `struct` for struct definition");
|
||||
err.span_suggestion_short(sp, &msg, " struct ".into());
|
||||
return Err(err);
|
||||
} else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
|
||||
let ident = self.parse_ident().unwrap();
|
||||
self.consume_block(token::Paren);
|
||||
let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) ||
|
||||
self.check(&token::OpenDelim(token::Brace))
|
||||
{
|
||||
("fn", "method", false)
|
||||
} else if self.check(&token::Colon) {
|
||||
let kw = "struct";
|
||||
(kw, kw, false)
|
||||
} else {
|
||||
("fn` or `struct", "method or struct", true)
|
||||
};
|
||||
|
||||
let msg = format!("missing `{}` for {} definition", kw, kw_name);
|
||||
let mut err = self.diagnostic().struct_span_err(sp, &msg);
|
||||
if !ambiguous {
|
||||
let suggestion = format!("add `{}` here to parse `{}` as a public {}",
|
||||
kw,
|
||||
ident,
|
||||
kw_name);
|
||||
err.span_suggestion_short(sp, &suggestion, format!(" {} ", kw));
|
||||
} else {
|
||||
if let Ok(snippet) = self.sess.codemap().span_to_snippet(ident_sp) {
|
||||
err.span_suggestion(
|
||||
full_sp,
|
||||
"if you meant to call a macro, write instead",
|
||||
format!("{}!", snippet));
|
||||
} else {
|
||||
err.help("if you meant to call a macro, remove the `pub` \
|
||||
and add a trailing `!` after the identifier");
|
||||
}
|
||||
}
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility)
|
||||
}
|
||||
|
||||
/// Parse a foreign item.
|
||||
|
|
|
@ -96,12 +96,18 @@ impl MultiItemModifier for ProcMacroDerive {
|
|||
}
|
||||
};
|
||||
|
||||
let error_count_before = ecx.parse_sess.span_diagnostic.err_count();
|
||||
__internal::set_sess(ecx, || {
|
||||
let msg = "proc-macro derive produced unparseable tokens";
|
||||
match __internal::token_stream_parse_items(stream) {
|
||||
// fail if there have been errors emitted
|
||||
Ok(_) if ecx.parse_sess.span_diagnostic.err_count() > error_count_before => {
|
||||
ecx.struct_span_fatal(span, msg).emit();
|
||||
panic!(FatalError);
|
||||
}
|
||||
Ok(new_items) => new_items.into_iter().map(Annotatable::Item).collect(),
|
||||
Err(_) => {
|
||||
// FIXME: handle this better
|
||||
let msg = "proc-macro derive produced unparseable tokens";
|
||||
ecx.struct_span_fatal(span, msg).emit();
|
||||
panic!(FatalError);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,8 @@ extern crate derive_bad;
|
|||
#[derive(
|
||||
A
|
||||
)]
|
||||
//~^^ ERROR: proc-macro derive produced unparseable tokens
|
||||
//~^^ ERROR proc-macro derive produced unparseable tokens
|
||||
//~| ERROR expected `:`, found `}`
|
||||
struct A;
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -9,12 +9,20 @@
|
|||
// except according to those terms.
|
||||
|
||||
// compile-flags: -Z continue-parse-after-error
|
||||
|
||||
struct X {
|
||||
a: u8 /** document a */,
|
||||
//~^ ERROR found a documentation comment that doesn't document anything
|
||||
//~| HELP maybe a comment was intended
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let y = X {a = 1};
|
||||
struct Y {
|
||||
a: u8 /// document a
|
||||
//~^ ERROR found a documentation comment that doesn't document anything
|
||||
//~| HELP maybe a comment was intended
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = X { a: 1 };
|
||||
let y = Y { a: 1 };
|
||||
}
|
||||
|
|
|
@ -17,5 +17,5 @@ struct X {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let y = X {a = 1};
|
||||
let y = X {a: 1};
|
||||
}
|
||||
|
|
|
@ -16,5 +16,5 @@ struct X {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let y = X {a = 1};
|
||||
let y = X {a: 1};
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ fn main() {
|
|||
println!("Y {}",x);
|
||||
return x;
|
||||
};
|
||||
//~^ ERROR expected item, found `;`
|
||||
|
||||
caller(bar_handler);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
macro_rules! failed {
|
||||
() => {{
|
||||
let x = 5 ""; //~ ERROR found `""`
|
||||
}} //~ ERROR macro expansion ignores token `}`
|
||||
}}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -15,4 +15,5 @@
|
|||
pub fn main() {
|
||||
struct Foo { x: isize }
|
||||
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected one of `:`, `;`, `=`, or `@`, found `{`
|
||||
//~^ ERROR expected item, found `=`
|
||||
}
|
||||
|
|
|
@ -9,5 +9,5 @@
|
|||
// except according to those terms.
|
||||
|
||||
fn main() {
|
||||
let v[0] = v[1]; //~ error: expected one of `:`, `;`, `=`, or `@`, found `[`
|
||||
let v[0] = v[1]; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[`
|
||||
}
|
||||
|
|
|
@ -16,4 +16,4 @@ struct Foo {
|
|||
pub(crate) () foo: usize, //~ ERROR expected identifier
|
||||
}
|
||||
|
||||
|
||||
fn main() {}
|
||||
|
|
16
src/test/ui/suggestions/pub-ident-fn-2.rs
Normal file
16
src/test/ui/suggestions/pub-ident-fn-2.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub foo(s: usize) { bar() }
|
||||
//~^ ERROR missing `fn` for method definition
|
||||
|
||||
fn main() {
|
||||
foo(2);
|
||||
}
|
12
src/test/ui/suggestions/pub-ident-fn-2.stderr
Normal file
12
src/test/ui/suggestions/pub-ident-fn-2.stderr
Normal file
|
@ -0,0 +1,12 @@
|
|||
error: missing `fn` for method definition
|
||||
--> $DIR/pub-ident-fn-2.rs:11:4
|
||||
|
|
||||
11 | pub foo(s: usize) { bar() }
|
||||
| ^
|
||||
help: add `fn` here to parse `foo` as a public method
|
||||
|
|
||||
11 | pub fn foo(s: usize) { bar() }
|
||||
| ^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
14
src/test/ui/suggestions/pub-ident-fn-or-struct-2.rs
Normal file
14
src/test/ui/suggestions/pub-ident-fn-or-struct-2.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub S();
|
||||
//~^ ERROR missing `fn` or `struct` for method or struct definition
|
||||
|
||||
fn main() {}
|
8
src/test/ui/suggestions/pub-ident-fn-or-struct-2.stderr
Normal file
8
src/test/ui/suggestions/pub-ident-fn-or-struct-2.stderr
Normal file
|
@ -0,0 +1,8 @@
|
|||
error: missing `fn` or `struct` for method or struct definition
|
||||
--> $DIR/pub-ident-fn-or-struct-2.rs:11:4
|
||||
|
|
||||
11 | pub S();
|
||||
| ---^- help: if you meant to call a macro, write instead: `S!`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
14
src/test/ui/suggestions/pub-ident-fn-or-struct.rs
Normal file
14
src/test/ui/suggestions/pub-ident-fn-or-struct.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub S (foo) bar
|
||||
//~^ ERROR missing `fn` or `struct` for method or struct definition
|
||||
|
||||
fn main() {}
|
8
src/test/ui/suggestions/pub-ident-fn-or-struct.stderr
Normal file
8
src/test/ui/suggestions/pub-ident-fn-or-struct.stderr
Normal file
|
@ -0,0 +1,8 @@
|
|||
error: missing `fn` or `struct` for method or struct definition
|
||||
--> $DIR/pub-ident-fn-or-struct.rs:11:4
|
||||
|
|
||||
11 | pub S (foo) bar
|
||||
| ---^- help: if you meant to call a macro, write instead: `S!`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
16
src/test/ui/suggestions/pub-ident-fn.rs
Normal file
16
src/test/ui/suggestions/pub-ident-fn.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub foo(s: usize) -> bool { true }
|
||||
//~^ ERROR missing `fn` for method definition
|
||||
|
||||
fn main() {
|
||||
foo(2);
|
||||
}
|
12
src/test/ui/suggestions/pub-ident-fn.stderr
Normal file
12
src/test/ui/suggestions/pub-ident-fn.stderr
Normal file
|
@ -0,0 +1,12 @@
|
|||
error: missing `fn` for method definition
|
||||
--> $DIR/pub-ident-fn.rs:11:4
|
||||
|
|
||||
11 | pub foo(s: usize) -> bool { true }
|
||||
| ^^^
|
||||
help: add `fn` here to parse `foo` as a public method
|
||||
|
|
||||
11 | pub fn foo(s: usize) -> bool { true }
|
||||
| ^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
14
src/test/ui/suggestions/pub-ident-struct.rs
Normal file
14
src/test/ui/suggestions/pub-ident-struct.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub S {
|
||||
//~^ ERROR missing `struct` for struct definition
|
||||
}
|
||||
fn main() {}
|
12
src/test/ui/suggestions/pub-ident-struct.stderr
Normal file
12
src/test/ui/suggestions/pub-ident-struct.stderr
Normal file
|
@ -0,0 +1,12 @@
|
|||
error: missing `struct` for struct definition
|
||||
--> $DIR/pub-ident-struct.rs:11:4
|
||||
|
|
||||
11 | pub S {
|
||||
| ^
|
||||
help: add `struct` here to parse `S` as a public struct
|
||||
|
|
||||
11 | pub struct S {
|
||||
| ^^^^^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Add table
Reference in a new issue