Factored out error reporting from smart_resolve_path_fragment
fn.
This commit is contained in:
parent
d173180116
commit
0e2d96e88c
1 changed files with 390 additions and 375 deletions
|
@ -3124,21 +3124,20 @@ impl<'a> Resolver<'a> {
|
|||
)
|
||||
}
|
||||
|
||||
fn smart_resolve_path_fragment(&mut self,
|
||||
id: NodeId,
|
||||
qself: Option<&QSelf>,
|
||||
/// Handles error reporting for `smart_resolve_path_fragment` function.
|
||||
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
|
||||
fn smart_resolve_report_errors(
|
||||
&mut self,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
source: PathSource<'_>,
|
||||
crate_lint: CrateLint)
|
||||
-> PathResolution {
|
||||
def: Option<Def>,
|
||||
) -> (DiagnosticBuilder<'a>, Vec<ImportSuggestion>) {
|
||||
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
|
||||
let ns = source.namespace();
|
||||
let is_expected = &|def| source.is_expected(def);
|
||||
let is_enum_variant = &|def| if let Def::Variant(..) = def { true } else { false };
|
||||
|
||||
// Base error is amended with one short label and possibly some longer helps/notes.
|
||||
let report_errors = |this: &mut Self, def: Option<Def>| {
|
||||
// Make the base error.
|
||||
let expected = source.descr_expected();
|
||||
let path_str = Segment::names_to_string(path);
|
||||
|
@ -3156,7 +3155,7 @@ impl<'a> Resolver<'a> {
|
|||
(String::new(), "the crate root".to_string())
|
||||
} else {
|
||||
let mod_path = &path[..path.len() - 1];
|
||||
let mod_prefix = match this.resolve_path_without_parent_scope(
|
||||
let mod_prefix = match self.resolve_path_without_parent_scope(
|
||||
mod_path, Some(TypeNS), false, span, CrateLint::No
|
||||
) {
|
||||
PathResult::Module(ModuleOrUniformRoot::Module(module)) =>
|
||||
|
@ -3171,11 +3170,11 @@ impl<'a> Resolver<'a> {
|
|||
};
|
||||
|
||||
let code = DiagnosticId::Error(code.into());
|
||||
let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code);
|
||||
let mut err = self.session.struct_span_err_with_code(base_span, &base_msg, code);
|
||||
|
||||
// Emit help message for fake-self from other languages like `this`(javascript)
|
||||
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
|
||||
if ["this", "my"].contains(&&*item_str.as_str())
|
||||
&& this.self_value_is_available(path[0].ident.span, span) {
|
||||
&& self.self_value_is_available(path[0].ident.span, span) {
|
||||
err.span_suggestion(
|
||||
span,
|
||||
"did you mean",
|
||||
|
@ -3193,7 +3192,7 @@ impl<'a> Resolver<'a> {
|
|||
return (err, Vec::new());
|
||||
}
|
||||
if is_self_value(path, ns) {
|
||||
debug!("smart_resolve_path_fragment E0424 source:{:?}", source);
|
||||
debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
|
||||
|
||||
__diagnostic_used!(E0424);
|
||||
err.code(DiagnosticId::Error("E0424".into()));
|
||||
|
@ -3212,12 +3211,12 @@ impl<'a> Resolver<'a> {
|
|||
return (err, Vec::new());
|
||||
}
|
||||
|
||||
// Try to lookup the name in more relaxed fashion for better error reporting.
|
||||
// Try to lookup name in more relaxed fashion for better error reporting.
|
||||
let ident = path.last().unwrap().ident;
|
||||
let candidates = this.lookup_import_candidates(ident, ns, is_expected);
|
||||
let candidates = self.lookup_import_candidates(ident, ns, is_expected);
|
||||
if candidates.is_empty() && is_expected(Def::Enum(DefId::local(CRATE_DEF_INDEX))) {
|
||||
let enum_candidates =
|
||||
this.lookup_import_candidates(ident, ns, is_enum_variant);
|
||||
self.lookup_import_candidates(ident, ns, is_enum_variant);
|
||||
let mut enum_candidates = enum_candidates.iter()
|
||||
.map(|suggestion| {
|
||||
import_candidate_to_enum_paths(&suggestion)
|
||||
|
@ -3225,8 +3224,8 @@ impl<'a> Resolver<'a> {
|
|||
enum_candidates.sort();
|
||||
|
||||
if !enum_candidates.is_empty() {
|
||||
// contextualize for E0412 "cannot find type", but don't belabor the point
|
||||
// (that it's a variant) for E0573 "expected type, found variant"
|
||||
// Contextualize for E0412 "cannot find type", but don't belabor the point
|
||||
// (that it's a variant) for E0573 "expected type, found variant".
|
||||
let preamble = if def.is_none() {
|
||||
let others = match enum_candidates.len() {
|
||||
1 => String::new(),
|
||||
|
@ -3245,22 +3244,23 @@ impl<'a> Resolver<'a> {
|
|||
&msg,
|
||||
enum_candidates.into_iter()
|
||||
.map(|(_variant_path, enum_ty_path)| enum_ty_path)
|
||||
// variants reëxported in prelude doesn't mean `prelude::v1` is the
|
||||
// type name! FIXME: is there a more principled way to do this that
|
||||
// would work for other reëxports?
|
||||
// Variants re-exported in prelude doesn't mean `prelude::v1` is the
|
||||
// type name!
|
||||
// FIXME: is there a more principled way to do this that
|
||||
// would work for other re-exports?
|
||||
.filter(|enum_ty_path| enum_ty_path != "std::prelude::v1")
|
||||
// also say `Option` rather than `std::prelude::v1::Option`
|
||||
// Also write `Option` rather than `std::prelude::v1::Option`.
|
||||
.map(|enum_ty_path| {
|
||||
// FIXME #56861: DRYer prelude filtering
|
||||
// FIXME #56861: DRYer prelude filtering.
|
||||
enum_ty_path.trim_start_matches("std::prelude::v1::").to_owned()
|
||||
}),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
if path.len() == 1 && this.self_type_is_available(span) {
|
||||
if let Some(candidate) = this.lookup_assoc_candidate(ident, ns, is_expected) {
|
||||
let self_is_available = this.self_value_is_available(path[0].ident.span, span);
|
||||
if path.len() == 1 && self.self_type_is_available(span) {
|
||||
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
|
||||
let self_is_available = self.self_value_is_available(path[0].ident.span, span);
|
||||
match candidate {
|
||||
AssocSuggestion::Field => {
|
||||
err.span_suggestion(
|
||||
|
@ -3299,7 +3299,7 @@ impl<'a> Resolver<'a> {
|
|||
let mut levenshtein_worked = false;
|
||||
|
||||
// Try Levenshtein algorithm.
|
||||
let suggestion = this.lookup_typo_candidate(path, ns, is_expected, span);
|
||||
let suggestion = self.lookup_typo_candidate(path, ns, is_expected, span);
|
||||
if let Some(suggestion) = suggestion {
|
||||
let msg = format!(
|
||||
"{} {} with a similar name exists",
|
||||
|
@ -3315,7 +3315,7 @@ impl<'a> Resolver<'a> {
|
|||
levenshtein_worked = true;
|
||||
}
|
||||
|
||||
// Try context dependent help if relaxed lookup didn't work.
|
||||
// Try context-dependent help if relaxed lookup didn't work.
|
||||
if let Some(def) = def {
|
||||
match (def, source) {
|
||||
(Def::Macro(..), _) => {
|
||||
|
@ -3358,7 +3358,7 @@ impl<'a> Resolver<'a> {
|
|||
},
|
||||
(Def::Enum(..), PathSource::TupleStruct)
|
||||
| (Def::Enum(..), PathSource::Expr(..)) => {
|
||||
if let Some(variants) = this.collect_enum_variants(def) {
|
||||
if let Some(variants) = self.collect_enum_variants(def) {
|
||||
err.note(&format!("did you mean to use one \
|
||||
of the following variants?\n{}",
|
||||
variants.iter()
|
||||
|
@ -3374,8 +3374,8 @@ impl<'a> Resolver<'a> {
|
|||
},
|
||||
(Def::Struct(def_id), _) if ns == ValueNS => {
|
||||
if let Some((ctor_def, ctor_vis))
|
||||
= this.struct_constructors.get(&def_id).cloned() {
|
||||
let accessible_ctor = this.is_accessible(ctor_vis);
|
||||
= self.struct_constructors.get(&def_id).cloned() {
|
||||
let accessible_ctor = self.is_accessible(ctor_vis);
|
||||
if is_expected(ctor_def) && !accessible_ctor {
|
||||
err.span_label(span, format!("constructor is not visible \
|
||||
here due to private fields"));
|
||||
|
@ -3385,7 +3385,7 @@ impl<'a> Resolver<'a> {
|
|||
// parser issue where a struct literal is being used on an expression
|
||||
// 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 `{`.
|
||||
let sm = this.session.source_map();
|
||||
let sm = self.session.source_map();
|
||||
let mut sp = span;
|
||||
loop {
|
||||
sp = sm.next_point(sp);
|
||||
|
@ -3421,8 +3421,10 @@ impl<'a> Resolver<'a> {
|
|||
_ => break,
|
||||
}
|
||||
i += 1;
|
||||
if i > 100 { // The bigger the span the more likely we're
|
||||
break; // incorrect. Bound it to 100 chars long.
|
||||
// The bigger the span, the more likely we're
|
||||
// incorrect. Bound it to 100 chars long.
|
||||
if i > 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
match source {
|
||||
|
@ -3503,12 +3505,24 @@ impl<'a> Resolver<'a> {
|
|||
// Fallback label.
|
||||
if !levenshtein_worked {
|
||||
err.span_label(base_span, fallback_label);
|
||||
this.type_ascription_suggestion(&mut err, base_span);
|
||||
self.type_ascription_suggestion(&mut err, base_span);
|
||||
}
|
||||
(err, candidates)
|
||||
};
|
||||
}
|
||||
|
||||
fn smart_resolve_path_fragment(&mut self,
|
||||
id: NodeId,
|
||||
qself: Option<&QSelf>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
source: PathSource,
|
||||
crate_lint: CrateLint)
|
||||
-> PathResolution {
|
||||
let ns = source.namespace();
|
||||
let is_expected = &|def| source.is_expected(def);
|
||||
|
||||
let report_errors = |this: &mut Self, def: Option<Def>| {
|
||||
let (err, candidates) = report_errors(this, def);
|
||||
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, def);
|
||||
let def_id = this.current_module.normal_ancestor_id;
|
||||
let node_id = this.definitions.as_local_node_id(def_id).unwrap();
|
||||
let better = def.is_some();
|
||||
|
@ -3579,7 +3593,8 @@ impl<'a> Resolver<'a> {
|
|||
debug!("self.current_type_ascription {:?}", self.current_type_ascription);
|
||||
if let Some(sp) = self.current_type_ascription.last() {
|
||||
let mut sp = *sp;
|
||||
loop { // try to find the `:`, bail on first non-':'/non-whitespace
|
||||
loop {
|
||||
// Try to find the `:`; bail on first non-':' / non-whitespace.
|
||||
sp = cm.next_point(sp);
|
||||
if let Ok(snippet) = cm.span_to_snippet(sp.to(cm.next_point(sp))) {
|
||||
debug!("snippet {:?}", snippet);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue