Skip to content

Commit 0f54963

Browse files
Rollup merge of rust-lang#144292 - joshtriplett:mbe-use-concrete-type-for-get-unused-rule, r=petrochenkov
mbe: Use concrete type for `get_unused_rule` Rather than adding `get_unused_rule` to the `TTMacroExpander` trait, put it on the concrete `MacroRulesMacroExpander`, and downcast to that type via `Any` in order to call it. Suggested-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com> r? ``````@petrochenkov``````
2 parents d53e34b + 551cb9f commit 0f54963

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::any::Any;
12
use std::default::Default;
23
use std::iter;
34
use std::path::Component::Prefix;
@@ -361,25 +362,21 @@ where
361362
}
362363

363364
/// Represents a thing that maps token trees to Macro Results
364-
pub trait TTMacroExpander {
365+
pub trait TTMacroExpander: Any {
365366
fn expand<'cx>(
366367
&self,
367368
ecx: &'cx mut ExtCtxt<'_>,
368369
span: Span,
369370
input: TokenStream,
370371
) -> MacroExpanderResult<'cx>;
371-
372-
fn get_unused_rule(&self, _rule_i: usize) -> Option<(&Ident, Span)> {
373-
None
374-
}
375372
}
376373

377374
pub type MacroExpanderResult<'cx> = ExpandResult<Box<dyn MacResult + 'cx>, ()>;
378375

379376
pub type MacroExpanderFn =
380377
for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>;
381378

382-
impl<F> TTMacroExpander for F
379+
impl<F: 'static> TTMacroExpander for F
383380
where
384381
F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>,
385382
{

compiler/rustc_expand/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod placeholders;
2222
mod proc_macro_server;
2323
mod stats;
2424

25-
pub use mbe::macro_rules::compile_declarative_macro;
25+
pub use mbe::macro_rules::{MacroRulesMacroExpander, compile_declarative_macro};
2626
pub mod base;
2727
pub mod config;
2828
pub mod expand;

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,22 @@ pub(super) struct MacroRule {
128128
rhs: mbe::TokenTree,
129129
}
130130

131-
struct MacroRulesMacroExpander {
131+
pub struct MacroRulesMacroExpander {
132132
node_id: NodeId,
133133
name: Ident,
134134
span: Span,
135135
transparency: Transparency,
136136
rules: Vec<MacroRule>,
137137
}
138138

139+
impl MacroRulesMacroExpander {
140+
pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, Span)> {
141+
// If the rhs contains an invocation like `compile_error!`, don't report it as unused.
142+
let rule = &self.rules[rule_i];
143+
if has_compile_error_macro(&rule.rhs) { None } else { Some((&self.name, rule.lhs_span)) }
144+
}
145+
}
146+
139147
impl TTMacroExpander for MacroRulesMacroExpander {
140148
fn expand<'cx>(
141149
&self,
@@ -154,12 +162,6 @@ impl TTMacroExpander for MacroRulesMacroExpander {
154162
&self.rules,
155163
))
156164
}
157-
158-
fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, Span)> {
159-
// If the rhs contains an invocation like `compile_error!`, don't report it as unused.
160-
let rule = &self.rules[rule_i];
161-
if has_compile_error_macro(&rule.rhs) { None } else { Some((&self.name, rule.lhs_span)) }
162-
}
163165
}
164166

165167
struct DummyExpander(ErrorGuaranteed);

compiler/rustc_resolve/src/macros.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4+
use std::any::Any;
45
use std::cell::Cell;
56
use std::mem;
67
use std::sync::Arc;
@@ -13,10 +14,10 @@ use rustc_expand::base::{
1314
Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension,
1415
SyntaxExtensionKind,
1516
};
16-
use rustc_expand::compile_declarative_macro;
1717
use rustc_expand::expand::{
1818
AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion,
1919
};
20+
use rustc_expand::{MacroRulesMacroExpander, compile_declarative_macro};
2021
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
2122
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
2223
use rustc_middle::middle::stability;
@@ -357,8 +358,12 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
357358
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
358359
continue;
359360
};
361+
let ext: &dyn Any = ext.as_ref();
362+
let Some(m) = ext.downcast_ref::<MacroRulesMacroExpander>() else {
363+
continue;
364+
};
360365
for arm_i in unused_arms.iter() {
361-
if let Some((ident, rule_span)) = ext.get_unused_rule(arm_i) {
366+
if let Some((ident, rule_span)) = m.get_unused_rule(arm_i) {
362367
self.lint_buffer.buffer_lint(
363368
UNUSED_MACRO_RULES,
364369
node_id,

0 commit comments

Comments
 (0)