Skip to content

Commit cd48f0d

Browse files
committed
Allow tools to preintern symbols like rustc does
1 parent a1fcc76 commit cd48f0d

File tree

16 files changed

+70
-43
lines changed

16 files changed

+70
-43
lines changed

src/librustc/session/config.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,11 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18521852
}
18531853

18541854
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
1855-
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
1856-
syntax::with_globals(move || {
1855+
pub fn parse_cfgspecs(
1856+
driver_symbols: &[&str],
1857+
cfgspecs: Vec<String>,
1858+
) -> FxHashSet<(String, Option<String>)> {
1859+
syntax::with_globals(driver_symbols, move || {
18571860
let cfg = cfgspecs.into_iter().map(|s| {
18581861
let sess = parse::ParseSess::new(FilePathMapping::empty());
18591862
let filename = FileName::cfg_spec_source_code(&s);
@@ -1918,6 +1921,7 @@ pub fn get_cmd_lint_options(matches: &getopts::Matches,
19181921
}
19191922

19201923
pub fn build_session_options_and_crate_config(
1924+
driver_symbols: &[&str],
19211925
matches: &getopts::Matches,
19221926
) -> (Options, FxHashSet<(String, Option<String>)>) {
19231927
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
@@ -2296,7 +2300,7 @@ pub fn build_session_options_and_crate_config(
22962300
})
22972301
.collect();
22982302

2299-
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
2303+
let cfg = parse_cfgspecs(driver_symbols, matches.opt_strs("cfg"));
23002304
let test = matches.opt_present("test");
23012305

23022306
let is_unstable_enabled = nightly_options::is_unstable_enabled(matches);

src/librustc_driver/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl Callbacks for DefaultCallbacks {}
122122
// The FileLoader provides a way to load files from sources other than the file system.
123123
pub fn run_compiler(
124124
args: &[String],
125+
driver_symbols: &'static [&'static str],
125126
callbacks: &mut (dyn Callbacks + Send),
126127
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
127128
emitter: Option<Box<dyn Write + Send>>
@@ -135,7 +136,7 @@ pub fn run_compiler(
135136

136137
install_panic_hook();
137138

138-
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
139+
let (sopts, cfg) = config::build_session_options_and_crate_config(driver_symbols, &matches);
139140

140141
let mut dummy_config = |sopts, cfg, diagnostic_output| {
141142
let mut config = interface::Config {
@@ -149,6 +150,7 @@ pub fn run_compiler(
149150
diagnostic_output,
150151
stderr: None,
151152
crate_name: None,
153+
driver_symbols,
152154
lint_caps: Default::default(),
153155
};
154156
callbacks.config(&mut config);
@@ -222,6 +224,7 @@ pub fn run_compiler(
222224
diagnostic_output,
223225
stderr: None,
224226
crate_name: None,
227+
driver_symbols,
225228
lint_caps: Default::default(),
226229
};
227230

@@ -1176,7 +1179,7 @@ pub fn main() {
11761179
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
11771180
}))
11781181
.collect::<Vec<_>>();
1179-
run_compiler(&args, &mut DefaultCallbacks, None, None)
1182+
run_compiler(&args, &[], &mut DefaultCallbacks, None, None)
11801183
}).and_then(|result| result);
11811184
process::exit(match result {
11821185
Ok(_) => EXIT_SUCCESS,

src/librustc_interface/interface.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub struct Config {
7676
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
7777
pub diagnostic_output: DiagnosticOutput,
7878

79+
pub driver_symbols: &'static [&'static str],
80+
7981
/// Set to capture stderr output during compiler execution
8082
pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
8183

@@ -136,15 +138,16 @@ where
136138
let stderr = config.stderr.take();
137139
util::spawn_thread_pool(
138140
config.opts.debugging_opts.threads,
141+
config.driver_symbols,
139142
&stderr,
140143
|| run_compiler_in_existing_thread_pool(config, f),
141144
)
142145
}
143146

144-
pub fn default_thread_pool<F, R>(f: F) -> R
147+
pub fn default_thread_pool<F, R>(driver_symbols: &[&str], f: F) -> R
145148
where
146149
F: FnOnce() -> R + Send,
147150
R: Send,
148151
{
149-
util::spawn_thread_pool(None, &None, f)
152+
util::spawn_thread_pool(None, driver_symbols, &None, f)
150153
}

src/librustc_interface/util.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f:
168168
#[cfg(not(parallel_compiler))]
169169
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
170170
_threads: Option<usize>,
171+
driver_symbols: &[&str],
171172
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
172173
f: F,
173174
) -> R {
@@ -178,7 +179,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
178179
}
179180

180181
scoped_thread(cfg, || {
181-
syntax::with_globals( || {
182+
syntax::with_globals(driver_symbols, || {
182183
ty::tls::GCX_PTR.set(&Lock::new(0), || {
183184
if let Some(stderr) = stderr {
184185
io::set_panic(Some(box Sink(stderr.clone())));
@@ -192,6 +193,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
192193
#[cfg(parallel_compiler)]
193194
pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
194195
threads: Option<usize>,
196+
driver_symbols: &[&str],
195197
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
196198
f: F,
197199
) -> R {
@@ -213,7 +215,7 @@ pub fn spawn_thread_pool<F: FnOnce() -> R + Send, R: Send>(
213215

214216
let with_pool = move |pool: &ThreadPool| pool.install(move || f());
215217

216-
syntax::with_globals(|| {
218+
syntax::with_globals(driver_symbols, || {
217219
syntax::GLOBALS.with(|syntax_globals| {
218220
syntax_pos::GLOBALS.with(|syntax_pos_globals| {
219221
// The main handler runs for each Rayon worker thread and sets up

src/librustc_macros/src/symbols.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ pub fn symbols(input: TokenStream) -> TokenStream {
166166
}
167167

168168
impl Interner {
169-
pub fn fresh() -> Self {
170-
Interner::prefill(&[
171-
#prefill_stream
172-
])
169+
/// If your driver adds more symbols, this is the first index you may use.
170+
/// Do not leave holes in the indexing scheme and add all symbols to the `Interner`
171+
/// created in the closure argument to `Interner::fresh`.
172+
pub const FIRST_DRIVER_INDEX: u32 = #counter;
173+
/// It is the driver's responsibility to not preeintern any symbols that are already
174+
/// in the list given to the closure.
175+
pub fn fresh(driver_symbols: &[&str]) -> Self {
176+
Interner::prefill(&[#prefill_stream], driver_symbols)
173177
}
174178
}
175179
});

src/librustdoc/clean/cfg.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ mod test {
466466

467467
#[test]
468468
fn test_cfg_not() {
469-
with_globals(|| {
469+
with_globals(&[], || {
470470
assert_eq!(!Cfg::False, Cfg::True);
471471
assert_eq!(!Cfg::True, Cfg::False);
472472
assert_eq!(!word_cfg("test"), Cfg::Not(Box::new(word_cfg("test"))));
@@ -484,7 +484,7 @@ mod test {
484484

485485
#[test]
486486
fn test_cfg_and() {
487-
with_globals(|| {
487+
with_globals(&[], || {
488488
let mut x = Cfg::False;
489489
x &= Cfg::True;
490490
assert_eq!(x, Cfg::False);
@@ -536,7 +536,7 @@ mod test {
536536

537537
#[test]
538538
fn test_cfg_or() {
539-
with_globals(|| {
539+
with_globals(&[], || {
540540
let mut x = Cfg::True;
541541
x |= Cfg::False;
542542
assert_eq!(x, Cfg::True);
@@ -588,7 +588,7 @@ mod test {
588588

589589
#[test]
590590
fn test_parse_ok() {
591-
with_globals(|| {
591+
with_globals(&[], || {
592592
let mi = dummy_meta_item_word("all");
593593
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));
594594

@@ -622,7 +622,7 @@ mod test {
622622

623623
#[test]
624624
fn test_parse_err() {
625-
with_globals(|| {
625+
with_globals(&[], || {
626626
let mi = attr::mk_name_value_item(
627627
DUMMY_SP,
628628
Ident::from_str("foo"),
@@ -661,7 +661,7 @@ mod test {
661661

662662
#[test]
663663
fn test_render_short_html() {
664-
with_globals(|| {
664+
with_globals(&[], || {
665665
assert_eq!(
666666
word_cfg("unix").render_short_html(),
667667
"Unix"
@@ -741,7 +741,7 @@ mod test {
741741

742742
#[test]
743743
fn test_render_long_html() {
744-
with_globals(|| {
744+
with_globals(&[], || {
745745
assert_eq!(
746746
word_cfg("unix").render_long_html(),
747747
"This is supported on <strong>Unix</strong> only."

src/librustdoc/core.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
326326

327327
let config = interface::Config {
328328
opts: sessopts,
329-
crate_cfg: config::parse_cfgspecs(cfgs),
329+
crate_cfg: config::parse_cfgspecs(&[], cfgs),
330+
driver_symbols: &[],
330331
input,
331332
input_path: cpath,
332333
output_file: None,

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn main() {
9494
rustc_driver::set_sigpipe_handler();
9595
env_logger::init();
9696
let res = std::thread::Builder::new().stack_size(thread_stack_size).spawn(move || {
97-
rustc_interface::interface::default_thread_pool(move || {
97+
rustc_interface::interface::default_thread_pool(&[], move || {
9898
get_args().map(|args| main_args(&args)).unwrap_or(1)
9999
})
100100
}).unwrap().join().unwrap_or(rustc_driver::EXIT_FAILURE);

src/librustdoc/test.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ pub fn run(options: Options) -> i32 {
6161

6262
let config = interface::Config {
6363
opts: sessopts,
64-
crate_cfg: config::parse_cfgspecs(options.cfgs.clone()),
64+
crate_cfg: config::parse_cfgspecs(&[], options.cfgs.clone()),
65+
driver_symbols: &[],
6566
input,
6667
input_path: None,
6768
output_file: None,
@@ -279,7 +280,8 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
279280

280281
let config = interface::Config {
281282
opts: sessopts,
282-
crate_cfg: config::parse_cfgspecs(cfgs),
283+
crate_cfg: config::parse_cfgspecs(&[], cfgs),
284+
driver_symbols: &[],
283285
input,
284286
input_path: None,
285287
output_file: Some(output_file.clone()),
@@ -385,7 +387,9 @@ pub fn make_test(s: &str,
385387

386388
// Uses libsyntax to parse the doctest and find if there's a main fn and the extern
387389
// crate already is included.
388-
let (already_has_main, already_has_extern_crate, found_macro) = crate::syntax::with_globals(|| {
390+
let (
391+
already_has_main, already_has_extern_crate, found_macro,
392+
) = crate::syntax::with_globals(&[],|| {
389393
use crate::syntax::{parse::{self, ParseSess}, source_map::FilePathMapping};
390394
use errors::emitter::EmitterWriter;
391395
use errors::Handler;

src/libsyntax/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,23 @@ pub struct Globals {
8282
}
8383

8484
impl Globals {
85-
fn new() -> Globals {
85+
/// See documentation on `syntax_pos::Globals` for information on the slice.
86+
/// It is solely forwareded to `syntax_pos::Globals::new`
87+
fn new(driver_symbols: &[&str]) -> Globals {
8688
Globals {
8789
// We have no idea how many attributes their will be, so just
8890
// initiate the vectors with 0 bits. We'll grow them as necessary.
8991
used_attrs: Lock::new(GrowableBitSet::new_empty()),
9092
known_attrs: Lock::new(GrowableBitSet::new_empty()),
91-
syntax_pos_globals: syntax_pos::Globals::new(),
93+
syntax_pos_globals: syntax_pos::Globals::new(driver_symbols),
9294
}
9395
}
9496
}
9597

96-
pub fn with_globals<F, R>(f: F) -> R
98+
pub fn with_globals<F, R>(driver_symbols: &[&str], f: F) -> R
9799
where F: FnOnce() -> R
98100
{
99-
let globals = Globals::new();
101+
let globals = Globals::new(driver_symbols);
100102
GLOBALS.set(&globals, || {
101103
syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
102104
})

0 commit comments

Comments
 (0)