suggest importing for partial mod path in name resolving
This commit is contained in:
parent
38b44eb233
commit
e7e1a39fa0
@ -3556,9 +3556,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
|
|||||||
_ => return Some(parent_err),
|
_ => return Some(parent_err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut err, candidates) =
|
let (mut err, mut candidates) =
|
||||||
this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None);
|
this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None);
|
||||||
|
|
||||||
|
if candidates.is_empty() {
|
||||||
|
candidates = this.smart_resolve_partial_mod_path_errors(prefix_path, path);
|
||||||
|
}
|
||||||
|
|
||||||
// There are two different error messages user might receive at
|
// There are two different error messages user might receive at
|
||||||
// this point:
|
// this point:
|
||||||
// - E0412 cannot find type `{}` in this scope
|
// - E0412 cannot find type `{}` in this scope
|
||||||
|
@ -4,6 +4,7 @@ use crate::late::{LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseS
|
|||||||
use crate::{errors, path_names_to_string};
|
use crate::{errors, path_names_to_string};
|
||||||
use crate::{Module, ModuleKind, ModuleOrUniformRoot};
|
use crate::{Module, ModuleKind, ModuleOrUniformRoot};
|
||||||
use crate::{PathResult, PathSource, Segment};
|
use crate::{PathResult, PathSource, Segment};
|
||||||
|
use rustc_hir::def::Namespace::{self, *};
|
||||||
|
|
||||||
use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt};
|
use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt};
|
||||||
use rustc_ast::{
|
use rustc_ast::{
|
||||||
@ -17,7 +18,6 @@ use rustc_errors::{
|
|||||||
MultiSpan,
|
MultiSpan,
|
||||||
};
|
};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def::Namespace::{self, *};
|
|
||||||
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
|
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
|
||||||
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
|
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
|
||||||
use rustc_hir::PrimTy;
|
use rustc_hir::PrimTy;
|
||||||
@ -221,10 +221,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||||||
let suggestion = if self.current_trait_ref.is_none()
|
let suggestion = if self.current_trait_ref.is_none()
|
||||||
&& let Some((fn_kind, _)) = self.diagnostic_metadata.current_function
|
&& let Some((fn_kind, _)) = self.diagnostic_metadata.current_function
|
||||||
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
|
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
|
||||||
|
&& let FnKind::Fn(_, _, sig, ..) = fn_kind
|
||||||
&& let Some(items) = self.diagnostic_metadata.current_impl_items
|
&& let Some(items) = self.diagnostic_metadata.current_impl_items
|
||||||
&& let Some(item) = items.iter().find(|i| {
|
&& let Some(item) = items.iter().find(|i| {
|
||||||
if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind
|
if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind
|
||||||
&& i.ident.name == item_str.name
|
&& i.ident.name == item_str.name
|
||||||
|
// don't suggest if the item is in Fn signature arguments
|
||||||
|
// issue #112590
|
||||||
|
&& !sig.span.contains(item_span)
|
||||||
{
|
{
|
||||||
debug!(?item_str.name);
|
debug!(?item_str.name);
|
||||||
return true
|
return true
|
||||||
@ -318,6 +322,30 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to suggest for a module path that cannot be resolved.
|
||||||
|
/// Such as `fmt::Debug` where `fmt` is not resolved without importing,
|
||||||
|
/// here we search with `lookup_import_candidates` for a module named `fmt`
|
||||||
|
/// with `TypeNS` as namespace.
|
||||||
|
///
|
||||||
|
/// We need a separate function here because we won't suggest for a path with single segment
|
||||||
|
/// and we won't change `SourcePath` api `is_expected` to match `Type` with `DefKind::Mod`
|
||||||
|
pub(crate) fn smart_resolve_partial_mod_path_errors(
|
||||||
|
&mut self,
|
||||||
|
prefix_path: &[Segment],
|
||||||
|
path: &[Segment],
|
||||||
|
) -> Vec<ImportSuggestion> {
|
||||||
|
if path.len() <= 1 {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
let ident = prefix_path.last().unwrap().ident;
|
||||||
|
self.r.lookup_import_candidates(
|
||||||
|
ident,
|
||||||
|
Namespace::TypeNS,
|
||||||
|
&self.parent_scope,
|
||||||
|
&|res: Res| matches!(res, Res::Def(DefKind::Mod, _)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Handles error reporting for `smart_resolve_path_fragment` function.
|
/// 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.
|
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
|
||||||
pub(crate) fn smart_resolve_report_errors(
|
pub(crate) fn smart_resolve_report_errors(
|
||||||
|
@ -13,6 +13,15 @@ LL | let _: u8 = ::core::default::Default();
|
|||||||
| ^^^^ maybe a missing crate `core`?
|
| ^^^^ maybe a missing crate `core`?
|
||||||
|
|
|
|
||||||
= help: consider adding `extern crate core` to use the `core` crate
|
= help: consider adding `extern crate core` to use the `core` crate
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use std::default;
|
||||||
|
|
|
||||||
|
help: if you import `default`, refer to it directly
|
||||||
|
|
|
||||||
|
LL - let _: u8 = ::core::default::Default();
|
||||||
|
LL + let _: u8 = default::Default();
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
@ -24,6 +24,8 @@ LL | fn f() { my_core::mem::drop(0); }
|
|||||||
LL | a!();
|
LL | a!();
|
||||||
| ---- in this macro invocation
|
| ---- in this macro invocation
|
||||||
|
|
|
|
||||||
|
= help: consider importing this module:
|
||||||
|
my_core::mem
|
||||||
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
|
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
|
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
|
||||||
@ -31,6 +33,16 @@ error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
|
|||||||
|
|
|
|
||||||
LL | fn f() { my_core::mem::drop(0); }
|
LL | fn f() { my_core::mem::drop(0); }
|
||||||
| ^^^^^^^ use of undeclared crate or module `my_core`
|
| ^^^^^^^ use of undeclared crate or module `my_core`
|
||||||
|
|
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use my_core::mem;
|
||||||
|
|
|
||||||
|
help: if you import `mem`, refer to it directly
|
||||||
|
|
|
||||||
|
LL - fn f() { my_core::mem::drop(0); }
|
||||||
|
LL + fn f() { mem::drop(0); }
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
@ -3,21 +3,37 @@ error[E0433]: failed to resolve: use of undeclared crate or module `env`
|
|||||||
|
|
|
|
||||||
LL | env::current_dir;
|
LL | env::current_dir;
|
||||||
| ^^^ use of undeclared crate or module `env`
|
| ^^^ use of undeclared crate or module `env`
|
||||||
|
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `vec`
|
|
||||||
--> $DIR/builtin-prelude-no-accidents.rs:7:14
|
|
||||||
|
|
|
|
||||||
LL | type B = vec::Vec<u8>;
|
help: consider importing this module
|
||||||
| ^^^
|
|
|
||||||
| |
|
LL + use std::env;
|
||||||
| use of undeclared crate or module `vec`
|
|
|
||||||
| help: a struct with a similar name exists (notice the capitalization): `Vec`
|
|
||||||
|
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `panic`
|
error[E0433]: failed to resolve: use of undeclared crate or module `panic`
|
||||||
--> $DIR/builtin-prelude-no-accidents.rs:6:14
|
--> $DIR/builtin-prelude-no-accidents.rs:6:14
|
||||||
|
|
|
|
||||||
LL | type A = panic::PanicInfo;
|
LL | type A = panic::PanicInfo;
|
||||||
| ^^^^^ use of undeclared crate or module `panic`
|
| ^^^^^ use of undeclared crate or module `panic`
|
||||||
|
|
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use std::panic;
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0433]: failed to resolve: use of undeclared crate or module `vec`
|
||||||
|
--> $DIR/builtin-prelude-no-accidents.rs:7:14
|
||||||
|
|
|
||||||
|
LL | type B = vec::Vec<u8>;
|
||||||
|
| ^^^ use of undeclared crate or module `vec`
|
||||||
|
|
|
||||||
|
help: a struct with a similar name exists
|
||||||
|
|
|
||||||
|
LL | type B = Vec::Vec<u8>;
|
||||||
|
| ~~~
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use std::vec;
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
@ -21,18 +21,28 @@ error: unexpected `const` parameter declaration
|
|||||||
LL | path::path::Struct::<const N: usize>()
|
LL | path::path::Struct::<const N: usize>()
|
||||||
| ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
|
| ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
|
||||||
|
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `path`
|
|
||||||
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5
|
|
||||||
|
|
|
||||||
LL | path::path::Struct::<const N: usize>()
|
|
||||||
| ^^^^ use of undeclared crate or module `path`
|
|
||||||
|
|
||||||
error[E0412]: cannot find type `T` in this scope
|
error[E0412]: cannot find type `T` in this scope
|
||||||
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15
|
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15
|
||||||
|
|
|
|
||||||
LL | fn banana(a: <T<const N: usize>>::BAR) {}
|
LL | fn banana(a: <T<const N: usize>>::BAR) {}
|
||||||
| ^ not found in this scope
|
| ^ not found in this scope
|
||||||
|
|
||||||
|
error[E0433]: failed to resolve: use of undeclared crate or module `path`
|
||||||
|
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5
|
||||||
|
|
|
||||||
|
LL | path::path::Struct::<const N: usize>()
|
||||||
|
| ^^^^ use of undeclared crate or module `path`
|
||||||
|
|
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use std::path;
|
||||||
|
|
|
||||||
|
help: if you import `path`, refer to it directly
|
||||||
|
|
|
||||||
|
LL - path::path::Struct::<const N: usize>()
|
||||||
|
LL + path::Struct::<const N: usize>()
|
||||||
|
|
|
||||||
|
|
||||||
error[E0308]: mismatched types
|
error[E0308]: mismatched types
|
||||||
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17
|
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17
|
||||||
|
|
|
|
||||||
|
@ -3,6 +3,11 @@ error[E0433]: failed to resolve: use of undeclared crate or module `foo`
|
|||||||
|
|
|
|
||||||
LL | pub fn bar() { foo::baz(); }
|
LL | pub fn bar() { foo::baz(); }
|
||||||
| ^^^ use of undeclared crate or module `foo`
|
| ^^^ use of undeclared crate or module `foo`
|
||||||
|
|
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use foo;
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -20,6 +20,17 @@ help: there is a crate or module with a similar name
|
|||||||
LL | use bar::bar;
|
LL | use bar::bar;
|
||||||
| ~~~
|
| ~~~
|
||||||
|
|
||||||
|
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
|
||||||
|
--> $DIR/crate-or-module-typo.rs:6:20
|
||||||
|
|
|
||||||
|
LL | pub fn bar() { bar::baz(); }
|
||||||
|
| ^^^ use of undeclared crate or module `bar`
|
||||||
|
|
|
||||||
|
help: consider importing this module
|
||||||
|
|
|
||||||
|
LL + use crate::bar;
|
||||||
|
|
|
||||||
|
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `st`
|
error[E0433]: failed to resolve: use of undeclared crate or module `st`
|
||||||
--> $DIR/crate-or-module-typo.rs:14:10
|
--> $DIR/crate-or-module-typo.rs:14:10
|
||||||
|
|
|
|
||||||
@ -30,12 +41,17 @@ help: there is a crate or module with a similar name
|
|||||||
|
|
|
|
||||||
LL | bar: std::cell::Cell<bool>
|
LL | bar: std::cell::Cell<bool>
|
||||||
| ~~~
|
| ~~~
|
||||||
|
help: consider importing one of these items
|
||||||
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
|
|
|
||||||
--> $DIR/crate-or-module-typo.rs:6:20
|
LL + use core::cell;
|
||||||
|
|
|
||||||
|
LL + use std::cell;
|
||||||
|
|
|
||||||
|
help: if you import `cell`, refer to it directly
|
||||||
|
|
|
||||||
|
LL - bar: st::cell::Cell<bool>
|
||||||
|
LL + bar: cell::Cell<bool>
|
||||||
|
|
|
|
||||||
LL | pub fn bar() { bar::baz(); }
|
|
||||||
| ^^^ use of undeclared crate or module `bar`
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user