Skip to content

Commit 3e735c6

Browse files
authored
Rollup merge of rust-lang#80850 - m-ou-se:rustc-builtin-macro-name, r=petrochenkov
Allow #[rustc_builtin_macro = "name"] This adds the option of specifying the name of a builtin macro in the `#[rustc_builtin_macro]` attribute: `#[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. This is needed to implement the edition-specific behaviour of the panic macros of RFC 3007. Also removes `SyntaxExtension::is_derive_copy`, as the macro name (e.g. `sym::Copy`) is now tracked and provides that information directly. r? ``@petrochenkov``
2 parents 5acac4c + b293bba commit 3e735c6

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-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::default(kind, edition),
5252
)
5353
};
5454
macro register_bang($($name:ident: $f:expr,)*) {

compiler/rustc_expand/src/base.rs

+6-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,9 @@ impl SyntaxExtension {
785782
}
786783
}
787784

788-
let is_builtin = sess.contains_name(attrs, sym::rustc_builtin_macro);
785+
let builtin_name = sess
786+
.find_by_name(attrs, sym::rustc_builtin_macro)
787+
.map(|a| a.value_str().unwrap_or(name));
789788
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
790789
if const_stability.is_some() {
791790
sess.parse_sess
@@ -803,8 +802,7 @@ impl SyntaxExtension {
803802
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
804803
helper_attrs,
805804
edition,
806-
is_builtin,
807-
is_derive_copy: is_builtin && name == sym::Copy,
805+
builtin_name,
808806
}
809807
}
810808

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
@@ -1447,7 +1447,7 @@ impl<'a> Resolver<'a> {
14471447
}
14481448

14491449
fn is_builtin_macro(&mut self, res: Res) -> bool {
1450-
self.get_macro(res).map_or(false, |ext| ext.is_builtin)
1450+
self.get_macro(res).map_or(false, |ext| ext.builtin_name.is_some())
14511451
}
14521452

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

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)