Suggest macro call when not sure that it is fn definition

This commit is contained in:
Esteban Küber 2017-11-22 09:49:27 -08:00
parent c82e9e8e1e
commit df357b20be
20 changed files with 33 additions and 37 deletions

View File

@ -3158,9 +3158,7 @@ impl<'a> Parser<'a> {
attrs.extend(iattrs); attrs.extend(iattrs);
let hi = self.prev_span; let hi = self.prev_span;
Ok(self.mk_expr(span_lo.to(hi), Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_ident), attrs))
ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
attrs))
} }
/// Parse a 'while' or 'while let' expression ('while' token already eaten) /// Parse a 'while' or 'while let' expression ('while' token already eaten)
@ -4252,13 +4250,11 @@ impl<'a> Parser<'a> {
return Err(e); return Err(e);
} }
Ok(self.parse_block_tail(lo, BlockCheckMode::Default)?) self.parse_block_tail(lo, BlockCheckMode::Default)
} }
/// Parse a block. Inner attrs are allowed. /// Parse a block. Inner attrs are allowed.
fn parse_inner_attrs_and_block(&mut self) fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
-> PResult<'a, (Vec<Attribute>, P<Block>)>
{
maybe_whole!(self, NtBlock, |x| (Vec::new(), x)); maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
let lo = self.span; let lo = self.span;
@ -4269,9 +4265,7 @@ impl<'a> Parser<'a> {
/// Parse the rest of a block expression or function body /// Parse the rest of a block expression or function body
/// Precondition: already parsed the '{'. /// Precondition: already parsed the '{'.
fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P<Block>> {
-> PResult<'a, P<Block>>
{
let mut stmts = vec![]; let mut stmts = vec![];
while !self.eat(&token::CloseDelim(token::Brace)) { while !self.eat(&token::CloseDelim(token::Brace)) {
@ -5340,32 +5334,23 @@ impl<'a> Parser<'a> {
} }
fn consume_block(&mut self, delim: token::DelimToken) { fn consume_block(&mut self, delim: token::DelimToken) {
debug!("consuming {:?}", delim);
debug!("self.token {:?}", self.token);
let mut brace_depth = 0; let mut brace_depth = 0;
if !self.eat(&token::OpenDelim(delim)) { if !self.eat(&token::OpenDelim(delim)) {
debug!("didn't eat delim");
return; return;
} }
loop { loop {
if self.eat(&token::OpenDelim(delim)) { if self.eat(&token::OpenDelim(delim)) {
debug!("add depth");
brace_depth += 1; brace_depth += 1;
} else if self.eat(&token::CloseDelim(delim)) { } else if self.eat(&token::CloseDelim(delim)) {
debug!("found closing");
if brace_depth == 0 { if brace_depth == 0 {
debug!("ending");
return; return;
} else { } else {
debug!("decrease");
brace_depth -= 1; brace_depth -= 1;
continue; continue;
} }
} else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) { } else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) {
debug!("eof or nodelim");
return; return;
} else { } else {
debug!("bump");
self.bump(); self.bump();
} }
} }
@ -6297,6 +6282,8 @@ impl<'a> Parser<'a> {
// pub S {} // pub S {}
// ^^^ `sp` points here // ^^^ `sp` points here
let sp = self.prev_span.between(self.span); 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)) { if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
// possible public struct definition where `struct` was forgotten // possible public struct definition where `struct` was forgotten
let ident = self.parse_ident().unwrap(); let ident = self.parse_ident().unwrap();
@ -6328,6 +6315,16 @@ impl<'a> Parser<'a> {
ident, ident,
kw_name); kw_name);
err.span_suggestion_short(sp, &suggestion, format!(" {} ", kw)); 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); return Err(err);
} }

View File

@ -16,5 +16,5 @@ struct X {
} }
fn main() { fn main() {
let y = X {a = 1}; let y = X {a: 1};
} }

View File

@ -17,5 +17,5 @@ struct X {
} }
fn main() { fn main() {
let y = X {a = 1}; let y = X {a: 1};
} }

View File

@ -16,5 +16,5 @@ struct X {
} }
fn main() { fn main() {
let y = X {a = 1}; let y = X {a: 1};
} }

View File

@ -16,6 +16,7 @@ fn main() {
println!("Y {}",x); println!("Y {}",x);
return x; return x;
}; };
//~^ ERROR expected item, found `;`
caller(bar_handler); caller(bar_handler);
} }

View File

@ -14,6 +14,6 @@ struct Foo<B> {
fn bar() { fn bar() {
let Foo<Vec<u8>> //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `<` let Foo<Vec<u8>> //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `<`
} } //~ ERROR expected item, found `}`
fn main() {} fn main() {}

View File

@ -10,4 +10,4 @@
fn main() { fn main() {
let buf[0] = 0; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[` let buf[0] = 0; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[`
} } //~ ERROR expected item, found `}`

View File

@ -15,4 +15,5 @@
pub fn main() { pub fn main() {
struct Foo { x: isize } struct Foo { x: isize }
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected one of `:`, `;`, `=`, or `@`, found `{` let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected one of `:`, `;`, `=`, or `@`, found `{`
//~^ ERROR expected item, found `=`
} }

View File

@ -9,5 +9,5 @@
// except according to those terms. // except according to those terms.
fn main() { fn main() {
let v[0] = v[1]; //~ error: expected one of `:`, `;`, `=`, or `@`, found `[` let v[0] = v[1]; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[`
} } //~ ERROR expected item, found `}`

View File

@ -12,4 +12,4 @@
fn main() { fn main() {
let macropus!() ..= 11 = 12; //~ error: expected one of `:`, `;`, or `=`, found `..=` let macropus!() ..= 11 = 12; //~ error: expected one of `:`, `;`, or `=`, found `..=`
} } //~ ERROR expected item, found `}`

View File

@ -12,4 +12,4 @@
fn main() { fn main() {
let 10 ..= makropulos!() = 12; //~ error: expected one of `::`, `:`, `;`, or `=`, found `!` let 10 ..= makropulos!() = 12; //~ error: expected one of `::`, `:`, `;`, or `=`, found `!`
} } //~ ERROR expected item, found `}`

View File

@ -12,4 +12,4 @@
fn main() { fn main() {
let 10 ..= 10 + 3 = 12; //~ expected one of `:`, `;`, or `=`, found `+` let 10 ..= 10 + 3 = 12; //~ expected one of `:`, `;`, or `=`, found `+`
} } //~ ERROR expected item, found `}`

View File

@ -13,4 +13,4 @@
fn main() { fn main() {
let 10 - 3 ..= 10 = 8; let 10 - 3 ..= 10 = 8;
//~^ error: expected one of `...`, `..=`, `..`, `:`, `;`, or `=`, found `-` //~^ error: expected one of `...`, `..=`, `..`, `:`, `;`, or `=`, found `-`
} } //~ ERROR expected item, found `}`

View File

@ -15,4 +15,4 @@
pub fn main() { pub fn main() {
let r = 1..2..3; let r = 1..2..3;
//~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..` //~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..`
} } //~ ERROR expected item, found `}`

View File

@ -15,4 +15,4 @@
pub fn main() { pub fn main() {
let r = ..1..2; let r = ..1..2;
//~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..` //~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..`
} } //~ ERROR expected item, found `}`

View File

@ -3,7 +3,6 @@ error: missing `fn` for method definition
| |
11 | pub foo(s: usize) { bar() } 11 | 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 method
| |
11 | pub fn foo(s: usize) { bar() } 11 | pub fn foo(s: usize) { bar() }

View File

@ -2,7 +2,7 @@ error: missing `fn` or `struct` for method or struct definition
--> $DIR/pub-ident-fn-or-struct-2.rs:11:4 --> $DIR/pub-ident-fn-or-struct-2.rs:11:4
| |
11 | pub S(); 11 | pub S();
| ^ | ---^- help: if you meant to call a macro, write instead: `S!`
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,7 +2,7 @@ error: missing `fn` or `struct` for method or struct definition
--> $DIR/pub-ident-fn-or-struct.rs:11:4 --> $DIR/pub-ident-fn-or-struct.rs:11:4
| |
11 | pub S (foo) bar 11 | pub S (foo) bar
| ^ | ---^- help: if you meant to call a macro, write instead: `S!`
error: aborting due to previous error error: aborting due to previous error

View File

@ -3,7 +3,6 @@ error: missing `fn` for method definition
| |
11 | pub foo(s: usize) -> bool { true } 11 | 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 method
| |
11 | pub fn foo(s: usize) -> bool { true } 11 | pub fn foo(s: usize) -> bool { true }

View File

@ -3,7 +3,6 @@ error: missing `struct` for struct definition
| |
11 | pub S { 11 | pub S {
| ^ | ^
|
help: add `struct` here to parse `S` as a public struct help: add `struct` here to parse `S` as a public struct
| |
11 | pub struct S { 11 | pub struct S {