1
Fork 0

Fix the bug of next_point in span

This commit is contained in:
yukang 2022-10-18 02:00:06 +08:00
parent 11432fe952
commit 0af255a5aa
7 changed files with 13 additions and 13 deletions

View file

@ -937,13 +937,12 @@ pub fn ensure_complete_parse<'a>(
kind_name, kind_name,
); );
err.note(&msg); err.note(&msg);
let semi_span = this.sess.source_map().next_point(span);
let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span)); let semi_span = this.sess.source_map().next_point(span);
match this.sess.source_map().span_to_snippet(semi_full_span) { match this.sess.source_map().span_to_snippet(semi_span) {
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => { Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
err.span_suggestion( err.span_suggestion(
semi_span, span.shrink_to_hi(),
"you might be missing a semicolon here", "you might be missing a semicolon here",
";", ";",
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,

View file

@ -82,7 +82,7 @@ fn emit_frag_parse_err(
); );
if !e.span.is_dummy() { if !e.span.is_dummy() {
// early end of macro arm (#52866) // early end of macro arm (#52866)
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span)); e.replace_span_with(parser.token.span.shrink_to_hi());
} }
} }
if e.span.is_dummy() { if e.span.is_dummy() {

View file

@ -1461,7 +1461,7 @@ impl<'a> Parser<'a> {
let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) { let (prev_sp, sp) = match (&self.token.kind, self.subparser_name) {
// Point at the end of the macro call when reaching end of macro arguments. // Point at the end of the macro call when reaching end of macro arguments.
(token::Eof, Some(_)) => { (token::Eof, Some(_)) => {
let sp = self.sess.source_map().next_point(self.prev_token.span); let sp = self.prev_token.span.shrink_to_hi();
(sp, sp) (sp, sp)
} }
// We don't want to point at the following span after DUMMY_SP. // We don't want to point at the following span after DUMMY_SP.
@ -2039,7 +2039,7 @@ impl<'a> Parser<'a> {
pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a, ErrorGuaranteed> { pub(super) fn expected_expression_found(&self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
let (span, msg) = match (&self.token.kind, self.subparser_name) { let (span, msg) = match (&self.token.kind, self.subparser_name) {
(&token::Eof, Some(origin)) => { (&token::Eof, Some(origin)) => {
let sp = self.sess.source_map().next_point(self.prev_token.span); let sp = self.prev_token.span.shrink_to_hi();
(sp, format!("expected expression, found end of {origin}")) (sp, format!("expected expression, found end of {origin}"))
} }
_ => ( _ => (

View file

@ -2172,7 +2172,7 @@ impl<'a> Parser<'a> {
}, },
ExprKind::Block(_, None) => { ExprKind::Block(_, None) => {
self.sess.emit_err(IfExpressionMissingCondition { self.sess.emit_err(IfExpressionMissingCondition {
if_span: self.sess.source_map().next_point(lo), if_span: lo.shrink_to_hi(),
block_span: self.sess.source_map().start_point(cond_span), block_span: self.sess.source_map().start_point(cond_span),
}); });
std::mem::replace(&mut cond, this.mk_expr_err(cond_span.shrink_to_hi())) std::mem::replace(&mut cond, this.mk_expr_err(cond_span.shrink_to_hi()))

View file

@ -1601,7 +1601,7 @@ impl<'a> Parser<'a> {
self.sess.emit_err(err); self.sess.emit_err(err);
} else { } else {
if !seen_comma { if !seen_comma {
let sp = self.sess.source_map().next_point(previous_span); let sp = previous_span.shrink_to_hi();
err.missing_comma = Some(sp); err.missing_comma = Some(sp);
} }
return Err(err.into_diagnostic(&self.sess.span_diagnostic)); return Err(err.into_diagnostic(&self.sess.span_diagnostic));

View file

@ -1731,7 +1731,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
for _ in 0..100 { for _ in 0..100 {
// Try to find an assignment // Try to find an assignment
sp = sm.next_point(sp); sp = sm.next_point(sp);
let snippet = sm.span_to_snippet(sp.to(sm.next_point(sp))); let snippet = sm.span_to_snippet(sp);
match snippet { match snippet {
Ok(ref x) if x.as_str() == "=" => { Ok(ref x) if x.as_str() == "=" => {
err.span_suggestion( err.span_suggestion(

View file

@ -859,14 +859,15 @@ 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.shrink_to_hi(), true); let width = self.find_width_of_character_at_span(sp, true);
debug_assert!(width > 0);
// If the width is 1, then the next span should point to the same `lo` and `hi`. However, // 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 // 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 - 1).unwrap_or(start_of_next_point); start_of_next_point.checked_add(width).unwrap_or(start_of_next_point);
let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point)); let end_of_next_point = BytePos(cmp::max(start_of_next_point + 1, end_of_next_point));
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None) Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
} }