Remove syntax and syntax_pos thread locals
This commit is contained in:
parent
fab632f975
commit
cbdf4ec03e
29 changed files with 1212 additions and 998 deletions
2
src/Cargo.lock
generated
2
src/Cargo.lock
generated
|
@ -2432,6 +2432,7 @@ dependencies = [
|
||||||
"rustc_cratesio_shim 0.0.0",
|
"rustc_cratesio_shim 0.0.0",
|
||||||
"rustc_data_structures 0.0.0",
|
"rustc_data_structures 0.0.0",
|
||||||
"rustc_errors 0.0.0",
|
"rustc_errors 0.0.0",
|
||||||
|
"scoped-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serialize 0.0.0",
|
"serialize 0.0.0",
|
||||||
"syntax_pos 0.0.0",
|
"syntax_pos 0.0.0",
|
||||||
]
|
]
|
||||||
|
@ -2453,6 +2454,7 @@ name = "syntax_pos"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rustc_data_structures 0.0.0",
|
"rustc_data_structures 0.0.0",
|
||||||
|
"scoped-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serialize 0.0.0",
|
"serialize 0.0.0",
|
||||||
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
|
@ -2391,6 +2391,7 @@ mod tests {
|
||||||
use super::{Externs, OutputType, OutputTypes};
|
use super::{Externs, OutputType, OutputTypes};
|
||||||
use rustc_back::{PanicStrategy, RelroLevel};
|
use rustc_back::{PanicStrategy, RelroLevel};
|
||||||
use syntax::symbol::Symbol;
|
use syntax::symbol::Symbol;
|
||||||
|
use syntax;
|
||||||
|
|
||||||
fn optgroups() -> getopts::Options {
|
fn optgroups() -> getopts::Options {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
|
@ -2411,6 +2412,7 @@ mod tests {
|
||||||
// When the user supplies --test we should implicitly supply --cfg test
|
// When the user supplies --test we should implicitly supply --cfg test
|
||||||
#[test]
|
#[test]
|
||||||
fn test_switch_implies_cfg_test() {
|
fn test_switch_implies_cfg_test() {
|
||||||
|
syntax::with_globals(|| {
|
||||||
let matches = &match optgroups().parse(&["--test".to_string()]) {
|
let matches = &match optgroups().parse(&["--test".to_string()]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
|
Err(f) => panic!("test_switch_implies_cfg_test: {}", f),
|
||||||
|
@ -2420,13 +2422,16 @@ mod tests {
|
||||||
let sess = build_session(sessopts, None, registry);
|
let sess = build_session(sessopts, None, registry);
|
||||||
let cfg = build_configuration(&sess, cfg);
|
let cfg = build_configuration(&sess, cfg);
|
||||||
assert!(cfg.contains(&(Symbol::intern("test"), None)));
|
assert!(cfg.contains(&(Symbol::intern("test"), None)));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the user supplies --test and --cfg test, don't implicitly add
|
// When the user supplies --test and --cfg test, don't implicitly add
|
||||||
// another --cfg test
|
// another --cfg test
|
||||||
#[test]
|
#[test]
|
||||||
fn test_switch_implies_cfg_test_unless_cfg_test() {
|
fn test_switch_implies_cfg_test_unless_cfg_test() {
|
||||||
let matches = &match optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]) {
|
syntax::with_globals(|| {
|
||||||
|
let matches = &match optgroups().parse(&["--test".to_string(),
|
||||||
|
"--cfg=test".to_string()]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
|
Err(f) => panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f),
|
||||||
};
|
};
|
||||||
|
@ -2437,19 +2442,20 @@ mod tests {
|
||||||
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
|
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
|
||||||
assert!(test_items.next().is_some());
|
assert!(test_items.next().is_some());
|
||||||
assert!(test_items.next().is_none());
|
assert!(test_items.next().is_none());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_can_print_warnings() {
|
fn test_can_print_warnings() {
|
||||||
{
|
syntax::with_globals(|| {
|
||||||
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
|
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
|
||||||
let registry = errors::registry::Registry::new(&[]);
|
let registry = errors::registry::Registry::new(&[]);
|
||||||
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
||||||
let sess = build_session(sessopts, None, registry);
|
let sess = build_session(sessopts, None, registry);
|
||||||
assert!(!sess.diagnostic().flags.can_emit_warnings);
|
assert!(!sess.diagnostic().flags.can_emit_warnings);
|
||||||
}
|
});
|
||||||
|
|
||||||
{
|
syntax::with_globals(|| {
|
||||||
let matches = optgroups()
|
let matches = optgroups()
|
||||||
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
|
.parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -2457,15 +2463,15 @@ mod tests {
|
||||||
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
||||||
let sess = build_session(sessopts, None, registry);
|
let sess = build_session(sessopts, None, registry);
|
||||||
assert!(sess.diagnostic().flags.can_emit_warnings);
|
assert!(sess.diagnostic().flags.can_emit_warnings);
|
||||||
}
|
});
|
||||||
|
|
||||||
{
|
syntax::with_globals(|| {
|
||||||
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
|
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
|
||||||
let registry = errors::registry::Registry::new(&[]);
|
let registry = errors::registry::Registry::new(&[]);
|
||||||
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
let (sessopts, _) = build_session_options_and_crate_config(&matches);
|
||||||
let sess = build_session(sessopts, None, registry);
|
let sess = build_session(sessopts, None, registry);
|
||||||
assert!(sess.diagnostic().flags.can_emit_warnings);
|
assert!(sess.diagnostic().flags.can_emit_warnings);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -447,6 +447,17 @@ pub fn run_compiler<'a>(args: &[String],
|
||||||
file_loader: Option<Box<FileLoader + 'static>>,
|
file_loader: Option<Box<FileLoader + 'static>>,
|
||||||
emitter_dest: Option<Box<Write + Send>>)
|
emitter_dest: Option<Box<Write + Send>>)
|
||||||
-> (CompileResult, Option<Session>)
|
-> (CompileResult, Option<Session>)
|
||||||
|
{
|
||||||
|
syntax::with_globals(|| {
|
||||||
|
run_compiler_impl(args, callbacks, file_loader, emitter_dest)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_compiler_impl<'a>(args: &[String],
|
||||||
|
callbacks: &mut CompilerCalls<'a>,
|
||||||
|
file_loader: Option<Box<FileLoader + 'static>>,
|
||||||
|
emitter_dest: Option<Box<Write + Send>>)
|
||||||
|
-> (CompileResult, Option<Session>)
|
||||||
{
|
{
|
||||||
macro_rules! do_or_return {($expr: expr, $sess: expr) => {
|
macro_rules! do_or_return {($expr: expr, $sess: expr) => {
|
||||||
match $expr {
|
match $expr {
|
||||||
|
|
|
@ -29,6 +29,7 @@ use rustc::hir::map as hir_map;
|
||||||
use rustc::session::{self, config};
|
use rustc::session::{self, config};
|
||||||
use rustc::session::config::{OutputFilenames, OutputTypes};
|
use rustc::session::config::{OutputFilenames, OutputTypes};
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
|
use syntax;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::abi::Abi;
|
use syntax::abi::Abi;
|
||||||
use syntax::codemap::{CodeMap, FilePathMapping, FileName};
|
use syntax::codemap::{CodeMap, FilePathMapping, FileName};
|
||||||
|
@ -93,6 +94,16 @@ fn errors(msgs: &[&str]) -> (Box<Emitter + Send>, usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_env<F>(source_string: &str,
|
fn test_env<F>(source_string: &str,
|
||||||
|
args: (Box<Emitter + Send>, usize),
|
||||||
|
body: F)
|
||||||
|
where F: FnOnce(Env)
|
||||||
|
{
|
||||||
|
syntax::with_globals(|| {
|
||||||
|
test_env_impl(source_string, args, body)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_env_impl<F>(source_string: &str,
|
||||||
(emitter, expected_err_count): (Box<Emitter + Send>, usize),
|
(emitter, expected_err_count): (Box<Emitter + Send>, usize),
|
||||||
body: F)
|
body: F)
|
||||||
where F: FnOnce(Env)
|
where F: FnOnce(Env)
|
||||||
|
|
|
@ -398,6 +398,7 @@ mod test {
|
||||||
use syntax::ast::*;
|
use syntax::ast::*;
|
||||||
use syntax::codemap::dummy_spanned;
|
use syntax::codemap::dummy_spanned;
|
||||||
use syntax_pos::DUMMY_SP;
|
use syntax_pos::DUMMY_SP;
|
||||||
|
use syntax::with_globals;
|
||||||
|
|
||||||
fn word_cfg(s: &str) -> Cfg {
|
fn word_cfg(s: &str) -> Cfg {
|
||||||
Cfg::Cfg(Symbol::intern(s), None)
|
Cfg::Cfg(Symbol::intern(s), None)
|
||||||
|
@ -409,6 +410,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cfg_not() {
|
fn test_cfg_not() {
|
||||||
|
with_globals(|| {
|
||||||
assert_eq!(!Cfg::False, Cfg::True);
|
assert_eq!(!Cfg::False, Cfg::True);
|
||||||
assert_eq!(!Cfg::True, Cfg::False);
|
assert_eq!(!Cfg::True, Cfg::False);
|
||||||
assert_eq!(!word_cfg("test"), Cfg::Not(Box::new(word_cfg("test"))));
|
assert_eq!(!word_cfg("test"), Cfg::Not(Box::new(word_cfg("test"))));
|
||||||
|
@ -421,10 +423,12 @@ mod test {
|
||||||
Cfg::Not(Box::new(Cfg::Any(vec![word_cfg("a"), word_cfg("b")])))
|
Cfg::Not(Box::new(Cfg::Any(vec![word_cfg("a"), word_cfg("b")])))
|
||||||
);
|
);
|
||||||
assert_eq!(!Cfg::Not(Box::new(word_cfg("test"))), word_cfg("test"));
|
assert_eq!(!Cfg::Not(Box::new(word_cfg("test"))), word_cfg("test"));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cfg_and() {
|
fn test_cfg_and() {
|
||||||
|
with_globals(|| {
|
||||||
let mut x = Cfg::False;
|
let mut x = Cfg::False;
|
||||||
x &= Cfg::True;
|
x &= Cfg::True;
|
||||||
assert_eq!(x, Cfg::False);
|
assert_eq!(x, Cfg::False);
|
||||||
|
@ -471,10 +475,12 @@ mod test {
|
||||||
word_cfg("a") & word_cfg("b") & word_cfg("c"),
|
word_cfg("a") & word_cfg("b") & word_cfg("c"),
|
||||||
Cfg::All(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
|
Cfg::All(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
|
||||||
);
|
);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cfg_or() {
|
fn test_cfg_or() {
|
||||||
|
with_globals(|| {
|
||||||
let mut x = Cfg::True;
|
let mut x = Cfg::True;
|
||||||
x |= Cfg::False;
|
x |= Cfg::False;
|
||||||
assert_eq!(x, Cfg::True);
|
assert_eq!(x, Cfg::True);
|
||||||
|
@ -521,10 +527,12 @@ mod test {
|
||||||
word_cfg("a") | word_cfg("b") | word_cfg("c"),
|
word_cfg("a") | word_cfg("b") | word_cfg("c"),
|
||||||
Cfg::Any(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
|
Cfg::Any(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
|
||||||
);
|
);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_ok() {
|
fn test_parse_ok() {
|
||||||
|
with_globals(|| {
|
||||||
let mi = MetaItem {
|
let mi = MetaItem {
|
||||||
name: Symbol::intern("all"),
|
name: Symbol::intern("all"),
|
||||||
node: MetaItemKind::Word,
|
node: MetaItemKind::Word,
|
||||||
|
@ -648,10 +656,12 @@ mod test {
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
};
|
};
|
||||||
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") & word_cfg("b") & word_cfg("c")));
|
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") & word_cfg("b") & word_cfg("c")));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_err() {
|
fn test_parse_err() {
|
||||||
|
with_globals(|| {
|
||||||
let mi = MetaItem {
|
let mi = MetaItem {
|
||||||
name: Symbol::intern("foo"),
|
name: Symbol::intern("foo"),
|
||||||
node: MetaItemKind::NameValue(dummy_spanned(LitKind::Bool(false))),
|
node: MetaItemKind::NameValue(dummy_spanned(LitKind::Bool(false))),
|
||||||
|
@ -745,10 +755,12 @@ mod test {
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
};
|
};
|
||||||
assert!(Cfg::parse(&mi).is_err());
|
assert!(Cfg::parse(&mi).is_err());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_short_html() {
|
fn test_render_short_html() {
|
||||||
|
with_globals(|| {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
word_cfg("unix").render_short_html(),
|
word_cfg("unix").render_short_html(),
|
||||||
"Unix"
|
"Unix"
|
||||||
|
@ -812,10 +824,12 @@ mod test {
|
||||||
).render_short_html(),
|
).render_short_html(),
|
||||||
"(Debug-assertions enabled or Windows) and Unix"
|
"(Debug-assertions enabled or Windows) and Unix"
|
||||||
);
|
);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_render_long_html() {
|
fn test_render_long_html() {
|
||||||
|
with_globals(|| {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
word_cfg("unix").render_long_html(),
|
word_cfg("unix").render_long_html(),
|
||||||
"This is supported on <strong>Unix</strong> only."
|
"This is supported on <strong>Unix</strong> only."
|
||||||
|
@ -848,15 +862,15 @@ mod test {
|
||||||
(
|
(
|
||||||
word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions")
|
word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions")
|
||||||
).render_long_html(),
|
).render_long_html(),
|
||||||
"This is supported on <strong>Unix and Windows and debug-assertions enabled</strong> \
|
"This is supported on <strong>Unix and Windows and debug-assertions enabled\
|
||||||
only."
|
</strong> only."
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(
|
(
|
||||||
word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")
|
word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")
|
||||||
).render_long_html(),
|
).render_long_html(),
|
||||||
"This is supported on <strong>Unix or Windows or debug-assertions enabled</strong> \
|
"This is supported on <strong>Unix or Windows or debug-assertions enabled\
|
||||||
only."
|
</strong> only."
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(
|
(
|
||||||
|
@ -870,7 +884,8 @@ mod test {
|
||||||
(word_cfg("unix") & name_value_cfg("target_arch", "x86_64")) |
|
(word_cfg("unix") & name_value_cfg("target_arch", "x86_64")) |
|
||||||
(word_cfg("windows") & name_value_cfg("target_pointer_width", "64"))
|
(word_cfg("windows") & name_value_cfg("target_pointer_width", "64"))
|
||||||
).render_long_html(),
|
).render_long_html(),
|
||||||
"This is supported on <strong>Unix and x86-64, or Windows and 64-bit</strong> only."
|
"This is supported on <strong>Unix and x86-64, or Windows and 64-bit</strong> \
|
||||||
|
only."
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(!(word_cfg("unix") & word_cfg("windows"))).render_long_html(),
|
(!(word_cfg("unix") & word_cfg("windows"))).render_long_html(),
|
||||||
|
@ -880,8 +895,9 @@ mod test {
|
||||||
(
|
(
|
||||||
(word_cfg("debug_assertions") | word_cfg("windows")) & word_cfg("unix")
|
(word_cfg("debug_assertions") | word_cfg("windows")) & word_cfg("unix")
|
||||||
).render_long_html(),
|
).render_long_html(),
|
||||||
"This is supported on <strong>(debug-assertions enabled or Windows) and Unix</strong> \
|
"This is supported on <strong>(debug-assertions enabled or Windows) and Unix\
|
||||||
only."
|
</strong> only."
|
||||||
);
|
);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,7 +102,9 @@ pub fn main() {
|
||||||
const STACK_SIZE: usize = 32_000_000; // 32MB
|
const STACK_SIZE: usize = 32_000_000; // 32MB
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let res = std::thread::Builder::new().stack_size(STACK_SIZE).spawn(move || {
|
let res = std::thread::Builder::new().stack_size(STACK_SIZE).spawn(move || {
|
||||||
|
syntax::with_globals(move || {
|
||||||
get_args().map(|args| main_args(&args)).unwrap_or(1)
|
get_args().map(|args| main_args(&args)).unwrap_or(1)
|
||||||
|
})
|
||||||
}).unwrap().join().unwrap_or(101);
|
}).unwrap().join().unwrap_or(101);
|
||||||
process::exit(res as i32);
|
process::exit(res as i32);
|
||||||
}
|
}
|
||||||
|
@ -554,7 +556,8 @@ where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
|
||||||
});
|
});
|
||||||
|
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
rustc_driver::monitor(move || {
|
|
||||||
|
rustc_driver::monitor(move || syntax::with_globals(move || {
|
||||||
use rustc::session::config::Input;
|
use rustc::session::config::Input;
|
||||||
|
|
||||||
let (mut krate, renderinfo) =
|
let (mut krate, renderinfo) =
|
||||||
|
@ -623,7 +626,7 @@ where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
|
||||||
let krate = pm.run_plugins(krate);
|
let krate = pm.run_plugins(krate);
|
||||||
|
|
||||||
tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes })).unwrap();
|
tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes })).unwrap();
|
||||||
});
|
}));
|
||||||
rx.recv().unwrap()
|
rx.recv().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ use rustc_resolve::MakeGlobMap;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap::CodeMap;
|
use syntax::codemap::CodeMap;
|
||||||
use syntax::feature_gate::UnstableFeatures;
|
use syntax::feature_gate::UnstableFeatures;
|
||||||
|
use syntax::with_globals;
|
||||||
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
|
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
|
||||||
use errors;
|
use errors;
|
||||||
use errors::emitter::ColorConfig;
|
use errors::emitter::ColorConfig;
|
||||||
|
@ -518,7 +519,7 @@ impl Collector {
|
||||||
let panic = io::set_panic(None);
|
let panic = io::set_panic(None);
|
||||||
let print = io::set_print(None);
|
let print = io::set_print(None);
|
||||||
match {
|
match {
|
||||||
rustc_driver::in_rustc_thread(move || {
|
rustc_driver::in_rustc_thread(move || with_globals(move || {
|
||||||
io::set_panic(panic);
|
io::set_panic(panic);
|
||||||
io::set_print(print);
|
io::set_print(print);
|
||||||
run_test(&test,
|
run_test(&test,
|
||||||
|
@ -536,7 +537,7 @@ impl Collector {
|
||||||
&opts,
|
&opts,
|
||||||
maybe_sysroot,
|
maybe_sysroot,
|
||||||
linker)
|
linker)
|
||||||
})
|
}))
|
||||||
} {
|
} {
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(err) => panic::resume_unwind(err),
|
Err(err) => panic::resume_unwind(err),
|
||||||
|
|
|
@ -12,6 +12,7 @@ crate-type = ["dylib"]
|
||||||
bitflags = "1.0"
|
bitflags = "1.0"
|
||||||
serialize = { path = "../libserialize" }
|
serialize = { path = "../libserialize" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
scoped-tls = "0.1"
|
||||||
syntax_pos = { path = "../libsyntax_pos" }
|
syntax_pos = { path = "../libsyntax_pos" }
|
||||||
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
|
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
|
||||||
rustc_errors = { path = "../librustc_errors" }
|
rustc_errors = { path = "../librustc_errors" }
|
||||||
|
|
|
@ -30,15 +30,10 @@ use ptr::P;
|
||||||
use symbol::Symbol;
|
use symbol::Symbol;
|
||||||
use tokenstream::{TokenStream, TokenTree, Delimited};
|
use tokenstream::{TokenStream, TokenTree, Delimited};
|
||||||
use util::ThinVec;
|
use util::ThinVec;
|
||||||
|
use GLOBALS;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::iter;
|
use std::iter;
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
|
|
||||||
static KNOWN_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
enum AttrError {
|
enum AttrError {
|
||||||
MultipleItem(Name),
|
MultipleItem(Name),
|
||||||
UnknownMetaItem(Name),
|
UnknownMetaItem(Name),
|
||||||
|
@ -65,22 +60,24 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
|
||||||
pub fn mark_used(attr: &Attribute) {
|
pub fn mark_used(attr: &Attribute) {
|
||||||
debug!("Marking {:?} as used.", attr);
|
debug!("Marking {:?} as used.", attr);
|
||||||
let AttrId(id) = attr.id;
|
let AttrId(id) = attr.id;
|
||||||
USED_ATTRS.with(|slot| {
|
GLOBALS.with(|globals| {
|
||||||
|
let mut slot = globals.used_attrs.lock();
|
||||||
let idx = (id / 64) as usize;
|
let idx = (id / 64) as usize;
|
||||||
let shift = id % 64;
|
let shift = id % 64;
|
||||||
if slot.borrow().len() <= idx {
|
if slot.len() <= idx {
|
||||||
slot.borrow_mut().resize(idx + 1, 0);
|
slot.resize(idx + 1, 0);
|
||||||
}
|
}
|
||||||
slot.borrow_mut()[idx] |= 1 << shift;
|
slot[idx] |= 1 << shift;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_used(attr: &Attribute) -> bool {
|
pub fn is_used(attr: &Attribute) -> bool {
|
||||||
let AttrId(id) = attr.id;
|
let AttrId(id) = attr.id;
|
||||||
USED_ATTRS.with(|slot| {
|
GLOBALS.with(|globals| {
|
||||||
|
let slot = globals.used_attrs.lock();
|
||||||
let idx = (id / 64) as usize;
|
let idx = (id / 64) as usize;
|
||||||
let shift = id % 64;
|
let shift = id % 64;
|
||||||
slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
|
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -88,22 +85,24 @@ pub fn is_used(attr: &Attribute) -> bool {
|
||||||
pub fn mark_known(attr: &Attribute) {
|
pub fn mark_known(attr: &Attribute) {
|
||||||
debug!("Marking {:?} as known.", attr);
|
debug!("Marking {:?} as known.", attr);
|
||||||
let AttrId(id) = attr.id;
|
let AttrId(id) = attr.id;
|
||||||
KNOWN_ATTRS.with(|slot| {
|
GLOBALS.with(|globals| {
|
||||||
|
let mut slot = globals.known_attrs.lock();
|
||||||
let idx = (id / 64) as usize;
|
let idx = (id / 64) as usize;
|
||||||
let shift = id % 64;
|
let shift = id % 64;
|
||||||
if slot.borrow().len() <= idx {
|
if slot.len() <= idx {
|
||||||
slot.borrow_mut().resize(idx + 1, 0);
|
slot.resize(idx + 1, 0);
|
||||||
}
|
}
|
||||||
slot.borrow_mut()[idx] |= 1 << shift;
|
slot[idx] |= 1 << shift;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_known(attr: &Attribute) -> bool {
|
pub fn is_known(attr: &Attribute) -> bool {
|
||||||
let AttrId(id) = attr.id;
|
let AttrId(id) = attr.id;
|
||||||
KNOWN_ATTRS.with(|slot| {
|
GLOBALS.with(|globals| {
|
||||||
|
let slot = globals.known_attrs.lock();
|
||||||
let idx = (id / 64) as usize;
|
let idx = (id / 64) as usize;
|
||||||
let shift = id % 64;
|
let shift = id % 64;
|
||||||
slot.borrow().get(idx).map(|bits| bits & (1 << shift) != 0)
|
slot.get(idx).map(|bits| bits & (1 << shift) != 0)
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1386,6 +1386,7 @@ mod tests {
|
||||||
use util::parser_testing::{string_to_crate, matches_codepattern};
|
use util::parser_testing::{string_to_crate, matches_codepattern};
|
||||||
use print::pprust;
|
use print::pprust;
|
||||||
use fold;
|
use fold;
|
||||||
|
use with_globals;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// this version doesn't care about getting comments or docstrings in.
|
// this version doesn't care about getting comments or docstrings in.
|
||||||
|
@ -1423,6 +1424,7 @@ mod tests {
|
||||||
|
|
||||||
// make sure idents get transformed everywhere
|
// make sure idents get transformed everywhere
|
||||||
#[test] fn ident_transformation () {
|
#[test] fn ident_transformation () {
|
||||||
|
with_globals(|| {
|
||||||
let mut zz_fold = ToZzIdentFolder;
|
let mut zz_fold = ToZzIdentFolder;
|
||||||
let ast = string_to_crate(
|
let ast = string_to_crate(
|
||||||
"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
|
"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
|
||||||
|
@ -1432,10 +1434,12 @@ mod tests {
|
||||||
"matches_codepattern",
|
"matches_codepattern",
|
||||||
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
||||||
"#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
|
"#[zz]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// even inside macro defs....
|
// even inside macro defs....
|
||||||
#[test] fn ident_transformation_in_defs () {
|
#[test] fn ident_transformation_in_defs () {
|
||||||
|
with_globals(|| {
|
||||||
let mut zz_fold = ToZzIdentFolder;
|
let mut zz_fold = ToZzIdentFolder;
|
||||||
let ast = string_to_crate(
|
let ast = string_to_crate(
|
||||||
"macro_rules! a {(b $c:expr $(d $e:token)f+ => \
|
"macro_rules! a {(b $c:expr $(d $e:token)f+ => \
|
||||||
|
@ -1446,5 +1450,6 @@ mod tests {
|
||||||
"matches_codepattern",
|
"matches_codepattern",
|
||||||
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
pprust::to_string(|s| fake_print_crate(s, &folded_crate)),
|
||||||
"macro_rules! zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
|
"macro_rules! zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,12 @@ extern crate std_unicode;
|
||||||
pub extern crate rustc_errors as errors;
|
pub extern crate rustc_errors as errors;
|
||||||
extern crate syntax_pos;
|
extern crate syntax_pos;
|
||||||
extern crate rustc_data_structures;
|
extern crate rustc_data_structures;
|
||||||
|
#[macro_use] extern crate scoped_tls;
|
||||||
|
|
||||||
extern crate serialize as rustc_serialize; // used by deriving
|
extern crate serialize as rustc_serialize; // used by deriving
|
||||||
|
|
||||||
|
use rustc_data_structures::sync::Lock;
|
||||||
|
|
||||||
// A variant of 'try!' that panics on an Err. This is used as a crutch on the
|
// A variant of 'try!' that panics on an Err. This is used as a crutch on the
|
||||||
// way towards a non-panic!-prone parser. It should be used for fatal parsing
|
// way towards a non-panic!-prone parser. It should be used for fatal parsing
|
||||||
// errors; eventually we plan to convert all code using panictry to just use
|
// errors; eventually we plan to convert all code using panictry to just use
|
||||||
|
@ -72,6 +75,33 @@ macro_rules! unwrap_or {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Globals {
|
||||||
|
used_attrs: Lock<Vec<u64>>,
|
||||||
|
known_attrs: Lock<Vec<u64>>,
|
||||||
|
syntax_pos_globals: syntax_pos::Globals,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Globals {
|
||||||
|
fn new() -> Globals {
|
||||||
|
Globals {
|
||||||
|
used_attrs: Lock::new(Vec::new()),
|
||||||
|
known_attrs: Lock::new(Vec::new()),
|
||||||
|
syntax_pos_globals: syntax_pos::Globals::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_globals<F, R>(f: F) -> R
|
||||||
|
where F: FnOnce() -> R
|
||||||
|
{
|
||||||
|
let globals = Globals::new();
|
||||||
|
GLOBALS.set(&globals, || {
|
||||||
|
syntax_pos::GLOBALS.set(&globals.syntax_pos_globals, f)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
scoped_thread_local!(static GLOBALS: Globals);
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod diagnostics {
|
pub mod diagnostics {
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
|
|
@ -1766,6 +1766,7 @@ mod tests {
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use diagnostics::plugin::ErrorMap;
|
use diagnostics::plugin::ErrorMap;
|
||||||
use rustc_data_structures::sync::Lock;
|
use rustc_data_structures::sync::Lock;
|
||||||
|
use with_globals;
|
||||||
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
|
fn mk_sess(cm: Lrc<CodeMap>) -> ParseSess {
|
||||||
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
|
let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()),
|
||||||
Some(cm.clone()),
|
Some(cm.clone()),
|
||||||
|
@ -1794,6 +1795,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn t1() {
|
fn t1() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
let mut string_reader = setup(&cm,
|
let mut string_reader = setup(&cm,
|
||||||
|
@ -1821,6 +1823,7 @@ mod tests {
|
||||||
assert_eq!(tok3, tok4);
|
assert_eq!(tok3, tok4);
|
||||||
// the lparen is already read:
|
// the lparen is already read:
|
||||||
assert_eq!(string_reader.pos.clone(), BytePos(29))
|
assert_eq!(string_reader.pos.clone(), BytePos(29))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that the given reader produces the desired stream
|
// check that the given reader produces the desired stream
|
||||||
|
@ -1838,80 +1841,99 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doublecolonparsing() {
|
fn doublecolonparsing() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
check_tokenization(setup(&cm, &sh, "a b".to_string()),
|
check_tokenization(setup(&cm, &sh, "a b".to_string()),
|
||||||
vec![mk_ident("a"), token::Whitespace, mk_ident("b")]);
|
vec![mk_ident("a"), token::Whitespace, mk_ident("b")]);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dcparsing_2() {
|
fn dcparsing_2() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
check_tokenization(setup(&cm, &sh, "a::b".to_string()),
|
check_tokenization(setup(&cm, &sh, "a::b".to_string()),
|
||||||
vec![mk_ident("a"), token::ModSep, mk_ident("b")]);
|
vec![mk_ident("a"), token::ModSep, mk_ident("b")]);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dcparsing_3() {
|
fn dcparsing_3() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
check_tokenization(setup(&cm, &sh, "a ::b".to_string()),
|
check_tokenization(setup(&cm, &sh, "a ::b".to_string()),
|
||||||
vec![mk_ident("a"), token::Whitespace, token::ModSep, mk_ident("b")]);
|
vec![mk_ident("a"), token::Whitespace, token::ModSep, mk_ident("b")]);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dcparsing_4() {
|
fn dcparsing_4() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
check_tokenization(setup(&cm, &sh, "a:: b".to_string()),
|
check_tokenization(setup(&cm, &sh, "a:: b".to_string()),
|
||||||
vec![mk_ident("a"), token::ModSep, token::Whitespace, mk_ident("b")]);
|
vec![mk_ident("a"), token::ModSep, token::Whitespace, mk_ident("b")]);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn character_a() {
|
fn character_a() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
assert_eq!(setup(&cm, &sh, "'a'".to_string()).next_token().tok,
|
assert_eq!(setup(&cm, &sh, "'a'".to_string()).next_token().tok,
|
||||||
token::Literal(token::Char(Symbol::intern("a")), None));
|
token::Literal(token::Char(Symbol::intern("a")), None));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn character_space() {
|
fn character_space() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
assert_eq!(setup(&cm, &sh, "' '".to_string()).next_token().tok,
|
assert_eq!(setup(&cm, &sh, "' '".to_string()).next_token().tok,
|
||||||
token::Literal(token::Char(Symbol::intern(" ")), None));
|
token::Literal(token::Char(Symbol::intern(" ")), None));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn character_escaped() {
|
fn character_escaped() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
assert_eq!(setup(&cm, &sh, "'\\n'".to_string()).next_token().tok,
|
assert_eq!(setup(&cm, &sh, "'\\n'".to_string()).next_token().tok,
|
||||||
token::Literal(token::Char(Symbol::intern("\\n")), None));
|
token::Literal(token::Char(Symbol::intern("\\n")), None));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lifetime_name() {
|
fn lifetime_name() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
assert_eq!(setup(&cm, &sh, "'abc".to_string()).next_token().tok,
|
assert_eq!(setup(&cm, &sh, "'abc".to_string()).next_token().tok,
|
||||||
token::Lifetime(Ident::from_str("'abc")));
|
token::Lifetime(Ident::from_str("'abc")));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn raw_string() {
|
fn raw_string() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
assert_eq!(setup(&cm, &sh, "r###\"\"#a\\b\x00c\"\"###".to_string())
|
assert_eq!(setup(&cm, &sh, "r###\"\"#a\\b\x00c\"\"###".to_string())
|
||||||
.next_token()
|
.next_token()
|
||||||
.tok,
|
.tok,
|
||||||
token::Literal(token::StrRaw(Symbol::intern("\"#a\\b\x00c\""), 3), None));
|
token::Literal(token::StrRaw(Symbol::intern("\"#a\\b\x00c\""), 3), None));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn literal_suffixes() {
|
fn literal_suffixes() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
macro_rules! test {
|
macro_rules! test {
|
||||||
|
@ -1945,6 +1967,7 @@ mod tests {
|
||||||
assert_eq!(setup(&cm, &sh, "br###\"raw\"###suffix".to_string()).next_token().tok,
|
assert_eq!(setup(&cm, &sh, "br###\"raw\"###suffix".to_string()).next_token().tok,
|
||||||
token::Literal(token::ByteStrRaw(Symbol::intern("raw"), 3),
|
token::Literal(token::ByteStrRaw(Symbol::intern("raw"), 3),
|
||||||
Some(Symbol::intern("suffix"))));
|
Some(Symbol::intern("suffix"))));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1956,6 +1979,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_block_comments() {
|
fn nested_block_comments() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
let mut lexer = setup(&cm, &sh, "/* /* */ */'a'".to_string());
|
let mut lexer = setup(&cm, &sh, "/* /* */ */'a'".to_string());
|
||||||
|
@ -1965,10 +1989,12 @@ mod tests {
|
||||||
}
|
}
|
||||||
assert_eq!(lexer.next_token().tok,
|
assert_eq!(lexer.next_token().tok,
|
||||||
token::Literal(token::Char(Symbol::intern("a")), None));
|
token::Literal(token::Char(Symbol::intern("a")), None));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn crlf_comments() {
|
fn crlf_comments() {
|
||||||
|
with_globals(|| {
|
||||||
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let cm = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
let sh = mk_sess(cm.clone());
|
let sh = mk_sess(cm.clone());
|
||||||
let mut lexer = setup(&cm, &sh, "// test\r\n/// test\r\n".to_string());
|
let mut lexer = setup(&cm, &sh, "// test\r\n/// test\r\n".to_string());
|
||||||
|
@ -1978,5 +2004,6 @@ mod tests {
|
||||||
assert_eq!(lexer.next_token().tok, token::Whitespace);
|
assert_eq!(lexer.next_token().tok, token::Whitespace);
|
||||||
assert_eq!(lexer.next_token().tok,
|
assert_eq!(lexer.next_token().tok,
|
||||||
token::DocComment(Symbol::intern("/// test")));
|
token::DocComment(Symbol::intern("/// test")));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -680,6 +680,7 @@ mod tests {
|
||||||
use util::parser_testing::{string_to_stream, string_to_parser};
|
use util::parser_testing::{string_to_stream, string_to_parser};
|
||||||
use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt};
|
use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt};
|
||||||
use util::ThinVec;
|
use util::ThinVec;
|
||||||
|
use with_globals;
|
||||||
|
|
||||||
// produce a syntax_pos::span
|
// produce a syntax_pos::span
|
||||||
fn sp(a: u32, b: u32) -> Span {
|
fn sp(a: u32, b: u32) -> Span {
|
||||||
|
@ -691,6 +692,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn path_exprs_1() {
|
#[test] fn path_exprs_1() {
|
||||||
|
with_globals(|| {
|
||||||
assert!(string_to_expr("a".to_string()) ==
|
assert!(string_to_expr("a".to_string()) ==
|
||||||
P(ast::Expr{
|
P(ast::Expr{
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
@ -701,9 +703,11 @@ mod tests {
|
||||||
span: sp(0, 1),
|
span: sp(0, 1),
|
||||||
attrs: ThinVec::new(),
|
attrs: ThinVec::new(),
|
||||||
}))
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn path_exprs_2 () {
|
#[test] fn path_exprs_2 () {
|
||||||
|
with_globals(|| {
|
||||||
assert!(string_to_expr("::a::b".to_string()) ==
|
assert!(string_to_expr("::a::b".to_string()) ==
|
||||||
P(ast::Expr {
|
P(ast::Expr {
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
@ -716,16 +720,20 @@ mod tests {
|
||||||
span: sp(0, 6),
|
span: sp(0, 6),
|
||||||
attrs: ThinVec::new(),
|
attrs: ThinVec::new(),
|
||||||
}))
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
#[test] fn bad_path_expr_1() {
|
#[test] fn bad_path_expr_1() {
|
||||||
|
with_globals(|| {
|
||||||
string_to_expr("::abc::def::return".to_string());
|
string_to_expr("::abc::def::return".to_string());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the token-tree-ization of macros
|
// check the token-tree-ization of macros
|
||||||
#[test]
|
#[test]
|
||||||
fn string_to_tts_macro () {
|
fn string_to_tts_macro () {
|
||||||
|
with_globals(|| {
|
||||||
let tts: Vec<_> =
|
let tts: Vec<_> =
|
||||||
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
|
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
|
||||||
let tts: &[TokenTree] = &tts[..];
|
let tts: &[TokenTree] = &tts[..];
|
||||||
|
@ -776,10 +784,12 @@ mod tests {
|
||||||
},
|
},
|
||||||
_ => panic!("value: {:?}",tts),
|
_ => panic!("value: {:?}",tts),
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn string_to_tts_1() {
|
fn string_to_tts_1() {
|
||||||
|
with_globals(|| {
|
||||||
let tts = string_to_stream("fn a (b : i32) { b; }".to_string());
|
let tts = string_to_stream("fn a (b : i32) { b; }".to_string());
|
||||||
|
|
||||||
let expected = TokenStream::concat(vec![
|
let expected = TokenStream::concat(vec![
|
||||||
|
@ -792,7 +802,8 @@ mod tests {
|
||||||
tts: TokenStream::concat(vec![
|
tts: TokenStream::concat(vec![
|
||||||
TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))).into(),
|
TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))).into(),
|
||||||
TokenTree::Token(sp(8, 9), token::Colon).into(),
|
TokenTree::Token(sp(8, 9), token::Colon).into(),
|
||||||
TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))).into(),
|
TokenTree::Token(sp(10, 13),
|
||||||
|
token::Ident(Ident::from_str("i32"))).into(),
|
||||||
]).into(),
|
]).into(),
|
||||||
}).into(),
|
}).into(),
|
||||||
TokenTree::Delimited(
|
TokenTree::Delimited(
|
||||||
|
@ -807,9 +818,11 @@ mod tests {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
assert_eq!(tts, expected);
|
assert_eq!(tts, expected);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn ret_expr() {
|
#[test] fn ret_expr() {
|
||||||
|
with_globals(|| {
|
||||||
assert!(string_to_expr("return d".to_string()) ==
|
assert!(string_to_expr("return d".to_string()) ==
|
||||||
P(ast::Expr{
|
P(ast::Expr{
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
@ -825,9 +838,11 @@ mod tests {
|
||||||
span:sp(0,8),
|
span:sp(0,8),
|
||||||
attrs: ThinVec::new(),
|
attrs: ThinVec::new(),
|
||||||
}))
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_stmt_1 () {
|
#[test] fn parse_stmt_1 () {
|
||||||
|
with_globals(|| {
|
||||||
assert!(string_to_stmt("b;".to_string()) ==
|
assert!(string_to_stmt("b;".to_string()) ==
|
||||||
Some(ast::Stmt {
|
Some(ast::Stmt {
|
||||||
node: ast::StmtKind::Expr(P(ast::Expr {
|
node: ast::StmtKind::Expr(P(ast::Expr {
|
||||||
|
@ -840,7 +855,7 @@ mod tests {
|
||||||
attrs: ThinVec::new()})),
|
attrs: ThinVec::new()})),
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
span: sp(0,1)}))
|
span: sp(0,1)}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parser_done(p: Parser){
|
fn parser_done(p: Parser){
|
||||||
|
@ -848,6 +863,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_ident_pat () {
|
#[test] fn parse_ident_pat () {
|
||||||
|
with_globals(|| {
|
||||||
let sess = ParseSess::new(FilePathMapping::empty());
|
let sess = ParseSess::new(FilePathMapping::empty());
|
||||||
let mut parser = string_to_parser(&sess, "b".to_string());
|
let mut parser = string_to_parser(&sess, "b".to_string());
|
||||||
assert!(panictry!(parser.parse_pat())
|
assert!(panictry!(parser.parse_pat())
|
||||||
|
@ -860,10 +876,12 @@ mod tests {
|
||||||
None),
|
None),
|
||||||
span: sp(0,1)}));
|
span: sp(0,1)}));
|
||||||
parser_done(parser);
|
parser_done(parser);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the contents of the tt manually:
|
// check the contents of the tt manually:
|
||||||
#[test] fn parse_fundecl () {
|
#[test] fn parse_fundecl () {
|
||||||
|
with_globals(|| {
|
||||||
// this test depends on the intern order of "fn" and "i32"
|
// this test depends on the intern order of "fn" and "i32"
|
||||||
let item = string_to_item("fn a (b : i32) { b; }".to_string()).map(|m| {
|
let item = string_to_item("fn a (b : i32) { b; }".to_string()).map(|m| {
|
||||||
m.map(|mut m| {
|
m.map(|mut m| {
|
||||||
|
@ -938,9 +956,11 @@ mod tests {
|
||||||
})),
|
})),
|
||||||
vis: respan(sp(0, 0), ast::VisibilityKind::Inherited),
|
vis: respan(sp(0, 0), ast::VisibilityKind::Inherited),
|
||||||
span: sp(0,21)})));
|
span: sp(0,21)})));
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_use() {
|
#[test] fn parse_use() {
|
||||||
|
with_globals(|| {
|
||||||
let use_s = "use foo::bar::baz;";
|
let use_s = "use foo::bar::baz;";
|
||||||
let vitem = string_to_item(use_s.to_string()).unwrap();
|
let vitem = string_to_item(use_s.to_string()).unwrap();
|
||||||
let vitem_s = item_to_string(&vitem);
|
let vitem_s = item_to_string(&vitem);
|
||||||
|
@ -950,9 +970,11 @@ mod tests {
|
||||||
let vitem = string_to_item(use_s.to_string()).unwrap();
|
let vitem = string_to_item(use_s.to_string()).unwrap();
|
||||||
let vitem_s = item_to_string(&vitem);
|
let vitem_s = item_to_string(&vitem);
|
||||||
assert_eq!(&vitem_s[..], use_s);
|
assert_eq!(&vitem_s[..], use_s);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_extern_crate() {
|
#[test] fn parse_extern_crate() {
|
||||||
|
with_globals(|| {
|
||||||
let ex_s = "extern crate foo;";
|
let ex_s = "extern crate foo;";
|
||||||
let vitem = string_to_item(ex_s.to_string()).unwrap();
|
let vitem = string_to_item(ex_s.to_string()).unwrap();
|
||||||
let vitem_s = item_to_string(&vitem);
|
let vitem_s = item_to_string(&vitem);
|
||||||
|
@ -962,6 +984,7 @@ mod tests {
|
||||||
let vitem = string_to_item(ex_s.to_string()).unwrap();
|
let vitem = string_to_item(ex_s.to_string()).unwrap();
|
||||||
let vitem_s = item_to_string(&vitem);
|
let vitem_s = item_to_string(&vitem);
|
||||||
assert_eq!(&vitem_s[..], ex_s);
|
assert_eq!(&vitem_s[..], ex_s);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
|
fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
|
||||||
|
@ -988,6 +1011,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn span_of_self_arg_pat_idents_are_correct() {
|
#[test] fn span_of_self_arg_pat_idents_are_correct() {
|
||||||
|
with_globals(|| {
|
||||||
|
|
||||||
let srcs = ["impl z { fn a (&self, &myarg: i32) {} }",
|
let srcs = ["impl z { fn a (&self, &myarg: i32) {} }",
|
||||||
"impl z { fn a (&mut self, &myarg: i32) {} }",
|
"impl z { fn a (&mut self, &myarg: i32) {} }",
|
||||||
|
@ -1003,15 +1027,19 @@ mod tests {
|
||||||
"\"{}\" != \"self\". src=\"{}\"",
|
"\"{}\" != \"self\". src=\"{}\"",
|
||||||
&src[lo.to_usize()..hi.to_usize()], src)
|
&src[lo.to_usize()..hi.to_usize()], src)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn parse_exprs () {
|
#[test] fn parse_exprs () {
|
||||||
|
with_globals(|| {
|
||||||
// just make sure that they parse....
|
// just make sure that they parse....
|
||||||
string_to_expr("3 + 4".to_string());
|
string_to_expr("3 + 4".to_string());
|
||||||
string_to_expr("a::z.froob(b,&(987+3))".to_string());
|
string_to_expr("a::z.froob(b,&(987+3))".to_string());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn attrs_fix_bug () {
|
#[test] fn attrs_fix_bug () {
|
||||||
|
with_globals(|| {
|
||||||
string_to_item("pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
|
string_to_item("pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
|
||||||
-> Result<Box<Writer>, String> {
|
-> Result<Box<Writer>, String> {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
|
@ -1024,9 +1052,11 @@ mod tests {
|
||||||
|
|
||||||
let mut fflags: c_int = wb();
|
let mut fflags: c_int = wb();
|
||||||
}".to_string());
|
}".to_string());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn crlf_doc_comments() {
|
#[test] fn crlf_doc_comments() {
|
||||||
|
with_globals(|| {
|
||||||
let sess = ParseSess::new(FilePathMapping::empty());
|
let sess = ParseSess::new(FilePathMapping::empty());
|
||||||
|
|
||||||
let name = FileName::Custom("source".to_string());
|
let name = FileName::Custom("source".to_string());
|
||||||
|
@ -1048,10 +1078,12 @@ mod tests {
|
||||||
let item = parse_item_from_source_str(name, source, &sess).unwrap().unwrap();
|
let item = parse_item_from_source_str(name, source, &sess).unwrap().unwrap();
|
||||||
let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
|
let doc = first_attr_value_str_by_name(&item.attrs, "doc").unwrap();
|
||||||
assert_eq!(doc, "/** doc comment\n * with CRLF */");
|
assert_eq!(doc, "/** doc comment\n * with CRLF */");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ttdelim_span() {
|
fn ttdelim_span() {
|
||||||
|
with_globals(|| {
|
||||||
let sess = ParseSess::new(FilePathMapping::empty());
|
let sess = ParseSess::new(FilePathMapping::empty());
|
||||||
let expr = parse::parse_expr_from_source_str(PathBuf::from("foo").into(),
|
let expr = parse::parse_expr_from_source_str(PathBuf::from("foo").into(),
|
||||||
"foo!( fn main() { body } )".to_string(), &sess).unwrap();
|
"foo!( fn main() { body } )".to_string(), &sess).unwrap();
|
||||||
|
@ -1067,6 +1099,7 @@ mod tests {
|
||||||
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
||||||
Err(_) => panic!("could not get snippet"),
|
Err(_) => panic!("could not get snippet"),
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This tests that when parsing a string (rather than a file) we don't try
|
// This tests that when parsing a string (rather than a file) we don't try
|
||||||
|
@ -1074,6 +1107,7 @@ mod tests {
|
||||||
// See `recurse_into_file_modules` in the parser.
|
// See `recurse_into_file_modules` in the parser.
|
||||||
#[test]
|
#[test]
|
||||||
fn out_of_line_mod() {
|
fn out_of_line_mod() {
|
||||||
|
with_globals(|| {
|
||||||
let sess = ParseSess::new(FilePathMapping::empty());
|
let sess = ParseSess::new(FilePathMapping::empty());
|
||||||
let item = parse_item_from_source_str(
|
let item = parse_item_from_source_str(
|
||||||
PathBuf::from("foo").into(),
|
PathBuf::from("foo").into(),
|
||||||
|
@ -1086,5 +1120,6 @@ mod tests {
|
||||||
} else {
|
} else {
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3178,9 +3178,11 @@ mod tests {
|
||||||
use ast;
|
use ast;
|
||||||
use codemap;
|
use codemap;
|
||||||
use syntax_pos;
|
use syntax_pos;
|
||||||
|
use with_globals;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fun_to_string() {
|
fn test_fun_to_string() {
|
||||||
|
with_globals(|| {
|
||||||
let abba_ident = ast::Ident::from_str("abba");
|
let abba_ident = ast::Ident::from_str("abba");
|
||||||
|
|
||||||
let decl = ast::FnDecl {
|
let decl = ast::FnDecl {
|
||||||
|
@ -3193,10 +3195,12 @@ mod tests {
|
||||||
ast::Constness::NotConst,
|
ast::Constness::NotConst,
|
||||||
abba_ident, &generics),
|
abba_ident, &generics),
|
||||||
"fn abba()");
|
"fn abba()");
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_variant_to_string() {
|
fn test_variant_to_string() {
|
||||||
|
with_globals(|| {
|
||||||
let ident = ast::Ident::from_str("principal_skinner");
|
let ident = ast::Ident::from_str("principal_skinner");
|
||||||
|
|
||||||
let var = codemap::respan(syntax_pos::DUMMY_SP, ast::Variant_ {
|
let var = codemap::respan(syntax_pos::DUMMY_SP, ast::Variant_ {
|
||||||
|
@ -3209,5 +3213,6 @@ mod tests {
|
||||||
|
|
||||||
let varstr = variant_to_string(&var);
|
let varstr = variant_to_string(&var);
|
||||||
assert_eq!(varstr, "principal_skinner");
|
assert_eq!(varstr, "principal_skinner");
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ use std::str;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use syntax_pos::{BytePos, NO_EXPANSION, Span, MultiSpan};
|
use syntax_pos::{BytePos, NO_EXPANSION, Span, MultiSpan};
|
||||||
|
use with_globals;
|
||||||
|
|
||||||
/// Identify a position in the text by the Nth occurrence of a string.
|
/// Identify a position in the text by the Nth occurrence of a string.
|
||||||
struct Position {
|
struct Position {
|
||||||
|
@ -46,6 +47,7 @@ impl<T: Write> Write for Shared<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
|
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
|
||||||
|
with_globals(|| {
|
||||||
let output = Arc::new(Mutex::new(Vec::new()));
|
let output = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
|
||||||
let code_map = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
let code_map = Lrc::new(CodeMap::new(FilePathMapping::empty()));
|
||||||
|
@ -77,6 +79,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
|
||||||
println!("actual output:\n------\n{}------", actual_output);
|
println!("actual output:\n------\n{}------", actual_output);
|
||||||
|
|
||||||
assert!(expected_output == actual_output)
|
assert!(expected_output == actual_output)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_span(file_text: &str, start: &Position, end: &Position) -> Span {
|
fn make_span(file_text: &str, start: &Position, end: &Position) -> Span {
|
||||||
|
|
|
@ -599,6 +599,7 @@ impl Hash for ThinTokenStream {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use syntax::ast::Ident;
|
use syntax::ast::Ident;
|
||||||
|
use with_globals;
|
||||||
use syntax_pos::{Span, BytePos, NO_EXPANSION};
|
use syntax_pos::{Span, BytePos, NO_EXPANSION};
|
||||||
use parse::token::Token;
|
use parse::token::Token;
|
||||||
use util::parser_testing::string_to_stream;
|
use util::parser_testing::string_to_stream;
|
||||||
|
@ -613,6 +614,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_concat() {
|
fn test_concat() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("foo::bar::baz");
|
let test_res = string_to_ts("foo::bar::baz");
|
||||||
let test_fst = string_to_ts("foo::bar");
|
let test_fst = string_to_ts("foo::bar");
|
||||||
let test_snd = string_to_ts("::baz");
|
let test_snd = string_to_ts("::baz");
|
||||||
|
@ -620,52 +622,66 @@ mod tests {
|
||||||
assert_eq!(test_res.trees().count(), 5);
|
assert_eq!(test_res.trees().count(), 5);
|
||||||
assert_eq!(eq_res.trees().count(), 5);
|
assert_eq!(eq_res.trees().count(), 5);
|
||||||
assert_eq!(test_res.eq_unspanned(&eq_res), true);
|
assert_eq!(test_res.eq_unspanned(&eq_res), true);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_to_from_bijection() {
|
fn test_to_from_bijection() {
|
||||||
|
with_globals(|| {
|
||||||
let test_start = string_to_ts("foo::bar(baz)");
|
let test_start = string_to_ts("foo::bar(baz)");
|
||||||
let test_end = test_start.trees().collect();
|
let test_end = test_start.trees().collect();
|
||||||
assert_eq!(test_start, test_end)
|
assert_eq!(test_start, test_end)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eq_0() {
|
fn test_eq_0() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("foo");
|
let test_res = string_to_ts("foo");
|
||||||
let test_eqs = string_to_ts("foo");
|
let test_eqs = string_to_ts("foo");
|
||||||
assert_eq!(test_res, test_eqs)
|
assert_eq!(test_res, test_eqs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eq_1() {
|
fn test_eq_1() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("::bar::baz");
|
let test_res = string_to_ts("::bar::baz");
|
||||||
let test_eqs = string_to_ts("::bar::baz");
|
let test_eqs = string_to_ts("::bar::baz");
|
||||||
assert_eq!(test_res, test_eqs)
|
assert_eq!(test_res, test_eqs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eq_3() {
|
fn test_eq_3() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("");
|
let test_res = string_to_ts("");
|
||||||
let test_eqs = string_to_ts("");
|
let test_eqs = string_to_ts("");
|
||||||
assert_eq!(test_res, test_eqs)
|
assert_eq!(test_res, test_eqs)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_diseq_0() {
|
fn test_diseq_0() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("::bar::baz");
|
let test_res = string_to_ts("::bar::baz");
|
||||||
let test_eqs = string_to_ts("bar::baz");
|
let test_eqs = string_to_ts("bar::baz");
|
||||||
assert_eq!(test_res == test_eqs, false)
|
assert_eq!(test_res == test_eqs, false)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_diseq_1() {
|
fn test_diseq_1() {
|
||||||
|
with_globals(|| {
|
||||||
let test_res = string_to_ts("(bar,baz)");
|
let test_res = string_to_ts("(bar,baz)");
|
||||||
let test_eqs = string_to_ts("bar,baz");
|
let test_eqs = string_to_ts("bar,baz");
|
||||||
assert_eq!(test_res == test_eqs, false)
|
assert_eq!(test_res == test_eqs, false)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_empty() {
|
fn test_is_empty() {
|
||||||
|
with_globals(|| {
|
||||||
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
|
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
|
||||||
let test1: TokenStream =
|
let test1: TokenStream =
|
||||||
TokenTree::Token(sp(0, 1), Token::Ident(Ident::from_str("a"))).into();
|
TokenTree::Token(sp(0, 1), Token::Ident(Ident::from_str("a"))).into();
|
||||||
|
@ -674,6 +690,7 @@ mod tests {
|
||||||
assert_eq!(test0.is_empty(), true);
|
assert_eq!(test0.is_empty(), true);
|
||||||
assert_eq!(test1.is_empty(), false);
|
assert_eq!(test1.is_empty(), false);
|
||||||
assert_eq!(test2.is_empty(), false);
|
assert_eq!(test2.is_empty(), false);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -11,4 +11,5 @@ crate-type = ["dylib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serialize = { path = "../libserialize" }
|
serialize = { path = "../libserialize" }
|
||||||
rustc_data_structures = { path = "../librustc_data_structures" }
|
rustc_data_structures = { path = "../librustc_data_structures" }
|
||||||
|
scoped-tls = { version = "0.1.1", features = ["nightly"] }
|
||||||
unicode-width = "0.1.4"
|
unicode-width = "0.1.4"
|
||||||
|
|
|
@ -15,11 +15,11 @@
|
||||||
//! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
|
//! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
|
||||||
//! DOI=10.1017/S0956796812000093 <http://dx.doi.org/10.1017/S0956796812000093>
|
//! DOI=10.1017/S0956796812000093 <http://dx.doi.org/10.1017/S0956796812000093>
|
||||||
|
|
||||||
|
use GLOBALS;
|
||||||
use Span;
|
use Span;
|
||||||
use symbol::{Ident, Symbol};
|
use symbol::{Ident, Symbol};
|
||||||
|
|
||||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ impl Mark {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HygieneData {
|
pub struct HygieneData {
|
||||||
marks: Vec<MarkData>,
|
marks: Vec<MarkData>,
|
||||||
syntax_contexts: Vec<SyntaxContextData>,
|
syntax_contexts: Vec<SyntaxContextData>,
|
||||||
markings: HashMap<(SyntaxContext, Mark), SyntaxContext>,
|
markings: HashMap<(SyntaxContext, Mark), SyntaxContext>,
|
||||||
|
@ -127,7 +127,7 @@ struct HygieneData {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HygieneData {
|
impl HygieneData {
|
||||||
fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
HygieneData {
|
HygieneData {
|
||||||
marks: vec![MarkData {
|
marks: vec![MarkData {
|
||||||
parent: Mark::root(),
|
parent: Mark::root(),
|
||||||
|
@ -145,10 +145,7 @@ impl HygieneData {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
|
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
|
||||||
thread_local! {
|
GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
|
||||||
static HYGIENE_DATA: RefCell<HygieneData> = RefCell::new(HygieneData::new());
|
|
||||||
}
|
|
||||||
HYGIENE_DATA.with(|data| f(&mut *data.borrow_mut()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,10 +35,13 @@ use std::ops::{Add, Sub};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use rustc_data_structures::stable_hasher::StableHasher;
|
use rustc_data_structures::stable_hasher::StableHasher;
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::{Lrc, Lock};
|
||||||
|
|
||||||
extern crate rustc_data_structures;
|
extern crate rustc_data_structures;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate scoped_tls;
|
||||||
|
|
||||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||||
|
|
||||||
extern crate serialize;
|
extern crate serialize;
|
||||||
|
@ -54,6 +57,24 @@ pub use span_encoding::{Span, DUMMY_SP};
|
||||||
|
|
||||||
pub mod symbol;
|
pub mod symbol;
|
||||||
|
|
||||||
|
pub struct Globals {
|
||||||
|
symbol_interner: Lock<symbol::Interner>,
|
||||||
|
span_interner: Lock<span_encoding::SpanInterner>,
|
||||||
|
hygiene_data: Lock<hygiene::HygieneData>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Globals {
|
||||||
|
pub fn new() -> Globals {
|
||||||
|
Globals {
|
||||||
|
symbol_interner: Lock::new(symbol::Interner::fresh()),
|
||||||
|
span_interner: Lock::new(span_encoding::SpanInterner::default()),
|
||||||
|
hygiene_data: Lock::new(hygiene::HygieneData::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scoped_thread_local!(pub static GLOBALS: Globals);
|
||||||
|
|
||||||
/// Differentiates between real files and common virtual files
|
/// Differentiates between real files and common virtual files
|
||||||
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, RustcDecodable, RustcEncodable)]
|
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, RustcDecodable, RustcEncodable)]
|
||||||
pub enum FileName {
|
pub enum FileName {
|
||||||
|
|
|
@ -14,11 +14,11 @@
|
||||||
// The encoding format for inline spans were obtained by optimizing over crates in rustc/libstd.
|
// The encoding format for inline spans were obtained by optimizing over crates in rustc/libstd.
|
||||||
// See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28
|
// See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28
|
||||||
|
|
||||||
|
use GLOBALS;
|
||||||
use {BytePos, SpanData};
|
use {BytePos, SpanData};
|
||||||
use hygiene::SyntaxContext;
|
use hygiene::SyntaxContext;
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
/// A compressed span.
|
/// A compressed span.
|
||||||
|
@ -133,7 +133,7 @@ fn decode(span: Span) -> SpanData {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct SpanInterner {
|
pub struct SpanInterner {
|
||||||
spans: FxHashMap<SpanData, u32>,
|
spans: FxHashMap<SpanData, u32>,
|
||||||
span_data: Vec<SpanData>,
|
span_data: Vec<SpanData>,
|
||||||
}
|
}
|
||||||
|
@ -156,11 +156,8 @@ impl SpanInterner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.
|
// If an interner exists, return it. Otherwise, prepare a fresh one.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T {
|
fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T {
|
||||||
thread_local!(static INTERNER: RefCell<SpanInterner> = {
|
GLOBALS.with(|globals| f(&mut *globals.span_interner.lock()))
|
||||||
RefCell::new(SpanInterner::default())
|
|
||||||
});
|
|
||||||
INTERNER.with(|interner| f(&mut *interner.borrow_mut()))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@
|
||||||
//! type, and vice versa.
|
//! type, and vice versa.
|
||||||
|
|
||||||
use hygiene::SyntaxContext;
|
use hygiene::SyntaxContext;
|
||||||
|
use GLOBALS;
|
||||||
|
|
||||||
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
@ -247,7 +247,7 @@ macro_rules! declare_keywords {(
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Interner {
|
impl Interner {
|
||||||
fn fresh() -> Self {
|
pub fn fresh() -> Self {
|
||||||
Interner::prefill(&[$($string,)*])
|
Interner::prefill(&[$($string,)*])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -330,12 +330,10 @@ declare_keywords! {
|
||||||
(60, Union, "union")
|
(60, Union, "union")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If an interner exists in TLS, return it. Otherwise, prepare a fresh one.
|
// If an interner exists, return it. Otherwise, prepare a fresh one.
|
||||||
|
#[inline]
|
||||||
fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
|
fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
|
||||||
thread_local!(static INTERNER: RefCell<Interner> = {
|
GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock()))
|
||||||
RefCell::new(Interner::fresh())
|
|
||||||
});
|
|
||||||
INTERNER.with(|interner| f(&mut *interner.borrow_mut()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a string stored in the thread-local interner. Because the
|
/// Represents a string stored in the thread-local interner. Because the
|
||||||
|
@ -422,6 +420,7 @@ impl Encodable for InternedString {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use Globals;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn interner_tests() {
|
fn interner_tests() {
|
||||||
|
@ -444,7 +443,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn without_first_quote_test() {
|
fn without_first_quote_test() {
|
||||||
|
GLOBALS.set(&Globals::new(), || {
|
||||||
let i = Ident::from_str("'break");
|
let i = Ident::from_str("'break");
|
||||||
assert_eq!(i.without_first_quote().name, keywords::Break.name());
|
assert_eq!(i.without_first_quote().name, keywords::Break.name());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,10 @@ use syntax::symbol::Symbol;
|
||||||
use syntax_pos::DUMMY_SP;
|
use syntax_pos::DUMMY_SP;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
syntax::with_globals(|| run());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() {
|
||||||
let ps = syntax::parse::ParseSess::new(codemap::FilePathMapping::empty());
|
let ps = syntax::parse::ParseSess::new(codemap::FilePathMapping::empty());
|
||||||
let mut resolver = syntax::ext::base::DummyResolver;
|
let mut resolver = syntax::ext::base::DummyResolver;
|
||||||
let mut cx = syntax::ext::base::ExtCtxt::new(
|
let mut cx = syntax::ext::base::ExtCtxt::new(
|
||||||
|
|
|
@ -69,6 +69,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc<CStore>, Box<TransCrate>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
||||||
|
syntax::with_globals(|| {
|
||||||
let (sess, cstore, trans) = basic_sess(sysroot);
|
let (sess, cstore, trans) = basic_sess(sysroot);
|
||||||
let control = CompileController::basic();
|
let control = CompileController::basic();
|
||||||
let input = Input::Str { name: FileName::Anon, input: code };
|
let input = Input::Str { name: FileName::Anon, input: code };
|
||||||
|
@ -83,4 +84,5 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
||||||
None,
|
None,
|
||||||
&control
|
&control
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,6 +115,10 @@ fn reject_stmt_parse(es: &str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
syntax::with_globals(|| run());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() {
|
||||||
let both = &["#[attr]", "#![attr]"];
|
let both = &["#[attr]", "#![attr]"];
|
||||||
let outer = &["#[attr]"];
|
let outer = &["#[attr]"];
|
||||||
let none = &[];
|
let none = &[];
|
||||||
|
|
|
@ -27,6 +27,10 @@ use syntax::ptr::P;
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
syntax::with_globals(|| run());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() {
|
||||||
let parse_sess = ParseSess::new(FilePathMapping::empty());
|
let parse_sess = ParseSess::new(FilePathMapping::empty());
|
||||||
let exp_cfg = ExpansionConfig::default("issue_35829".to_owned());
|
let exp_cfg = ExpansionConfig::default("issue_35829".to_owned());
|
||||||
let mut resolver = DummyResolver;
|
let mut resolver = DummyResolver;
|
||||||
|
|
|
@ -221,8 +221,11 @@ impl Folder for AddParens {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
syntax::with_globals(|| run());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() {
|
||||||
let ps = ParseSess::new(FilePathMapping::empty());
|
let ps = ParseSess::new(FilePathMapping::empty());
|
||||||
|
|
||||||
iter_exprs(2, &mut |e| {
|
iter_exprs(2, &mut |e| {
|
||||||
|
|
|
@ -21,6 +21,10 @@ use syntax::symbol::Symbol;
|
||||||
use syntax_pos::DUMMY_SP;
|
use syntax_pos::DUMMY_SP;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
syntax::with_globals(|| run());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() {
|
||||||
let ps = syntax::parse::ParseSess::new(FilePathMapping::empty());
|
let ps = syntax::parse::ParseSess::new(FilePathMapping::empty());
|
||||||
let mut resolver = syntax::ext::base::DummyResolver;
|
let mut resolver = syntax::ext::base::DummyResolver;
|
||||||
let mut cx = syntax::ext::base::ExtCtxt::new(
|
let mut cx = syntax::ext::base::ExtCtxt::new(
|
||||||
|
|
|
@ -263,7 +263,10 @@ fn main() {
|
||||||
*slot.borrow_mut() = Some((None, String::from("https://play.rust-lang.org/")));
|
*slot.borrow_mut() = Some((None, String::from("https://play.rust-lang.org/")));
|
||||||
});
|
});
|
||||||
let (format, dst) = parse_args();
|
let (format, dst) = parse_args();
|
||||||
if let Err(e) = main_with_result(format, &dst) {
|
let result = syntax::with_globals(move || {
|
||||||
|
main_with_result(format, &dst)
|
||||||
|
});
|
||||||
|
if let Err(e) = result {
|
||||||
panic!("{}", e.description());
|
panic!("{}", e.description());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,7 @@ static WHITELIST: &'static [Crate] = &[
|
||||||
Crate("regex-syntax"),
|
Crate("regex-syntax"),
|
||||||
Crate("remove_dir_all"),
|
Crate("remove_dir_all"),
|
||||||
Crate("rustc-demangle"),
|
Crate("rustc-demangle"),
|
||||||
|
Crate("scoped-tls"),
|
||||||
Crate("smallvec"),
|
Crate("smallvec"),
|
||||||
Crate("stable_deref_trait"),
|
Crate("stable_deref_trait"),
|
||||||
Crate("tempdir"),
|
Crate("tempdir"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue