Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a2d03e7
coretests/num: use ldexp instead of hard-coding a power of 2
RalfJung Jul 21, 2025
bcaa795
removed tidy check on issues files
Kivooeo Jul 19, 2025
58537fb
Fix broken TLS destructors on 32-bit win7
roblabla Jul 20, 2025
b2f8b40
Don't ICE on non-TypeId metadata within TypeId
oli-obk Jul 21, 2025
6df15a1
update SUMMARY.md
makai410 Jul 16, 2025
551cb9f
mbe: Use concrete type for `get_unused_rule`
joshtriplett Jul 22, 2025
f877aa7
coverage: Enlarge empty spans during MIR instrumentation, not codegen
Zalathar May 4, 2025
2832517
Clean code for `rustc_parse/src/lexer`
xizheyin Jul 21, 2025
f5a33e8
Add powerpc64le-unknown-linux-musl to CI rustc targets
Gelbpunkt Jul 22, 2025
25bbaf0
bootstrap: add package.json and package-lock.json to dist tarball
lolbinarycat Jul 22, 2025
a9fd8d0
bootstrap: Move musl-root fallback out of sanity check
Gelbpunkt Jul 22, 2025
f4d0e6b
Rollup merge of #144173 - Kivooeo:tidy_checks, r=jieyouxu
matthiaskrgr Jul 23, 2025
c5dc674
Rollup merge of #144234 - roblabla:fix-win7-tls-dtors, r=ChrisDenton
matthiaskrgr Jul 23, 2025
7599e0c
Rollup merge of #144239 - xizheyin:clean-lexer, r=fee1-dead
matthiaskrgr Jul 23, 2025
8f9f926
Rollup merge of #144247 - RalfJung:ldexp, r=tgross35
matthiaskrgr Jul 23, 2025
fd8e482
Rollup merge of #144256 - oli-obk:type-id-ice, r=RalfJung
matthiaskrgr Jul 23, 2025
bbec721
Rollup merge of #144290 - makai410:summary-ups, r=jieyouxu
matthiaskrgr Jul 23, 2025
e8500bd
Rollup merge of #144292 - joshtriplett:mbe-use-concrete-type-for-get-…
matthiaskrgr Jul 23, 2025
12d5565
Rollup merge of #144298 - Zalathar:empty-span, r=wesleywiser
matthiaskrgr Jul 23, 2025
db66845
Rollup merge of #144311 - Gelbpunkt:ci-rustc-ppc64le-musl, r=Kobzol
matthiaskrgr Jul 23, 2025
9d52046
Rollup merge of #144315 - lolbinarycat:bootstrap-dist-package.json, r…
matthiaskrgr Jul 23, 2025
f8e6bc4
Rollup merge of #144316 - Gelbpunkt:musl-libdir-bootstrap, r=Kobzol
matthiaskrgr Jul 23, 2025
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
Prev Previous commit
Next Next commit
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 <[email protected]>
  • Loading branch information
joshtriplett committed Jul 22, 2025
commit 551cb9f1f4fa315639033e3db39794e10a14b7f7
9 changes: 3 additions & 6 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::any::Any;
use std::default::Default;
use std::iter;
use std::path::Component::Prefix;
Expand Down Expand Up @@ -361,25 +362,21 @@ where
}

/// Represents a thing that maps token trees to Macro Results
pub trait TTMacroExpander {
pub trait TTMacroExpander: Any {
fn expand<'cx>(
&self,
ecx: &'cx mut ExtCtxt<'_>,
span: Span,
input: TokenStream,
) -> MacroExpanderResult<'cx>;

fn get_unused_rule(&self, _rule_i: usize) -> Option<(&Ident, Span)> {
None
}
}

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

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

impl<F> TTMacroExpander for F
impl<F: 'static> TTMacroExpander for F
where
F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>,
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod placeholders;
mod proc_macro_server;
mod stats;

pub use mbe::macro_rules::compile_declarative_macro;
pub use mbe::macro_rules::{MacroRulesMacroExpander, compile_declarative_macro};
pub mod base;
pub mod config;
pub mod expand;
Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,22 @@ pub(super) struct MacroRule {
rhs: mbe::TokenTree,
}

struct MacroRulesMacroExpander {
pub struct MacroRulesMacroExpander {
node_id: NodeId,
name: Ident,
span: Span,
transparency: Transparency,
rules: Vec<MacroRule>,
}

impl MacroRulesMacroExpander {
pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, Span)> {
// If the rhs contains an invocation like `compile_error!`, don't report it as unused.
let rule = &self.rules[rule_i];
if has_compile_error_macro(&rule.rhs) { None } else { Some((&self.name, rule.lhs_span)) }
}
}

impl TTMacroExpander for MacroRulesMacroExpander {
fn expand<'cx>(
&self,
Expand All @@ -154,12 +162,6 @@ impl TTMacroExpander for MacroRulesMacroExpander {
&self.rules,
))
}

fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, Span)> {
// If the rhs contains an invocation like `compile_error!`, don't report it as unused.
let rule = &self.rules[rule_i];
if has_compile_error_macro(&rule.rhs) { None } else { Some((&self.name, rule.lhs_span)) }
}
}

struct DummyExpander(ErrorGuaranteed);
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.

use std::any::Any;
use std::cell::Cell;
use std::mem;
use std::sync::Arc;
Expand All @@ -13,10 +14,10 @@ use rustc_expand::base::{
Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension,
SyntaxExtensionKind,
};
use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{
AstFragment, AstFragmentKind, Invocation, InvocationKind, SupportsMacroExpansion,
};
use rustc_expand::{MacroRulesMacroExpander, compile_declarative_macro};
use rustc_hir::def::{self, DefKind, Namespace, NonMacroAttrKind};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_middle::middle::stability;
Expand Down Expand Up @@ -357,8 +358,12 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
continue;
};
let ext: &dyn Any = ext.as_ref();
let Some(m) = ext.downcast_ref::<MacroRulesMacroExpander>() else {
continue;
};
for arm_i in unused_arms.iter() {
if let Some((ident, rule_span)) = ext.get_unused_rule(arm_i) {
if let Some((ident, rule_span)) = m.get_unused_rule(arm_i) {
self.lint_buffer.buffer_lint(
UNUSED_MACRO_RULES,
node_id,
Expand Down
Loading