Skip to content

Commit d1364a6

Browse files
committed
Auto merge of #45997 - estebank:pub-ident, r=nikomatsakis
Account for missing keyword in fn/struct definition Fix #38911.
2 parents 804b15b + cf9283e commit d1364a6

24 files changed

+267
-21
lines changed

src/librustc/hir/lowering.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,9 @@ impl<'a> LoweringContext<'a> {
18901890
bounds,
18911891
items)
18921892
}
1893-
ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
1893+
ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
1894+
panic!("Shouldn't still be around")
1895+
}
18941896
}
18951897

18961898
// [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to

src/librustc_errors/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl EmitterWriter {
11561156
let start = parts[0].snippet.len() - parts[0].snippet.trim_left().len();
11571157
let sub_len = parts[0].snippet.trim().len();
11581158
let underline_start = span_start_pos.col.0 + start;
1159-
let underline_end = span_start_pos.col.0 + sub_len;
1159+
let underline_end = span_start_pos.col.0 + start + sub_len;
11601160
for p in underline_start..underline_end {
11611161
buffer.putc(row_num,
11621162
max_line_num_len + 3 + p,

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ impl<'a> Resolver<'a> {
19451945
self.resolve_use_tree(item, use_tree, &path);
19461946
}
19471947

1948-
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_)=> {
1948+
ItemKind::ExternCrate(_) | ItemKind::MacroDef(..) | ItemKind::GlobalAsm(_) => {
19491949
// do nothing, these are just around to be encoded
19501950
}
19511951

src/libsyntax/parse/parser.rs

+111-10
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ impl<'a> Parser<'a> {
11561156
None => token::CloseDelim(self.token_cursor.frame.delim),
11571157
})
11581158
}
1159+
11591160
fn look_ahead_span(&self, dist: usize) -> Span {
11601161
if dist == 0 {
11611162
return self.span
@@ -4268,7 +4269,16 @@ impl<'a> Parser<'a> {
42684269
let mut stmts = vec![];
42694270

42704271
while !self.eat(&token::CloseDelim(token::Brace)) {
4271-
if let Some(stmt) = self.parse_full_stmt(false)? {
4272+
let stmt = match self.parse_full_stmt(false) {
4273+
Err(mut err) => {
4274+
err.emit();
4275+
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Break);
4276+
self.eat(&token::CloseDelim(token::Brace));
4277+
break;
4278+
}
4279+
Ok(stmt) => stmt,
4280+
};
4281+
if let Some(stmt) = stmt {
42724282
stmts.push(stmt);
42734283
} else if self.token == token::Eof {
42744284
break;
@@ -4277,7 +4287,6 @@ impl<'a> Parser<'a> {
42774287
continue;
42784288
};
42794289
}
4280-
42814290
Ok(P(ast::Block {
42824291
stmts,
42834292
id: ast::DUMMY_NODE_ID,
@@ -5325,18 +5334,45 @@ impl<'a> Parser<'a> {
53255334
Ok((class_name, ItemKind::Union(vdata, generics), None))
53265335
}
53275336

5337+
fn consume_block(&mut self, delim: token::DelimToken) {
5338+
let mut brace_depth = 0;
5339+
if !self.eat(&token::OpenDelim(delim)) {
5340+
return;
5341+
}
5342+
loop {
5343+
if self.eat(&token::OpenDelim(delim)) {
5344+
brace_depth += 1;
5345+
} else if self.eat(&token::CloseDelim(delim)) {
5346+
if brace_depth == 0 {
5347+
return;
5348+
} else {
5349+
brace_depth -= 1;
5350+
continue;
5351+
}
5352+
} else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) {
5353+
return;
5354+
} else {
5355+
self.bump();
5356+
}
5357+
}
5358+
}
5359+
53285360
pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
53295361
let mut fields = Vec::new();
53305362
if self.eat(&token::OpenDelim(token::Brace)) {
53315363
while self.token != token::CloseDelim(token::Brace) {
5332-
fields.push(self.parse_struct_decl_field().map_err(|e| {
5364+
let field = self.parse_struct_decl_field().map_err(|e| {
53335365
self.recover_stmt();
5334-
self.eat(&token::CloseDelim(token::Brace));
53355366
e
5336-
})?);
5367+
});
5368+
match field {
5369+
Ok(field) => fields.push(field),
5370+
Err(mut err) => {
5371+
err.emit();
5372+
}
5373+
}
53375374
}
5338-
5339-
self.bump();
5375+
self.eat(&token::CloseDelim(token::Brace));
53405376
} else {
53415377
let token_str = self.this_token_to_string();
53425378
return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
@@ -5384,8 +5420,15 @@ impl<'a> Parser<'a> {
53845420
self.bump();
53855421
}
53865422
token::CloseDelim(token::Brace) => {}
5387-
token::DocComment(_) => return Err(self.span_fatal_err(self.span,
5388-
Error::UselessDocComment)),
5423+
token::DocComment(_) => {
5424+
let mut err = self.span_fatal_err(self.span, Error::UselessDocComment);
5425+
self.bump(); // consume the doc comment
5426+
if self.eat(&token::Comma) || self.token == token::CloseDelim(token::Brace) {
5427+
err.emit();
5428+
} else {
5429+
return Err(err);
5430+
}
5431+
}
53895432
_ => return Err(self.span_fatal_help(self.span,
53905433
&format!("expected `,`, or `}}`, found `{}`", self.this_token_to_string()),
53915434
"struct fields should be separated by commas")),
@@ -6241,7 +6284,65 @@ impl<'a> Parser<'a> {
62416284
return Ok(Some(macro_def));
62426285
}
62436286

6244-
self.parse_macro_use_or_failure(attrs,macros_allowed,attributes_allowed,lo,visibility)
6287+
// Verify wether we have encountered a struct or method definition where the user forgot to
6288+
// add the `struct` or `fn` keyword after writing `pub`: `pub S {}`
6289+
if visibility == Visibility::Public &&
6290+
self.check_ident() &&
6291+
self.look_ahead(1, |t| *t != token::Not)
6292+
{
6293+
// Space between `pub` keyword and the identifier
6294+
//
6295+
// pub S {}
6296+
// ^^^ `sp` points here
6297+
let sp = self.prev_span.between(self.span);
6298+
let full_sp = self.prev_span.to(self.span);
6299+
let ident_sp = self.span;
6300+
if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
6301+
// possible public struct definition where `struct` was forgotten
6302+
let ident = self.parse_ident().unwrap();
6303+
let msg = format!("add `struct` here to parse `{}` as a public struct",
6304+
ident);
6305+
let mut err = self.diagnostic()
6306+
.struct_span_err(sp, "missing `struct` for struct definition");
6307+
err.span_suggestion_short(sp, &msg, " struct ".into());
6308+
return Err(err);
6309+
} else if self.look_ahead(1, |t| *t == token::OpenDelim(token::Paren)) {
6310+
let ident = self.parse_ident().unwrap();
6311+
self.consume_block(token::Paren);
6312+
let (kw, kw_name, ambiguous) = if self.check(&token::RArrow) ||
6313+
self.check(&token::OpenDelim(token::Brace))
6314+
{
6315+
("fn", "method", false)
6316+
} else if self.check(&token::Colon) {
6317+
let kw = "struct";
6318+
(kw, kw, false)
6319+
} else {
6320+
("fn` or `struct", "method or struct", true)
6321+
};
6322+
6323+
let msg = format!("missing `{}` for {} definition", kw, kw_name);
6324+
let mut err = self.diagnostic().struct_span_err(sp, &msg);
6325+
if !ambiguous {
6326+
let suggestion = format!("add `{}` here to parse `{}` as a public {}",
6327+
kw,
6328+
ident,
6329+
kw_name);
6330+
err.span_suggestion_short(sp, &suggestion, format!(" {} ", kw));
6331+
} else {
6332+
if let Ok(snippet) = self.sess.codemap().span_to_snippet(ident_sp) {
6333+
err.span_suggestion(
6334+
full_sp,
6335+
"if you meant to call a macro, write instead",
6336+
format!("{}!", snippet));
6337+
} else {
6338+
err.help("if you meant to call a macro, remove the `pub` \
6339+
and add a trailing `!` after the identifier");
6340+
}
6341+
}
6342+
return Err(err);
6343+
}
6344+
}
6345+
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility)
62456346
}
62466347

62476348
/// Parse a foreign item.

src/libsyntax_ext/deriving/custom.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,18 @@ impl MultiItemModifier for ProcMacroDerive {
9696
}
9797
};
9898

99+
let error_count_before = ecx.parse_sess.span_diagnostic.err_count();
99100
__internal::set_sess(ecx, || {
101+
let msg = "proc-macro derive produced unparseable tokens";
100102
match __internal::token_stream_parse_items(stream) {
103+
// fail if there have been errors emitted
104+
Ok(_) if ecx.parse_sess.span_diagnostic.err_count() > error_count_before => {
105+
ecx.struct_span_fatal(span, msg).emit();
106+
panic!(FatalError);
107+
}
101108
Ok(new_items) => new_items.into_iter().map(Annotatable::Item).collect(),
102109
Err(_) => {
103110
// FIXME: handle this better
104-
let msg = "proc-macro derive produced unparseable tokens";
105111
ecx.struct_span_fatal(span, msg).emit();
106112
panic!(FatalError);
107113
}

src/test/compile-fail-fulldeps/proc-macro/derive-bad.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ extern crate derive_bad;
1717
#[derive(
1818
A
1919
)]
20-
//~^^ ERROR: proc-macro derive produced unparseable tokens
20+
//~^^ ERROR proc-macro derive produced unparseable tokens
21+
//~| ERROR expected `:`, found `}`
2122
struct A;
2223

2324
fn main() {}

src/test/parse-fail/doc-after-struct-field.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99
// except according to those terms.
1010

1111
// compile-flags: -Z continue-parse-after-error
12+
1213
struct X {
1314
a: u8 /** document a */,
1415
//~^ ERROR found a documentation comment that doesn't document anything
1516
//~| HELP maybe a comment was intended
1617
}
1718

19+
struct Y {
20+
a: u8 /// document a
21+
//~^ ERROR found a documentation comment that doesn't document anything
22+
//~| HELP maybe a comment was intended
23+
}
24+
1825
fn main() {
19-
let y = X {a = 1};
26+
let x = X { a: 1 };
27+
let y = Y { a: 1 };
2028
}

src/test/parse-fail/doc-before-struct-rbrace-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ struct X {
1717
}
1818

1919
fn main() {
20-
let y = X {a = 1};
20+
let y = X {a: 1};
2121
}

src/test/parse-fail/doc-before-struct-rbrace-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ struct X {
1616
}
1717

1818
fn main() {
19-
let y = X {a = 1};
19+
let y = X {a: 1};
2020
}

src/test/parse-fail/issue-22647.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
println!("Y {}",x);
1717
return x;
1818
};
19+
//~^ ERROR expected item, found `;`
1920

2021
caller(bar_handler);
2122
}

src/test/parse-fail/issue-37234.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
macro_rules! failed {
1212
() => {{
1313
let x = 5 ""; //~ ERROR found `""`
14-
}} //~ ERROR macro expansion ignores token `}`
14+
}}
1515
}
1616

1717
fn main() {

src/test/parse-fail/mut-patterns.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
pub fn main() {
1616
struct Foo { x: isize }
1717
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected one of `:`, `;`, `=`, or `@`, found `{`
18+
//~^ ERROR expected item, found `=`
1819
}

src/test/parse-fail/pat-lt-bracket-5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
let v[0] = v[1]; //~ error: expected one of `:`, `;`, `=`, or `@`, found `[`
12+
let v[0] = v[1]; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[`
1313
}

src/test/ui/pub/pub-restricted-error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ struct Foo {
1616
pub(crate) () foo: usize, //~ ERROR expected identifier
1717
}
1818

19-
19+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub foo(s: usize) { bar() }
12+
//~^ ERROR missing `fn` for method definition
13+
14+
fn main() {
15+
foo(2);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: missing `fn` for method definition
2+
--> $DIR/pub-ident-fn-2.rs:11:4
3+
|
4+
11 | pub foo(s: usize) { bar() }
5+
| ^
6+
help: add `fn` here to parse `foo` as a public method
7+
|
8+
11 | pub fn foo(s: usize) { bar() }
9+
| ^^
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub S();
12+
//~^ ERROR missing `fn` or `struct` for method or struct definition
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: missing `fn` or `struct` for method or struct definition
2+
--> $DIR/pub-ident-fn-or-struct-2.rs:11:4
3+
|
4+
11 | pub S();
5+
| ---^- help: if you meant to call a macro, write instead: `S!`
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub S (foo) bar
12+
//~^ ERROR missing `fn` or `struct` for method or struct definition
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: missing `fn` or `struct` for method or struct definition
2+
--> $DIR/pub-ident-fn-or-struct.rs:11:4
3+
|
4+
11 | pub S (foo) bar
5+
| ---^- help: if you meant to call a macro, write instead: `S!`
6+
7+
error: aborting due to previous error
8+
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub foo(s: usize) -> bool { true }
12+
//~^ ERROR missing `fn` for method definition
13+
14+
fn main() {
15+
foo(2);
16+
}

0 commit comments

Comments
 (0)