Rename directories for some crates from syntax_x to rustc_x
`syntax_expand` -> `rustc_expand` `syntax_pos` -> `rustc_span` `syntax_ext` -> `rustc_builtin_macros`
This commit is contained in:
parent
0fb4380136
commit
b683de4ad7
71 changed files with 0 additions and 0 deletions
21
src/librustc_span/Cargo.toml
Normal file
21
src/librustc_span/Cargo.toml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
[package]
|
||||
authors = ["The Rust Project Developers"]
|
||||
name = "syntax_pos"
|
||||
version = "0.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "syntax_pos"
|
||||
path = "lib.rs"
|
||||
doctest = false
|
||||
|
||||
[dependencies]
|
||||
rustc_serialize = { path = "../libserialize", package = "serialize" }
|
||||
rustc_macros = { path = "../librustc_macros" }
|
||||
rustc_data_structures = { path = "../librustc_data_structures" }
|
||||
rustc_index = { path = "../librustc_index" }
|
||||
arena = { path = "../libarena" }
|
||||
scoped-tls = "1.0"
|
||||
unicode-width = "0.1.4"
|
||||
cfg-if = "0.1.2"
|
||||
log = "0.4"
|
||||
274
src/librustc_span/analyze_source_file.rs
Normal file
274
src/librustc_span/analyze_source_file.rs
Normal file
|
|
@ -0,0 +1,274 @@
|
|||
use super::*;
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Finds all newlines, multi-byte characters, and non-narrow characters in a
|
||||
/// SourceFile.
|
||||
///
|
||||
/// This function will use an SSE2 enhanced implementation if hardware support
|
||||
/// is detected at runtime.
|
||||
pub fn analyze_source_file(
|
||||
src: &str,
|
||||
source_file_start_pos: BytePos,
|
||||
) -> (Vec<BytePos>, Vec<MultiByteChar>, Vec<NonNarrowChar>) {
|
||||
let mut lines = vec![source_file_start_pos];
|
||||
let mut multi_byte_chars = vec![];
|
||||
let mut non_narrow_chars = vec![];
|
||||
|
||||
// Calls the right implementation, depending on hardware support available.
|
||||
analyze_source_file_dispatch(
|
||||
src,
|
||||
source_file_start_pos,
|
||||
&mut lines,
|
||||
&mut multi_byte_chars,
|
||||
&mut non_narrow_chars,
|
||||
);
|
||||
|
||||
// The code above optimistically registers a new line *after* each \n
|
||||
// it encounters. If that point is already outside the source_file, remove
|
||||
// it again.
|
||||
if let Some(&last_line_start) = lines.last() {
|
||||
let source_file_end = source_file_start_pos + BytePos::from_usize(src.len());
|
||||
assert!(source_file_end >= last_line_start);
|
||||
if last_line_start == source_file_end {
|
||||
lines.pop();
|
||||
}
|
||||
}
|
||||
|
||||
(lines, multi_byte_chars, non_narrow_chars)
|
||||
}
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(all(any(target_arch = "x86", target_arch = "x86_64")))] {
|
||||
fn analyze_source_file_dispatch(src: &str,
|
||||
source_file_start_pos: BytePos,
|
||||
lines: &mut Vec<BytePos>,
|
||||
multi_byte_chars: &mut Vec<MultiByteChar>,
|
||||
non_narrow_chars: &mut Vec<NonNarrowChar>) {
|
||||
if is_x86_feature_detected!("sse2") {
|
||||
unsafe {
|
||||
analyze_source_file_sse2(src,
|
||||
source_file_start_pos,
|
||||
lines,
|
||||
multi_byte_chars,
|
||||
non_narrow_chars);
|
||||
}
|
||||
} else {
|
||||
analyze_source_file_generic(src,
|
||||
src.len(),
|
||||
source_file_start_pos,
|
||||
lines,
|
||||
multi_byte_chars,
|
||||
non_narrow_chars);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks 16 byte chunks of text at a time. If the chunk contains
|
||||
/// something other than printable ASCII characters and newlines, the
|
||||
/// function falls back to the generic implementation. Otherwise it uses
|
||||
/// SSE2 intrinsics to quickly find all newlines.
|
||||
#[target_feature(enable = "sse2")]
|
||||
unsafe fn analyze_source_file_sse2(src: &str,
|
||||
output_offset: BytePos,
|
||||
lines: &mut Vec<BytePos>,
|
||||
multi_byte_chars: &mut Vec<MultiByteChar>,
|
||||
non_narrow_chars: &mut Vec<NonNarrowChar>) {
|
||||
#[cfg(target_arch = "x86")]
|
||||
use std::arch::x86::*;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use std::arch::x86_64::*;
|
||||
|
||||
const CHUNK_SIZE: usize = 16;
|
||||
|
||||
let src_bytes = src.as_bytes();
|
||||
|
||||
let chunk_count = src.len() / CHUNK_SIZE;
|
||||
|
||||
// This variable keeps track of where we should start decoding a
|
||||
// chunk. If a multi-byte character spans across chunk boundaries,
|
||||
// we need to skip that part in the next chunk because we already
|
||||
// handled it.
|
||||
let mut intra_chunk_offset = 0;
|
||||
|
||||
for chunk_index in 0 .. chunk_count {
|
||||
let ptr = src_bytes.as_ptr() as *const __m128i;
|
||||
// We don't know if the pointer is aligned to 16 bytes, so we
|
||||
// use `loadu`, which supports unaligned loading.
|
||||
let chunk = _mm_loadu_si128(ptr.offset(chunk_index as isize));
|
||||
|
||||
// For character in the chunk, see if its byte value is < 0, which
|
||||
// indicates that it's part of a UTF-8 char.
|
||||
let multibyte_test = _mm_cmplt_epi8(chunk, _mm_set1_epi8(0));
|
||||
// Create a bit mask from the comparison results.
|
||||
let multibyte_mask = _mm_movemask_epi8(multibyte_test);
|
||||
|
||||
// If the bit mask is all zero, we only have ASCII chars here:
|
||||
if multibyte_mask == 0 {
|
||||
assert!(intra_chunk_offset == 0);
|
||||
|
||||
// Check if there are any control characters in the chunk. All
|
||||
// control characters that we can encounter at this point have a
|
||||
// byte value less than 32 or ...
|
||||
let control_char_test0 = _mm_cmplt_epi8(chunk, _mm_set1_epi8(32));
|
||||
let control_char_mask0 = _mm_movemask_epi8(control_char_test0);
|
||||
|
||||
// ... it's the ASCII 'DEL' character with a value of 127.
|
||||
let control_char_test1 = _mm_cmpeq_epi8(chunk, _mm_set1_epi8(127));
|
||||
let control_char_mask1 = _mm_movemask_epi8(control_char_test1);
|
||||
|
||||
let control_char_mask = control_char_mask0 | control_char_mask1;
|
||||
|
||||
if control_char_mask != 0 {
|
||||
// Check for newlines in the chunk
|
||||
let newlines_test = _mm_cmpeq_epi8(chunk, _mm_set1_epi8(b'\n' as i8));
|
||||
let newlines_mask = _mm_movemask_epi8(newlines_test);
|
||||
|
||||
if control_char_mask == newlines_mask {
|
||||
// All control characters are newlines, record them
|
||||
let mut newlines_mask = 0xFFFF0000 | newlines_mask as u32;
|
||||
let output_offset = output_offset +
|
||||
BytePos::from_usize(chunk_index * CHUNK_SIZE + 1);
|
||||
|
||||
loop {
|
||||
let index = newlines_mask.trailing_zeros();
|
||||
|
||||
if index >= CHUNK_SIZE as u32 {
|
||||
// We have arrived at the end of the chunk.
|
||||
break
|
||||
}
|
||||
|
||||
lines.push(BytePos(index) + output_offset);
|
||||
|
||||
// Clear the bit, so we can find the next one.
|
||||
newlines_mask &= (!1) << index;
|
||||
}
|
||||
|
||||
// We are done for this chunk. All control characters were
|
||||
// newlines and we took care of those.
|
||||
continue
|
||||
} else {
|
||||
// Some of the control characters are not newlines,
|
||||
// fall through to the slow path below.
|
||||
}
|
||||
} else {
|
||||
// No control characters, nothing to record for this chunk
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// The slow path.
|
||||
// There are control chars in here, fallback to generic decoding.
|
||||
let scan_start = chunk_index * CHUNK_SIZE + intra_chunk_offset;
|
||||
intra_chunk_offset = analyze_source_file_generic(
|
||||
&src[scan_start .. ],
|
||||
CHUNK_SIZE - intra_chunk_offset,
|
||||
BytePos::from_usize(scan_start) + output_offset,
|
||||
lines,
|
||||
multi_byte_chars,
|
||||
non_narrow_chars
|
||||
);
|
||||
}
|
||||
|
||||
// There might still be a tail left to analyze
|
||||
let tail_start = chunk_count * CHUNK_SIZE + intra_chunk_offset;
|
||||
if tail_start < src.len() {
|
||||
analyze_source_file_generic(&src[tail_start as usize ..],
|
||||
src.len() - tail_start,
|
||||
output_offset + BytePos::from_usize(tail_start),
|
||||
lines,
|
||||
multi_byte_chars,
|
||||
non_narrow_chars);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
// The target (or compiler version) does not support SSE2 ...
|
||||
fn analyze_source_file_dispatch(src: &str,
|
||||
source_file_start_pos: BytePos,
|
||||
lines: &mut Vec<BytePos>,
|
||||
multi_byte_chars: &mut Vec<MultiByteChar>,
|
||||
non_narrow_chars: &mut Vec<NonNarrowChar>) {
|
||||
analyze_source_file_generic(src,
|
||||
src.len(),
|
||||
source_file_start_pos,
|
||||
lines,
|
||||
multi_byte_chars,
|
||||
non_narrow_chars);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// `scan_len` determines the number of bytes in `src` to scan. Note that the
|
||||
// function can read past `scan_len` if a multi-byte character start within the
|
||||
// range but extends past it. The overflow is returned by the function.
|
||||
fn analyze_source_file_generic(
|
||||
src: &str,
|
||||
scan_len: usize,
|
||||
output_offset: BytePos,
|
||||
lines: &mut Vec<BytePos>,
|
||||
multi_byte_chars: &mut Vec<MultiByteChar>,
|
||||
non_narrow_chars: &mut Vec<NonNarrowChar>,
|
||||
) -> usize {
|
||||
assert!(src.len() >= scan_len);
|
||||
let mut i = 0;
|
||||
let src_bytes = src.as_bytes();
|
||||
|
||||
while i < scan_len {
|
||||
let byte = unsafe {
|
||||
// We verified that i < scan_len <= src.len()
|
||||
*src_bytes.get_unchecked(i as usize)
|
||||
};
|
||||
|
||||
// How much to advance in order to get to the next UTF-8 char in the
|
||||
// string.
|
||||
let mut char_len = 1;
|
||||
|
||||
if byte < 32 {
|
||||
// This is an ASCII control character, it could be one of the cases
|
||||
// that are interesting to us.
|
||||
|
||||
let pos = BytePos::from_usize(i) + output_offset;
|
||||
|
||||
match byte {
|
||||
b'\n' => {
|
||||
lines.push(pos + BytePos(1));
|
||||
}
|
||||
b'\t' => {
|
||||
non_narrow_chars.push(NonNarrowChar::Tab(pos));
|
||||
}
|
||||
_ => {
|
||||
non_narrow_chars.push(NonNarrowChar::ZeroWidth(pos));
|
||||
}
|
||||
}
|
||||
} else if byte >= 127 {
|
||||
// The slow path:
|
||||
// This is either ASCII control character "DEL" or the beginning of
|
||||
// a multibyte char. Just decode to `char`.
|
||||
let c = (&src[i..]).chars().next().unwrap();
|
||||
char_len = c.len_utf8();
|
||||
|
||||
let pos = BytePos::from_usize(i) + output_offset;
|
||||
|
||||
if char_len > 1 {
|
||||
assert!(char_len >= 2 && char_len <= 4);
|
||||
let mbc = MultiByteChar { pos, bytes: char_len as u8 };
|
||||
multi_byte_chars.push(mbc);
|
||||
}
|
||||
|
||||
// Assume control characters are zero width.
|
||||
// FIXME: How can we decide between `width` and `width_cjk`?
|
||||
let char_width = UnicodeWidthChar::width(c).unwrap_or(0);
|
||||
|
||||
if char_width != 1 {
|
||||
non_narrow_chars.push(NonNarrowChar::new(pos, char_width));
|
||||
}
|
||||
}
|
||||
|
||||
i += char_len;
|
||||
}
|
||||
|
||||
i - scan_len
|
||||
}
|
||||
142
src/librustc_span/analyze_source_file/tests.rs
Normal file
142
src/librustc_span/analyze_source_file/tests.rs
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
use super::*;
|
||||
|
||||
macro_rules! test {
|
||||
(case: $test_name:ident,
|
||||
text: $text:expr,
|
||||
source_file_start_pos: $source_file_start_pos:expr,
|
||||
lines: $lines:expr,
|
||||
multi_byte_chars: $multi_byte_chars:expr,
|
||||
non_narrow_chars: $non_narrow_chars:expr,) => {
|
||||
#[test]
|
||||
fn $test_name() {
|
||||
let (lines, multi_byte_chars, non_narrow_chars) =
|
||||
analyze_source_file($text, BytePos($source_file_start_pos));
|
||||
|
||||
let expected_lines: Vec<BytePos> = $lines.into_iter().map(|pos| BytePos(pos)).collect();
|
||||
|
||||
assert_eq!(lines, expected_lines);
|
||||
|
||||
let expected_mbcs: Vec<MultiByteChar> = $multi_byte_chars
|
||||
.into_iter()
|
||||
.map(|(pos, bytes)| MultiByteChar { pos: BytePos(pos), bytes })
|
||||
.collect();
|
||||
|
||||
assert_eq!(multi_byte_chars, expected_mbcs);
|
||||
|
||||
let expected_nncs: Vec<NonNarrowChar> = $non_narrow_chars
|
||||
.into_iter()
|
||||
.map(|(pos, width)| NonNarrowChar::new(BytePos(pos), width))
|
||||
.collect();
|
||||
|
||||
assert_eq!(non_narrow_chars, expected_nncs);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
test!(
|
||||
case: empty_text,
|
||||
text: "",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: newlines_short,
|
||||
text: "a\nc",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0, 2],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: newlines_long,
|
||||
text: "012345678\nabcdef012345678\na",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0, 10, 26],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: newline_and_multi_byte_char_in_same_chunk,
|
||||
text: "01234β789\nbcdef0123456789abcdef",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0, 11],
|
||||
multi_byte_chars: vec![(5, 2)],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: newline_and_control_char_in_same_chunk,
|
||||
text: "01234\u{07}6789\nbcdef0123456789abcdef",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0, 11],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![(5, 0)],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: multi_byte_char_short,
|
||||
text: "aβc",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![(1, 2)],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: multi_byte_char_long,
|
||||
text: "0123456789abcΔf012345β",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![(13, 2), (22, 2)],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: multi_byte_char_across_chunk_boundary,
|
||||
text: "0123456789abcdeΔ123456789abcdef01234",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![(15, 2)],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: multi_byte_char_across_chunk_boundary_tail,
|
||||
text: "0123456789abcdeΔ....",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![(15, 2)],
|
||||
non_narrow_chars: vec![],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: non_narrow_short,
|
||||
text: "0\t2",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![(1, 4)],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: non_narrow_long,
|
||||
text: "01\t3456789abcdef01234567\u{07}9",
|
||||
source_file_start_pos: 0,
|
||||
lines: vec![0],
|
||||
multi_byte_chars: vec![],
|
||||
non_narrow_chars: vec![(2, 4), (24, 0)],
|
||||
);
|
||||
|
||||
test!(
|
||||
case: output_offset_all,
|
||||
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
|
||||
source_file_start_pos: 1000,
|
||||
lines: vec![0 + 1000, 7 + 1000, 27 + 1000],
|
||||
multi_byte_chars: vec![(13 + 1000, 2), (29 + 1000, 2)],
|
||||
non_narrow_chars: vec![(2 + 1000, 4), (24 + 1000, 0)],
|
||||
);
|
||||
108
src/librustc_span/caching_source_map_view.rs
Normal file
108
src/librustc_span/caching_source_map_view.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
use crate::source_map::SourceMap;
|
||||
use crate::{BytePos, SourceFile};
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct CacheEntry {
|
||||
time_stamp: usize,
|
||||
line_number: usize,
|
||||
line_start: BytePos,
|
||||
line_end: BytePos,
|
||||
file: Lrc<SourceFile>,
|
||||
file_index: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CachingSourceMapView<'cm> {
|
||||
source_map: &'cm SourceMap,
|
||||
line_cache: [CacheEntry; 3],
|
||||
time_stamp: usize,
|
||||
}
|
||||
|
||||
impl<'cm> CachingSourceMapView<'cm> {
|
||||
pub fn new(source_map: &'cm SourceMap) -> CachingSourceMapView<'cm> {
|
||||
let files = source_map.files();
|
||||
let first_file = files[0].clone();
|
||||
let entry = CacheEntry {
|
||||
time_stamp: 0,
|
||||
line_number: 0,
|
||||
line_start: BytePos(0),
|
||||
line_end: BytePos(0),
|
||||
file: first_file,
|
||||
file_index: 0,
|
||||
};
|
||||
|
||||
CachingSourceMapView {
|
||||
source_map,
|
||||
line_cache: [entry.clone(), entry.clone(), entry],
|
||||
time_stamp: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn byte_pos_to_line_and_col(
|
||||
&mut self,
|
||||
pos: BytePos,
|
||||
) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
|
||||
self.time_stamp += 1;
|
||||
|
||||
// Check if the position is in one of the cached lines
|
||||
for cache_entry in self.line_cache.iter_mut() {
|
||||
if pos >= cache_entry.line_start && pos < cache_entry.line_end {
|
||||
cache_entry.time_stamp = self.time_stamp;
|
||||
|
||||
return Some((
|
||||
cache_entry.file.clone(),
|
||||
cache_entry.line_number,
|
||||
pos - cache_entry.line_start,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// No cache hit ...
|
||||
let mut oldest = 0;
|
||||
for index in 1..self.line_cache.len() {
|
||||
if self.line_cache[index].time_stamp < self.line_cache[oldest].time_stamp {
|
||||
oldest = index;
|
||||
}
|
||||
}
|
||||
|
||||
let cache_entry = &mut self.line_cache[oldest];
|
||||
|
||||
// If the entry doesn't point to the correct file, fix it up
|
||||
if pos < cache_entry.file.start_pos || pos >= cache_entry.file.end_pos {
|
||||
let file_valid;
|
||||
if self.source_map.files().len() > 0 {
|
||||
let file_index = self.source_map.lookup_source_file_idx(pos);
|
||||
let file = self.source_map.files()[file_index].clone();
|
||||
|
||||
if pos >= file.start_pos && pos < file.end_pos {
|
||||
cache_entry.file = file;
|
||||
cache_entry.file_index = file_index;
|
||||
file_valid = true;
|
||||
} else {
|
||||
file_valid = false;
|
||||
}
|
||||
} else {
|
||||
file_valid = false;
|
||||
}
|
||||
|
||||
if !file_valid {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let line_index = cache_entry.file.lookup_line(pos).unwrap();
|
||||
let line_bounds = cache_entry.file.line_bounds(line_index);
|
||||
|
||||
cache_entry.line_number = line_index + 1;
|
||||
cache_entry.line_start = line_bounds.0;
|
||||
cache_entry.line_end = line_bounds.1;
|
||||
cache_entry.time_stamp = self.time_stamp;
|
||||
|
||||
return Some((
|
||||
cache_entry.file.clone(),
|
||||
cache_entry.line_number,
|
||||
pos - cache_entry.line_start,
|
||||
));
|
||||
}
|
||||
}
|
||||
83
src/librustc_span/edition.rs
Normal file
83
src/librustc_span/edition.rs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
use crate::symbol::{sym, Symbol};
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use rustc_macros::HashStable_Generic;
|
||||
|
||||
/// The edition of the compiler (RFC 2052)
|
||||
#[derive(
|
||||
Clone,
|
||||
Copy,
|
||||
Hash,
|
||||
PartialEq,
|
||||
PartialOrd,
|
||||
Debug,
|
||||
RustcEncodable,
|
||||
RustcDecodable,
|
||||
Eq,
|
||||
HashStable_Generic
|
||||
)]
|
||||
pub enum Edition {
|
||||
// editions must be kept in order, oldest to newest
|
||||
/// The 2015 edition
|
||||
Edition2015,
|
||||
/// The 2018 edition
|
||||
Edition2018,
|
||||
// when adding new editions, be sure to update:
|
||||
//
|
||||
// - Update the `ALL_EDITIONS` const
|
||||
// - Update the EDITION_NAME_LIST const
|
||||
// - add a `rust_####()` function to the session
|
||||
// - update the enum in Cargo's sources as well
|
||||
}
|
||||
|
||||
// must be in order from oldest to newest
|
||||
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
|
||||
|
||||
pub const EDITION_NAME_LIST: &str = "2015|2018";
|
||||
|
||||
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
|
||||
|
||||
impl fmt::Display for Edition {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let s = match *self {
|
||||
Edition::Edition2015 => "2015",
|
||||
Edition::Edition2018 => "2018",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Edition {
|
||||
pub fn lint_name(&self) -> &'static str {
|
||||
match *self {
|
||||
Edition::Edition2015 => "rust_2015_compatibility",
|
||||
Edition::Edition2018 => "rust_2018_compatibility",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn feature_name(&self) -> Symbol {
|
||||
match *self {
|
||||
Edition::Edition2015 => sym::rust_2015_preview,
|
||||
Edition::Edition2018 => sym::rust_2018_preview,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_stable(&self) -> bool {
|
||||
match *self {
|
||||
Edition::Edition2015 => true,
|
||||
Edition::Edition2018 => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Edition {
|
||||
type Err = ();
|
||||
fn from_str(s: &str) -> Result<Self, ()> {
|
||||
match s {
|
||||
"2015" => Ok(Edition::Edition2015),
|
||||
"2018" => Ok(Edition::Edition2018),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
26
src/librustc_span/fatal_error.rs
Normal file
26
src/librustc_span/fatal_error.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/// Used as a return value to signify a fatal error occurred. (It is also
|
||||
/// used as the argument to panic at the moment, but that will eventually
|
||||
/// not be true.)
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[must_use]
|
||||
pub struct FatalError;
|
||||
|
||||
pub struct FatalErrorMarker;
|
||||
|
||||
// Don't implement Send on FatalError. This makes it impossible to panic!(FatalError).
|
||||
// We don't want to invoke the panic handler and print a backtrace for fatal errors.
|
||||
impl !Send for FatalError {}
|
||||
|
||||
impl FatalError {
|
||||
pub fn raise(self) -> ! {
|
||||
std::panic::resume_unwind(Box::new(FatalErrorMarker))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for FatalError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "parser fatal error")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for FatalError {}
|
||||
850
src/librustc_span/hygiene.rs
Normal file
850
src/librustc_span/hygiene.rs
Normal file
|
|
@ -0,0 +1,850 @@
|
|||
//! Machinery for hygienic macros, inspired by the `MTWT[1]` paper.
|
||||
//!
|
||||
//! `[1]` Matthew Flatt, Ryan Culpepper, David Darais, and Robert Bruce Findler. 2012.
|
||||
//! *Macros that work together: Compile-time bindings, partial expansion,
|
||||
//! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
|
||||
//! DOI=10.1017/S0956796812000093 <https://doi.org/10.1017/S0956796812000093>
|
||||
|
||||
// Hygiene data is stored in a global variable and accessed via TLS, which
|
||||
// means that accesses are somewhat expensive. (`HygieneData::with`
|
||||
// encapsulates a single access.) Therefore, on hot code paths it is worth
|
||||
// ensuring that multiple HygieneData accesses are combined into a single
|
||||
// `HygieneData::with`.
|
||||
//
|
||||
// This explains why `HygieneData`, `SyntaxContext` and `ExpnId` have interfaces
|
||||
// with a certain amount of redundancy in them. For example,
|
||||
// `SyntaxContext::outer_expn_data` combines `SyntaxContext::outer` and
|
||||
// `ExpnId::expn_data` so that two `HygieneData` accesses can be performed within
|
||||
// a single `HygieneData::with` call.
|
||||
//
|
||||
// It also explains why many functions appear in `HygieneData` and again in
|
||||
// `SyntaxContext` or `ExpnId`. For example, `HygieneData::outer` and
|
||||
// `SyntaxContext::outer` do the same thing, but the former is for use within a
|
||||
// `HygieneData::with` call while the latter is for use outside such a call.
|
||||
// When modifying this file it is important to understand this distinction,
|
||||
// because getting it wrong can lead to nested `HygieneData::with` calls that
|
||||
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
|
||||
|
||||
use crate::edition::Edition;
|
||||
use crate::symbol::{kw, sym, Symbol};
|
||||
use crate::GLOBALS;
|
||||
use crate::{Span, DUMMY_SP};
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_macros::HashStable_Generic;
|
||||
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
||||
use std::fmt;
|
||||
|
||||
/// A `SyntaxContext` represents a chain of pairs `(ExpnId, Transparency)` named "marks".
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct SyntaxContext(u32);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SyntaxContextData {
|
||||
outer_expn: ExpnId,
|
||||
outer_transparency: Transparency,
|
||||
parent: SyntaxContext,
|
||||
/// This context, but with all transparent and semi-transparent expansions filtered away.
|
||||
opaque: SyntaxContext,
|
||||
/// This context, but with all transparent expansions filtered away.
|
||||
opaque_and_semitransparent: SyntaxContext,
|
||||
/// Name of the crate to which `$crate` with this context would resolve.
|
||||
dollar_crate_name: Symbol,
|
||||
}
|
||||
|
||||
/// A unique ID associated with a macro invocation and expansion.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
||||
pub struct ExpnId(u32);
|
||||
|
||||
/// A property of a macro expansion that determines how identifiers
|
||||
/// produced by that expansion are resolved.
|
||||
#[derive(
|
||||
Copy,
|
||||
Clone,
|
||||
PartialEq,
|
||||
Eq,
|
||||
PartialOrd,
|
||||
Hash,
|
||||
Debug,
|
||||
RustcEncodable,
|
||||
RustcDecodable,
|
||||
HashStable_Generic
|
||||
)]
|
||||
pub enum Transparency {
|
||||
/// Identifier produced by a transparent expansion is always resolved at call-site.
|
||||
/// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
|
||||
Transparent,
|
||||
/// Identifier produced by a semi-transparent expansion may be resolved
|
||||
/// either at call-site or at definition-site.
|
||||
/// If it's a local variable, label or `$crate` then it's resolved at def-site.
|
||||
/// Otherwise it's resolved at call-site.
|
||||
/// `macro_rules` macros behave like this, built-in macros currently behave like this too,
|
||||
/// but that's an implementation detail.
|
||||
SemiTransparent,
|
||||
/// Identifier produced by an opaque expansion is always resolved at definition-site.
|
||||
/// Def-site spans in procedural macros, identifiers from `macro` by default use this.
|
||||
Opaque,
|
||||
}
|
||||
|
||||
impl ExpnId {
|
||||
pub fn fresh(expn_data: Option<ExpnData>) -> Self {
|
||||
HygieneData::with(|data| data.fresh_expn(expn_data))
|
||||
}
|
||||
|
||||
/// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
|
||||
#[inline]
|
||||
pub fn root() -> Self {
|
||||
ExpnId(0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_u32(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_u32(raw: u32) -> ExpnId {
|
||||
ExpnId(raw)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expn_data(self) -> ExpnData {
|
||||
HygieneData::with(|data| data.expn_data(self).clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_expn_data(self, expn_data: ExpnData) {
|
||||
HygieneData::with(|data| {
|
||||
let old_expn_data = &mut data.expn_data[self.0 as usize];
|
||||
assert!(old_expn_data.is_none(), "expansion data is reset for an expansion ID");
|
||||
*old_expn_data = Some(expn_data);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_descendant_of(self, ancestor: ExpnId) -> bool {
|
||||
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
|
||||
}
|
||||
|
||||
/// `expn_id.outer_expn_is_descendant_of(ctxt)` is equivalent to but faster than
|
||||
/// `expn_id.is_descendant_of(ctxt.outer_expn())`.
|
||||
pub fn outer_expn_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
|
||||
HygieneData::with(|data| data.is_descendant_of(self, data.outer_expn(ctxt)))
|
||||
}
|
||||
|
||||
/// Returns span for the macro which originally caused this expansion to happen.
|
||||
///
|
||||
/// Stops backtracing at include! boundary.
|
||||
pub fn expansion_cause(mut self) -> Option<Span> {
|
||||
let mut last_macro = None;
|
||||
loop {
|
||||
let expn_data = self.expn_data();
|
||||
// Stop going up the backtrace once include! is encountered
|
||||
if expn_data.is_root() || expn_data.kind.descr() == sym::include {
|
||||
break;
|
||||
}
|
||||
self = expn_data.call_site.ctxt().outer_expn();
|
||||
last_macro = Some(expn_data.call_site);
|
||||
}
|
||||
last_macro
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
crate struct HygieneData {
|
||||
/// Each expansion should have an associated expansion data, but sometimes there's a delay
|
||||
/// between creation of an expansion ID and obtaining its data (e.g. macros are collected
|
||||
/// first and then resolved later), so we use an `Option` here.
|
||||
expn_data: Vec<Option<ExpnData>>,
|
||||
syntax_context_data: Vec<SyntaxContextData>,
|
||||
syntax_context_map: FxHashMap<(SyntaxContext, ExpnId, Transparency), SyntaxContext>,
|
||||
}
|
||||
|
||||
impl HygieneData {
|
||||
crate fn new(edition: Edition) -> Self {
|
||||
HygieneData {
|
||||
expn_data: vec![Some(ExpnData::default(ExpnKind::Root, DUMMY_SP, edition))],
|
||||
syntax_context_data: vec![SyntaxContextData {
|
||||
outer_expn: ExpnId::root(),
|
||||
outer_transparency: Transparency::Opaque,
|
||||
parent: SyntaxContext(0),
|
||||
opaque: SyntaxContext(0),
|
||||
opaque_and_semitransparent: SyntaxContext(0),
|
||||
dollar_crate_name: kw::DollarCrate,
|
||||
}],
|
||||
syntax_context_map: FxHashMap::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
|
||||
GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
|
||||
}
|
||||
|
||||
fn fresh_expn(&mut self, expn_data: Option<ExpnData>) -> ExpnId {
|
||||
self.expn_data.push(expn_data);
|
||||
ExpnId(self.expn_data.len() as u32 - 1)
|
||||
}
|
||||
|
||||
fn expn_data(&self, expn_id: ExpnId) -> &ExpnData {
|
||||
self.expn_data[expn_id.0 as usize].as_ref().expect("no expansion data for an expansion ID")
|
||||
}
|
||||
|
||||
fn is_descendant_of(&self, mut expn_id: ExpnId, ancestor: ExpnId) -> bool {
|
||||
while expn_id != ancestor {
|
||||
if expn_id == ExpnId::root() {
|
||||
return false;
|
||||
}
|
||||
expn_id = self.expn_data(expn_id).parent;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
||||
self.syntax_context_data[ctxt.0 as usize].opaque
|
||||
}
|
||||
|
||||
fn modern_and_legacy(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
||||
self.syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent
|
||||
}
|
||||
|
||||
fn outer_expn(&self, ctxt: SyntaxContext) -> ExpnId {
|
||||
self.syntax_context_data[ctxt.0 as usize].outer_expn
|
||||
}
|
||||
|
||||
fn outer_mark(&self, ctxt: SyntaxContext) -> (ExpnId, Transparency) {
|
||||
let data = &self.syntax_context_data[ctxt.0 as usize];
|
||||
(data.outer_expn, data.outer_transparency)
|
||||
}
|
||||
|
||||
fn parent_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
||||
self.syntax_context_data[ctxt.0 as usize].parent
|
||||
}
|
||||
|
||||
fn remove_mark(&self, ctxt: &mut SyntaxContext) -> (ExpnId, Transparency) {
|
||||
let outer_mark = self.outer_mark(*ctxt);
|
||||
*ctxt = self.parent_ctxt(*ctxt);
|
||||
outer_mark
|
||||
}
|
||||
|
||||
fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(ExpnId, Transparency)> {
|
||||
let mut marks = Vec::new();
|
||||
while ctxt != SyntaxContext::root() {
|
||||
marks.push(self.outer_mark(ctxt));
|
||||
ctxt = self.parent_ctxt(ctxt);
|
||||
}
|
||||
marks.reverse();
|
||||
marks
|
||||
}
|
||||
|
||||
fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
|
||||
while span.from_expansion() && span.ctxt() != to {
|
||||
span = self.expn_data(self.outer_expn(span.ctxt())).call_site;
|
||||
}
|
||||
span
|
||||
}
|
||||
|
||||
fn adjust(&self, ctxt: &mut SyntaxContext, expn_id: ExpnId) -> Option<ExpnId> {
|
||||
let mut scope = None;
|
||||
while !self.is_descendant_of(expn_id, self.outer_expn(*ctxt)) {
|
||||
scope = Some(self.remove_mark(ctxt).0);
|
||||
}
|
||||
scope
|
||||
}
|
||||
|
||||
fn apply_mark(
|
||||
&mut self,
|
||||
ctxt: SyntaxContext,
|
||||
expn_id: ExpnId,
|
||||
transparency: Transparency,
|
||||
) -> SyntaxContext {
|
||||
assert_ne!(expn_id, ExpnId::root());
|
||||
if transparency == Transparency::Opaque {
|
||||
return self.apply_mark_internal(ctxt, expn_id, transparency);
|
||||
}
|
||||
|
||||
let call_site_ctxt = self.expn_data(expn_id).call_site.ctxt();
|
||||
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
|
||||
self.modern(call_site_ctxt)
|
||||
} else {
|
||||
self.modern_and_legacy(call_site_ctxt)
|
||||
};
|
||||
|
||||
if call_site_ctxt == SyntaxContext::root() {
|
||||
return self.apply_mark_internal(ctxt, expn_id, transparency);
|
||||
}
|
||||
|
||||
// Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
|
||||
// macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
|
||||
//
|
||||
// In this case, the tokens from the macros 1.0 definition inherit the hygiene
|
||||
// at their invocation. That is, we pretend that the macros 1.0 definition
|
||||
// was defined at its invocation (i.e., inside the macros 2.0 definition)
|
||||
// so that the macros 2.0 definition remains hygienic.
|
||||
//
|
||||
// See the example at `test/ui/hygiene/legacy_interaction.rs`.
|
||||
for (expn_id, transparency) in self.marks(ctxt) {
|
||||
call_site_ctxt = self.apply_mark_internal(call_site_ctxt, expn_id, transparency);
|
||||
}
|
||||
self.apply_mark_internal(call_site_ctxt, expn_id, transparency)
|
||||
}
|
||||
|
||||
fn apply_mark_internal(
|
||||
&mut self,
|
||||
ctxt: SyntaxContext,
|
||||
expn_id: ExpnId,
|
||||
transparency: Transparency,
|
||||
) -> SyntaxContext {
|
||||
let syntax_context_data = &mut self.syntax_context_data;
|
||||
let mut opaque = syntax_context_data[ctxt.0 as usize].opaque;
|
||||
let mut opaque_and_semitransparent =
|
||||
syntax_context_data[ctxt.0 as usize].opaque_and_semitransparent;
|
||||
|
||||
if transparency >= Transparency::Opaque {
|
||||
let parent = opaque;
|
||||
opaque = *self
|
||||
.syntax_context_map
|
||||
.entry((parent, expn_id, transparency))
|
||||
.or_insert_with(|| {
|
||||
let new_opaque = SyntaxContext(syntax_context_data.len() as u32);
|
||||
syntax_context_data.push(SyntaxContextData {
|
||||
outer_expn: expn_id,
|
||||
outer_transparency: transparency,
|
||||
parent,
|
||||
opaque: new_opaque,
|
||||
opaque_and_semitransparent: new_opaque,
|
||||
dollar_crate_name: kw::DollarCrate,
|
||||
});
|
||||
new_opaque
|
||||
});
|
||||
}
|
||||
|
||||
if transparency >= Transparency::SemiTransparent {
|
||||
let parent = opaque_and_semitransparent;
|
||||
opaque_and_semitransparent = *self
|
||||
.syntax_context_map
|
||||
.entry((parent, expn_id, transparency))
|
||||
.or_insert_with(|| {
|
||||
let new_opaque_and_semitransparent =
|
||||
SyntaxContext(syntax_context_data.len() as u32);
|
||||
syntax_context_data.push(SyntaxContextData {
|
||||
outer_expn: expn_id,
|
||||
outer_transparency: transparency,
|
||||
parent,
|
||||
opaque,
|
||||
opaque_and_semitransparent: new_opaque_and_semitransparent,
|
||||
dollar_crate_name: kw::DollarCrate,
|
||||
});
|
||||
new_opaque_and_semitransparent
|
||||
});
|
||||
}
|
||||
|
||||
let parent = ctxt;
|
||||
*self.syntax_context_map.entry((parent, expn_id, transparency)).or_insert_with(|| {
|
||||
let new_opaque_and_semitransparent_and_transparent =
|
||||
SyntaxContext(syntax_context_data.len() as u32);
|
||||
syntax_context_data.push(SyntaxContextData {
|
||||
outer_expn: expn_id,
|
||||
outer_transparency: transparency,
|
||||
parent,
|
||||
opaque,
|
||||
opaque_and_semitransparent,
|
||||
dollar_crate_name: kw::DollarCrate,
|
||||
});
|
||||
new_opaque_and_semitransparent_and_transparent
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_syntax_context_map() {
|
||||
HygieneData::with(|data| data.syntax_context_map = FxHashMap::default());
|
||||
}
|
||||
|
||||
pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
|
||||
HygieneData::with(|data| data.walk_chain(span, to))
|
||||
}
|
||||
|
||||
pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
|
||||
// The new contexts that need updating are at the end of the list and have `$crate` as a name.
|
||||
let (len, to_update) = HygieneData::with(|data| {
|
||||
(
|
||||
data.syntax_context_data.len(),
|
||||
data.syntax_context_data
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|scdata| scdata.dollar_crate_name == kw::DollarCrate)
|
||||
.count(),
|
||||
)
|
||||
});
|
||||
// The callback must be called from outside of the `HygieneData` lock,
|
||||
// since it will try to acquire it too.
|
||||
let range_to_update = len - to_update..len;
|
||||
let names: Vec<_> =
|
||||
range_to_update.clone().map(|idx| get_name(SyntaxContext::from_u32(idx as u32))).collect();
|
||||
HygieneData::with(|data| {
|
||||
range_to_update.zip(names.into_iter()).for_each(|(idx, name)| {
|
||||
data.syntax_context_data[idx].dollar_crate_name = name;
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn debug_hygiene_data(verbose: bool) -> String {
|
||||
HygieneData::with(|data| {
|
||||
if verbose {
|
||||
format!("{:#?}", data)
|
||||
} else {
|
||||
let mut s = String::from("");
|
||||
s.push_str("Expansions:");
|
||||
data.expn_data.iter().enumerate().for_each(|(id, expn_info)| {
|
||||
let expn_info = expn_info.as_ref().expect("no expansion data for an expansion ID");
|
||||
s.push_str(&format!(
|
||||
"\n{}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}",
|
||||
id,
|
||||
expn_info.parent,
|
||||
expn_info.call_site.ctxt(),
|
||||
expn_info.kind,
|
||||
));
|
||||
});
|
||||
s.push_str("\n\nSyntaxContexts:");
|
||||
data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
|
||||
s.push_str(&format!(
|
||||
"\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
|
||||
id, ctxt.parent, ctxt.outer_expn, ctxt.outer_transparency,
|
||||
));
|
||||
});
|
||||
s
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
impl SyntaxContext {
|
||||
#[inline]
|
||||
pub const fn root() -> Self {
|
||||
SyntaxContext(0)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
crate fn as_u32(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
crate fn from_u32(raw: u32) -> SyntaxContext {
|
||||
SyntaxContext(raw)
|
||||
}
|
||||
|
||||
/// Extend a syntax context with a given expansion and transparency.
|
||||
crate fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
|
||||
HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
|
||||
}
|
||||
|
||||
/// Pulls a single mark off of the syntax context. This effectively moves the
|
||||
/// context up one macro definition level. That is, if we have a nested macro
|
||||
/// definition as follows:
|
||||
///
|
||||
/// ```rust
|
||||
/// macro_rules! f {
|
||||
/// macro_rules! g {
|
||||
/// ...
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// and we have a SyntaxContext that is referring to something declared by an invocation
|
||||
/// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
|
||||
/// invocation of f that created g1.
|
||||
/// Returns the mark that was removed.
|
||||
pub fn remove_mark(&mut self) -> ExpnId {
|
||||
HygieneData::with(|data| data.remove_mark(self).0)
|
||||
}
|
||||
|
||||
pub fn marks(self) -> Vec<(ExpnId, Transparency)> {
|
||||
HygieneData::with(|data| data.marks(self))
|
||||
}
|
||||
|
||||
/// Adjust this context for resolution in a scope created by the given expansion.
|
||||
/// For example, consider the following three resolutions of `f`:
|
||||
///
|
||||
/// ```rust
|
||||
/// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
|
||||
/// m!(f);
|
||||
/// macro m($f:ident) {
|
||||
/// mod bar {
|
||||
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
|
||||
/// pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
|
||||
/// }
|
||||
/// foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
|
||||
/// //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
|
||||
/// //| and it resolves to `::foo::f`.
|
||||
/// bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
|
||||
/// //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
|
||||
/// //| and it resolves to `::bar::f`.
|
||||
/// bar::$f(); // `f`'s `SyntaxContext` is empty.
|
||||
/// //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
|
||||
/// //| and it resolves to `::bar::$f`.
|
||||
/// }
|
||||
/// ```
|
||||
/// This returns the expansion whose definition scope we use to privacy check the resolution,
|
||||
/// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
|
||||
pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
||||
HygieneData::with(|data| data.adjust(self, expn_id))
|
||||
}
|
||||
|
||||
/// Like `SyntaxContext::adjust`, but also modernizes `self`.
|
||||
pub fn modernize_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
||||
HygieneData::with(|data| {
|
||||
*self = data.modern(*self);
|
||||
data.adjust(self, expn_id)
|
||||
})
|
||||
}
|
||||
|
||||
/// Adjust this context for resolution in a scope created by the given expansion
|
||||
/// via a glob import with the given `SyntaxContext`.
|
||||
/// For example:
|
||||
///
|
||||
/// ```rust
|
||||
/// m!(f);
|
||||
/// macro m($i:ident) {
|
||||
/// mod foo {
|
||||
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
|
||||
/// pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
|
||||
/// }
|
||||
/// n(f);
|
||||
/// macro n($j:ident) {
|
||||
/// use foo::*;
|
||||
/// f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
|
||||
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
|
||||
/// $i(); // `$i`'s `SyntaxContext` has a mark from `n`
|
||||
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
|
||||
/// $j(); // `$j`'s `SyntaxContext` has a mark from `m`
|
||||
/// //^ This cannot be glob-adjusted, so this is a resolution error.
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// This returns `None` if the context cannot be glob-adjusted.
|
||||
/// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
|
||||
pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
|
||||
HygieneData::with(|data| {
|
||||
let mut scope = None;
|
||||
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
||||
while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
|
||||
scope = Some(data.remove_mark(&mut glob_ctxt).0);
|
||||
if data.remove_mark(self).0 != scope.unwrap() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if data.adjust(self, expn_id).is_some() {
|
||||
return None;
|
||||
}
|
||||
Some(scope)
|
||||
})
|
||||
}
|
||||
|
||||
/// Undo `glob_adjust` if possible:
|
||||
///
|
||||
/// ```rust
|
||||
/// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
|
||||
/// assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn reverse_glob_adjust(
|
||||
&mut self,
|
||||
expn_id: ExpnId,
|
||||
glob_span: Span,
|
||||
) -> Option<Option<ExpnId>> {
|
||||
HygieneData::with(|data| {
|
||||
if data.adjust(self, expn_id).is_some() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
||||
let mut marks = Vec::new();
|
||||
while !data.is_descendant_of(expn_id, data.outer_expn(glob_ctxt)) {
|
||||
marks.push(data.remove_mark(&mut glob_ctxt));
|
||||
}
|
||||
|
||||
let scope = marks.last().map(|mark| mark.0);
|
||||
while let Some((expn_id, transparency)) = marks.pop() {
|
||||
*self = data.apply_mark(*self, expn_id, transparency);
|
||||
}
|
||||
Some(scope)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn hygienic_eq(self, other: SyntaxContext, expn_id: ExpnId) -> bool {
|
||||
HygieneData::with(|data| {
|
||||
let mut self_modern = data.modern(self);
|
||||
data.adjust(&mut self_modern, expn_id);
|
||||
self_modern == data.modern(other)
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn modern(self) -> SyntaxContext {
|
||||
HygieneData::with(|data| data.modern(self))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn modern_and_legacy(self) -> SyntaxContext {
|
||||
HygieneData::with(|data| data.modern_and_legacy(self))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn outer_expn(self) -> ExpnId {
|
||||
HygieneData::with(|data| data.outer_expn(self))
|
||||
}
|
||||
|
||||
/// `ctxt.outer_expn_data()` is equivalent to but faster than
|
||||
/// `ctxt.outer_expn().expn_data()`.
|
||||
#[inline]
|
||||
pub fn outer_expn_data(self) -> ExpnData {
|
||||
HygieneData::with(|data| data.expn_data(data.outer_expn(self)).clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn outer_mark_with_data(self) -> (ExpnId, Transparency, ExpnData) {
|
||||
HygieneData::with(|data| {
|
||||
let (expn_id, transparency) = data.outer_mark(self);
|
||||
(expn_id, transparency, data.expn_data(expn_id).clone())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn dollar_crate_name(self) -> Symbol {
|
||||
HygieneData::with(|data| data.syntax_context_data[self.0 as usize].dollar_crate_name)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for SyntaxContext {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "#{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Span {
|
||||
/// Creates a fresh expansion with given properties.
|
||||
/// Expansions are normally created by macros, but in some cases expansions are created for
|
||||
/// other compiler-generated code to set per-span properties like allowed unstable features.
|
||||
/// The returned span belongs to the created expansion and has the new properties,
|
||||
/// but its location is inherited from the current span.
|
||||
pub fn fresh_expansion(self, expn_data: ExpnData) -> Span {
|
||||
self.fresh_expansion_with_transparency(expn_data, Transparency::Transparent)
|
||||
}
|
||||
|
||||
pub fn fresh_expansion_with_transparency(
|
||||
self,
|
||||
expn_data: ExpnData,
|
||||
transparency: Transparency,
|
||||
) -> Span {
|
||||
HygieneData::with(|data| {
|
||||
let expn_id = data.fresh_expn(Some(expn_data));
|
||||
self.with_ctxt(data.apply_mark(SyntaxContext::root(), expn_id, transparency))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A subset of properties from both macro definition and macro call available through global data.
|
||||
/// Avoid using this if you have access to the original definition or call structures.
|
||||
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
|
||||
pub struct ExpnData {
|
||||
// --- The part unique to each expansion.
|
||||
/// The kind of this expansion - macro or compiler desugaring.
|
||||
pub kind: ExpnKind,
|
||||
/// The expansion that produced this expansion.
|
||||
#[stable_hasher(ignore)]
|
||||
pub parent: ExpnId,
|
||||
/// The location of the actual macro invocation or syntax sugar , e.g.
|
||||
/// `let x = foo!();` or `if let Some(y) = x {}`
|
||||
///
|
||||
/// This may recursively refer to other macro invocations, e.g., if
|
||||
/// `foo!()` invoked `bar!()` internally, and there was an
|
||||
/// expression inside `bar!`; the call_site of the expression in
|
||||
/// the expansion would point to the `bar!` invocation; that
|
||||
/// call_site span would have its own ExpnData, with the call_site
|
||||
/// pointing to the `foo!` invocation.
|
||||
pub call_site: Span,
|
||||
|
||||
// --- The part specific to the macro/desugaring definition.
|
||||
// --- It may be reasonable to share this part between expansions with the same definition,
|
||||
// --- but such sharing is known to bring some minor inconveniences without also bringing
|
||||
// --- noticeable perf improvements (PR #62898).
|
||||
/// The span of the macro definition (possibly dummy).
|
||||
/// This span serves only informational purpose and is not used for resolution.
|
||||
pub def_site: Span,
|
||||
/// List of #[unstable]/feature-gated features that the macro is allowed to use
|
||||
/// internally without forcing the whole crate to opt-in
|
||||
/// to them.
|
||||
pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
|
||||
/// Whether the macro is allowed to use `unsafe` internally
|
||||
/// even if the user crate has `#![forbid(unsafe_code)]`.
|
||||
pub allow_internal_unsafe: bool,
|
||||
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
|
||||
/// for a given macro.
|
||||
pub local_inner_macros: bool,
|
||||
/// Edition of the crate in which the macro is defined.
|
||||
pub edition: Edition,
|
||||
}
|
||||
|
||||
impl ExpnData {
|
||||
/// Constructs expansion data with default properties.
|
||||
pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnData {
|
||||
ExpnData {
|
||||
kind,
|
||||
parent: ExpnId::root(),
|
||||
call_site,
|
||||
def_site: DUMMY_SP,
|
||||
allow_internal_unstable: None,
|
||||
allow_internal_unsafe: false,
|
||||
local_inner_macros: false,
|
||||
edition,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allow_unstable(
|
||||
kind: ExpnKind,
|
||||
call_site: Span,
|
||||
edition: Edition,
|
||||
allow_internal_unstable: Lrc<[Symbol]>,
|
||||
) -> ExpnData {
|
||||
ExpnData {
|
||||
allow_internal_unstable: Some(allow_internal_unstable),
|
||||
..ExpnData::default(kind, call_site, edition)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_root(&self) -> bool {
|
||||
if let ExpnKind::Root = self.kind { true } else { false }
|
||||
}
|
||||
}
|
||||
|
||||
/// Expansion kind.
|
||||
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
|
||||
pub enum ExpnKind {
|
||||
/// No expansion, aka root expansion. Only `ExpnId::root()` has this kind.
|
||||
Root,
|
||||
/// Expansion produced by a macro.
|
||||
Macro(MacroKind, Symbol),
|
||||
/// Transform done by the compiler on the AST.
|
||||
AstPass(AstPass),
|
||||
/// Desugaring done by the compiler during HIR lowering.
|
||||
Desugaring(DesugaringKind),
|
||||
}
|
||||
|
||||
impl ExpnKind {
|
||||
pub fn descr(&self) -> Symbol {
|
||||
match *self {
|
||||
ExpnKind::Root => kw::PathRoot,
|
||||
ExpnKind::Macro(_, descr) => descr,
|
||||
ExpnKind::AstPass(kind) => Symbol::intern(kind.descr()),
|
||||
ExpnKind::Desugaring(kind) => Symbol::intern(kind.descr()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of macro invocation or definition.
|
||||
#[derive(
|
||||
Clone,
|
||||
Copy,
|
||||
PartialEq,
|
||||
Eq,
|
||||
RustcEncodable,
|
||||
RustcDecodable,
|
||||
Hash,
|
||||
Debug,
|
||||
HashStable_Generic
|
||||
)]
|
||||
pub enum MacroKind {
|
||||
/// A bang macro `foo!()`.
|
||||
Bang,
|
||||
/// An attribute macro `#[foo]`.
|
||||
Attr,
|
||||
/// A derive macro `#[derive(Foo)]`
|
||||
Derive,
|
||||
}
|
||||
|
||||
impl MacroKind {
|
||||
pub fn descr(self) -> &'static str {
|
||||
match self {
|
||||
MacroKind::Bang => "macro",
|
||||
MacroKind::Attr => "attribute macro",
|
||||
MacroKind::Derive => "derive macro",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn descr_expected(self) -> &'static str {
|
||||
match self {
|
||||
MacroKind::Attr => "attribute",
|
||||
_ => self.descr(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn article(self) -> &'static str {
|
||||
match self {
|
||||
MacroKind::Attr => "an",
|
||||
_ => "a",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of AST transform.
|
||||
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
|
||||
pub enum AstPass {
|
||||
StdImports,
|
||||
TestHarness,
|
||||
ProcMacroHarness,
|
||||
}
|
||||
|
||||
impl AstPass {
|
||||
fn descr(self) -> &'static str {
|
||||
match self {
|
||||
AstPass::StdImports => "standard library imports",
|
||||
AstPass::TestHarness => "test harness",
|
||||
AstPass::ProcMacroHarness => "proc macro harness",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of compiler desugaring.
|
||||
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
|
||||
pub enum DesugaringKind {
|
||||
/// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
|
||||
/// However, we do not want to blame `c` for unreachability but rather say that `i`
|
||||
/// is unreachable. This desugaring kind allows us to avoid blaming `c`.
|
||||
/// This also applies to `while` loops.
|
||||
CondTemporary,
|
||||
QuestionMark,
|
||||
TryBlock,
|
||||
/// Desugaring of an `impl Trait` in return type position
|
||||
/// to an `type Foo = impl Trait;` and replacing the
|
||||
/// `impl Trait` with `Foo`.
|
||||
OpaqueTy,
|
||||
Async,
|
||||
Await,
|
||||
ForLoop,
|
||||
}
|
||||
|
||||
impl DesugaringKind {
|
||||
/// The description wording should combine well with "desugaring of {}".
|
||||
fn descr(self) -> &'static str {
|
||||
match self {
|
||||
DesugaringKind::CondTemporary => "`if` or `while` condition",
|
||||
DesugaringKind::Async => "`async` block or function",
|
||||
DesugaringKind::Await => "`await` expression",
|
||||
DesugaringKind::QuestionMark => "operator `?`",
|
||||
DesugaringKind::TryBlock => "`try` block",
|
||||
DesugaringKind::OpaqueTy => "`impl Trait`",
|
||||
DesugaringKind::ForLoop => "`for` loop",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for ExpnId {
|
||||
fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
|
||||
Ok(()) // FIXME(jseyfried) intercrate hygiene
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ExpnId {
|
||||
fn decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> {
|
||||
Ok(ExpnId::root()) // FIXME(jseyfried) intercrate hygiene
|
||||
}
|
||||
}
|
||||
1672
src/librustc_span/lib.rs
Normal file
1672
src/librustc_span/lib.rs
Normal file
File diff suppressed because it is too large
Load diff
984
src/librustc_span/source_map.rs
Normal file
984
src/librustc_span/source_map.rs
Normal file
|
|
@ -0,0 +1,984 @@
|
|||
//! The `SourceMap` tracks all the source code used within a single crate, mapping
|
||||
//! from integer byte positions to the original source code location. Each bit
|
||||
//! of source parsed during crate parsing (typically files, in-memory strings,
|
||||
//! or various bits of macro expansion) cover a continuous range of bytes in the
|
||||
//! `SourceMap` and are represented by `SourceFile`s. Byte positions are stored in
|
||||
//! `Span` and used pervasively in the compiler. They are absolute positions
|
||||
//! within the `SourceMap`, which upon request can be converted to line and column
|
||||
//! information, source code snippets, etc.
|
||||
|
||||
pub use crate::hygiene::{ExpnData, ExpnKind};
|
||||
pub use crate::*;
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::stable_hasher::StableHasher;
|
||||
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, MappedLockGuard};
|
||||
use std::cmp;
|
||||
use std::hash::Hash;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use log::debug;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Returns the span itself if it doesn't come from a macro expansion,
|
||||
/// otherwise return the call site span up to the `enclosing_sp` by
|
||||
/// following the `expn_data` chain.
|
||||
pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span {
|
||||
let expn_data1 = sp.ctxt().outer_expn_data();
|
||||
let expn_data2 = enclosing_sp.ctxt().outer_expn_data();
|
||||
if expn_data1.is_root() || !expn_data2.is_root() && expn_data1.call_site == expn_data2.call_site
|
||||
{
|
||||
sp
|
||||
} else {
|
||||
original_sp(expn_data1.call_site, enclosing_sp)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, HashStable_Generic)]
|
||||
pub struct Spanned<T> {
|
||||
pub node: T,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
|
||||
Spanned { node: t, span: sp }
|
||||
}
|
||||
|
||||
pub fn dummy_spanned<T>(t: T) -> Spanned<T> {
|
||||
respan(DUMMY_SP, t)
|
||||
}
|
||||
|
||||
// _____________________________________________________________________________
|
||||
// SourceFile, MultiByteChar, FileName, FileLines
|
||||
//
|
||||
|
||||
/// An abstraction over the fs operations used by the Parser.
|
||||
pub trait FileLoader {
|
||||
/// Query the existence of a file.
|
||||
fn file_exists(&self, path: &Path) -> bool;
|
||||
|
||||
/// Returns an absolute path to a file, if possible.
|
||||
fn abs_path(&self, path: &Path) -> Option<PathBuf>;
|
||||
|
||||
/// Read the contents of an UTF-8 file into memory.
|
||||
fn read_file(&self, path: &Path) -> io::Result<String>;
|
||||
}
|
||||
|
||||
/// A FileLoader that uses std::fs to load real files.
|
||||
pub struct RealFileLoader;
|
||||
|
||||
impl FileLoader for RealFileLoader {
|
||||
fn file_exists(&self, path: &Path) -> bool {
|
||||
fs::metadata(path).is_ok()
|
||||
}
|
||||
|
||||
fn abs_path(&self, path: &Path) -> Option<PathBuf> {
|
||||
if path.is_absolute() {
|
||||
Some(path.to_path_buf())
|
||||
} else {
|
||||
env::current_dir().ok().map(|cwd| cwd.join(path))
|
||||
}
|
||||
}
|
||||
|
||||
fn read_file(&self, path: &Path) -> io::Result<String> {
|
||||
fs::read_to_string(path)
|
||||
}
|
||||
}
|
||||
|
||||
// This is a `SourceFile` identifier that is used to correlate `SourceFile`s between
|
||||
// subsequent compilation sessions (which is something we need to do during
|
||||
// incremental compilation).
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Debug)]
|
||||
pub struct StableSourceFileId(u128);
|
||||
|
||||
impl StableSourceFileId {
|
||||
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
|
||||
StableSourceFileId::new_from_pieces(
|
||||
&source_file.name,
|
||||
source_file.name_was_remapped,
|
||||
source_file.unmapped_path.as_ref(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_from_pieces(
|
||||
name: &FileName,
|
||||
name_was_remapped: bool,
|
||||
unmapped_path: Option<&FileName>,
|
||||
) -> StableSourceFileId {
|
||||
let mut hasher = StableHasher::new();
|
||||
|
||||
name.hash(&mut hasher);
|
||||
name_was_remapped.hash(&mut hasher);
|
||||
unmapped_path.hash(&mut hasher);
|
||||
|
||||
StableSourceFileId(hasher.finish())
|
||||
}
|
||||
}
|
||||
|
||||
// _____________________________________________________________________________
|
||||
// SourceMap
|
||||
//
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct SourceMapFiles {
|
||||
source_files: Vec<Lrc<SourceFile>>,
|
||||
stable_id_to_source_file: FxHashMap<StableSourceFileId, Lrc<SourceFile>>,
|
||||
}
|
||||
|
||||
pub struct SourceMap {
|
||||
files: Lock<SourceMapFiles>,
|
||||
file_loader: Box<dyn FileLoader + Sync + Send>,
|
||||
// This is used to apply the file path remapping as specified via
|
||||
// `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
|
||||
path_mapping: FilePathMapping,
|
||||
}
|
||||
|
||||
impl SourceMap {
|
||||
pub fn new(path_mapping: FilePathMapping) -> SourceMap {
|
||||
SourceMap { files: Default::default(), file_loader: Box::new(RealFileLoader), path_mapping }
|
||||
}
|
||||
|
||||
pub fn with_file_loader(
|
||||
file_loader: Box<dyn FileLoader + Sync + Send>,
|
||||
path_mapping: FilePathMapping,
|
||||
) -> SourceMap {
|
||||
SourceMap { files: Default::default(), file_loader, path_mapping }
|
||||
}
|
||||
|
||||
pub fn path_mapping(&self) -> &FilePathMapping {
|
||||
&self.path_mapping
|
||||
}
|
||||
|
||||
pub fn file_exists(&self, path: &Path) -> bool {
|
||||
self.file_loader.file_exists(path)
|
||||
}
|
||||
|
||||
pub fn load_file(&self, path: &Path) -> io::Result<Lrc<SourceFile>> {
|
||||
let src = self.file_loader.read_file(path)?;
|
||||
let filename = path.to_owned().into();
|
||||
Ok(self.new_source_file(filename, src))
|
||||
}
|
||||
|
||||
/// Loads source file as a binary blob.
|
||||
///
|
||||
/// Unlike `load_file`, guarantees that no normalization like BOM-removal
|
||||
/// takes place.
|
||||
pub fn load_binary_file(&self, path: &Path) -> io::Result<Vec<u8>> {
|
||||
// Ideally, this should use `self.file_loader`, but it can't
|
||||
// deal with binary files yet.
|
||||
let bytes = fs::read(path)?;
|
||||
|
||||
// We need to add file to the `SourceMap`, so that it is present
|
||||
// in dep-info. There's also an edge case that file might be both
|
||||
// loaded as a binary via `include_bytes!` and as proper `SourceFile`
|
||||
// via `mod`, so we try to use real file contents and not just an
|
||||
// empty string.
|
||||
let text = std::str::from_utf8(&bytes).unwrap_or("").to_string();
|
||||
self.new_source_file(path.to_owned().into(), text);
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
pub fn files(&self) -> MappedLockGuard<'_, Vec<Lrc<SourceFile>>> {
|
||||
LockGuard::map(self.files.borrow(), |files| &mut files.source_files)
|
||||
}
|
||||
|
||||
pub fn source_file_by_stable_id(
|
||||
&self,
|
||||
stable_id: StableSourceFileId,
|
||||
) -> Option<Lrc<SourceFile>> {
|
||||
self.files.borrow().stable_id_to_source_file.get(&stable_id).map(|sf| sf.clone())
|
||||
}
|
||||
|
||||
fn next_start_pos(&self) -> usize {
|
||||
match self.files.borrow().source_files.last() {
|
||||
None => 0,
|
||||
// Add one so there is some space between files. This lets us distinguish
|
||||
// positions in the `SourceMap`, even in the presence of zero-length files.
|
||||
Some(last) => last.end_pos.to_usize() + 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new `SourceFile`.
|
||||
/// If a file already exists in the `SourceMap` with the same ID, that file is returned
|
||||
/// unmodified.
|
||||
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
|
||||
self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
|
||||
eprintln!("fatal error: rustc does not support files larger than 4GB");
|
||||
crate::fatal_error::FatalError.raise()
|
||||
})
|
||||
}
|
||||
|
||||
fn try_new_source_file(
|
||||
&self,
|
||||
filename: FileName,
|
||||
src: String,
|
||||
) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
|
||||
let start_pos = self.next_start_pos();
|
||||
|
||||
// The path is used to determine the directory for loading submodules and
|
||||
// include files, so it must be before remapping.
|
||||
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
|
||||
// but this is okay because the directory determined by `path.pop()` will
|
||||
// be empty, so the working directory will be used.
|
||||
let unmapped_path = filename.clone();
|
||||
|
||||
let (filename, was_remapped) = match filename {
|
||||
FileName::Real(filename) => {
|
||||
let (filename, was_remapped) = self.path_mapping.map_prefix(filename);
|
||||
(FileName::Real(filename), was_remapped)
|
||||
}
|
||||
other => (other, false),
|
||||
};
|
||||
|
||||
let file_id =
|
||||
StableSourceFileId::new_from_pieces(&filename, was_remapped, Some(&unmapped_path));
|
||||
|
||||
let lrc_sf = match self.source_file_by_stable_id(file_id) {
|
||||
Some(lrc_sf) => lrc_sf,
|
||||
None => {
|
||||
let source_file = Lrc::new(SourceFile::new(
|
||||
filename,
|
||||
was_remapped,
|
||||
unmapped_path,
|
||||
src,
|
||||
Pos::from_usize(start_pos),
|
||||
)?);
|
||||
|
||||
let mut files = self.files.borrow_mut();
|
||||
|
||||
files.source_files.push(source_file.clone());
|
||||
files.stable_id_to_source_file.insert(file_id, source_file.clone());
|
||||
|
||||
source_file
|
||||
}
|
||||
};
|
||||
Ok(lrc_sf)
|
||||
}
|
||||
|
||||
/// Allocates a new `SourceFile` representing a source file from an external
|
||||
/// crate. The source code of such an "imported `SourceFile`" is not available,
|
||||
/// but we still know enough to generate accurate debuginfo location
|
||||
/// information for things inlined from other crates.
|
||||
pub fn new_imported_source_file(
|
||||
&self,
|
||||
filename: FileName,
|
||||
name_was_remapped: bool,
|
||||
crate_of_origin: u32,
|
||||
src_hash: u128,
|
||||
name_hash: u128,
|
||||
source_len: usize,
|
||||
mut file_local_lines: Vec<BytePos>,
|
||||
mut file_local_multibyte_chars: Vec<MultiByteChar>,
|
||||
mut file_local_non_narrow_chars: Vec<NonNarrowChar>,
|
||||
mut file_local_normalized_pos: Vec<NormalizedPos>,
|
||||
) -> Lrc<SourceFile> {
|
||||
let start_pos = self.next_start_pos();
|
||||
|
||||
let end_pos = Pos::from_usize(start_pos + source_len);
|
||||
let start_pos = Pos::from_usize(start_pos);
|
||||
|
||||
for pos in &mut file_local_lines {
|
||||
*pos = *pos + start_pos;
|
||||
}
|
||||
|
||||
for mbc in &mut file_local_multibyte_chars {
|
||||
mbc.pos = mbc.pos + start_pos;
|
||||
}
|
||||
|
||||
for swc in &mut file_local_non_narrow_chars {
|
||||
*swc = *swc + start_pos;
|
||||
}
|
||||
|
||||
for nc in &mut file_local_normalized_pos {
|
||||
nc.pos = nc.pos + start_pos;
|
||||
}
|
||||
|
||||
let source_file = Lrc::new(SourceFile {
|
||||
name: filename,
|
||||
name_was_remapped,
|
||||
unmapped_path: None,
|
||||
crate_of_origin,
|
||||
src: None,
|
||||
src_hash,
|
||||
external_src: Lock::new(ExternalSource::AbsentOk),
|
||||
start_pos,
|
||||
end_pos,
|
||||
lines: file_local_lines,
|
||||
multibyte_chars: file_local_multibyte_chars,
|
||||
non_narrow_chars: file_local_non_narrow_chars,
|
||||
normalized_pos: file_local_normalized_pos,
|
||||
name_hash,
|
||||
});
|
||||
|
||||
let mut files = self.files.borrow_mut();
|
||||
|
||||
files.source_files.push(source_file.clone());
|
||||
files
|
||||
.stable_id_to_source_file
|
||||
.insert(StableSourceFileId::new(&source_file), source_file.clone());
|
||||
|
||||
source_file
|
||||
}
|
||||
|
||||
pub fn mk_substr_filename(&self, sp: Span) -> String {
|
||||
let pos = self.lookup_char_pos(sp.lo());
|
||||
format!("<{}:{}:{}>", pos.file.name, pos.line, pos.col.to_usize() + 1)
|
||||
}
|
||||
|
||||
// If there is a doctest offset, applies it to the line.
|
||||
pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
|
||||
return match file {
|
||||
FileName::DocTest(_, offset) => {
|
||||
return if *offset >= 0 {
|
||||
orig + *offset as usize
|
||||
} else {
|
||||
orig - (-(*offset)) as usize
|
||||
};
|
||||
}
|
||||
_ => orig,
|
||||
};
|
||||
}
|
||||
|
||||
/// Looks up source information about a `BytePos`.
|
||||
pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
|
||||
let chpos = self.bytepos_to_file_charpos(pos);
|
||||
match self.lookup_line(pos) {
|
||||
Ok(SourceFileAndLine { sf: f, line: a }) => {
|
||||
let line = a + 1; // Line numbers start at 1
|
||||
let linebpos = f.lines[a];
|
||||
let linechpos = self.bytepos_to_file_charpos(linebpos);
|
||||
let col = chpos - linechpos;
|
||||
|
||||
let col_display = {
|
||||
let start_width_idx = f
|
||||
.non_narrow_chars
|
||||
.binary_search_by_key(&linebpos, |x| x.pos())
|
||||
.unwrap_or_else(|x| x);
|
||||
let end_width_idx = f
|
||||
.non_narrow_chars
|
||||
.binary_search_by_key(&pos, |x| x.pos())
|
||||
.unwrap_or_else(|x| x);
|
||||
let special_chars = end_width_idx - start_width_idx;
|
||||
let non_narrow: usize = f.non_narrow_chars[start_width_idx..end_width_idx]
|
||||
.into_iter()
|
||||
.map(|x| x.width())
|
||||
.sum();
|
||||
col.0 - special_chars + non_narrow
|
||||
};
|
||||
debug!("byte pos {:?} is on the line at byte pos {:?}", pos, linebpos);
|
||||
debug!("char pos {:?} is on the line at char pos {:?}", chpos, linechpos);
|
||||
debug!("byte is on line: {}", line);
|
||||
assert!(chpos >= linechpos);
|
||||
Loc { file: f, line, col, col_display }
|
||||
}
|
||||
Err(f) => {
|
||||
let col_display = {
|
||||
let end_width_idx = f
|
||||
.non_narrow_chars
|
||||
.binary_search_by_key(&pos, |x| x.pos())
|
||||
.unwrap_or_else(|x| x);
|
||||
let non_narrow: usize =
|
||||
f.non_narrow_chars[0..end_width_idx].into_iter().map(|x| x.width()).sum();
|
||||
chpos.0 - end_width_idx + non_narrow
|
||||
};
|
||||
Loc { file: f, line: 0, col: chpos, col_display }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the corresponding `SourceFile` is empty, does not return a line number.
|
||||
pub fn lookup_line(&self, pos: BytePos) -> Result<SourceFileAndLine, Lrc<SourceFile>> {
|
||||
let idx = self.lookup_source_file_idx(pos);
|
||||
|
||||
let f = (*self.files.borrow().source_files)[idx].clone();
|
||||
|
||||
match f.lookup_line(pos) {
|
||||
Some(line) => Ok(SourceFileAndLine { sf: f, line }),
|
||||
None => Err(f),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Some(span)`, a union of the LHS and RHS span. The LHS must precede the RHS. If
|
||||
/// there are gaps between LHS and RHS, the resulting union will cross these gaps.
|
||||
/// For this to work,
|
||||
///
|
||||
/// * the syntax contexts of both spans much match,
|
||||
/// * the LHS span needs to end on the same line the RHS span begins,
|
||||
/// * the LHS span must start at or before the RHS span.
|
||||
pub fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span> {
|
||||
// Ensure we're at the same expansion ID.
|
||||
if sp_lhs.ctxt() != sp_rhs.ctxt() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let lhs_end = match self.lookup_line(sp_lhs.hi()) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return None,
|
||||
};
|
||||
let rhs_begin = match self.lookup_line(sp_rhs.lo()) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return None,
|
||||
};
|
||||
|
||||
// If we must cross lines to merge, don't merge.
|
||||
if lhs_end.line != rhs_begin.line {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Ensure these follow the expected order and that we don't overlap.
|
||||
if (sp_lhs.lo() <= sp_rhs.lo()) && (sp_lhs.hi() <= sp_rhs.lo()) {
|
||||
Some(sp_lhs.to(sp_rhs))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn span_to_string(&self, sp: Span) -> String {
|
||||
if self.files.borrow().source_files.is_empty() && sp.is_dummy() {
|
||||
return "no-location".to_string();
|
||||
}
|
||||
|
||||
let lo = self.lookup_char_pos(sp.lo());
|
||||
let hi = self.lookup_char_pos(sp.hi());
|
||||
format!(
|
||||
"{}:{}:{}: {}:{}",
|
||||
lo.file.name,
|
||||
lo.line,
|
||||
lo.col.to_usize() + 1,
|
||||
hi.line,
|
||||
hi.col.to_usize() + 1,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn span_to_filename(&self, sp: Span) -> FileName {
|
||||
self.lookup_char_pos(sp.lo()).file.name.clone()
|
||||
}
|
||||
|
||||
pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
|
||||
self.lookup_char_pos(sp.lo())
|
||||
.file
|
||||
.unmapped_path
|
||||
.clone()
|
||||
.expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
|
||||
}
|
||||
|
||||
pub fn is_multiline(&self, sp: Span) -> bool {
|
||||
let lo = self.lookup_char_pos(sp.lo());
|
||||
let hi = self.lookup_char_pos(sp.hi());
|
||||
lo.line != hi.line
|
||||
}
|
||||
|
||||
pub fn span_to_lines(&self, sp: Span) -> FileLinesResult {
|
||||
debug!("span_to_lines(sp={:?})", sp);
|
||||
|
||||
let lo = self.lookup_char_pos(sp.lo());
|
||||
debug!("span_to_lines: lo={:?}", lo);
|
||||
let hi = self.lookup_char_pos(sp.hi());
|
||||
debug!("span_to_lines: hi={:?}", hi);
|
||||
|
||||
if lo.file.start_pos != hi.file.start_pos {
|
||||
return Err(SpanLinesError::DistinctSources(DistinctSources {
|
||||
begin: (lo.file.name.clone(), lo.file.start_pos),
|
||||
end: (hi.file.name.clone(), hi.file.start_pos),
|
||||
}));
|
||||
}
|
||||
assert!(hi.line >= lo.line);
|
||||
|
||||
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
|
||||
|
||||
// The span starts partway through the first line,
|
||||
// but after that it starts from offset 0.
|
||||
let mut start_col = lo.col;
|
||||
|
||||
// For every line but the last, it extends from `start_col`
|
||||
// and to the end of the line. Be careful because the line
|
||||
// numbers in Loc are 1-based, so we subtract 1 to get 0-based
|
||||
// lines.
|
||||
for line_index in lo.line - 1..hi.line - 1 {
|
||||
let line_len = lo.file.get_line(line_index).map(|s| s.chars().count()).unwrap_or(0);
|
||||
lines.push(LineInfo { line_index, start_col, end_col: CharPos::from_usize(line_len) });
|
||||
start_col = CharPos::from_usize(0);
|
||||
}
|
||||
|
||||
// For the last line, it extends from `start_col` to `hi.col`:
|
||||
lines.push(LineInfo { line_index: hi.line - 1, start_col, end_col: hi.col });
|
||||
|
||||
Ok(FileLines { file: lo.file, lines })
|
||||
}
|
||||
|
||||
/// Extracts the source surrounding the given `Span` using the `extract_source` function. The
|
||||
/// extract function takes three arguments: a string slice containing the source, an index in
|
||||
/// the slice for the beginning of the span and an index in the slice for the end of the span.
|
||||
fn span_to_source<F>(&self, sp: Span, extract_source: F) -> Result<String, SpanSnippetError>
|
||||
where
|
||||
F: Fn(&str, usize, usize) -> Result<String, SpanSnippetError>,
|
||||
{
|
||||
let local_begin = self.lookup_byte_offset(sp.lo());
|
||||
let local_end = self.lookup_byte_offset(sp.hi());
|
||||
|
||||
if local_begin.sf.start_pos != local_end.sf.start_pos {
|
||||
return Err(SpanSnippetError::DistinctSources(DistinctSources {
|
||||
begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
|
||||
end: (local_end.sf.name.clone(), local_end.sf.start_pos),
|
||||
}));
|
||||
} else {
|
||||
self.ensure_source_file_source_present(local_begin.sf.clone());
|
||||
|
||||
let start_index = local_begin.pos.to_usize();
|
||||
let end_index = local_end.pos.to_usize();
|
||||
let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
|
||||
|
||||
if start_index > end_index || end_index > source_len {
|
||||
return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
|
||||
name: local_begin.sf.name.clone(),
|
||||
source_len,
|
||||
begin_pos: local_begin.pos,
|
||||
end_pos: local_end.pos,
|
||||
}));
|
||||
}
|
||||
|
||||
if let Some(ref src) = local_begin.sf.src {
|
||||
return extract_source(src, start_index, end_index);
|
||||
} else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
|
||||
return extract_source(src, start_index, end_index);
|
||||
} else {
|
||||
return Err(SpanSnippetError::SourceNotAvailable {
|
||||
filename: local_begin.sf.name.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the source snippet as `String` corresponding to the given `Span`.
|
||||
pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
|
||||
self.span_to_source(sp, |src, start_index, end_index| {
|
||||
src.get(start_index..end_index)
|
||||
.map(|s| s.to_string())
|
||||
.ok_or_else(|| SpanSnippetError::IllFormedSpan(sp))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
|
||||
match self.span_to_prev_source(sp) {
|
||||
Err(_) => None,
|
||||
Ok(source) => source
|
||||
.split('\n')
|
||||
.last()
|
||||
.map(|last_line| last_line.len() - last_line.trim_start().len()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the source snippet as `String` before the given `Span`.
|
||||
pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
|
||||
self.span_to_source(sp, |src, start_index, _| {
|
||||
src.get(..start_index)
|
||||
.map(|s| s.to_string())
|
||||
.ok_or_else(|| SpanSnippetError::IllFormedSpan(sp))
|
||||
})
|
||||
}
|
||||
|
||||
/// Extends the given `Span` to just after the previous occurrence of `c`. Return the same span
|
||||
/// if no character could be found or if an error occurred while retrieving the code snippet.
|
||||
pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span {
|
||||
if let Ok(prev_source) = self.span_to_prev_source(sp) {
|
||||
let prev_source = prev_source.rsplit(c).nth(0).unwrap_or("").trim_start();
|
||||
if !prev_source.is_empty() && !prev_source.contains('\n') {
|
||||
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
|
||||
}
|
||||
}
|
||||
|
||||
sp
|
||||
}
|
||||
|
||||
/// Extends the given `Span` to just after the previous occurrence of `pat` when surrounded by
|
||||
/// whitespace. Returns the same span if no character could be found or if an error occurred
|
||||
/// while retrieving the code snippet.
|
||||
pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str, accept_newlines: bool) -> Span {
|
||||
// assure that the pattern is delimited, to avoid the following
|
||||
// fn my_fn()
|
||||
// ^^^^ returned span without the check
|
||||
// ---------- correct span
|
||||
for ws in &[" ", "\t", "\n"] {
|
||||
let pat = pat.to_owned() + ws;
|
||||
if let Ok(prev_source) = self.span_to_prev_source(sp) {
|
||||
let prev_source = prev_source.rsplit(&pat).nth(0).unwrap_or("").trim_start();
|
||||
if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
|
||||
return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sp
|
||||
}
|
||||
|
||||
/// Given a `Span`, tries to get a shorter span ending before the first occurrence of `char`
|
||||
/// `c`.
|
||||
pub fn span_until_char(&self, sp: Span, c: char) -> Span {
|
||||
match self.span_to_snippet(sp) {
|
||||
Ok(snippet) => {
|
||||
let snippet = snippet.split(c).nth(0).unwrap_or("").trim_end();
|
||||
if !snippet.is_empty() && !snippet.contains('\n') {
|
||||
sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32))
|
||||
} else {
|
||||
sp
|
||||
}
|
||||
}
|
||||
_ => sp,
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a `Span`, tries to get a shorter span ending just after the first occurrence of `char`
|
||||
/// `c`.
|
||||
pub fn span_through_char(&self, sp: Span, c: char) -> Span {
|
||||
if let Ok(snippet) = self.span_to_snippet(sp) {
|
||||
if let Some(offset) = snippet.find(c) {
|
||||
return sp.with_hi(BytePos(sp.lo().0 + (offset + c.len_utf8()) as u32));
|
||||
}
|
||||
}
|
||||
sp
|
||||
}
|
||||
|
||||
/// Given a `Span`, gets a new `Span` covering the first token and all its trailing whitespace
|
||||
/// or the original `Span`.
|
||||
///
|
||||
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
|
||||
pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
|
||||
let mut whitespace_found = false;
|
||||
|
||||
self.span_take_while(sp, |c| {
|
||||
if !whitespace_found && c.is_whitespace() {
|
||||
whitespace_found = true;
|
||||
}
|
||||
|
||||
if whitespace_found && !c.is_whitespace() { false } else { true }
|
||||
})
|
||||
}
|
||||
|
||||
/// Given a `Span`, gets a new `Span` covering the first token without its trailing whitespace
|
||||
/// or the original `Span` in case of error.
|
||||
///
|
||||
/// If `sp` points to `"let mut x"`, then a span pointing at `"let"` will be returned.
|
||||
pub fn span_until_whitespace(&self, sp: Span) -> Span {
|
||||
self.span_take_while(sp, |c| !c.is_whitespace())
|
||||
}
|
||||
|
||||
/// Given a `Span`, gets a shorter one until `predicate` yields `false`.
|
||||
pub fn span_take_while<P>(&self, sp: Span, predicate: P) -> Span
|
||||
where
|
||||
P: for<'r> FnMut(&'r char) -> bool,
|
||||
{
|
||||
if let Ok(snippet) = self.span_to_snippet(sp) {
|
||||
let offset = snippet.chars().take_while(predicate).map(|c| c.len_utf8()).sum::<usize>();
|
||||
|
||||
sp.with_hi(BytePos(sp.lo().0 + (offset as u32)))
|
||||
} else {
|
||||
sp
|
||||
}
|
||||
}
|
||||
|
||||
pub fn def_span(&self, sp: Span) -> Span {
|
||||
self.span_until_char(sp, '{')
|
||||
}
|
||||
|
||||
/// Returns a new span representing just the start point of this span.
|
||||
pub fn start_point(&self, sp: Span) -> Span {
|
||||
let pos = sp.lo().0;
|
||||
let width = self.find_width_of_character_at_span(sp, false);
|
||||
let corrected_start_position = pos.checked_add(width).unwrap_or(pos);
|
||||
let end_point = BytePos(cmp::max(corrected_start_position, sp.lo().0));
|
||||
sp.with_hi(end_point)
|
||||
}
|
||||
|
||||
/// Returns a new span representing just the end point of this span.
|
||||
pub fn end_point(&self, sp: Span) -> Span {
|
||||
let pos = sp.hi().0;
|
||||
|
||||
let width = self.find_width_of_character_at_span(sp, false);
|
||||
let corrected_end_position = pos.checked_sub(width).unwrap_or(pos);
|
||||
|
||||
let end_point = BytePos(cmp::max(corrected_end_position, sp.lo().0));
|
||||
sp.with_lo(end_point)
|
||||
}
|
||||
|
||||
/// Returns a new span representing the next character after the end-point of this span.
|
||||
pub fn next_point(&self, sp: Span) -> Span {
|
||||
let start_of_next_point = sp.hi().0;
|
||||
|
||||
let width = self.find_width_of_character_at_span(sp, true);
|
||||
// If the width is 1, then the next span should point to the same `lo` and `hi`. However,
|
||||
// in the case of a multibyte character, where the width != 1, the next span should
|
||||
// span multiple bytes to include the whole character.
|
||||
let end_of_next_point =
|
||||
start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
|
||||
|
||||
let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
|
||||
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt())
|
||||
}
|
||||
|
||||
/// Finds the width of a character, either before or after the provided span.
|
||||
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
|
||||
let sp = sp.data();
|
||||
if sp.lo == sp.hi {
|
||||
debug!("find_width_of_character_at_span: early return empty span");
|
||||
return 1;
|
||||
}
|
||||
|
||||
let local_begin = self.lookup_byte_offset(sp.lo);
|
||||
let local_end = self.lookup_byte_offset(sp.hi);
|
||||
debug!(
|
||||
"find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
|
||||
local_begin, local_end
|
||||
);
|
||||
|
||||
if local_begin.sf.start_pos != local_end.sf.start_pos {
|
||||
debug!("find_width_of_character_at_span: begin and end are in different files");
|
||||
return 1;
|
||||
}
|
||||
|
||||
let start_index = local_begin.pos.to_usize();
|
||||
let end_index = local_end.pos.to_usize();
|
||||
debug!(
|
||||
"find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
|
||||
start_index, end_index
|
||||
);
|
||||
|
||||
// Disregard indexes that are at the start or end of their spans, they can't fit bigger
|
||||
// characters.
|
||||
if (!forwards && end_index == usize::min_value())
|
||||
|| (forwards && start_index == usize::max_value())
|
||||
{
|
||||
debug!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
|
||||
return 1;
|
||||
}
|
||||
|
||||
let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
|
||||
debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
|
||||
// Ensure indexes are also not malformed.
|
||||
if start_index > end_index || end_index > source_len {
|
||||
debug!("find_width_of_character_at_span: source indexes are malformed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
let src = local_begin.sf.external_src.borrow();
|
||||
|
||||
// We need to extend the snippet to the end of the src rather than to end_index so when
|
||||
// searching forwards for boundaries we've got somewhere to search.
|
||||
let snippet = if let Some(ref src) = local_begin.sf.src {
|
||||
let len = src.len();
|
||||
(&src[start_index..len])
|
||||
} else if let Some(src) = src.get_source() {
|
||||
let len = src.len();
|
||||
(&src[start_index..len])
|
||||
} else {
|
||||
return 1;
|
||||
};
|
||||
debug!("find_width_of_character_at_span: snippet=`{:?}`", snippet);
|
||||
|
||||
let mut target = if forwards { end_index + 1 } else { end_index - 1 };
|
||||
debug!("find_width_of_character_at_span: initial target=`{:?}`", target);
|
||||
|
||||
while !snippet.is_char_boundary(target - start_index) && target < source_len {
|
||||
target = if forwards {
|
||||
target + 1
|
||||
} else {
|
||||
match target.checked_sub(1) {
|
||||
Some(target) => target,
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
debug!("find_width_of_character_at_span: target=`{:?}`", target);
|
||||
}
|
||||
debug!("find_width_of_character_at_span: final target=`{:?}`", target);
|
||||
|
||||
if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
|
||||
}
|
||||
|
||||
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
|
||||
for sf in self.files.borrow().source_files.iter() {
|
||||
if *filename == sf.name {
|
||||
return Some(sf.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
|
||||
pub fn lookup_byte_offset(&self, bpos: BytePos) -> SourceFileAndBytePos {
|
||||
let idx = self.lookup_source_file_idx(bpos);
|
||||
let sf = (*self.files.borrow().source_files)[idx].clone();
|
||||
let offset = bpos - sf.start_pos;
|
||||
SourceFileAndBytePos { sf, pos: offset }
|
||||
}
|
||||
|
||||
/// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`.
|
||||
pub fn bytepos_to_file_charpos(&self, bpos: BytePos) -> CharPos {
|
||||
let idx = self.lookup_source_file_idx(bpos);
|
||||
let map = &(*self.files.borrow().source_files)[idx];
|
||||
|
||||
// The number of extra bytes due to multibyte chars in the `SourceFile`.
|
||||
let mut total_extra_bytes = 0;
|
||||
|
||||
for mbc in map.multibyte_chars.iter() {
|
||||
debug!("{}-byte char at {:?}", mbc.bytes, mbc.pos);
|
||||
if mbc.pos < bpos {
|
||||
// Every character is at least one byte, so we only
|
||||
// count the actual extra bytes.
|
||||
total_extra_bytes += mbc.bytes as u32 - 1;
|
||||
// We should never see a byte position in the middle of a
|
||||
// character.
|
||||
assert!(bpos.to_u32() >= mbc.pos.to_u32() + mbc.bytes as u32);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert!(map.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32());
|
||||
CharPos(bpos.to_usize() - map.start_pos.to_usize() - total_extra_bytes as usize)
|
||||
}
|
||||
|
||||
// Returns the index of the `SourceFile` (in `self.files`) that contains `pos`.
|
||||
pub fn lookup_source_file_idx(&self, pos: BytePos) -> usize {
|
||||
self.files
|
||||
.borrow()
|
||||
.source_files
|
||||
.binary_search_by_key(&pos, |key| key.start_pos)
|
||||
.unwrap_or_else(|p| p - 1)
|
||||
}
|
||||
|
||||
pub fn count_lines(&self) -> usize {
|
||||
self.files().iter().fold(0, |a, f| a + f.count_lines())
|
||||
}
|
||||
|
||||
pub fn generate_fn_name_span(&self, span: Span) -> Option<Span> {
|
||||
let prev_span = self.span_extend_to_prev_str(span, "fn", true);
|
||||
self.span_to_snippet(prev_span)
|
||||
.map(|snippet| {
|
||||
let len = snippet
|
||||
.find(|c: char| !c.is_alphanumeric() && c != '_')
|
||||
.expect("no label after fn");
|
||||
prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32))
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Takes the span of a type parameter in a function signature and try to generate a span for
|
||||
/// the function name (with generics) and a new snippet for this span with the pointed type
|
||||
/// parameter as a new local type parameter.
|
||||
///
|
||||
/// For instance:
|
||||
/// ```rust,ignore (pseudo-Rust)
|
||||
/// // Given span
|
||||
/// fn my_function(param: T)
|
||||
/// // ^ Original span
|
||||
///
|
||||
/// // Result
|
||||
/// fn my_function(param: T)
|
||||
/// // ^^^^^^^^^^^ Generated span with snippet `my_function<T>`
|
||||
/// ```
|
||||
///
|
||||
/// Attention: The method used is very fragile since it essentially duplicates the work of the
|
||||
/// parser. If you need to use this function or something similar, please consider updating the
|
||||
/// `SourceMap` functions and this function to something more robust.
|
||||
pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, String)> {
|
||||
// Try to extend the span to the previous "fn" keyword to retrieve the function
|
||||
// signature.
|
||||
let sugg_span = self.span_extend_to_prev_str(span, "fn", false);
|
||||
if sugg_span != span {
|
||||
if let Ok(snippet) = self.span_to_snippet(sugg_span) {
|
||||
// Consume the function name.
|
||||
let mut offset = snippet
|
||||
.find(|c: char| !c.is_alphanumeric() && c != '_')
|
||||
.expect("no label after fn");
|
||||
|
||||
// Consume the generics part of the function signature.
|
||||
let mut bracket_counter = 0;
|
||||
let mut last_char = None;
|
||||
for c in snippet[offset..].chars() {
|
||||
match c {
|
||||
'<' => bracket_counter += 1,
|
||||
'>' => bracket_counter -= 1,
|
||||
'(' => {
|
||||
if bracket_counter == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
offset += c.len_utf8();
|
||||
last_char = Some(c);
|
||||
}
|
||||
|
||||
// Adjust the suggestion span to encompass the function name with its generics.
|
||||
let sugg_span = sugg_span.with_hi(BytePos(sugg_span.lo().0 + offset as u32));
|
||||
|
||||
// Prepare the new suggested snippet to append the type parameter that triggered
|
||||
// the error in the generics of the function signature.
|
||||
let mut new_snippet = if last_char == Some('>') {
|
||||
format!("{}, ", &snippet[..(offset - '>'.len_utf8())])
|
||||
} else {
|
||||
format!("{}<", &snippet[..offset])
|
||||
};
|
||||
new_snippet
|
||||
.push_str(&self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string()));
|
||||
new_snippet.push('>');
|
||||
|
||||
return Some((sugg_span, new_snippet));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
|
||||
source_file.add_external_src(|| match source_file.name {
|
||||
FileName::Real(ref name) => self.file_loader.read_file(name).ok(),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
pub fn call_span_if_macro(&self, sp: Span) -> Span {
|
||||
if self.span_to_filename(sp.clone()).is_macros() {
|
||||
let v = sp.macro_backtrace();
|
||||
if let Some(use_site) = v.last() {
|
||||
return use_site.call_site;
|
||||
}
|
||||
}
|
||||
sp
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FilePathMapping {
|
||||
mapping: Vec<(PathBuf, PathBuf)>,
|
||||
}
|
||||
|
||||
impl FilePathMapping {
|
||||
pub fn empty() -> FilePathMapping {
|
||||
FilePathMapping { mapping: vec![] }
|
||||
}
|
||||
|
||||
pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
|
||||
FilePathMapping { mapping }
|
||||
}
|
||||
|
||||
/// Applies any path prefix substitution as defined by the mapping.
|
||||
/// The return value is the remapped path and a boolean indicating whether
|
||||
/// the path was affected by the mapping.
|
||||
pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
|
||||
// NOTE: We are iterating over the mapping entries from last to first
|
||||
// because entries specified later on the command line should
|
||||
// take precedence.
|
||||
for &(ref from, ref to) in self.mapping.iter().rev() {
|
||||
if let Ok(rest) = path.strip_prefix(from) {
|
||||
return (to.join(rest), true);
|
||||
}
|
||||
}
|
||||
|
||||
(path, false)
|
||||
}
|
||||
}
|
||||
216
src/librustc_span/source_map/tests.rs
Normal file
216
src/librustc_span/source_map/tests.rs
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
use super::*;
|
||||
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
|
||||
fn init_source_map() -> SourceMap {
|
||||
let sm = SourceMap::new(FilePathMapping::empty());
|
||||
sm.new_source_file(PathBuf::from("blork.rs").into(), "first line.\nsecond line".to_string());
|
||||
sm.new_source_file(PathBuf::from("empty.rs").into(), String::new());
|
||||
sm.new_source_file(PathBuf::from("blork2.rs").into(), "first line.\nsecond line".to_string());
|
||||
sm
|
||||
}
|
||||
|
||||
/// Tests `lookup_byte_offset`.
|
||||
#[test]
|
||||
fn t3() {
|
||||
let sm = init_source_map();
|
||||
|
||||
let srcfbp1 = sm.lookup_byte_offset(BytePos(23));
|
||||
assert_eq!(srcfbp1.sf.name, PathBuf::from("blork.rs").into());
|
||||
assert_eq!(srcfbp1.pos, BytePos(23));
|
||||
|
||||
let srcfbp1 = sm.lookup_byte_offset(BytePos(24));
|
||||
assert_eq!(srcfbp1.sf.name, PathBuf::from("empty.rs").into());
|
||||
assert_eq!(srcfbp1.pos, BytePos(0));
|
||||
|
||||
let srcfbp2 = sm.lookup_byte_offset(BytePos(25));
|
||||
assert_eq!(srcfbp2.sf.name, PathBuf::from("blork2.rs").into());
|
||||
assert_eq!(srcfbp2.pos, BytePos(0));
|
||||
}
|
||||
|
||||
/// Tests `bytepos_to_file_charpos`.
|
||||
#[test]
|
||||
fn t4() {
|
||||
let sm = init_source_map();
|
||||
|
||||
let cp1 = sm.bytepos_to_file_charpos(BytePos(22));
|
||||
assert_eq!(cp1, CharPos(22));
|
||||
|
||||
let cp2 = sm.bytepos_to_file_charpos(BytePos(25));
|
||||
assert_eq!(cp2, CharPos(0));
|
||||
}
|
||||
|
||||
/// Tests zero-length `SourceFile`s.
|
||||
#[test]
|
||||
fn t5() {
|
||||
let sm = init_source_map();
|
||||
|
||||
let loc1 = sm.lookup_char_pos(BytePos(22));
|
||||
assert_eq!(loc1.file.name, PathBuf::from("blork.rs").into());
|
||||
assert_eq!(loc1.line, 2);
|
||||
assert_eq!(loc1.col, CharPos(10));
|
||||
|
||||
let loc2 = sm.lookup_char_pos(BytePos(25));
|
||||
assert_eq!(loc2.file.name, PathBuf::from("blork2.rs").into());
|
||||
assert_eq!(loc2.line, 1);
|
||||
assert_eq!(loc2.col, CharPos(0));
|
||||
}
|
||||
|
||||
fn init_source_map_mbc() -> SourceMap {
|
||||
let sm = SourceMap::new(FilePathMapping::empty());
|
||||
// "€" is a three-byte UTF8 char.
|
||||
sm.new_source_file(
|
||||
PathBuf::from("blork.rs").into(),
|
||||
"fir€st €€€€ line.\nsecond line".to_string(),
|
||||
);
|
||||
sm.new_source_file(
|
||||
PathBuf::from("blork2.rs").into(),
|
||||
"first line€€.\n€ second line".to_string(),
|
||||
);
|
||||
sm
|
||||
}
|
||||
|
||||
/// Tests `bytepos_to_file_charpos` in the presence of multi-byte chars.
|
||||
#[test]
|
||||
fn t6() {
|
||||
let sm = init_source_map_mbc();
|
||||
|
||||
let cp1 = sm.bytepos_to_file_charpos(BytePos(3));
|
||||
assert_eq!(cp1, CharPos(3));
|
||||
|
||||
let cp2 = sm.bytepos_to_file_charpos(BytePos(6));
|
||||
assert_eq!(cp2, CharPos(4));
|
||||
|
||||
let cp3 = sm.bytepos_to_file_charpos(BytePos(56));
|
||||
assert_eq!(cp3, CharPos(12));
|
||||
|
||||
let cp4 = sm.bytepos_to_file_charpos(BytePos(61));
|
||||
assert_eq!(cp4, CharPos(15));
|
||||
}
|
||||
|
||||
/// Test `span_to_lines` for a span ending at the end of a `SourceFile`.
|
||||
#[test]
|
||||
fn t7() {
|
||||
let sm = init_source_map();
|
||||
let span = Span::with_root_ctxt(BytePos(12), BytePos(23));
|
||||
let file_lines = sm.span_to_lines(span).unwrap();
|
||||
|
||||
assert_eq!(file_lines.file.name, PathBuf::from("blork.rs").into());
|
||||
assert_eq!(file_lines.lines.len(), 1);
|
||||
assert_eq!(file_lines.lines[0].line_index, 1);
|
||||
}
|
||||
|
||||
/// Given a string like " ~~~~~~~~~~~~ ", produces a span
|
||||
/// converting that range. The idea is that the string has the same
|
||||
/// length as the input, and we uncover the byte positions. Note
|
||||
/// that this can span lines and so on.
|
||||
fn span_from_selection(input: &str, selection: &str) -> Span {
|
||||
assert_eq!(input.len(), selection.len());
|
||||
let left_index = selection.find('~').unwrap() as u32;
|
||||
let right_index = selection.rfind('~').map(|x| x as u32).unwrap_or(left_index);
|
||||
Span::with_root_ctxt(BytePos(left_index), BytePos(right_index + 1))
|
||||
}
|
||||
|
||||
/// Tests `span_to_snippet` and `span_to_lines` for a span converting 3
|
||||
/// lines in the middle of a file.
|
||||
#[test]
|
||||
fn span_to_snippet_and_lines_spanning_multiple_lines() {
|
||||
let sm = SourceMap::new(FilePathMapping::empty());
|
||||
let inputtext = "aaaaa\nbbbbBB\nCCC\nDDDDDddddd\neee\n";
|
||||
let selection = " \n ~~\n~~~\n~~~~~ \n \n";
|
||||
sm.new_source_file(Path::new("blork.rs").to_owned().into(), inputtext.to_string());
|
||||
let span = span_from_selection(inputtext, selection);
|
||||
|
||||
// Check that we are extracting the text we thought we were extracting.
|
||||
assert_eq!(&sm.span_to_snippet(span).unwrap(), "BB\nCCC\nDDDDD");
|
||||
|
||||
// Check that span_to_lines gives us the complete result with the lines/cols we expected.
|
||||
let lines = sm.span_to_lines(span).unwrap();
|
||||
let expected = vec![
|
||||
LineInfo { line_index: 1, start_col: CharPos(4), end_col: CharPos(6) },
|
||||
LineInfo { line_index: 2, start_col: CharPos(0), end_col: CharPos(3) },
|
||||
LineInfo { line_index: 3, start_col: CharPos(0), end_col: CharPos(5) },
|
||||
];
|
||||
assert_eq!(lines.lines, expected);
|
||||
}
|
||||
|
||||
/// Test span_to_snippet for a span ending at the end of a `SourceFile`.
|
||||
#[test]
|
||||
fn t8() {
|
||||
let sm = init_source_map();
|
||||
let span = Span::with_root_ctxt(BytePos(12), BytePos(23));
|
||||
let snippet = sm.span_to_snippet(span);
|
||||
|
||||
assert_eq!(snippet, Ok("second line".to_string()));
|
||||
}
|
||||
|
||||
/// Test `span_to_str` for a span ending at the end of a `SourceFile`.
|
||||
#[test]
|
||||
fn t9() {
|
||||
let sm = init_source_map();
|
||||
let span = Span::with_root_ctxt(BytePos(12), BytePos(23));
|
||||
let sstr = sm.span_to_string(span);
|
||||
|
||||
assert_eq!(sstr, "blork.rs:2:1: 2:12");
|
||||
}
|
||||
|
||||
/// Tests failing to merge two spans on different lines.
|
||||
#[test]
|
||||
fn span_merging_fail() {
|
||||
let sm = SourceMap::new(FilePathMapping::empty());
|
||||
let inputtext = "bbbb BB\ncc CCC\n";
|
||||
let selection1 = " ~~\n \n";
|
||||
let selection2 = " \n ~~~\n";
|
||||
sm.new_source_file(Path::new("blork.rs").to_owned().into(), inputtext.to_owned());
|
||||
let span1 = span_from_selection(inputtext, selection1);
|
||||
let span2 = span_from_selection(inputtext, selection2);
|
||||
|
||||
assert!(sm.merge_spans(span1, span2).is_none());
|
||||
}
|
||||
|
||||
/// Returns the span corresponding to the `n`th occurrence of `substring` in `source_text`.
|
||||
trait SourceMapExtension {
|
||||
fn span_substr(
|
||||
&self,
|
||||
file: &Lrc<SourceFile>,
|
||||
source_text: &str,
|
||||
substring: &str,
|
||||
n: usize,
|
||||
) -> Span;
|
||||
}
|
||||
|
||||
impl SourceMapExtension for SourceMap {
|
||||
fn span_substr(
|
||||
&self,
|
||||
file: &Lrc<SourceFile>,
|
||||
source_text: &str,
|
||||
substring: &str,
|
||||
n: usize,
|
||||
) -> Span {
|
||||
println!(
|
||||
"span_substr(file={:?}/{:?}, substring={:?}, n={})",
|
||||
file.name, file.start_pos, substring, n
|
||||
);
|
||||
let mut i = 0;
|
||||
let mut hi = 0;
|
||||
loop {
|
||||
let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
|
||||
panic!(
|
||||
"source_text `{}` does not have {} occurrences of `{}`, only {}",
|
||||
source_text, n, substring, i
|
||||
);
|
||||
});
|
||||
let lo = hi + offset;
|
||||
hi = lo + substring.len();
|
||||
if i == n {
|
||||
let span = Span::with_root_ctxt(
|
||||
BytePos(lo as u32 + file.start_pos.0),
|
||||
BytePos(hi as u32 + file.start_pos.0),
|
||||
);
|
||||
assert_eq!(&self.span_to_snippet(span).unwrap()[..], substring);
|
||||
return span;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
140
src/librustc_span/span_encoding.rs
Normal file
140
src/librustc_span/span_encoding.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
// Spans are encoded using 1-bit tag and 2 different encoding formats (one for each tag value).
|
||||
// One format is used for keeping span data inline,
|
||||
// another contains index into an out-of-line span interner.
|
||||
// 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
|
||||
|
||||
use crate::hygiene::SyntaxContext;
|
||||
use crate::GLOBALS;
|
||||
use crate::{BytePos, SpanData};
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
|
||||
/// A compressed span.
|
||||
///
|
||||
/// `SpanData` is 12 bytes, which is a bit too big to stick everywhere. `Span`
|
||||
/// is a form that only takes up 8 bytes, with less space for the length and
|
||||
/// context. The vast majority (99.9%+) of `SpanData` instances will fit within
|
||||
/// those 8 bytes; any `SpanData` whose fields don't fit into a `Span` are
|
||||
/// stored in a separate interner table, and the `Span` will index into that
|
||||
/// table. Interning is rare enough that the cost is low, but common enough
|
||||
/// that the code is exercised regularly.
|
||||
///
|
||||
/// An earlier version of this code used only 4 bytes for `Span`, but that was
|
||||
/// slower because only 80--90% of spans could be stored inline (even less in
|
||||
/// very large crates) and so the interner was used a lot more.
|
||||
///
|
||||
/// Inline (compressed) format:
|
||||
/// - `span.base_or_index == span_data.lo`
|
||||
/// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`)
|
||||
/// - `span.ctxt == span_data.ctxt` (must be `<= MAX_CTXT`)
|
||||
///
|
||||
/// Interned format:
|
||||
/// - `span.base_or_index == index` (indexes into the interner table)
|
||||
/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero)
|
||||
/// - `span.ctxt == 0`
|
||||
///
|
||||
/// The inline form uses 0 for the tag value (rather than 1) so that we don't
|
||||
/// need to mask out the tag bit when getting the length, and so that the
|
||||
/// dummy span can be all zeroes.
|
||||
///
|
||||
/// Notes about the choice of field sizes:
|
||||
/// - `base` is 32 bits in both `Span` and `SpanData`, which means that `base`
|
||||
/// values never cause interning. The number of bits needed for `base`
|
||||
/// depends on the crate size. 32 bits allows up to 4 GiB of code in a crate.
|
||||
/// `script-servo` is the largest crate in `rustc-perf`, requiring 26 bits
|
||||
/// for some spans.
|
||||
/// - `len` is 15 bits in `Span` (a u16, minus 1 bit for the tag) and 32 bits
|
||||
/// in `SpanData`, which means that large `len` values will cause interning.
|
||||
/// The number of bits needed for `len` does not depend on the crate size.
|
||||
/// The most common number of bits for `len` are 0--7, with a peak usually at
|
||||
/// 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough
|
||||
/// for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur
|
||||
/// dozens of times in a typical crate.
|
||||
/// - `ctxt` is 16 bits in `Span` and 32 bits in `SpanData`, which means that
|
||||
/// large `ctxt` values will cause interning. The number of bits needed for
|
||||
/// `ctxt` values depend partly on the crate size and partly on the form of
|
||||
/// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt`,
|
||||
/// but larger crates might need more than 16 bits.
|
||||
///
|
||||
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub struct Span {
|
||||
base_or_index: u32,
|
||||
len_or_tag: u16,
|
||||
ctxt_or_zero: u16,
|
||||
}
|
||||
|
||||
const LEN_TAG: u16 = 0b1000_0000_0000_0000;
|
||||
const MAX_LEN: u32 = 0b0111_1111_1111_1111;
|
||||
const MAX_CTXT: u32 = 0b1111_1111_1111_1111;
|
||||
|
||||
/// Dummy span, both position and length are zero, syntax context is zero as well.
|
||||
pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_zero: 0 };
|
||||
|
||||
impl Span {
|
||||
#[inline]
|
||||
pub fn new(mut lo: BytePos, mut hi: BytePos, ctxt: SyntaxContext) -> Self {
|
||||
if lo > hi {
|
||||
std::mem::swap(&mut lo, &mut hi);
|
||||
}
|
||||
|
||||
let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
|
||||
|
||||
if len <= MAX_LEN && ctxt2 <= MAX_CTXT {
|
||||
// Inline format.
|
||||
Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_zero: ctxt2 as u16 }
|
||||
} else {
|
||||
// Interned format.
|
||||
let index = with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt }));
|
||||
Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_zero: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn data(self) -> SpanData {
|
||||
if self.len_or_tag != LEN_TAG {
|
||||
// Inline format.
|
||||
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
|
||||
SpanData {
|
||||
lo: BytePos(self.base_or_index),
|
||||
hi: BytePos(self.base_or_index + self.len_or_tag as u32),
|
||||
ctxt: SyntaxContext::from_u32(self.ctxt_or_zero as u32),
|
||||
}
|
||||
} else {
|
||||
// Interned format.
|
||||
debug_assert!(self.ctxt_or_zero == 0);
|
||||
let index = self.base_or_index;
|
||||
with_span_interner(|interner| *interner.get(index))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SpanInterner {
|
||||
spans: FxHashMap<SpanData, u32>,
|
||||
span_data: Vec<SpanData>,
|
||||
}
|
||||
|
||||
impl SpanInterner {
|
||||
fn intern(&mut self, span_data: &SpanData) -> u32 {
|
||||
if let Some(index) = self.spans.get(span_data) {
|
||||
return *index;
|
||||
}
|
||||
|
||||
let index = self.spans.len() as u32;
|
||||
self.span_data.push(*span_data);
|
||||
self.spans.insert(*span_data, index);
|
||||
index
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get(&self, index: u32) -> &SpanData {
|
||||
&self.span_data[index as usize]
|
||||
}
|
||||
}
|
||||
|
||||
// If an interner exists, return it. Otherwise, prepare a fresh one.
|
||||
#[inline]
|
||||
fn with_span_interner<T, F: FnOnce(&mut SpanInterner) -> T>(f: F) -> T {
|
||||
GLOBALS.with(|globals| f(&mut *globals.span_interner.lock()))
|
||||
}
|
||||
1213
src/librustc_span/symbol.rs
Normal file
1213
src/librustc_span/symbol.rs
Normal file
File diff suppressed because it is too large
Load diff
25
src/librustc_span/symbol/tests.rs
Normal file
25
src/librustc_span/symbol/tests.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use super::*;
|
||||
|
||||
use crate::{edition, Globals};
|
||||
|
||||
#[test]
|
||||
fn interner_tests() {
|
||||
let mut i: Interner = Interner::default();
|
||||
// first one is zero:
|
||||
assert_eq!(i.intern("dog"), Symbol::new(0));
|
||||
// re-use gets the same entry:
|
||||
assert_eq!(i.intern("dog"), Symbol::new(0));
|
||||
// different string gets a different #:
|
||||
assert_eq!(i.intern("cat"), Symbol::new(1));
|
||||
assert_eq!(i.intern("cat"), Symbol::new(1));
|
||||
// dog is still at zero
|
||||
assert_eq!(i.intern("dog"), Symbol::new(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_first_quote_test() {
|
||||
GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
|
||||
let i = Ident::from_str("'break");
|
||||
assert_eq!(i.without_first_quote().name, kw::Break);
|
||||
});
|
||||
}
|
||||
40
src/librustc_span/tests.rs
Normal file
40
src/librustc_span/tests.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_lookup_line() {
|
||||
let lines = &[BytePos(3), BytePos(17), BytePos(28)];
|
||||
|
||||
assert_eq!(lookup_line(lines, BytePos(0)), -1);
|
||||
assert_eq!(lookup_line(lines, BytePos(3)), 0);
|
||||
assert_eq!(lookup_line(lines, BytePos(4)), 0);
|
||||
|
||||
assert_eq!(lookup_line(lines, BytePos(16)), 0);
|
||||
assert_eq!(lookup_line(lines, BytePos(17)), 1);
|
||||
assert_eq!(lookup_line(lines, BytePos(18)), 1);
|
||||
|
||||
assert_eq!(lookup_line(lines, BytePos(28)), 2);
|
||||
assert_eq!(lookup_line(lines, BytePos(29)), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_newlines() {
|
||||
fn check(before: &str, after: &str, expected_positions: &[u32]) {
|
||||
let mut actual = before.to_string();
|
||||
let mut actual_positions = vec![];
|
||||
normalize_newlines(&mut actual, &mut actual_positions);
|
||||
let actual_positions: Vec<_> = actual_positions.into_iter().map(|nc| nc.pos.0).collect();
|
||||
assert_eq!(actual.as_str(), after);
|
||||
assert_eq!(actual_positions, expected_positions);
|
||||
}
|
||||
check("", "", &[]);
|
||||
check("\n", "\n", &[]);
|
||||
check("\r", "\r", &[]);
|
||||
check("\r\r", "\r\r", &[]);
|
||||
check("\r\n", "\n", &[1]);
|
||||
check("hello world", "hello world", &[]);
|
||||
check("hello\nworld", "hello\nworld", &[]);
|
||||
check("hello\r\nworld", "hello\nworld", &[6]);
|
||||
check("\r\nhello\r\nworld\r\n", "\nhello\nworld\n", &[1, 7, 13]);
|
||||
check("\r\r\n", "\r\n", &[2]);
|
||||
check("hello\rworld", "hello\rworld", &[]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue