Skip to content

Commit f73c0a8

Browse files
committed
Auto merge of #30598 - est31:macro_export_help_note, r=Manishearth
The current help message is too much about "normal" macros to be used as general message. Keep it for normal macros, and add custom help and error messages for macro definitions.
2 parents 19a351c + 40a9481 commit f73c0a8

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use parse::classify;
6767
use parse::common::{SeqSep, seq_sep_none, seq_sep_trailing_allowed};
6868
use parse::lexer::{Reader, TokenAndSpan};
6969
use parse::obsolete::{ParserObsoleteMethods, ObsoleteSyntax};
70-
use parse::token::{self, MatchNt, SubstNt, SpecialVarNt, InternedString};
70+
use parse::token::{self, intern, MatchNt, SubstNt, SpecialVarNt, InternedString};
7171
use parse::token::{keywords, special_idents, SpecialMacroVar};
7272
use parse::{new_sub_parser_from_file, ParseSess};
7373
use util::parser::{AssocOp, Fixity};
@@ -4622,10 +4622,22 @@ impl<'a> Parser<'a> {
46224622
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
46234623
match visa {
46244624
Public => {
4625-
self.diagnostic().struct_span_err(span, "can't qualify macro invocation with `pub`")
4626-
.fileline_help(span, "try adjusting the macro to put `pub` inside \
4627-
the invocation")
4628-
.emit();
4625+
let is_macro_rules: bool = match self.token {
4626+
token::Ident(sid, _) => sid.name == intern("macro_rules"),
4627+
_ => false,
4628+
};
4629+
if is_macro_rules {
4630+
self.diagnostic().struct_span_err(span, "can't qualify macro_rules \
4631+
invocation with `pub`")
4632+
.fileline_help(span, "did you mean #[macro_export]?")
4633+
.emit();
4634+
} else {
4635+
self.diagnostic().struct_span_err(span, "can't qualify macro \
4636+
invocation with `pub`")
4637+
.fileline_help(span, "try adjusting the macro to put `pub` \
4638+
inside the invocation")
4639+
.emit();
4640+
}
46294641
}
46304642
Inherited => (),
46314643
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015 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+
#[macro_use] mod bleh {
12+
pub macro_rules! foo { //~ ERROR can't qualify macro_rules invocation with `pub`
13+
//~^ HELP did you mean #[macro_export]?
14+
($n:ident) => (
15+
fn $n () -> i32 {
16+
1
17+
}
18+
)
19+
}
20+
21+
}
22+
23+
foo!(meh);
24+
25+
fn main() {
26+
println!("{}", meh());
27+
}

0 commit comments

Comments
 (0)