fix #101749, use . instead of :: when accessing a method of an object

This commit is contained in:
yukang 2022-09-18 15:35:21 +08:00
parent 28a53cdb46
commit fb004e9a95
15 changed files with 218 additions and 66 deletions

View file

@ -1840,13 +1840,16 @@ impl<'a> Resolver<'a> {
(format!("use of undeclared type `{}`", ident), suggestion)
} else {
let suggestion = if ident.name == sym::alloc {
Some((
let mut suggestion = None;
if ident.name == sym::alloc {
suggestion = Some((
vec![],
String::from("add `extern crate alloc` to use the `alloc` crate"),
Applicability::MaybeIncorrect,
))
} else {
}
suggestion = suggestion.or_else(|| {
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
|sugg| {
(
@ -1856,7 +1859,7 @@ impl<'a> Resolver<'a> {
)
},
)
};
});
(format!("use of undeclared crate or module `{}`", ident), suggestion)
}
}

View file

@ -3364,13 +3364,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// Before we start looking for candidates, we have to get our hands
// on the type user is trying to perform invocation on; basically:
// we're transforming `HashMap::new` into just `HashMap`.
let path = match path.split_last() {
let prefix_path = match path.split_last() {
Some((_, path)) if !path.is_empty() => path,
_ => return Some(parent_err),
};
let (mut err, candidates) =
this.smart_resolve_report_errors(path, path_span, PathSource::Type, None);
this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None);
// There are two different error messages user might receive at
// this point:
@ -3414,11 +3414,23 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if this.should_report_errs() {
if candidates.is_empty() {
// When there is no suggested imports, we can just emit the error
// and suggestions immediately. Note that we bypass the usually error
// reporting routine (ie via `self.r.report_error`) because we need
// to post-process the `ResolutionError` above.
err.emit();
if path.len() == 2 && prefix_path.len() == 1 {
// Delay to check whether methond name is an associated function or not
// ```
// let foo = Foo {};
// foo::bar(); // possibly suggest to foo.bar();
//```
err.stash(
prefix_path[0].ident.span,
rustc_errors::StashKey::CallAssocMethod,
);
} else {
// When there is no suggested imports, we can just emit the error
// and suggestions immediately. Note that we bypass the usually error
// reporting routine (ie via `self.r.report_error`) because we need
// to post-process the `ResolutionError` above.
err.emit();
}
} else {
// If there are suggested imports, the error reporting is delayed
this.r.use_injections.push(UseError {
@ -3427,7 +3439,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
def_id,
instead: false,
suggestion: None,
path: path.into(),
path: prefix_path.into(),
is_call: source.is_call(),
});
}