Skip to content

Commit

Permalink
Use Rc<[Symbol]> instead of Vec<Symbol> to reduce # of allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 11, 2019
1 parent 1dba7cb commit b681433
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 50 deletions.
19 changes: 10 additions & 9 deletions src/librustc/hir/lowering.rs
Expand Up @@ -52,6 +52,7 @@ use crate::util::nodemap::{DefIdMap, NodeMap};
use std::collections::{BTreeSet, BTreeMap};
use std::fmt::Debug;
use std::mem;
use std::rc::Rc;
use smallvec::SmallVec;
use syntax::attr;
use syntax::ast;
Expand Down Expand Up @@ -687,7 +688,7 @@ impl<'a> LoweringContext<'a> {
&self,
reason: CompilerDesugaringKind,
span: Span,
allow_internal_unstable: Vec<Symbol>,
allow_internal_unstable: Option<Rc<[Symbol]>>,
) -> Span {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(source_map::ExpnInfo {
Expand Down Expand Up @@ -974,9 +975,9 @@ impl<'a> LoweringContext<'a> {
let unstable_span = self.mark_span_with_reason(
CompilerDesugaringKind::Async,
span,
vec![
Some(vec![
Symbol::intern("gen_future"),
],
].into()),
);
let gen_future = self.expr_std_path(
unstable_span, &["future", "from_generator"], None, ThinVec::new());
Expand Down Expand Up @@ -1376,7 +1377,7 @@ impl<'a> LoweringContext<'a> {
let exist_ty_span = self.mark_span_with_reason(
CompilerDesugaringKind::ExistentialReturnType,
span,
Vec::new(), // doesn'c actually allow anything unstable
None,
);

let exist_ty_def_index = self
Expand Down Expand Up @@ -3944,9 +3945,9 @@ impl<'a> LoweringContext<'a> {
let unstable_span = this.mark_span_with_reason(
CompilerDesugaringKind::TryBlock,
body.span,
vec![
Some(vec![
Symbol::intern("try_trait"),
],
].into()),
);
let mut block = this.lower_block(body, true).into_inner();
let tail = block.expr.take().map_or_else(
Expand Down Expand Up @@ -4382,7 +4383,7 @@ impl<'a> LoweringContext<'a> {
let desugared_span = self.mark_span_with_reason(
CompilerDesugaringKind::ForLoop,
head_sp,
Vec::new(),
None,
);

let iter = self.str_to_ident("iter");
Expand Down Expand Up @@ -4548,9 +4549,9 @@ impl<'a> LoweringContext<'a> {
let unstable_span = self.mark_span_with_reason(
CompilerDesugaringKind::QuestionMark,
e.span,
vec![
Some(vec![
Symbol::intern("try_trait")
],
].into()),
);

// `Try::into_result(<expr>)`
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_allocator/expand.rs
Expand Up @@ -91,9 +91,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
call_site: item.span, // use the call site of the static
def_site: None,
format: MacroAttribute(Symbol::intern(name)),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("rustc_attrs"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/creader.rs
Expand Up @@ -570,7 +570,7 @@ impl<'a> CrateLoader<'a> {
ProcMacro::Bang { name, client } => {
(name, SyntaxExtension::ProcMacro {
expander: Box::new(BangProcMacro { client }),
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
edition: root.edition,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -425,9 +425,9 @@ impl cstore::CStore {
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
let ext = SyntaxExtension::ProcMacro {
expander: Box::new(BangProcMacro { client }),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("proc_macro_def_site"),
],
].into()),
edition: data.root.edition,
};
return LoadedMacro::ProcMacro(Lrc::new(ext));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_plugin/registry.rs
Expand Up @@ -126,7 +126,7 @@ impl<'a> Registry<'a> {
self.register_syntax_extension(Symbol::intern(name), NormalTT {
expander: Box::new(expander),
def_info: None,
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/base.rs
Expand Up @@ -622,7 +622,7 @@ pub enum SyntaxExtension {
ProcMacro {
expander: Box<dyn ProcMacro + sync::Sync + sync::Send>,
/// Whitelist of unstable features that are treated as stable inside this macro
allow_internal_unstable: Vec<Symbol>,
allow_internal_unstable: Option<Rc<[Symbol]>>,
edition: Edition,
},

Expand All @@ -642,7 +642,7 @@ pub enum SyntaxExtension {
/// directly use `#[unstable]` things.
///
/// Only allows things that require a feature gate in the given whitelist
allow_internal_unstable: Vec<Symbol>,
allow_internal_unstable: Option<Rc<[Symbol]>>,
/// Whether the contents of the macro can use `unsafe`
/// without triggering the `unsafe_code` lint.
allow_internal_unsafe: bool,
Expand All @@ -660,7 +660,7 @@ pub enum SyntaxExtension {
IdentTT {
expander: Box<dyn IdentMacroExpander + sync::Sync + sync::Send>,
span: Option<Span>,
allow_internal_unstable: Vec<Symbol>,
allow_internal_unstable: Option<Rc<[Symbol]>>,
},

/// An attribute-like procedural macro. TokenStream -> TokenStream.
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/derive.rs
Expand Up @@ -58,10 +58,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
call_site: span,
def_site: None,
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("rustc_attrs"),
Symbol::intern("structural_match"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -558,7 +558,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
call_site: attr.span,
def_site: None,
format: MacroAttribute(Symbol::intern(&attr.path.to_string())),
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
edition: ext.edition(),
Expand Down Expand Up @@ -758,7 +758,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let opt_expanded = match *ext {
DeclMacro { ref expander, def_info, edition, .. } => {
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
Vec::new(), false, false, None,
None, false, false, None,
edition) {
dummy_span
} else {
Expand Down Expand Up @@ -919,7 +919,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
call_site: span,
def_site: None,
format: MacroAttribute(pretty_name),
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
edition: ext.edition(),
Expand All @@ -938,12 +938,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
Some(invoc.fragment_kind.expect_from_annotatables(items))
}
BuiltinDerive(func) => {
expn_info.allow_internal_unstable = vec![
expn_info.allow_internal_unstable = Some(vec![
Symbol::intern("rustc_attrs"),
Symbol::intern("derive_clone_copy"),
Symbol::intern("derive_eq"),
Symbol::intern("libstd_sys_internals"), // RustcDeserialize and RustcSerialize
];
].into());
invoc.expansion_data.mark.set_expn_info(expn_info);
let span = span.with_ctxt(self.cx.backtrace());
let mut items = Vec::new();
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/tt/macro_rules.rs
Expand Up @@ -377,21 +377,21 @@ pub fn compile(

if body.legacy {
let allow_internal_unstable = attr::find_by_name(&def.attrs, "allow_internal_unstable")
.map_or(Vec::new(), |attr| attr
.map(|attr| attr
.meta_item_list()
.map(|list| list.iter()
.map(|it| it.name().unwrap_or_else(|| sess.span_diagnostic.span_bug(
it.span, "allow internal unstable expects feature names",
)))
.collect()
.collect::<Vec<Symbol>>().into()
)
.unwrap_or_else(|| {
sess.span_diagnostic.span_warn(
attr.span, "allow_internal_unstable expects list of feature names. In the \
future this will become a hard error. Please use `allow_internal_unstable(\
foo, bar)` to only allow the `foo` and `bar` features",
);
vec![Symbol::intern("allow_internal_unstable_backcompat_hack")]
vec![Symbol::intern("allow_internal_unstable_backcompat_hack")].into()
})
);
let allow_internal_unsafe = attr::contains_name(&def.attrs, "allow_internal_unsafe");
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/std_inject.rs
Expand Up @@ -20,9 +20,9 @@ fn ignored_span(sp: Span) -> Span {
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("std_inject")),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("prelude_import"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/test.rs
Expand Up @@ -285,11 +285,11 @@ fn generate_test_harness(sess: &ParseSess,
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("test_case")),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("main"),
Symbol::intern("test"),
Symbol::intern("rustc_attrs"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax_ext/deriving/mod.rs
Expand Up @@ -138,13 +138,14 @@ fn call_intrinsic(cx: &ExtCtxt<'_>,
-> P<ast::Expr> {
let intrinsic_allowed_via_allow_internal_unstable = cx
.current_expansion.mark.expn_info().unwrap()
.allow_internal_unstable.iter()
.any(|&s| s == "core_intrinsics");
.allow_internal_unstable.map_or(false, |features| features.iter().any(|&s|
s == "core_intrinsics"
));
if intrinsic_allowed_via_allow_internal_unstable {
span = span.with_ctxt(cx.backtrace());
} else { // Avoid instability errors with user defined curstom derives, cc #36316
let mut info = cx.current_expansion.mark.expn_info().unwrap();
info.allow_internal_unstable = vec![Symbol::intern("core_intrinsics")];
info.allow_internal_unstable = Some(vec![Symbol::intern("core_intrinsics")].into());
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(info);
span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax_ext/lib.rs
Expand Up @@ -60,7 +60,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
NormalTT {
expander: Box::new($f as MacroExpanderFn),
def_info: None,
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand Down Expand Up @@ -103,9 +103,9 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
NormalTT {
expander: Box::new(format::expand_format_args),
def_info: None,
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("fmt_internals"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand All @@ -115,9 +115,9 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
NormalTT {
expander: Box::new(format::expand_format_args_nl),
def_info: None,
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("fmt_internals"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/proc_macro_decls.rs
Expand Up @@ -333,10 +333,10 @@ fn mk_decls(
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("proc_macro")),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("rustc_attrs"),
Symbol::intern("proc_macro_internals"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/test.rs
Expand Up @@ -66,10 +66,10 @@ pub fn expand_test_or_bench(
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("test")),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("rustc_attrs"),
Symbol::intern("test"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/test_case.rs
Expand Up @@ -41,10 +41,10 @@ pub fn expand(
call_site: DUMMY_SP,
def_site: None,
format: MacroAttribute(Symbol::intern("test_case")),
allow_internal_unstable: vec![
allow_internal_unstable: Some(vec![
Symbol::intern("test"),
Symbol::intern("rustc_attrs"),
],
].into()),
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax_pos/hygiene.rs
Expand Up @@ -13,6 +13,7 @@ use crate::symbol::{keywords, Symbol};
use serialize::{Encodable, Decodable, Encoder, Decoder};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use std::{fmt, mem};
use std::rc::Rc;

/// A SyntaxContext represents a chain of macro expansions (represented by marks).
#[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -553,7 +554,7 @@ pub struct ExpnInfo {
/// List of #[unstable]/feature-gated features that the macro is allowed to use
/// internally without forcing the whole crate to opt-in
/// to them.
pub allow_internal_unstable: Vec<Symbol>,
pub allow_internal_unstable: Option<Rc<[Symbol]>>,
/// Whether the macro is allowed to use `unsafe` internally
/// even if the user crate has `#![forbid(unsafe_code)]`.
pub allow_internal_unsafe: bool,
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax_pos/lib.rs
Expand Up @@ -389,8 +389,9 @@ impl Span {
match self.ctxt().outer().expn_info() {
Some(info) => info
.allow_internal_unstable
.iter()
.any(|&f| f == feature || f == "allow_internal_unstable_backcompat_hack"),
.map_or(false, |features| features.iter().any(|&f|
f == feature || f == "allow_internal_unstable_backcompat_hack"
)),
None => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass-fulldeps/auxiliary/plugin_args.rs
Expand Up @@ -43,7 +43,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
NormalTT {
expander: Box::new(Expander { args: args, }),
def_info: None,
allow_internal_unstable: Vec::new(),
allow_internal_unstable: None,
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand Down

0 comments on commit b681433

Please sign in to comment.