Skip to content

Commit eb0fcc5

Browse files
committed
refactor on span_look_ahead
1 parent 026c4b6 commit eb0fcc5

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
10081008
span: Span,
10091009
) {
10101010
if let Some((trait_ref, self_ty)) =
1011-
self.diagnostic_metadata.currently_processing_impl_trait.clone()
1011+
self.diagnostic_metadata.currently_processing_impl_trait.clone()
10121012
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
10131013
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
10141014
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None)
@@ -1217,15 +1217,15 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
12171217
// where a brace being opened means a block is being started. Look
12181218
// ahead for the next text to see if `span` is followed by a `{`.
12191219
let sm = self.r.tcx.sess.source_map();
1220-
let sp = sm.span_look_ahead(span, None, Some(50));
1221-
let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{");
1222-
// In case this could be a struct literal that needs to be surrounded
1223-
// by parentheses, find the appropriate span.
1224-
let closing_span = sm.span_look_ahead(span, Some("}"), Some(50));
1225-
let closing_brace: Option<Span> = sm
1226-
.span_to_snippet(closing_span)
1227-
.map_or(None, |s| if s == "}" { Some(span.to(closing_span)) } else { None });
1228-
(followed_by_brace, closing_brace)
1220+
if let Some(followed_brace_span) = sm.span_look_ahead(span, "{", Some(50)) {
1221+
// In case this could be a struct literal that needs to be surrounded
1222+
// by parentheses, find the appropriate span.
1223+
let close_brace_span = sm.span_look_ahead(followed_brace_span, "}", Some(50));
1224+
let closing_brace = close_brace_span.map(|sp| span.to(sp));
1225+
(true, closing_brace)
1226+
} else {
1227+
(false, None)
1228+
}
12291229
}
12301230

12311231
/// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`

compiler/rustc_span/src/source_map.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -973,24 +973,21 @@ impl SourceMap {
973973
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
974974
}
975975

976-
/// Returns a new span to check next none-whitespace character or some specified expected character
977-
/// If `expect` is none, the first span of non-whitespace character is returned.
978-
/// If `expect` presented, the first span of the character `expect` is returned
979-
/// Otherwise, the span reached to limit is returned.
980-
pub fn span_look_ahead(&self, span: Span, expect: Option<&str>, limit: Option<usize>) -> Span {
976+
/// Check whether span is followed by some specified expected string in limit scope
977+
pub fn span_look_ahead(&self, span: Span, expect: &str, limit: Option<usize>) -> Option<Span> {
981978
let mut sp = span;
982979
for _ in 0..limit.unwrap_or(100_usize) {
983980
sp = self.next_point(sp);
984981
if let Ok(ref snippet) = self.span_to_snippet(sp) {
985-
if expect.is_some_and(|es| snippet == es) {
986-
break;
982+
if snippet == expect {
983+
return Some(sp);
987984
}
988-
if expect.is_none() && snippet.chars().any(|c| !c.is_whitespace()) {
985+
if snippet.chars().any(|c| !c.is_whitespace()) {
989986
break;
990987
}
991988
}
992989
}
993-
sp
990+
None
994991
}
995992

996993
/// Finds the width of the character, either before or after the end of provided span,

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -1428,20 +1428,18 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
14281428

14291429
// Issue #109436, we need to add parentheses properly for method calls
14301430
// for example, `foo.into()` should be `(&foo).into()`
1431-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(
1432-
self.tcx.sess.source_map().span_look_ahead(span, Some("."), Some(50)),
1433-
) {
1434-
if snippet == "." {
1435-
err.multipart_suggestion_verbose(
1436-
sugg_msg,
1437-
vec![
1438-
(span.shrink_to_lo(), format!("({sugg_prefix}")),
1439-
(span.shrink_to_hi(), ")".to_string()),
1440-
],
1441-
Applicability::MaybeIncorrect,
1442-
);
1443-
return true;
1444-
}
1431+
if let Some(_) =
1432+
self.tcx.sess.source_map().span_look_ahead(span, ".", Some(50))
1433+
{
1434+
err.multipart_suggestion_verbose(
1435+
sugg_msg,
1436+
vec![
1437+
(span.shrink_to_lo(), format!("({sugg_prefix}")),
1438+
(span.shrink_to_hi(), ")".to_string()),
1439+
],
1440+
Applicability::MaybeIncorrect,
1441+
);
1442+
return true;
14451443
}
14461444

14471445
// Issue #104961, we need to add parentheses properly for compound expressions

0 commit comments

Comments
 (0)