Skip to content

Commit 0952856

Browse files
committed
Suggest an appropriate token when encountering pub Ident<'a>
1 parent 3dde9e1 commit 0952856

7 files changed

+75
-11
lines changed

src/libsyntax/parse/parser.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -5802,20 +5802,14 @@ impl<'a> Parser<'a> {
58025802
}
58035803

58045804
fn complain_if_pub_macro(&mut self, vis: &VisibilityKind, sp: Span) {
5805-
if let Err(mut err) = self.complain_if_pub_macro_diag(vis, sp) {
5806-
err.emit();
5807-
}
5808-
}
5809-
5810-
fn complain_if_pub_macro_diag(&mut self, vis: &VisibilityKind, sp: Span) -> PResult<'a, ()> {
58115805
match *vis {
5812-
VisibilityKind::Inherited => Ok(()),
5806+
VisibilityKind::Inherited => {}
58135807
_ => {
58145808
let is_macro_rules: bool = match self.token {
58155809
token::Ident(sid, _) => sid.name == Symbol::intern("macro_rules"),
58165810
_ => false,
58175811
};
5818-
if is_macro_rules {
5812+
let mut err = if is_macro_rules {
58195813
let mut err = self.diagnostic()
58205814
.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
58215815
err.span_suggestion_with_applicability(
@@ -5824,13 +5818,14 @@ impl<'a> Parser<'a> {
58245818
"#[macro_export]".to_owned(),
58255819
Applicability::MaybeIncorrect // speculative
58265820
);
5827-
Err(err)
5821+
err
58285822
} else {
58295823
let mut err = self.diagnostic()
58305824
.struct_span_err(sp, "can't qualify macro invocation with `pub`");
58315825
err.help("try adjusting the macro to put `pub` inside the invocation");
5832-
Err(err)
5833-
}
5826+
err
5827+
};
5828+
err.emit();
58345829
}
58355830
}
58365831
}
@@ -7439,6 +7434,28 @@ impl<'a> Parser<'a> {
74397434
}
74407435
}
74417436
return Err(err);
7437+
} else if self.look_ahead(1, |t| *t == token::Lt) {
7438+
let ident = self.parse_ident().unwrap();
7439+
self.eat_to_tokens(&[&token::Gt]);
7440+
self.bump();
7441+
let (kw, kw_name, ambiguous) = if self.check(&token::OpenDelim(token::Paren)) {
7442+
("fn", "method", false)
7443+
} else if self.check(&token::OpenDelim(token::Brace)) {
7444+
("struct", "struct", false)
7445+
} else {
7446+
("fn` or `struct", "method or struct", true)
7447+
};
7448+
let msg = format!("missing `{}` for {} definition", kw, kw_name);
7449+
let mut err = self.diagnostic().struct_span_err(sp, &msg);
7450+
if !ambiguous {
7451+
err.span_suggestion_short_with_applicability(
7452+
sp,
7453+
&format!("add `{}` here to parse `{}` as a public {}", kw, ident, kw_name),
7454+
format!(" {} ", kw),
7455+
Applicability::MachineApplicable,
7456+
);
7457+
}
7458+
return Err(err);
74427459
}
74437460
}
74447461
self.parse_macro_use_or_failure(attrs, macros_allowed, attributes_allowed, lo, visibility)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub foo<'a>(_s: &'a usize) -> bool { true }
2+
//~^ ERROR missing `fn` for method definition
3+
4+
fn main() {
5+
foo(2);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: missing `fn` for method definition
2+
--> $DIR/pub-ident-fn-with-lifetime.rs:1:4
3+
|
4+
LL | pub foo<'a>(_s: &'a usize) -> bool { true }
5+
| ^^^
6+
help: add `fn` here to parse `foo` as a public method
7+
|
8+
LL | pub fn foo<'a>(_s: &'a usize) -> bool { true }
9+
| ^^
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub S<'a> {
2+
//~^ ERROR missing `struct` for struct definition
3+
}
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: missing `struct` for struct definition
2+
--> $DIR/pub-ident-struct-with-lifetime.rs:1:4
3+
|
4+
LL | pub S<'a> {
5+
| ^
6+
help: add `struct` here to parse `S` as a public struct
7+
|
8+
LL | pub struct S<'a> {
9+
| ^^^^^^
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
}
3+
4+
pub foo<'a>
5+
//~^ ERROR missing `fn` or `struct` for method or struct definition
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-with-lifetime-incomplete.rs:4:4
3+
|
4+
LL | pub foo<'a>
5+
| ^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)