Skip to content

Commit cf167c9

Browse files
committed
Only emit lint for local macros
1 parent 886dea2 commit cf167c9

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ crate struct ParserAnyMacro<'a> {
4545
lint_node_id: NodeId,
4646
is_trailing_mac: bool,
4747
arm_span: Span,
48+
/// Whether or not this macro is defined in the current crate
49+
is_local: bool,
4850
}
4951

5052
crate fn annotate_err_with_kind(
@@ -124,6 +126,7 @@ impl<'a> ParserAnyMacro<'a> {
124126
lint_node_id,
125127
arm_span,
126128
is_trailing_mac,
129+
is_local,
127130
} = *self;
128131
let snapshot = &mut parser.clone();
129132
let fragment = match parse_ast_fragment(parser, kind) {
@@ -138,13 +141,15 @@ impl<'a> ParserAnyMacro<'a> {
138141
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
139142
// but `m!()` is allowed in expression positions (cf. issue #34706).
140143
if kind == AstFragmentKind::Expr && parser.token == token::Semi {
141-
parser.sess.buffer_lint_with_diagnostic(
142-
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
143-
parser.token.span,
144-
lint_node_id,
145-
"trailing semicolon in macro used in expression position",
146-
BuiltinLintDiagnostics::TrailingMacro(is_trailing_mac, macro_ident),
147-
);
144+
if is_local {
145+
parser.sess.buffer_lint_with_diagnostic(
146+
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
147+
parser.token.span,
148+
lint_node_id,
149+
"trailing semicolon in macro used in expression position",
150+
BuiltinLintDiagnostics::TrailingMacro(is_trailing_mac, macro_ident),
151+
);
152+
}
148153
parser.bump();
149154
}
150155

@@ -162,6 +167,7 @@ struct MacroRulesMacroExpander {
162167
lhses: Vec<mbe::TokenTree>,
163168
rhses: Vec<mbe::TokenTree>,
164169
valid: bool,
170+
is_local: bool,
165171
}
166172

167173
impl TTMacroExpander for MacroRulesMacroExpander {
@@ -183,6 +189,7 @@ impl TTMacroExpander for MacroRulesMacroExpander {
183189
input,
184190
&self.lhses,
185191
&self.rhses,
192+
self.is_local,
186193
)
187194
}
188195
}
@@ -210,6 +217,7 @@ fn generic_extension<'cx>(
210217
arg: TokenStream,
211218
lhses: &[mbe::TokenTree],
212219
rhses: &[mbe::TokenTree],
220+
is_local: bool,
213221
) -> Box<dyn MacResult + 'cx> {
214222
let sess = &cx.sess.parse_sess;
215223

@@ -311,6 +319,7 @@ fn generic_extension<'cx>(
311319
lint_node_id: cx.current_expansion.lint_node_id,
312320
is_trailing_mac: cx.current_expansion.is_trailing_mac,
313321
arm_span,
322+
is_local,
314323
});
315324
}
316325
Failure(token, msg) => match best_failure {
@@ -544,6 +553,9 @@ pub fn compile_declarative_macro(
544553
lhses,
545554
rhses,
546555
valid,
556+
// Macros defined in the current crate have a real node id,
557+
// whereas macros from an external crate have a dummy id.
558+
is_local: def.id != DUMMY_NODE_ID,
547559
}))
548560
}
549561

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[macro_export]
2+
macro_rules! my_macro {
3+
() => { true; }
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// aux-build:foreign-crate.rs
2+
// check-pass
3+
4+
extern crate foreign_crate;
5+
6+
// Test that we do not lint for a macro in a foreign crate
7+
fn main() {
8+
let _ = foreign_crate::my_macro!();
9+
}

0 commit comments

Comments
 (0)