Skip to content

Commit

Permalink
Allow #[rustc_builtin_macro = "name"].
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-ou-se committed Jan 9, 2021
1 parent 1f9dc9a commit d651fa7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/lib.rs
Expand Up @@ -48,7 +48,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand, edition: Editi
let mut register = |name, kind| {
resolver.register_builtin_macro(
Ident::with_dummy_span(name),
SyntaxExtension { is_builtin: true, ..SyntaxExtension::default(kind, edition) },
SyntaxExtension { builtin_name: Some(name), ..SyntaxExtension::default(kind, edition) },
)
};
macro register_bang($($name:ident: $f:expr,)*) {
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_expand/src/base.rs
Expand Up @@ -728,9 +728,7 @@ pub struct SyntaxExtension {
pub edition: Edition,
/// Built-in macros have a couple of special properties like availability
/// in `#[no_implicit_prelude]` modules, so we have to keep this flag.
pub is_builtin: bool,
/// We have to identify macros providing a `Copy` impl early for compatibility reasons.
pub is_derive_copy: bool,
pub builtin_name: Option<Symbol>,
}

impl SyntaxExtension {
Expand Down Expand Up @@ -758,8 +756,7 @@ impl SyntaxExtension {
deprecation: None,
helper_attrs: Vec::new(),
edition,
is_builtin: false,
is_derive_copy: false,
builtin_name: None,
kind,
}
}
Expand All @@ -785,7 +782,7 @@ impl SyntaxExtension {
}
}

let is_builtin = sess.contains_name(attrs, sym::rustc_builtin_macro);
let builtin_name = sess.find_by_name(attrs, sym::rustc_builtin_macro).map(|a| a.value_str().unwrap_or(name));
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
if const_stability.is_some() {
sess.parse_sess
Expand All @@ -803,8 +800,7 @@ impl SyntaxExtension {
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
helper_attrs,
edition,
is_builtin,
is_derive_copy: is_builtin && name == sym::Copy,
builtin_name,
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Expand Up @@ -442,7 +442,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// Internal attributes, Macro related:
// ==========================================================================

rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word), IMPL_DETAIL),
rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word, NameValueStr: "name"), IMPL_DETAIL),
rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
rustc_attr!(
rustc_macro_transparency, AssumedUsed,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -1443,7 +1443,7 @@ impl<'a> Resolver<'a> {
}

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

fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
Expand Down Expand Up @@ -2010,7 +2010,7 @@ impl<'a> Resolver<'a> {
// The macro is a proc macro derive
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
let ext = self.get_macro_by_def_id(def_id);
if !ext.is_builtin
if ext.builtin_name.is_none()
&& ext.macro_kind() == MacroKind::Derive
&& parent.expansion.outer_expn_is_descendant_of(span.ctxt())
{
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/macros.rs
Expand Up @@ -285,7 +285,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
helper_attrs.extend(
ext.helper_attrs.iter().map(|name| Ident::new(*name, span)),
);
if ext.is_derive_copy {
if ext.builtin_name == Some(sym::Copy) {
self.containers_deriving_copy.insert(invoc_id);
}
ext
Expand Down Expand Up @@ -1089,9 +1089,9 @@ impl<'a> Resolver<'a> {
edition,
);

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

0 comments on commit d651fa7

Please sign in to comment.