Skip to content

Commit df357b2

Browse files
committed
Suggest macro call when not sure that it is fn definition
1 parent c82e9e8 commit df357b2

20 files changed

+33
-37
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,9 +3158,7 @@ impl<'a> Parser<'a> {
31583158
attrs.extend(iattrs);
31593159

31603160
let hi = self.prev_span;
3161-
Ok(self.mk_expr(span_lo.to(hi),
3162-
ExprKind::ForLoop(pat, expr, loop_block, opt_ident),
3163-
attrs))
3161+
Ok(self.mk_expr(span_lo.to(hi), ExprKind::ForLoop(pat, expr, loop_block, opt_ident), attrs))
31643162
}
31653163

31663164
/// Parse a 'while' or 'while let' expression ('while' token already eaten)
@@ -4252,13 +4250,11 @@ impl<'a> Parser<'a> {
42524250
return Err(e);
42534251
}
42544252

4255-
Ok(self.parse_block_tail(lo, BlockCheckMode::Default)?)
4253+
self.parse_block_tail(lo, BlockCheckMode::Default)
42564254
}
42574255

42584256
/// Parse a block. Inner attrs are allowed.
4259-
fn parse_inner_attrs_and_block(&mut self)
4260-
-> PResult<'a, (Vec<Attribute>, P<Block>)>
4261-
{
4257+
fn parse_inner_attrs_and_block(&mut self) -> PResult<'a, (Vec<Attribute>, P<Block>)> {
42624258
maybe_whole!(self, NtBlock, |x| (Vec::new(), x));
42634259

42644260
let lo = self.span;
@@ -4269,9 +4265,7 @@ impl<'a> Parser<'a> {
42694265

42704266
/// Parse the rest of a block expression or function body
42714267
/// Precondition: already parsed the '{'.
4272-
fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode)
4273-
-> PResult<'a, P<Block>>
4274-
{
4268+
fn parse_block_tail(&mut self, lo: Span, s: BlockCheckMode) -> PResult<'a, P<Block>> {
42754269
let mut stmts = vec![];
42764270

42774271
while !self.eat(&token::CloseDelim(token::Brace)) {
@@ -5340,32 +5334,23 @@ impl<'a> Parser<'a> {
53405334
}
53415335

53425336
fn consume_block(&mut self, delim: token::DelimToken) {
5343-
debug!("consuming {:?}", delim);
5344-
debug!("self.token {:?}", self.token);
53455337
let mut brace_depth = 0;
53465338
if !self.eat(&token::OpenDelim(delim)) {
5347-
debug!("didn't eat delim");
53485339
return;
53495340
}
53505341
loop {
53515342
if self.eat(&token::OpenDelim(delim)) {
5352-
debug!("add depth");
53535343
brace_depth += 1;
53545344
} else if self.eat(&token::CloseDelim(delim)) {
5355-
debug!("found closing");
53565345
if brace_depth == 0 {
5357-
debug!("ending");
53585346
return;
53595347
} else {
5360-
debug!("decrease");
53615348
brace_depth -= 1;
53625349
continue;
53635350
}
53645351
} else if self.eat(&token::Eof) || self.eat(&token::CloseDelim(token::NoDelim)) {
5365-
debug!("eof or nodelim");
53665352
return;
53675353
} else {
5368-
debug!("bump");
53695354
self.bump();
53705355
}
53715356
}
@@ -6297,6 +6282,8 @@ impl<'a> Parser<'a> {
62976282
// pub S {}
62986283
// ^^^ `sp` points here
62996284
let sp = self.prev_span.between(self.span);
6285+
let full_sp = self.prev_span.to(self.span);
6286+
let ident_sp = self.span;
63006287
if self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) {
63016288
// possible public struct definition where `struct` was forgotten
63026289
let ident = self.parse_ident().unwrap();
@@ -6328,6 +6315,16 @@ impl<'a> Parser<'a> {
63286315
ident,
63296316
kw_name);
63306317
err.span_suggestion_short(sp, &suggestion, format!(" {} ", kw));
6318+
} else {
6319+
if let Ok(snippet) = self.sess.codemap().span_to_snippet(ident_sp) {
6320+
err.span_suggestion(
6321+
full_sp,
6322+
"if you meant to call a macro, write instead",
6323+
format!("{}!", snippet));
6324+
} else {
6325+
err.help("if you meant to call a macro, remove the `pub` \
6326+
and add a trailing `!` after the identifier");
6327+
}
63316328
}
63326329
return Err(err);
63336330
}

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

Lines changed: 1 addition & 1 deletion
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/doc-before-struct-rbrace-1.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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-22712.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ struct Foo<B> {
1414

1515
fn bar() {
1616
let Foo<Vec<u8>> //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `<`
17-
}
17+
} //~ ERROR expected item, found `}`
1818

1919
fn main() {}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

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

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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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 `[`
13-
}
12+
let v[0] = v[1]; //~ ERROR expected one of `:`, `;`, `=`, or `@`, found `[`
13+
} //~ ERROR expected item, found `}`

src/test/parse-fail/pat-ranges-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

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

src/test/parse-fail/pat-ranges-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

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

src/test/parse-fail/pat-ranges-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

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

src/test/parse-fail/pat-ranges-4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
fn main() {
1414
let 10 - 3 ..= 10 = 8;
1515
//~^ error: expected one of `...`, `..=`, `..`, `:`, `;`, or `=`, found `-`
16-
}
16+
} //~ ERROR expected item, found `}`

src/test/parse-fail/range-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
pub fn main() {
1616
let r = 1..2..3;
1717
//~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..`
18-
}
18+
} //~ ERROR expected item, found `}`

src/test/parse-fail/range-4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
pub fn main() {
1616
let r = ..1..2;
1717
//~^ ERROR expected one of `.`, `;`, `?`, or an operator, found `..`
18-
}
18+
} //~ ERROR expected item, found `}`

src/test/ui/suggestions/pub-ident-fn-2.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ error: missing `fn` for method definition
33
|
44
11 | pub foo(s: usize) { bar() }
55
| ^
6-
|
76
help: add `fn` here to parse `foo` as a public method
87
|
98
11 | pub fn foo(s: usize) { bar() }

src/test/ui/suggestions/pub-ident-fn-or-struct-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: missing `fn` or `struct` for method or struct definition
22
--> $DIR/pub-ident-fn-or-struct-2.rs:11:4
33
|
44
11 | pub S();
5-
| ^
5+
| ---^- help: if you meant to call a macro, write instead: `S!`
66

77
error: aborting due to previous error
88

src/test/ui/suggestions/pub-ident-fn-or-struct.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: missing `fn` or `struct` for method or struct definition
22
--> $DIR/pub-ident-fn-or-struct.rs:11:4
33
|
44
11 | pub S (foo) bar
5-
| ^
5+
| ---^- help: if you meant to call a macro, write instead: `S!`
66

77
error: aborting due to previous error
88

src/test/ui/suggestions/pub-ident-fn.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ error: missing `fn` for method definition
33
|
44
11 | pub foo(s: usize) -> bool { true }
55
| ^^^
6-
|
76
help: add `fn` here to parse `foo` as a public method
87
|
98
11 | pub fn foo(s: usize) -> bool { true }

src/test/ui/suggestions/pub-ident-struct.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ error: missing `struct` for struct definition
33
|
44
11 | pub S {
55
| ^
6-
|
76
help: add `struct` here to parse `S` as a public struct
87
|
98
11 | pub struct S {

0 commit comments

Comments
 (0)