Rollup merge of #123609 - compiler-errors:greek-question-mark, r=jieyouxu

Don't use bytepos offsets when computing semicolon span for removal

Causes problems when we recover confusable characters w/ a different byte width

Fixes #123607
This commit is contained in:
Matthias Krüger 2024-04-10 04:27:39 +02:00 committed by GitHub
commit 4bc891aebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 60 additions and 27 deletions

View File

@ -346,7 +346,7 @@ fn check_trait_fn_not_const(&self, constness: Const, parent: &TraitOrTraitImpl<'
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }), in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
const_context_label: parent_constness, const_context_label: parent_constness,
remove_const_sugg: ( remove_const_sugg: (
self.session.source_map().span_extend_while(span, |c| c == ' ').unwrap_or(span), self.session.source_map().span_extend_while_whitespace(span),
match parent_constness { match parent_constness {
Some(_) => rustc_errors::Applicability::MachineApplicable, Some(_) => rustc_errors::Applicability::MachineApplicable,
None => rustc_errors::Applicability::MaybeIncorrect, None => rustc_errors::Applicability::MaybeIncorrect,

View File

@ -2023,8 +2023,7 @@ fn suggest_fru_from_range_and_emit(
.tcx .tcx
.sess .sess
.source_map() .source_map()
.span_extend_while(range_start.span, |c| c.is_whitespace()) .span_extend_while_whitespace(range_start.span)
.unwrap_or(range_start.span)
.shrink_to_hi() .shrink_to_hi()
.to(range_end.span); .to(range_end.span);

View File

@ -15,7 +15,7 @@
}; };
use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt}; use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
use rustc_span::{sym, BytePos, Span}; use rustc_span::{sym, Span};
use crate::errors::{ use crate::errors::{
ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes, ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
@ -763,8 +763,14 @@ pub fn could_remove_semicolon(
let mac_call = rustc_span::source_map::original_sp(last_stmt.span, blk.span); let mac_call = rustc_span::source_map::original_sp(last_stmt.span, blk.span);
self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)? self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)?
} else { } else {
last_stmt.span.with_lo(last_stmt.span.hi() - BytePos(1)) self.tcx
.sess
.source_map()
.span_extend_while_whitespace(last_expr.span)
.shrink_to_hi()
.with_hi(last_stmt.span.hi())
}; };
Some((span, needs_box)) Some((span, needs_box))
} }
@ -867,10 +873,7 @@ pub fn consider_returning_binding_diag(
format!(" {ident} ") format!(" {ident} ")
}; };
let left_span = sm.span_through_char(blk.span, '{').shrink_to_hi(); let left_span = sm.span_through_char(blk.span, '{').shrink_to_hi();
( (sm.span_extend_while_whitespace(left_span), sugg)
sm.span_extend_while(left_span, |c| c.is_whitespace()).unwrap_or(left_span),
sugg,
)
}; };
Some(SuggestRemoveSemiOrReturnBinding::Add { sp: span, code: sugg, ident: *ident }) Some(SuggestRemoveSemiOrReturnBinding::Add { sp: span, code: sugg, ident: *ident })
} }

View File

@ -215,10 +215,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
if let Some(deletion_span) = deletion_span { if let Some(deletion_span) = deletion_span {
let msg = "elide the single-use lifetime"; let msg = "elide the single-use lifetime";
let (use_span, replace_lt) = if elide { let (use_span, replace_lt) = if elide {
let use_span = sess let use_span = sess.source_map().span_extend_while_whitespace(use_span);
.source_map()
.span_extend_while(use_span, char::is_whitespace)
.unwrap_or(use_span);
(use_span, String::new()) (use_span, String::new())
} else { } else {
(use_span, "'_".to_owned()) (use_span, "'_".to_owned())

View File

@ -654,6 +654,12 @@ pub fn span_extend_while(
}) })
} }
/// Extends the span to include any trailing whitespace, or returns the original
/// span if a `SpanSnippetError` was encountered.
pub fn span_extend_while_whitespace(&self, span: Span) -> Span {
self.span_extend_while(span, char::is_whitespace).unwrap_or(span)
}
/// Extends the given `Span` to previous character while the previous character matches the predicate /// Extends the given `Span` to previous character while the previous character matches the predicate
pub fn span_extend_prev_while( pub fn span_extend_prev_while(
&self, &self,
@ -1034,12 +1040,9 @@ pub fn stmt_span(&self, stmt_span: Span, block_span: Span) -> Span {
/// // ^^^^^^ input /// // ^^^^^^ input
/// ``` /// ```
pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> { pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?; let span = self.span_extend_while_whitespace(mac_call);
let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?)); let span = self.next_point(span);
if self.span_to_snippet(span).as_deref() != Ok(";") { if self.span_to_snippet(span).as_deref() == Ok(";") { Some(span) } else { None }
return None;
}
Some(span)
} }
} }

View File

@ -1592,8 +1592,7 @@ fn suggest_remove_await(&self, obligation: &PredicateObligation<'tcx>, err: &mut
.tcx .tcx
.sess .sess
.source_map() .source_map()
.span_extend_while(expr_span, char::is_whitespace) .span_extend_while_whitespace(expr_span)
.unwrap_or(expr_span)
.shrink_to_hi() .shrink_to_hi()
.to(await_expr.span.shrink_to_hi()); .to(await_expr.span.shrink_to_hi());
err.span_suggestion( err.span_suggestion(
@ -4851,10 +4850,7 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
let hir::IsAsync::Async(async_span) = sig.header.asyncness else { let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
return None; return None;
}; };
let Ok(async_span) = tcx.sess.source_map().span_extend_while(async_span, |c| c.is_whitespace()) let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);
else {
return None;
};
let future = tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty(); let future = tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else { let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {

View File

@ -294,8 +294,7 @@ fn elision_suggestions(
let span = cx let span = cx
.sess() .sess()
.source_map() .source_map()
.span_extend_while(usage.ident.span, |ch| ch.is_ascii_whitespace()) .span_extend_while_whitespace(usage.ident.span);
.unwrap_or(usage.ident.span);
(span, String::new()) (span, String::new())
}, },

View File

@ -0,0 +1,11 @@
// Ensures our "remove semicolon" suggestion isn't hardcoded with a character width,
// in case it was accidentally mixed up with a greek question mark.
// issue: rust-lang/rust#123607
pub fn square(num: i32) -> i32 {
//~^ ERROR mismatched types
num * num;
//~^ ERROR unknown start of token
}
fn main() {}

View File

@ -0,0 +1,25 @@
error: unknown start of token: \u{37e}
--> $DIR/remove-semi-but-confused-char.rs:7:14
|
LL | num * num;
| ^
|
help: Unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it is not
|
LL | num * num;
| ~
error[E0308]: mismatched types
--> $DIR/remove-semi-but-confused-char.rs:5:28
|
LL | pub fn square(num: i32) -> i32 {
| ------ ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
LL |
LL | num * num;
| - help: remove this semicolon to return this value
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.