3 use crate::json
::JsonEmitter
;
4 use rustc_span
::source_map
::{FilePathMapping, SourceMap}
;
6 use crate::emitter
::{ColorConfig, HumanReadableErrorType}
;
8 use rustc_serialize
::json
::decode
;
9 use rustc_span
::{BytePos, Span}
;
13 #[derive(Decodable, Debug, PartialEq, Eq)]
15 spans
: Vec
<SpanTestData
>,
18 #[derive(Decodable, Debug, PartialEq, Eq)]
23 pub column_start
: u32,
32 impl<T
: Write
> Write
for Shared
<T
> {
33 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
34 self.data
.lock().unwrap().write(buf
)
37 fn flush(&mut self) -> io
::Result
<()> {
38 self.data
.lock().unwrap().flush()
42 fn with_default_session_globals(f
: impl FnOnce()) {
43 let session_globals
= rustc_span
::SessionGlobals
::new(rustc_span
::edition
::DEFAULT_EDITION
);
44 rustc_span
::SESSION_GLOBALS
.set(&session_globals
, f
);
47 /// Test the span yields correct positions in JSON.
48 fn test_positions(code
: &str, span
: (u32, u32), expected_output
: SpanTestData
) {
49 let expected_output
= TestData { spans: vec![expected_output] }
;
51 with_default_session_globals(|| {
52 let sm
= Lrc
::new(SourceMap
::new(FilePathMapping
::empty()));
53 sm
.new_source_file(Path
::new("test.rs").to_owned().into(), code
.to_owned());
55 let output
= Arc
::new(Mutex
::new(Vec
::new()));
56 let je
= JsonEmitter
::new(
57 Box
::new(Shared { data: output.clone() }
),
61 HumanReadableErrorType
::Short(ColorConfig
::Never
),
66 let span
= Span
::with_root_ctxt(BytePos(span
.0), BytePos(span
.1));
67 let handler
= Handler
::with_emitter(true, None
, Box
::new(je
));
68 handler
.span_err(span
, "foo");
70 let bytes
= output
.lock().unwrap();
71 let actual_output
= str::from_utf8(&bytes
).unwrap();
72 let actual_output
: TestData
= decode(actual_output
).unwrap();
74 assert_eq
!(expected_output
, actual_output
)
113 "\nmod foo;\nmod bar;\n",
129 "\r\nmod foo;\r\nmod bar;\r\n",
143 fn crlf_newlines_with_bom() {
145 "\u{feff}\r\nmod foo;\r\nmod bar;\r\n",
159 fn span_before_crlf() {
191 fn span_after_crlf() {