Skip to content

Commit 66a12f5

Browse files
committed
Custom help message for people trying to make macro public
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.
1 parent 176ee34 commit 66a12f5

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 15 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};
@@ -4612,10 +4612,20 @@ impl<'a> Parser<'a> {
46124612
fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) {
46134613
match visa {
46144614
Public => {
4615-
self.diagnostic().struct_span_err(span, "can't qualify macro invocation with `pub`")
4616-
.fileline_help(span, "try adjusting the macro to put `pub` inside \
4617-
the invocation")
4618-
.emit();
4615+
let is_macro_rules :bool = match self.token {
4616+
token::Ident(sid, _) => sid.name == intern("macro_rules"),
4617+
_ => false,
4618+
};
4619+
if is_macro_rules {
4620+
self.diagnostic().struct_span_err(span, "can't qualify macro_rules invocation with `pub`")
4621+
.fileline_help(span, "did you mean #[macro_export]?")
4622+
.emit();
4623+
} else {
4624+
self.diagnostic().struct_span_err(span, "can't qualify macro invocation with `pub`")
4625+
.fileline_help(span, "try adjusting the macro to put `pub` \
4626+
inside the invocation")
4627+
.emit();
4628+
}
46194629
}
46204630
Inherited => (),
46214631
}
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)