Skip to content

Commit b26701e

Browse files
committed
add testcase for 112590
1 parent e7e1a39 commit b26701e

12 files changed

+132
-72
lines changed

compiler/rustc_resolve/src/late.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -3499,7 +3499,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
34993499
let report_errors = |this: &mut Self, res: Option<Res>| {
35003500
if this.should_report_errs() {
35013501
let (err, candidates) =
3502-
this.smart_resolve_report_errors(path, path_span, source, res);
3502+
this.smart_resolve_report_errors(path, path, path_span, source, res);
35033503

35043504
let def_id = this.parent_scope.module.nearest_parent_mod();
35053505
let instead = res.is_some();
@@ -3556,12 +3556,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
35563556
_ => return Some(parent_err),
35573557
};
35583558

3559-
let (mut err, mut candidates) =
3560-
this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None);
3561-
3562-
if candidates.is_empty() {
3563-
candidates = this.smart_resolve_partial_mod_path_errors(prefix_path, path);
3564-
}
3559+
let (mut err, candidates) = this.smart_resolve_report_errors(
3560+
prefix_path,
3561+
path,
3562+
path_span,
3563+
PathSource::Type,
3564+
None,
3565+
);
35653566

35663567
// There are two different error messages user might receive at
35673568
// this point:

compiler/rustc_resolve/src/late/diagnostics.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,44 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
334334
prefix_path: &[Segment],
335335
path: &[Segment],
336336
) -> Vec<ImportSuggestion> {
337-
if path.len() <= 1 {
338-
return Vec::new();
337+
let next_seg = if path.len() >= prefix_path.len() + 1 && prefix_path.len() == 1 {
338+
path.get(prefix_path.len())
339+
} else {
340+
None
341+
};
342+
if let Some(segment) = prefix_path.last() &&
343+
let Some(next_seg) = next_seg {
344+
let candidates = self.r.lookup_import_candidates(
345+
segment.ident,
346+
Namespace::TypeNS,
347+
&self.parent_scope,
348+
&|res: Res| matches!(res, Res::Def(DefKind::Mod, _)),
349+
);
350+
// double check next seg is valid
351+
candidates
352+
.into_iter()
353+
.filter(|candidate| {
354+
if let Some(def_id) = candidate.did &&
355+
let Some(module) = self.r.get_module(def_id) {
356+
self.r.resolutions(module).borrow().iter().any(|(key, _r)| {
357+
key.ident.name == next_seg.ident.name
358+
})
359+
} else {
360+
false
361+
}
362+
})
363+
.collect::<Vec<_>>()
364+
} else {
365+
Vec::new()
339366
}
340-
let ident = prefix_path.last().unwrap().ident;
341-
self.r.lookup_import_candidates(
342-
ident,
343-
Namespace::TypeNS,
344-
&self.parent_scope,
345-
&|res: Res| matches!(res, Res::Def(DefKind::Mod, _)),
346-
)
347367
}
348368

349369
/// Handles error reporting for `smart_resolve_path_fragment` function.
350370
/// Creates base error and amends it with one short label and possibly some longer helps/notes.
351371
pub(crate) fn smart_resolve_report_errors(
352372
&mut self,
353373
path: &[Segment],
374+
full_path: &[Segment],
354375
span: Span,
355376
source: PathSource<'_>,
356377
res: Option<Res>,
@@ -392,7 +413,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
392413
}
393414

394415
let (found, candidates) =
395-
self.try_lookup_name_relaxed(&mut err, source, path, span, res, &base_error);
416+
self.try_lookup_name_relaxed(&mut err, source, path, full_path, span, res, &base_error);
396417
if found {
397418
return (err, candidates);
398419
}
@@ -498,6 +519,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
498519
err: &mut Diagnostic,
499520
source: PathSource<'_>,
500521
path: &[Segment],
522+
full_path: &[Segment],
501523
span: Span,
502524
res: Option<Res>,
503525
base_error: &BaseError,
@@ -667,6 +689,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
667689
}
668690
}
669691

692+
if candidates.is_empty() {
693+
candidates = self.smart_resolve_partial_mod_path_errors(path, full_path);
694+
}
695+
670696
return (false, candidates);
671697
}
672698

tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr

-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ LL | let _: u8 = ::core::default::Default();
1313
| ^^^^ maybe a missing crate `core`?
1414
|
1515
= help: consider adding `extern crate core` to use the `core` crate
16-
help: consider importing this module
17-
|
18-
LL + use std::default;
19-
|
20-
help: if you import `default`, refer to it directly
21-
|
22-
LL - let _: u8 = ::core::default::Default();
23-
LL + let _: u8 = default::Default();
24-
|
2516

2617
error: aborting due to 2 previous errors
2718

tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,13 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27-
= help: consider importing this module:
28-
my_core::mem
2927
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
3028

3129
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
3230
--> $DIR/extern-prelude-from-opaque-fail.rs:24:14
3331
|
3432
LL | fn f() { my_core::mem::drop(0); }
3533
| ^^^^^^^ use of undeclared crate or module `my_core`
36-
|
37-
help: consider importing this module
38-
|
39-
LL + use my_core::mem;
40-
|
41-
help: if you import `mem`, refer to it directly
42-
|
43-
LL - fn f() { my_core::mem::drop(0); }
44-
LL + fn f() { mem::drop(0); }
45-
|
4634

4735
error: aborting due to 4 previous errors
4836

tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr

+5-15
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,17 @@ error: unexpected `const` parameter declaration
2121
LL | path::path::Struct::<const N: usize>()
2222
| ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration
2323

24-
error[E0412]: cannot find type `T` in this scope
25-
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15
26-
|
27-
LL | fn banana(a: <T<const N: usize>>::BAR) {}
28-
| ^ not found in this scope
29-
3024
error[E0433]: failed to resolve: use of undeclared crate or module `path`
3125
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5
3226
|
3327
LL | path::path::Struct::<const N: usize>()
3428
| ^^^^ use of undeclared crate or module `path`
29+
30+
error[E0412]: cannot find type `T` in this scope
31+
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15
3532
|
36-
help: consider importing this module
37-
|
38-
LL + use std::path;
39-
|
40-
help: if you import `path`, refer to it directly
41-
|
42-
LL - path::path::Struct::<const N: usize>()
43-
LL + path::Struct::<const N: usize>()
44-
|
33+
LL | fn banana(a: <T<const N: usize>>::BAR) {}
34+
| ^ not found in this scope
4535

4636
error[E0308]: mismatched types
4737
--> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
3+
// In this test baz isn't resolved when called as foo.baz even though
4+
// it's called from inside foo. This is somewhat surprising and may
5+
// want to change eventually.
6+
7+
mod foo {
8+
pub fn bar() { foo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `foo`
9+
10+
fn baz() { }
11+
}
12+
13+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
2+
--> $DIR/export-fully-qualified-2018.rs:8:20
3+
|
4+
LL | pub fn bar() { foo::baz(); }
5+
| ^^^ use of undeclared crate or module `foo`
6+
|
7+
help: consider importing this module
8+
|
9+
LL + use crate::foo;
10+
|
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0433`.

tests/ui/resolve/export-fully-qualified.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// edition:2015
2+
13
// In this test baz isn't resolved when called as foo.baz even though
24
// it's called from inside foo. This is somewhat surprising and may
35
// want to change eventually.

tests/ui/resolve/export-fully-qualified.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: use of undeclared crate or module `foo`
2-
--> $DIR/export-fully-qualified.rs:6:20
2+
--> $DIR/export-fully-qualified.rs:8:20
33
|
44
LL | pub fn bar() { foo::baz(); }
55
| ^^^ use of undeclared crate or module `foo`

tests/ui/suggestions/crate-or-module-typo.stderr

+7-18
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ help: there is a crate or module with a similar name
2020
LL | use bar::bar;
2121
| ~~~
2222

23-
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
24-
--> $DIR/crate-or-module-typo.rs:6:20
25-
|
26-
LL | pub fn bar() { bar::baz(); }
27-
| ^^^ use of undeclared crate or module `bar`
28-
|
29-
help: consider importing this module
30-
|
31-
LL + use crate::bar;
32-
|
33-
3423
error[E0433]: failed to resolve: use of undeclared crate or module `st`
3524
--> $DIR/crate-or-module-typo.rs:14:10
3625
|
@@ -41,16 +30,16 @@ help: there is a crate or module with a similar name
4130
|
4231
LL | bar: std::cell::Cell<bool>
4332
| ~~~
44-
help: consider importing one of these items
45-
|
46-
LL + use core::cell;
33+
34+
error[E0433]: failed to resolve: use of undeclared crate or module `bar`
35+
--> $DIR/crate-or-module-typo.rs:6:20
4736
|
48-
LL + use std::cell;
37+
LL | pub fn bar() { bar::baz(); }
38+
| ^^^ use of undeclared crate or module `bar`
4939
|
50-
help: if you import `cell`, refer to it directly
40+
help: consider importing this module
5141
|
52-
LL - bar: st::cell::Cell<bool>
53-
LL + bar: cell::Cell<bool>
42+
LL + use crate::bar;
5443
|
5544

5645
error: aborting due to 4 previous errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub struct S;
2+
3+
impl fmt::Debug for S { //~ ERROR failed to resolve: use of undeclared crate or module `fmt`
4+
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { //~ ERROR failed to resolve: use of undeclared crate or module `fmt`
5+
//~^ ERROR failed to resolve: use of undeclared crate or module `fmt`
6+
Ok(())
7+
}
8+
}
9+
10+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0433]: failed to resolve: use of undeclared crate or module `fmt`
2+
--> $DIR/issue-112590-suggest-import.rs:3:6
3+
|
4+
LL | impl fmt::Debug for S {
5+
| ^^^ use of undeclared crate or module `fmt`
6+
|
7+
help: consider importing this module
8+
|
9+
LL + use std::fmt;
10+
|
11+
12+
error[E0433]: failed to resolve: use of undeclared crate or module `fmt`
13+
--> $DIR/issue-112590-suggest-import.rs:4:28
14+
|
15+
LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
| ^^^ use of undeclared crate or module `fmt`
17+
|
18+
help: consider importing this module
19+
|
20+
LL + use std::fmt;
21+
|
22+
23+
error[E0433]: failed to resolve: use of undeclared crate or module `fmt`
24+
--> $DIR/issue-112590-suggest-import.rs:4:51
25+
|
26+
LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
| ^^^ use of undeclared crate or module `fmt`
28+
|
29+
help: consider importing this module
30+
|
31+
LL + use std::fmt;
32+
|
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)