Skip to content

Commit d651fa7

Browse files
committed
Allow #[rustc_builtin_macro = "name"].
This makes it possible to have both std::panic and core::panic as a builtin macro, by using different builtin macro names for each. Also removes SyntaxExtension::is_derive_copy, as the macro name (e.g. sym::Copy) is now tracked and provides that information directly.
1 parent 1f9dc9a commit d651fa7

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand, edition: Editi
4848
let mut register = |name, kind| {
4949
resolver.register_builtin_macro(
5050
Ident::with_dummy_span(name),
51-
SyntaxExtension { is_builtin: true, ..SyntaxExtension::default(kind, edition) },
51+
SyntaxExtension { builtin_name: Some(name), ..SyntaxExtension::default(kind, edition) },
5252
)
5353
};
5454
macro register_bang($($name:ident: $f:expr,)*) {

compiler/rustc_expand/src/base.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,7 @@ pub struct SyntaxExtension {
728728
pub edition: Edition,
729729
/// Built-in macros have a couple of special properties like availability
730730
/// in `#[no_implicit_prelude]` modules, so we have to keep this flag.
731-
pub is_builtin: bool,
732-
/// We have to identify macros providing a `Copy` impl early for compatibility reasons.
733-
pub is_derive_copy: bool,
731+
pub builtin_name: Option<Symbol>,
734732
}
735733

736734
impl SyntaxExtension {
@@ -758,8 +756,7 @@ impl SyntaxExtension {
758756
deprecation: None,
759757
helper_attrs: Vec::new(),
760758
edition,
761-
is_builtin: false,
762-
is_derive_copy: false,
759+
builtin_name: None,
763760
kind,
764761
}
765762
}
@@ -785,7 +782,7 @@ impl SyntaxExtension {
785782
}
786783
}
787784

788-
let is_builtin = sess.contains_name(attrs, sym::rustc_builtin_macro);
785+
let builtin_name = sess.find_by_name(attrs, sym::rustc_builtin_macro).map(|a| a.value_str().unwrap_or(name));
789786
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
790787
if const_stability.is_some() {
791788
sess.parse_sess
@@ -803,8 +800,7 @@ impl SyntaxExtension {
803800
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
804801
helper_attrs,
805802
edition,
806-
is_builtin,
807-
is_derive_copy: is_builtin && name == sym::Copy,
803+
builtin_name,
808804
}
809805
}
810806

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
442442
// Internal attributes, Macro related:
443443
// ==========================================================================
444444

445-
rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word), IMPL_DETAIL),
445+
rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word, NameValueStr: "name"), IMPL_DETAIL),
446446
rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
447447
rustc_attr!(
448448
rustc_macro_transparency, AssumedUsed,

compiler/rustc_resolve/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ impl<'a> Resolver<'a> {
14431443
}
14441444

14451445
fn is_builtin_macro(&mut self, res: Res) -> bool {
1446-
self.get_macro(res).map_or(false, |ext| ext.is_builtin)
1446+
self.get_macro(res).map_or(false, |ext| ext.builtin_name.is_some())
14471447
}
14481448

14491449
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
@@ -2010,7 +2010,7 @@ impl<'a> Resolver<'a> {
20102010
// The macro is a proc macro derive
20112011
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
20122012
let ext = self.get_macro_by_def_id(def_id);
2013-
if !ext.is_builtin
2013+
if ext.builtin_name.is_none()
20142014
&& ext.macro_kind() == MacroKind::Derive
20152015
&& parent.expansion.outer_expn_is_descendant_of(span.ctxt())
20162016
{

compiler/rustc_resolve/src/macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
285285
helper_attrs.extend(
286286
ext.helper_attrs.iter().map(|name| Ident::new(*name, span)),
287287
);
288-
if ext.is_derive_copy {
288+
if ext.builtin_name == Some(sym::Copy) {
289289
self.containers_deriving_copy.insert(invoc_id);
290290
}
291291
ext
@@ -1089,9 +1089,9 @@ impl<'a> Resolver<'a> {
10891089
edition,
10901090
);
10911091

1092-
if result.is_builtin {
1092+
if let Some(builtin_name) = result.builtin_name {
10931093
// The macro was marked with `#[rustc_builtin_macro]`.
1094-
if let Some(builtin_macro) = self.builtin_macros.get_mut(&item.ident.name) {
1094+
if let Some(builtin_macro) = self.builtin_macros.get_mut(&builtin_name) {
10951095
// The macro is a built-in, replace its expander function
10961096
// while still taking everything else from the source code.
10971097
// If we already loaded this builtin macro, give a better error message than 'no such builtin macro'.

0 commit comments

Comments
 (0)