Skip to content

Commit 4330241

Browse files
committed
syntax_ext: Turn #[global_allocator] into a regular attribute macro
1 parent a93fdfe commit 4330241

File tree

19 files changed

+205
-235
lines changed

19 files changed

+205
-235
lines changed

src/libcore/macros.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,13 @@ mod builtin {
12811281
#[rustc_macro_transparency = "semitransparent"]
12821282
pub macro test_case($item:item) { /* compiler built-in */ }
12831283

1284+
/// Attribute macro applied to a static to register it as a global allocator.
1285+
#[stable(feature = "global_allocator", since = "1.28.0")]
1286+
#[allow_internal_unstable(rustc_attrs)]
1287+
#[rustc_builtin_macro]
1288+
#[rustc_macro_transparency = "semitransparent"]
1289+
pub macro global_allocator($item:item) { /* compiler built-in */ }
1290+
12841291
/// Derive macro generating an impl of the trait `Clone`.
12851292
#[rustc_builtin_macro]
12861293
#[rustc_macro_transparency = "semitransparent"]

src/librustc/middle/dead.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,6 @@ fn has_allow_dead_code_or_lang_attr(
320320
return true;
321321
}
322322

323-
// Don't lint about global allocators
324-
if attr::contains_name(attrs, sym::global_allocator) {
325-
return true;
326-
}
327-
328323
let def_id = tcx.hir().local_def_id(id);
329324
let cg_attrs = tcx.codegen_fn_attrs(def_id);
330325

src/librustc_interface/passes.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ fn configure_and_expand_inner<'a>(
468468
util::ReplaceBodyWithLoop::new(sess).visit_crate(&mut krate);
469469
}
470470

471-
let (has_proc_macro_decls, has_global_allocator) = time(sess, "AST validation", || {
471+
let has_proc_macro_decls = time(sess, "AST validation", || {
472472
ast_validation::check_crate(sess, &krate)
473473
});
474474

@@ -494,19 +494,6 @@ fn configure_and_expand_inner<'a>(
494494
});
495495
}
496496

497-
if has_global_allocator {
498-
// Expand global allocators, which are treated as an in-tree proc macro
499-
time(sess, "creating allocators", || {
500-
syntax_ext::global_allocator::modify(
501-
&sess.parse_sess,
502-
&mut resolver,
503-
&mut krate,
504-
crate_name.to_string(),
505-
sess.diagnostic(),
506-
)
507-
});
508-
}
509-
510497
// Done with macro expansion!
511498

512499
if sess.opts.debugging_opts.input_stats {

src/librustc_metadata/creader.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use std::{cmp, fs};
2525

2626
use syntax::ast;
2727
use syntax::attr;
28-
use syntax::ext::allocator::AllocatorKind;
28+
use syntax::ext::allocator::{global_allocator_spans, AllocatorKind};
2929
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind};
3030
use syntax::symbol::{Symbol, sym};
31-
use syntax::visit;
3231
use syntax::{span_err, span_fatal};
3332
use syntax_pos::{Span, DUMMY_SP};
3433
use log::{debug, info, log_enabled};
@@ -888,7 +887,14 @@ impl<'a> CrateLoader<'a> {
888887
}
889888

890889
fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
891-
let has_global_allocator = has_global_allocator(krate);
890+
let has_global_allocator = match &*global_allocator_spans(krate) {
891+
[span1, span2, ..] => {
892+
self.sess.struct_span_err(*span2, "cannot define multiple global allocators")
893+
.span_note(*span1, "the previous global allocator is defined here").emit();
894+
true
895+
}
896+
spans => !spans.is_empty()
897+
};
892898
self.sess.has_global_allocator.set(has_global_allocator);
893899

894900
// Check to see if we actually need an allocator. This desire comes
@@ -975,25 +981,8 @@ impl<'a> CrateLoader<'a> {
975981
that implements the GlobalAlloc trait.");
976982
}
977983
self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib));
978-
979-
fn has_global_allocator(krate: &ast::Crate) -> bool {
980-
struct Finder(bool);
981-
let mut f = Finder(false);
982-
visit::walk_crate(&mut f, krate);
983-
return f.0;
984-
985-
impl<'ast> visit::Visitor<'ast> for Finder {
986-
fn visit_item(&mut self, i: &'ast ast::Item) {
987-
if attr::contains_name(&i.attrs, sym::global_allocator) {
988-
self.0 = true;
989-
}
990-
visit::walk_item(self, i)
991-
}
992-
}
993-
}
994984
}
995985

996-
997986
fn inject_dependency_if(&self,
998987
krate: CrateNum,
999988
what: &str,

src/librustc_metadata/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22

33
#![feature(box_patterns)]
4+
#![feature(crate_visibility_modifier)]
45
#![feature(drain_filter)]
56
#![feature(in_band_lifetimes)]
67
#![feature(libc)]
78
#![feature(nll)]
89
#![feature(proc_macro_internals)]
910
#![feature(proc_macro_quote)]
1011
#![feature(rustc_diagnostic_macros)]
11-
#![feature(crate_visibility_modifier)]
12-
#![feature(specialization)]
1312
#![feature(rustc_private)]
13+
#![feature(slice_patterns)]
14+
#![feature(specialization)]
1415

1516
#![recursion_limit="256"]
1617

src/librustc_passes/ast_validation.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ impl OuterImplTrait {
5151
struct AstValidator<'a> {
5252
session: &'a Session,
5353
has_proc_macro_decls: bool,
54-
has_global_allocator: bool,
5554

5655
/// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
5756
/// Nested `impl Trait` _is_ allowed in associated type position,
@@ -539,10 +538,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
539538
self.has_proc_macro_decls = true;
540539
}
541540

542-
if attr::contains_name(&item.attrs, sym::global_allocator) {
543-
self.has_global_allocator = true;
544-
}
545-
546541
match item.node {
547542
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
548543
self.invalid_visibility(&item.vis, None);
@@ -848,11 +843,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
848843
}
849844
}
850845

851-
pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
846+
pub fn check_crate(session: &Session, krate: &Crate) -> bool {
852847
let mut validator = AstValidator {
853848
session,
854849
has_proc_macro_decls: false,
855-
has_global_allocator: false,
856850
outer_impl_trait: None,
857851
is_impl_trait_banned: false,
858852
is_assoc_ty_bound_banned: false,
@@ -861,5 +855,5 @@ pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
861855
};
862856
visit::walk_crate(&mut validator, krate);
863857

864-
(validator.has_proc_macro_decls, validator.has_global_allocator)
858+
validator.has_proc_macro_decls
865859
}

src/libsyntax/ext/allocator.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::{ast, attr, visit};
2+
use crate::symbol::{sym, Symbol};
3+
use syntax_pos::Span;
4+
15
#[derive(Clone, Copy)]
26
pub enum AllocatorKind {
37
Global,
@@ -51,3 +55,21 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
5155
output: AllocatorTy::ResultPtr,
5256
},
5357
];
58+
59+
pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
60+
struct Finder { name: Symbol, spans: Vec<Span> }
61+
impl<'ast> visit::Visitor<'ast> for Finder {
62+
fn visit_item(&mut self, item: &'ast ast::Item) {
63+
if item.ident.name == self.name &&
64+
attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol) {
65+
self.spans.push(item.span);
66+
}
67+
visit::walk_item(self, item)
68+
}
69+
}
70+
71+
let name = Symbol::intern(&AllocatorKind::Global.fn_name("alloc"));
72+
let mut f = Finder { name, spans: Vec::new() };
73+
visit::walk_crate(&mut f, krate);
74+
f.spans
75+
}

src/libsyntax/feature_gate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11171117
"the `#[rustc_const_unstable]` attribute \
11181118
is an internal feature",
11191119
cfg_fn!(rustc_const_unstable))),
1120-
(sym::global_allocator, Normal, template!(Word), Ungated),
11211120
(sym::default_lib_allocator, Whitelisted, template!(Word), Gated(Stability::Unstable,
11221121
sym::allocator_internals,
11231122
"the `#[default_lib_allocator]` \

0 commit comments

Comments
 (0)