Skip to content

Commit 53728ff

Browse files
committed
Auto merge of #103185 - chenyukang:yukang/fix-span-next-point, r=davidtwco
Fix the bug of next_point in source_map There is a bug in `next_point`, the new span won't move to next position when be called in the first time. For this reason, our current code is working like this: 1. When we really want to move to the next position, we called two times of `next_point` 2. Some code which use `next_point` actually done the same thing with `shrink_to_hi` This fix make sure when `next_point` is called, span will move with the width at least 1, and also work correctly in the scenario of multiple bytes. Ref: #103140 (comment) r? `@davidtwco`
2 parents 4b3b731 + eb8aa97 commit 53728ff

File tree

9 files changed

+71
-20
lines changed

9 files changed

+71
-20
lines changed

compiler/rustc_expand/src/expand.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,12 @@ pub fn ensure_complete_parse<'a>(
937937
kind_name,
938938
);
939939
err.note(&msg);
940-
let semi_span = this.sess.source_map().next_point(span);
941940

942-
let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span));
943-
match this.sess.source_map().span_to_snippet(semi_full_span) {
941+
let semi_span = this.sess.source_map().next_point(span);
942+
match this.sess.source_map().span_to_snippet(semi_span) {
944943
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
945944
err.span_suggestion(
946-
semi_span,
945+
span.shrink_to_hi(),
947946
"you might be missing a semicolon here",
948947
";",
949948
Applicability::MaybeIncorrect,

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn emit_frag_parse_err(
8282
);
8383
if !e.span.is_dummy() {
8484
// early end of macro arm (#52866)
85-
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
85+
e.replace_span_with(parser.token.span.shrink_to_hi());
8686
}
8787
}
8888
if e.span.is_dummy() {

compiler/rustc_parse/src/parser/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ impl<'a> Parser<'a> {
14611461
let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
14621462
// Point at the end of the macro call when reaching end of macro arguments.
14631463
(token::Eof, Some(_)) => {
1464-
let sp = self.sess.source_map().next_point(self.prev_token.span);
1464+
let sp = self.prev_token.span.shrink_to_hi();
14651465
(sp, sp)
14661466
}
14671467
// We don't want to point at the following span after DUMMY_SP.
@@ -2039,7 +2039,7 @@ impl<'a> Parser<'a> {
20392039
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
20402040
let (span, msg) = match (&self.token.kind, self.subparser_name) {
20412041
(&token::Eof, Some(origin)) => {
2042-
let sp = self.sess.source_map().next_point(self.prev_token.span);
2042+
let sp = self.prev_token.span.shrink_to_hi();
20432043
(sp, format!("expected expression, found end of {origin}"))
20442044
}
20452045
_ => (

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ impl<'a> Parser<'a> {
21722172
},
21732173
ExprKind::Block(_, None) => {
21742174
self.sess.emit_err(IfExpressionMissingCondition {
2175-
if_span: self.sess.source_map().next_point(lo),
2175+
if_span: lo.shrink_to_hi(),
21762176
block_span: self.sess.source_map().start_point(cond_span),
21772177
});
21782178
std::mem::replace(&mut cond, this.mk_expr_err(cond_span.shrink_to_hi()))

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ impl<'a> Parser<'a> {
16011601
self.sess.emit_err(err);
16021602
} else {
16031603
if !seen_comma {
1604-
let sp = self.sess.source_map().next_point(previous_span);
1604+
let sp = previous_span.shrink_to_hi();
16051605
err.missing_comma = Some(sp);
16061606
}
16071607
return Err(err.into_diagnostic(&self.sess.span_diagnostic));

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
17311731
for _ in 0..100 {
17321732
// Try to find an assignment
17331733
sp = sm.next_point(sp);
1734-
let snippet = sm.span_to_snippet(sp.to(sm.next_point(sp)));
1734+
let snippet = sm.span_to_snippet(sp);
17351735
match snippet {
17361736
Ok(ref x) if x.as_str() == "=" => {
17371737
err.span_suggestion(

compiler/rustc_span/src/source_map.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -853,28 +853,36 @@ impl SourceMap {
853853
}
854854

855855
/// Returns a new span representing the next character after the end-point of this span.
856+
/// Special cases:
857+
/// - if span is a dummy one, returns the same span
858+
/// - if next_point reached the end of source, return span with lo = hi
859+
/// - respect multi-byte characters
856860
pub fn next_point(&self, sp: Span) -> Span {
857861
if sp.is_dummy() {
858862
return sp;
859863
}
860864
let start_of_next_point = sp.hi().0;
861865

862-
let width = self.find_width_of_character_at_span(sp.shrink_to_hi(), true);
863-
// If the width is 1, then the next span should point to the same `lo` and `hi`. However,
864-
// in the case of a multibyte character, where the width != 1, the next span should
866+
let width = self.find_width_of_character_at_span(sp, true);
867+
if width == 0 {
868+
return Span::new(sp.hi(), sp.hi(), sp.ctxt(), None);
869+
}
870+
// If the width is 1, then the next span should only contain the next char besides current ending.
871+
// However, in the case of a multibyte character, where the width != 1, the next span should
865872
// span multiple bytes to include the whole character.
866873
let end_of_next_point =
867-
start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
874+
start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
868875

869-
let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
876+
let end_of_next_point = BytePos(cmp::max(start_of_next_point + 1, end_of_next_point));
870877
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
871878
}
872879

873880
/// Finds the width of the character, either before or after the end of provided span,
874881
/// depending on the `forwards` parameter.
875882
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
876883
let sp = sp.data();
877-
if sp.lo == sp.hi {
884+
885+
if sp.lo == sp.hi && !forwards {
878886
debug!("find_width_of_character_at_span: early return empty span");
879887
return 1;
880888
}
@@ -908,9 +916,9 @@ impl SourceMap {
908916
let source_len = (local_begin.sf.end_pos - local_begin.sf.start_pos).to_usize();
909917
debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
910918
// Ensure indexes are also not malformed.
911-
if start_index > end_index || end_index > source_len {
919+
if start_index > end_index || end_index > source_len - 1 {
912920
debug!("find_width_of_character_at_span: source indexes are malformed");
913-
return 1;
921+
return 0;
914922
}
915923

916924
let src = local_begin.sf.external_src.borrow();

compiler/rustc_span/src/source_map/tests.rs

+45
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,48 @@ fn path_prefix_remapping_expand_to_absolute() {
479479
RealFileName::Remapped { local_path: None, virtual_name: path("XYZ/src/main.rs") }
480480
);
481481
}
482+
483+
#[test]
484+
fn test_next_point() {
485+
let sm = SourceMap::new(FilePathMapping::empty());
486+
sm.new_source_file(PathBuf::from("example.rs").into(), "a…b".to_string());
487+
488+
// Dummy spans don't advance.
489+
let span = DUMMY_SP;
490+
let span = sm.next_point(span);
491+
assert_eq!(span.lo().0, 0);
492+
assert_eq!(span.hi().0, 0);
493+
494+
// Span advance respect multi-byte character
495+
let span = Span::with_root_ctxt(BytePos(0), BytePos(1));
496+
assert_eq!(sm.span_to_snippet(span), Ok("a".to_string()));
497+
let span = sm.next_point(span);
498+
assert_eq!(sm.span_to_snippet(span), Ok("…".to_string()));
499+
assert_eq!(span.lo().0, 1);
500+
assert_eq!(span.hi().0, 4);
501+
502+
// An empty span pointing just before a multi-byte character should
503+
// advance to contain the multi-byte character.
504+
let span = Span::with_root_ctxt(BytePos(1), BytePos(1));
505+
let span = sm.next_point(span);
506+
assert_eq!(span.lo().0, 1);
507+
assert_eq!(span.hi().0, 4);
508+
509+
let span = Span::with_root_ctxt(BytePos(1), BytePos(4));
510+
let span = sm.next_point(span);
511+
assert_eq!(span.lo().0, 4);
512+
assert_eq!(span.hi().0, 5);
513+
514+
// A non-empty span at the last byte should advance to create an empty
515+
// span pointing at the end of the file.
516+
let span = Span::with_root_ctxt(BytePos(4), BytePos(5));
517+
let span = sm.next_point(span);
518+
assert_eq!(span.lo().0, 5);
519+
assert_eq!(span.hi().0, 5);
520+
521+
// Empty span pointing just past the last byte.
522+
let span = Span::with_root_ctxt(BytePos(5), BytePos(5));
523+
let span = sm.next_point(span);
524+
assert_eq!(span.lo().0, 5);
525+
assert_eq!(span.hi().0, 5);
526+
}

src/tools/clippy/clippy_utils/src/sugg.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,7 @@ impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
769769

770770
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability) {
771771
let mut remove_span = item;
772-
let hi = cx.sess().source_map().next_point(remove_span).hi();
773-
let fmpos = cx.sess().source_map().lookup_byte_offset(hi);
772+
let fmpos = cx.sess().source_map().lookup_byte_offset(remove_span.hi());
774773

775774
if let Some(ref src) = fmpos.sf.src {
776775
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');

0 commit comments

Comments
 (0)