Skip to content

Commit b681433

Browse files
committed
Use Rc<[Symbol]> instead of Vec<Symbol> to reduce # of allocs
1 parent 1dba7cb commit b681433

File tree

19 files changed

+54
-50
lines changed

19 files changed

+54
-50
lines changed

src/librustc/hir/lowering.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use crate::util::nodemap::{DefIdMap, NodeMap};
5252
use std::collections::{BTreeSet, BTreeMap};
5353
use std::fmt::Debug;
5454
use std::mem;
55+
use std::rc::Rc;
5556
use smallvec::SmallVec;
5657
use syntax::attr;
5758
use syntax::ast;
@@ -687,7 +688,7 @@ impl<'a> LoweringContext<'a> {
687688
&self,
688689
reason: CompilerDesugaringKind,
689690
span: Span,
690-
allow_internal_unstable: Vec<Symbol>,
691+
allow_internal_unstable: Option<Rc<[Symbol]>>,
691692
) -> Span {
692693
let mark = Mark::fresh(Mark::root());
693694
mark.set_expn_info(source_map::ExpnInfo {
@@ -974,9 +975,9 @@ impl<'a> LoweringContext<'a> {
974975
let unstable_span = self.mark_span_with_reason(
975976
CompilerDesugaringKind::Async,
976977
span,
977-
vec![
978+
Some(vec![
978979
Symbol::intern("gen_future"),
979-
],
980+
].into()),
980981
);
981982
let gen_future = self.expr_std_path(
982983
unstable_span, &["future", "from_generator"], None, ThinVec::new());
@@ -1376,7 +1377,7 @@ impl<'a> LoweringContext<'a> {
13761377
let exist_ty_span = self.mark_span_with_reason(
13771378
CompilerDesugaringKind::ExistentialReturnType,
13781379
span,
1379-
Vec::new(), // doesn'c actually allow anything unstable
1380+
None,
13801381
);
13811382

13821383
let exist_ty_def_index = self
@@ -3944,9 +3945,9 @@ impl<'a> LoweringContext<'a> {
39443945
let unstable_span = this.mark_span_with_reason(
39453946
CompilerDesugaringKind::TryBlock,
39463947
body.span,
3947-
vec![
3948+
Some(vec![
39483949
Symbol::intern("try_trait"),
3949-
],
3950+
].into()),
39503951
);
39513952
let mut block = this.lower_block(body, true).into_inner();
39523953
let tail = block.expr.take().map_or_else(
@@ -4382,7 +4383,7 @@ impl<'a> LoweringContext<'a> {
43824383
let desugared_span = self.mark_span_with_reason(
43834384
CompilerDesugaringKind::ForLoop,
43844385
head_sp,
4385-
Vec::new(),
4386+
None,
43864387
);
43874388

43884389
let iter = self.str_to_ident("iter");
@@ -4548,9 +4549,9 @@ impl<'a> LoweringContext<'a> {
45484549
let unstable_span = self.mark_span_with_reason(
45494550
CompilerDesugaringKind::QuestionMark,
45504551
e.span,
4551-
vec![
4552+
Some(vec![
45524553
Symbol::intern("try_trait")
4553-
],
4554+
].into()),
45544555
);
45554556

45564557
// `Try::into_result(<expr>)`

src/librustc_allocator/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
9191
call_site: item.span, // use the call site of the static
9292
def_site: None,
9393
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: vec![
94+
allow_internal_unstable: Some(vec![
9595
Symbol::intern("rustc_attrs"),
96-
],
96+
].into()),
9797
allow_internal_unsafe: false,
9898
local_inner_macros: false,
9999
edition: hygiene::default_edition(),

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl<'a> CrateLoader<'a> {
570570
ProcMacro::Bang { name, client } => {
571571
(name, SyntaxExtension::ProcMacro {
572572
expander: Box::new(BangProcMacro { client }),
573-
allow_internal_unstable: Vec::new(),
573+
allow_internal_unstable: None,
574574
edition: root.edition,
575575
})
576576
}

src/librustc_metadata/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ impl cstore::CStore {
425425
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
426426
let ext = SyntaxExtension::ProcMacro {
427427
expander: Box::new(BangProcMacro { client }),
428-
allow_internal_unstable: vec![
428+
allow_internal_unstable: Some(vec![
429429
Symbol::intern("proc_macro_def_site"),
430-
],
430+
].into()),
431431
edition: data.root.edition,
432432
};
433433
return LoadedMacro::ProcMacro(Lrc::new(ext));

src/librustc_plugin/registry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a> Registry<'a> {
126126
self.register_syntax_extension(Symbol::intern(name), NormalTT {
127127
expander: Box::new(expander),
128128
def_info: None,
129-
allow_internal_unstable: Vec::new(),
129+
allow_internal_unstable: None,
130130
allow_internal_unsafe: false,
131131
local_inner_macros: false,
132132
unstable_feature: None,

src/libsyntax/ext/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ pub enum SyntaxExtension {
622622
ProcMacro {
623623
expander: Box<dyn ProcMacro + sync::Sync + sync::Send>,
624624
/// Whitelist of unstable features that are treated as stable inside this macro
625-
allow_internal_unstable: Vec<Symbol>,
625+
allow_internal_unstable: Option<Rc<[Symbol]>>,
626626
edition: Edition,
627627
},
628628

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

666666
/// An attribute-like procedural macro. TokenStream -> TokenStream.

src/libsyntax/ext/derive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
5858
call_site: span,
5959
def_site: None,
6060
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
61-
allow_internal_unstable: vec![
61+
allow_internal_unstable: Some(vec![
6262
Symbol::intern("rustc_attrs"),
6363
Symbol::intern("structural_match"),
64-
],
64+
].into()),
6565
allow_internal_unsafe: false,
6666
local_inner_macros: false,
6767
edition: hygiene::default_edition(),

src/libsyntax/ext/expand.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
558558
call_site: attr.span,
559559
def_site: None,
560560
format: MacroAttribute(Symbol::intern(&attr.path.to_string())),
561-
allow_internal_unstable: Vec::new(),
561+
allow_internal_unstable: None,
562562
allow_internal_unsafe: false,
563563
local_inner_macros: false,
564564
edition: ext.edition(),
@@ -758,7 +758,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
758758
let opt_expanded = match *ext {
759759
DeclMacro { ref expander, def_info, edition, .. } => {
760760
if let Err(dummy_span) = validate_and_set_expn_info(self, def_info.map(|(_, s)| s),
761-
Vec::new(), false, false, None,
761+
None, false, false, None,
762762
edition) {
763763
dummy_span
764764
} else {
@@ -919,7 +919,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
919919
call_site: span,
920920
def_site: None,
921921
format: MacroAttribute(pretty_name),
922-
allow_internal_unstable: Vec::new(),
922+
allow_internal_unstable: None,
923923
allow_internal_unsafe: false,
924924
local_inner_macros: false,
925925
edition: ext.edition(),
@@ -938,12 +938,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
938938
Some(invoc.fragment_kind.expect_from_annotatables(items))
939939
}
940940
BuiltinDerive(func) => {
941-
expn_info.allow_internal_unstable = vec![
941+
expn_info.allow_internal_unstable = Some(vec![
942942
Symbol::intern("rustc_attrs"),
943943
Symbol::intern("derive_clone_copy"),
944944
Symbol::intern("derive_eq"),
945945
Symbol::intern("libstd_sys_internals"), // RustcDeserialize and RustcSerialize
946-
];
946+
].into());
947947
invoc.expansion_data.mark.set_expn_info(expn_info);
948948
let span = span.with_ctxt(self.cx.backtrace());
949949
let mut items = Vec::new();

src/libsyntax/ext/tt/macro_rules.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,21 @@ pub fn compile(
377377

378378
if body.legacy {
379379
let allow_internal_unstable = attr::find_by_name(&def.attrs, "allow_internal_unstable")
380-
.map_or(Vec::new(), |attr| attr
380+
.map(|attr| attr
381381
.meta_item_list()
382382
.map(|list| list.iter()
383383
.map(|it| it.name().unwrap_or_else(|| sess.span_diagnostic.span_bug(
384384
it.span, "allow internal unstable expects feature names",
385385
)))
386-
.collect()
386+
.collect::<Vec<Symbol>>().into()
387387
)
388388
.unwrap_or_else(|| {
389389
sess.span_diagnostic.span_warn(
390390
attr.span, "allow_internal_unstable expects list of feature names. In the \
391391
future this will become a hard error. Please use `allow_internal_unstable(\
392392
foo, bar)` to only allow the `foo` and `bar` features",
393393
);
394-
vec![Symbol::intern("allow_internal_unstable_backcompat_hack")]
394+
vec![Symbol::intern("allow_internal_unstable_backcompat_hack")].into()
395395
})
396396
);
397397
let allow_internal_unsafe = attr::contains_name(&def.attrs, "allow_internal_unsafe");

src/libsyntax/std_inject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fn ignored_span(sp: Span) -> Span {
2020
call_site: DUMMY_SP,
2121
def_site: None,
2222
format: MacroAttribute(Symbol::intern("std_inject")),
23-
allow_internal_unstable: vec![
23+
allow_internal_unstable: Some(vec![
2424
Symbol::intern("prelude_import"),
25-
],
25+
].into()),
2626
allow_internal_unsafe: false,
2727
local_inner_macros: false,
2828
edition: hygiene::default_edition(),

src/libsyntax/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ fn generate_test_harness(sess: &ParseSess,
285285
call_site: DUMMY_SP,
286286
def_site: None,
287287
format: MacroAttribute(Symbol::intern("test_case")),
288-
allow_internal_unstable: vec![
288+
allow_internal_unstable: Some(vec![
289289
Symbol::intern("main"),
290290
Symbol::intern("test"),
291291
Symbol::intern("rustc_attrs"),
292-
],
292+
].into()),
293293
allow_internal_unsafe: false,
294294
local_inner_macros: false,
295295
edition: hygiene::default_edition(),

src/libsyntax_ext/deriving/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ fn call_intrinsic(cx: &ExtCtxt<'_>,
138138
-> P<ast::Expr> {
139139
let intrinsic_allowed_via_allow_internal_unstable = cx
140140
.current_expansion.mark.expn_info().unwrap()
141-
.allow_internal_unstable.iter()
142-
.any(|&s| s == "core_intrinsics");
141+
.allow_internal_unstable.map_or(false, |features| features.iter().any(|&s|
142+
s == "core_intrinsics"
143+
));
143144
if intrinsic_allowed_via_allow_internal_unstable {
144145
span = span.with_ctxt(cx.backtrace());
145146
} else { // Avoid instability errors with user defined curstom derives, cc #36316
146147
let mut info = cx.current_expansion.mark.expn_info().unwrap();
147-
info.allow_internal_unstable = vec![Symbol::intern("core_intrinsics")];
148+
info.allow_internal_unstable = Some(vec![Symbol::intern("core_intrinsics")].into());
148149
let mark = Mark::fresh(Mark::root());
149150
mark.set_expn_info(info);
150151
span = span.with_ctxt(SyntaxContext::empty().apply_mark(mark));

src/libsyntax_ext/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
6060
NormalTT {
6161
expander: Box::new($f as MacroExpanderFn),
6262
def_info: None,
63-
allow_internal_unstable: Vec::new(),
63+
allow_internal_unstable: None,
6464
allow_internal_unsafe: false,
6565
local_inner_macros: false,
6666
unstable_feature: None,
@@ -103,9 +103,9 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
103103
NormalTT {
104104
expander: Box::new(format::expand_format_args),
105105
def_info: None,
106-
allow_internal_unstable: vec![
106+
allow_internal_unstable: Some(vec![
107107
Symbol::intern("fmt_internals"),
108-
],
108+
].into()),
109109
allow_internal_unsafe: false,
110110
local_inner_macros: false,
111111
unstable_feature: None,
@@ -115,9 +115,9 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
115115
NormalTT {
116116
expander: Box::new(format::expand_format_args_nl),
117117
def_info: None,
118-
allow_internal_unstable: vec![
118+
allow_internal_unstable: Some(vec![
119119
Symbol::intern("fmt_internals"),
120-
],
120+
].into()),
121121
allow_internal_unsafe: false,
122122
local_inner_macros: false,
123123
unstable_feature: None,

src/libsyntax_ext/proc_macro_decls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,10 @@ fn mk_decls(
333333
call_site: DUMMY_SP,
334334
def_site: None,
335335
format: MacroAttribute(Symbol::intern("proc_macro")),
336-
allow_internal_unstable: vec![
336+
allow_internal_unstable: Some(vec![
337337
Symbol::intern("rustc_attrs"),
338338
Symbol::intern("proc_macro_internals"),
339-
],
339+
].into()),
340340
allow_internal_unsafe: false,
341341
local_inner_macros: false,
342342
edition: hygiene::default_edition(),

src/libsyntax_ext/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ pub fn expand_test_or_bench(
6666
call_site: DUMMY_SP,
6767
def_site: None,
6868
format: MacroAttribute(Symbol::intern("test")),
69-
allow_internal_unstable: vec![
69+
allow_internal_unstable: Some(vec![
7070
Symbol::intern("rustc_attrs"),
7171
Symbol::intern("test"),
72-
],
72+
].into()),
7373
allow_internal_unsafe: false,
7474
local_inner_macros: false,
7575
edition: hygiene::default_edition(),

src/libsyntax_ext/test_case.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ pub fn expand(
4141
call_site: DUMMY_SP,
4242
def_site: None,
4343
format: MacroAttribute(Symbol::intern("test_case")),
44-
allow_internal_unstable: vec![
44+
allow_internal_unstable: Some(vec![
4545
Symbol::intern("test"),
4646
Symbol::intern("rustc_attrs"),
47-
],
47+
].into()),
4848
allow_internal_unsafe: false,
4949
local_inner_macros: false,
5050
edition: hygiene::default_edition(),

src/libsyntax_pos/hygiene.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::symbol::{keywords, Symbol};
1313
use serialize::{Encodable, Decodable, Encoder, Decoder};
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use std::{fmt, mem};
16+
use std::rc::Rc;
1617

1718
/// A SyntaxContext represents a chain of macro expansions (represented by marks).
1819
#[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
@@ -553,7 +554,7 @@ pub struct ExpnInfo {
553554
/// List of #[unstable]/feature-gated features that the macro is allowed to use
554555
/// internally without forcing the whole crate to opt-in
555556
/// to them.
556-
pub allow_internal_unstable: Vec<Symbol>,
557+
pub allow_internal_unstable: Option<Rc<[Symbol]>>,
557558
/// Whether the macro is allowed to use `unsafe` internally
558559
/// even if the user crate has `#![forbid(unsafe_code)]`.
559560
pub allow_internal_unsafe: bool,

src/libsyntax_pos/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,9 @@ impl Span {
389389
match self.ctxt().outer().expn_info() {
390390
Some(info) => info
391391
.allow_internal_unstable
392-
.iter()
393-
.any(|&f| f == feature || f == "allow_internal_unstable_backcompat_hack"),
392+
.map_or(false, |features| features.iter().any(|&f|
393+
f == feature || f == "allow_internal_unstable_backcompat_hack"
394+
)),
394395
None => false,
395396
}
396397
}

src/test/run-pass-fulldeps/auxiliary/plugin_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
4343
NormalTT {
4444
expander: Box::new(Expander { args: args, }),
4545
def_info: None,
46-
allow_internal_unstable: Vec::new(),
46+
allow_internal_unstable: None,
4747
allow_internal_unsafe: false,
4848
local_inner_macros: false,
4949
unstable_feature: None,

0 commit comments

Comments
 (0)