Add testcase for next_point, fix more trivial issues in find_width_of_character_at_span
This commit is contained in:
parent
0af255a5aa
commit
eb8aa9759d
3 changed files with 59 additions and 8 deletions
|
@ -853,6 +853,10 @@ impl SourceMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a new span representing the next character after the end-point of this span.
|
/// Returns a new span representing the next character after the end-point of this span.
|
||||||
|
/// Special cases:
|
||||||
|
/// - if span is a dummy one, returns the same span
|
||||||
|
/// - if next_point reached the end of source, return span with lo = hi
|
||||||
|
/// - respect multi-byte characters
|
||||||
pub fn next_point(&self, sp: Span) -> Span {
|
pub fn next_point(&self, sp: Span) -> Span {
|
||||||
if sp.is_dummy() {
|
if sp.is_dummy() {
|
||||||
return sp;
|
return sp;
|
||||||
|
@ -860,9 +864,11 @@ impl SourceMap {
|
||||||
let start_of_next_point = sp.hi().0;
|
let start_of_next_point = sp.hi().0;
|
||||||
|
|
||||||
let width = self.find_width_of_character_at_span(sp, true);
|
let width = self.find_width_of_character_at_span(sp, true);
|
||||||
debug_assert!(width > 0);
|
if width == 0 {
|
||||||
// If the width is 1, then the next span should point to the same `lo` and `hi`. However,
|
return Span::new(sp.hi(), sp.hi(), sp.ctxt(), None);
|
||||||
// in the case of a multibyte character, where the width != 1, the next span should
|
}
|
||||||
|
// If the width is 1, then the next span should only contain the next char besides current ending.
|
||||||
|
// However, in the case of a multibyte character, where the width != 1, the next span should
|
||||||
// span multiple bytes to include the whole character.
|
// span multiple bytes to include the whole character.
|
||||||
let end_of_next_point =
|
let end_of_next_point =
|
||||||
start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
|
start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
|
||||||
|
@ -875,7 +881,8 @@ impl SourceMap {
|
||||||
/// depending on the `forwards` parameter.
|
/// depending on the `forwards` parameter.
|
||||||
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
|
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
|
||||||
let sp = sp.data();
|
let sp = sp.data();
|
||||||
if sp.lo == sp.hi {
|
|
||||||
|
if sp.lo == sp.hi && !forwards {
|
||||||
debug!("find_width_of_character_at_span: early return empty span");
|
debug!("find_width_of_character_at_span: early return empty span");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -909,9 +916,9 @@ impl SourceMap {
|
||||||
let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
|
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);
|
debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
|
||||||
// Ensure indexes are also not malformed.
|
// Ensure indexes are also not malformed.
|
||||||
if start_index > end_index || end_index > source_len {
|
if start_index > end_index || end_index > source_len - 1 {
|
||||||
debug!("find_width_of_character_at_span: source indexes are malformed");
|
debug!("find_width_of_character_at_span: source indexes are malformed");
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let src = local_begin.sf.external_src.borrow();
|
let src = local_begin.sf.external_src.borrow();
|
||||||
|
|
|
@ -479,3 +479,48 @@ fn path_prefix_remapping_expand_to_absolute() {
|
||||||
RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") }
|
RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_next_point() {
|
||||||
|
let sm = SourceMap::new(FilePathMapping::empty());
|
||||||
|
sm.new_source_file(PathBuf::from("example.rs").into(), "a…b".to_string());
|
||||||
|
|
||||||
|
// Dummy spans don't advance.
|
||||||
|
let span = DUMMY_SP;
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(span.lo().0, 0);
|
||||||
|
assert_eq!(span.hi().0, 0);
|
||||||
|
|
||||||
|
// Span advance respect multi-byte character
|
||||||
|
let span = Span::with_root_ctxt(BytePos(0), BytePos(1));
|
||||||
|
assert_eq!(sm.span_to_snippet(span), Ok("a".to_string()));
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(sm.span_to_snippet(span), Ok("…".to_string()));
|
||||||
|
assert_eq!(span.lo().0, 1);
|
||||||
|
assert_eq!(span.hi().0, 4);
|
||||||
|
|
||||||
|
// An empty span pointing just before a multi-byte character should
|
||||||
|
// advance to contain the multi-byte character.
|
||||||
|
let span = Span::with_root_ctxt(BytePos(1), BytePos(1));
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(span.lo().0, 1);
|
||||||
|
assert_eq!(span.hi().0, 4);
|
||||||
|
|
||||||
|
let span = Span::with_root_ctxt(BytePos(1), BytePos(4));
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(span.lo().0, 4);
|
||||||
|
assert_eq!(span.hi().0, 5);
|
||||||
|
|
||||||
|
// A non-empty span at the last byte should advance to create an empty
|
||||||
|
// span pointing at the end of the file.
|
||||||
|
let span = Span::with_root_ctxt(BytePos(4), BytePos(5));
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(span.lo().0, 5);
|
||||||
|
assert_eq!(span.hi().0, 5);
|
||||||
|
|
||||||
|
// Empty span pointing just past the last byte.
|
||||||
|
let span = Span::with_root_ctxt(BytePos(5), BytePos(5));
|
||||||
|
let span = sm.next_point(span);
|
||||||
|
assert_eq!(span.lo().0, 5);
|
||||||
|
assert_eq!(span.hi().0, 5);
|
||||||
|
}
|
||||||
|
|
|
@ -769,8 +769,7 @@ impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
|
||||||
|
|
||||||
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability) {
|
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability) {
|
||||||
let mut remove_span = item;
|
let mut remove_span = item;
|
||||||
let hi = cx.sess().source_map().next_point(remove_span).hi();
|
let fmpos = cx.sess().source_map().lookup_byte_offset(remove_span.hi());
|
||||||
let fmpos = cx.sess().source_map().lookup_byte_offset(hi);
|
|
||||||
|
|
||||||
if let Some(ref src) = fmpos.sf.src {
|
if let Some(ref src) = fmpos.sf.src {
|
||||||
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');
|
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue