1
Fork 0

Rollup merge of #114549 - chenyukang:yukang-review-resolve-part, r=petrochenkov

Style fix and refactor on resolve diagnostics

- coding style
- refactor api of `span_look_ahead`
This commit is contained in:
Matthias Krüger 2023-08-07 05:29:13 +02:00 committed by GitHub
commit d804b74c6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 61 deletions

View file

@ -555,7 +555,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let crate_def_id = CRATE_DEF_ID.to_def_id();
// Try to filter out intrinsics candidates, as long as we have // Try to filter out intrinsics candidates, as long as we have
// some other candidates to suggest. // some other candidates to suggest.
let intrinsic_candidates: Vec<_> = candidates let intrinsic_candidates: Vec<_> = candidates
@ -566,8 +565,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
.collect(); .collect();
if candidates.is_empty() { if candidates.is_empty() {
// Put them back if we have no more candidates to suggest... // Put them back if we have no more candidates to suggest...
candidates.extend(intrinsic_candidates); candidates = intrinsic_candidates;
} }
let crate_def_id = CRATE_DEF_ID.to_def_id();
if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) { if candidates.is_empty() && is_expected(Res::Def(DefKind::Enum, crate_def_id)) {
let mut enum_candidates: Vec<_> = self let mut enum_candidates: Vec<_> = self
.r .r
@ -1180,37 +1180,34 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
/// return the span of whole call and the span for all arguments expect the first one (`self`). /// return the span of whole call and the span for all arguments expect the first one (`self`).
fn call_has_self_arg(&self, source: PathSource<'_>) -> Option<(Span, Option<Span>)> { fn call_has_self_arg(&self, source: PathSource<'_>) -> Option<(Span, Option<Span>)> {
let mut has_self_arg = None; let mut has_self_arg = None;
if let PathSource::Expr(Some(parent)) = source { if let PathSource::Expr(Some(parent)) = source
match &parent.kind { && let ExprKind::Call(_, args) = &parent.kind
ExprKind::Call(_, args) if !args.is_empty() => { && !args.is_empty() {
let mut expr_kind = &args[0].kind; let mut expr_kind = &args[0].kind;
loop { loop {
match expr_kind { match expr_kind {
ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => { ExprKind::Path(_, arg_name) if arg_name.segments.len() == 1 => {
if arg_name.segments[0].ident.name == kw::SelfLower { if arg_name.segments[0].ident.name == kw::SelfLower {
let call_span = parent.span; let call_span = parent.span;
let tail_args_span = if args.len() > 1 { let tail_args_span = if args.len() > 1 {
Some(Span::new( Some(Span::new(
args[1].span.lo(), args[1].span.lo(),
args.last().unwrap().span.hi(), args.last().unwrap().span.hi(),
call_span.ctxt(), call_span.ctxt(),
None, None,
)) ))
} else { } else {
None None
}; };
has_self_arg = Some((call_span, tail_args_span)); has_self_arg = Some((call_span, tail_args_span));
}
break;
} }
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind, break;
_ => break,
} }
ExprKind::AddrOf(_, _, expr) => expr_kind = &expr.kind,
_ => break,
} }
} }
_ => (), }
}
};
has_self_arg has_self_arg
} }
@ -1220,15 +1217,15 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
// where a brace being opened means a block is being started. Look // where a brace being opened means a block is being started. Look
// ahead for the next text to see if `span` is followed by a `{`. // ahead for the next text to see if `span` is followed by a `{`.
let sm = self.r.tcx.sess.source_map(); let sm = self.r.tcx.sess.source_map();
let sp = sm.span_look_ahead(span, None, Some(50)); if let Some(followed_brace_span) = sm.span_look_ahead(span, "{", Some(50)) {
let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{"); // In case this could be a struct literal that needs to be surrounded
// In case this could be a struct literal that needs to be surrounded // by parentheses, find the appropriate span.
// by parentheses, find the appropriate span. let close_brace_span = sm.span_look_ahead(followed_brace_span, "}", Some(50));
let closing_span = sm.span_look_ahead(span, Some("}"), Some(50)); let closing_brace = close_brace_span.map(|sp| span.to(sp));
let closing_brace: Option<Span> = sm (true, closing_brace)
.span_to_snippet(closing_span) } else {
.map_or(None, |s| if s == "}" { Some(span.to(closing_span)) } else { None }); (false, None)
(followed_by_brace, closing_brace) }
} }
/// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment` /// Provides context-dependent help for errors reported by the `smart_resolve_path_fragment`

View file

@ -973,24 +973,21 @@ impl SourceMap {
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)
} }
/// Returns a new span to check next none-whitespace character or some specified expected character /// Check whether span is followed by some specified expected string in limit scope
/// If `expect` is none, the first span of non-whitespace character is returned. pub fn span_look_ahead(&self, span: Span, expect: &str, limit: Option<usize>) -> Option<Span> {
/// If `expect` presented, the first span of the character `expect` is returned
/// Otherwise, the span reached to limit is returned.
pub fn span_look_ahead(&self, span: Span, expect: Option<&str>, limit: Option<usize>) -> Span {
let mut sp = span; let mut sp = span;
for _ in 0..limit.unwrap_or(100_usize) { for _ in 0..limit.unwrap_or(100_usize) {
sp = self.next_point(sp); sp = self.next_point(sp);
if let Ok(ref snippet) = self.span_to_snippet(sp) { if let Ok(ref snippet) = self.span_to_snippet(sp) {
if expect.is_some_and(|es| snippet == es) { if snippet == expect {
break; return Some(sp);
} }
if expect.is_none() && snippet.chars().any(|c| !c.is_whitespace()) { if snippet.chars().any(|c| !c.is_whitespace()) {
break; break;
} }
} }
} }
sp None
} }
/// Finds the width of the character, either before or after the end of provided span, /// Finds the width of the character, either before or after the end of provided span,

View file

@ -1429,20 +1429,18 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Issue #109436, we need to add parentheses properly for method calls // Issue #109436, we need to add parentheses properly for method calls
// for example, `foo.into()` should be `(&foo).into()` // for example, `foo.into()` should be `(&foo).into()`
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet( if let Some(_) =
self.tcx.sess.source_map().span_look_ahead(span, Some("."), Some(50)), self.tcx.sess.source_map().span_look_ahead(span, ".", Some(50))
) { {
if snippet == "." { err.multipart_suggestion_verbose(
err.multipart_suggestion_verbose( sugg_msg,
sugg_msg, vec![
vec![ (span.shrink_to_lo(), format!("({sugg_prefix}")),
(span.shrink_to_lo(), format!("({sugg_prefix}")), (span.shrink_to_hi(), ")".to_string()),
(span.shrink_to_hi(), ")".to_string()), ],
], Applicability::MaybeIncorrect,
Applicability::MaybeIncorrect, );
); return true;
return true;
}
} }
// Issue #104961, we need to add parentheses properly for compound expressions // Issue #104961, we need to add parentheses properly for compound expressions