Skip to content

Identify missing item category in impls #40815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4644,25 +4644,30 @@ impl<'a> Parser<'a> {
})
}

fn complain_if_pub_macro(&mut self, visa: &Visibility, span: Span) {
match *visa {
Visibility::Inherited => (),
fn complain_if_pub_macro(&mut self, vis: &Visibility, sp: Span) {
if let Err(mut err) = self.complain_if_pub_macro_diag(vis, sp) {
err.emit();
}
}

fn complain_if_pub_macro_diag(&mut self, vis: &Visibility, sp: Span) -> PResult<'a, ()> {
match *vis {
Visibility::Inherited => Ok(()),
_ => {
let is_macro_rules: bool = match self.token {
token::Ident(sid) => sid.name == Symbol::intern("macro_rules"),
_ => false,
};
if is_macro_rules {
self.diagnostic().struct_span_err(span, "can't qualify macro_rules \
invocation with `pub`")
.help("did you mean #[macro_export]?")
.emit();
let mut err = self.diagnostic()
.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
err.help("did you mean #[macro_export]?");
Err(err)
} else {
self.diagnostic().struct_span_err(span, "can't qualify macro \
invocation with `pub`")
.help("try adjusting the macro to put `pub` \
inside the invocation")
.emit();
let mut err = self.diagnostic()
.struct_span_err(sp, "can't qualify macro invocation with `pub`");
err.help("try adjusting the macro to put `pub` inside the invocation");
Err(err)
}
}
}
Expand All @@ -4673,14 +4678,36 @@ impl<'a> Parser<'a> {
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
// code copied from parse_macro_use_or_failure... abstraction!
if self.token.is_path_start() {
// method macro.
// Method macro.

let prev_span = self.prev_span;
self.complain_if_pub_macro(&vis, prev_span);
// Before complaining about trying to set a macro as `pub`,
// check if `!` comes after the path.
let err = self.complain_if_pub_macro_diag(&vis, prev_span);

let lo = self.span;
let pth = self.parse_path(PathStyle::Mod)?;
self.expect(&token::Not)?;
let bang_err = self.expect(&token::Not);
if let Err(mut err) = err {
if let Err(mut bang_err) = bang_err {
// Given this code `pub path(`, it seems like this is not setting the
// visibility of a macro invocation, but rather a mistyped method declaration.
// Create a diagnostic pointing out that `fn` is missing.
//
// x | pub path(&self) {
// | ^ missing `fn` for method declaration

err.cancel();
bang_err.cancel();
// pub path(
// ^^ `sp` below will point to this
let sp = prev_span.between(self.prev_span);
err = self.diagnostic()
.struct_span_err(sp, "missing `fn` for method declaration");
err.span_label(sp, &"missing `fn`");
}
return Err(err);
}

// eat a matched-delimiter token tree:
let (delim, tts) = self.expect_delimited_token_tree()?;
Expand Down
24 changes: 24 additions & 0 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,30 @@ impl Span {
Span { hi: end.hi, ..self }
}
}

pub fn between(self, end: Span) -> Span {
Span {
lo: self.hi,
hi: end.lo,
ctxt: if end.ctxt == SyntaxContext::empty() {
end.ctxt
} else {
self.ctxt
}
}
}

pub fn until(self, end: Span) -> Span {
Span {
lo: self.lo,
hi: end.lo,
ctxt: if end.ctxt == SyntaxContext::empty() {
end.ctxt
} else {
self.ctxt
}
}
}
}

#[derive(Clone, Debug)]
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/did_you_mean/issue-40006.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

struct S;

impl S {
pub hello_method(&self) {
println!("Hello");
}
}

fn main() {
S.hello_method();
}
8 changes: 8 additions & 0 deletions src/test/ui/did_you_mean/issue-40006.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: missing `fn` for method declaration
--> $DIR/issue-40006.rs:14:8
|
14 | pub hello_method(&self) {
| ^ missing `fn`

error: aborting due to previous error