Skip to content

Commit 9f7f949

Browse files
authored
Rollup merge of #56620 - petrochenkov:noclutter, r=estebank
resolve: Reduce some clutter in import ambiguity errors Noticed in https://www.reddit.com/r/rust/comments/a3pyrw/announcing_rust_131_and_rust_2018/eb8alhi/. The first error is distracting, but unnecessary, it's a *consequence* of the ambiguity error and appears because one of the ambiguous `actix` modules (unsurprisingly) doesn't have the expected name in it.
2 parents 7f076fa + 2010b4f commit 9f7f949

File tree

3 files changed

+30
-40
lines changed

3 files changed

+30
-40
lines changed

src/librustc_resolve/resolve_imports.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -843,14 +843,16 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
843843
self.current_module = directive.parent_scope.module;
844844

845845
let orig_vis = directive.vis.replace(ty::Visibility::Invisible);
846+
let prev_ambiguity_errors_len = self.ambiguity_errors.len();
846847
let path_res = self.resolve_path(&directive.module_path, None, &directive.parent_scope,
847848
true, directive.span, directive.crate_lint());
849+
let no_ambiguity = self.ambiguity_errors.len() == prev_ambiguity_errors_len;
848850
directive.vis.set(orig_vis);
849851
let module = match path_res {
850852
PathResult::Module(module) => {
851853
// Consistency checks, analogous to `finalize_current_module_macro_resolutions`.
852854
if let Some(initial_module) = directive.imported_module.get() {
853-
if module != initial_module && self.ambiguity_errors.is_empty() {
855+
if module != initial_module && no_ambiguity {
854856
span_bug!(directive.span, "inconsistent resolution for an import");
855857
}
856858
} else {
@@ -864,30 +866,32 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
864866
module
865867
}
866868
PathResult::Failed(span, msg, false) => {
867-
assert!(!self.ambiguity_errors.is_empty() ||
868-
directive.imported_module.get().is_none());
869-
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
869+
if no_ambiguity {
870+
assert!(directive.imported_module.get().is_none());
871+
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
872+
}
870873
return None;
871874
}
872875
PathResult::Failed(span, msg, true) => {
873-
assert!(!self.ambiguity_errors.is_empty() ||
874-
directive.imported_module.get().is_none());
875-
return if let Some((suggested_path, note)) = self.make_path_suggestion(
876-
span, directive.module_path.clone(), &directive.parent_scope
877-
) {
878-
Some((
879-
span,
880-
format!("did you mean `{}`?", Segment::names_to_string(&suggested_path)),
881-
note,
882-
))
883-
} else {
884-
Some((span, msg, None))
885-
};
876+
if no_ambiguity {
877+
assert!(directive.imported_module.get().is_none());
878+
return Some(match self.make_path_suggestion(span, directive.module_path.clone(),
879+
&directive.parent_scope) {
880+
Some((suggestion, note)) => (
881+
span,
882+
format!("did you mean `{}`?", Segment::names_to_string(&suggestion)),
883+
note,
884+
),
885+
None => (span, msg, None),
886+
});
887+
}
888+
return None;
886889
}
887890
PathResult::NonModule(path_res) if path_res.base_def() == Def::Err => {
891+
if no_ambiguity {
892+
assert!(directive.imported_module.get().is_none());
893+
}
888894
// The error was already reported earlier.
889-
assert!(!self.ambiguity_errors.is_empty() ||
890-
directive.imported_module.get().is_none());
891895
return None;
892896
}
893897
PathResult::Indeterminate | PathResult::NonModule(..) => unreachable!(),

src/test/ui/imports/issue-56125.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
mod m1 {
88
use issue_56125::last_segment::*;
99
//~^ ERROR `issue_56125` is ambiguous
10-
//~| ERROR unresolved import `issue_56125::last_segment`
1110
}
1211

1312
mod m2 {
1413
use issue_56125::non_last_segment::non_last_segment::*;
1514
//~^ ERROR `issue_56125` is ambiguous
16-
//~| ERROR failed to resolve: could not find `non_last_segment` in `issue_56125`
1715
}
1816

1917
mod m3 {
+7-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
error[E0433]: failed to resolve: could not find `non_last_segment` in `issue_56125`
2-
--> $DIR/issue-56125.rs:14:22
3-
|
4-
LL | use issue_56125::non_last_segment::non_last_segment::*;
5-
| ^^^^^^^^^^^^^^^^ could not find `non_last_segment` in `issue_56125`
6-
7-
error[E0432]: unresolved import `issue_56125::last_segment`
8-
--> $DIR/issue-56125.rs:8:22
9-
|
10-
LL | use issue_56125::last_segment::*;
11-
| ^^^^^^^^^^^^ could not find `last_segment` in `issue_56125`
12-
131
error[E0432]: unresolved import `empty::issue_56125`
14-
--> $DIR/issue-56125.rs:21:9
2+
--> $DIR/issue-56125.rs:19:9
153
|
164
LL | use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
175
| ^^^^^^^^^^^^^^^^^^ no `issue_56125` in `m3::empty`
@@ -32,36 +20,36 @@ LL | use issue_56125::last_segment::*;
3220
= help: use `self::issue_56125` to refer to this module unambiguously
3321

3422
error[E0659]: `issue_56125` is ambiguous (name vs any other name during import resolution)
35-
--> $DIR/issue-56125.rs:14:9
23+
--> $DIR/issue-56125.rs:13:9
3624
|
3725
LL | use issue_56125::non_last_segment::non_last_segment::*;
3826
| ^^^^^^^^^^^ ambiguous name
3927
|
4028
= note: `issue_56125` could refer to an extern crate passed with `--extern`
4129
= help: use `::issue_56125` to refer to this extern crate unambiguously
4230
note: `issue_56125` could also refer to the module imported here
43-
--> $DIR/issue-56125.rs:14:9
31+
--> $DIR/issue-56125.rs:13:9
4432
|
4533
LL | use issue_56125::non_last_segment::non_last_segment::*;
4634
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4735
= help: use `self::issue_56125` to refer to this module unambiguously
4836

4937
error[E0659]: `issue_56125` is ambiguous (name vs any other name during import resolution)
50-
--> $DIR/issue-56125.rs:22:9
38+
--> $DIR/issue-56125.rs:20:9
5139
|
5240
LL | use issue_56125::*; //~ ERROR `issue_56125` is ambiguous
5341
| ^^^^^^^^^^^ ambiguous name
5442
|
5543
= note: `issue_56125` could refer to an extern crate passed with `--extern`
5644
= help: use `::issue_56125` to refer to this extern crate unambiguously
5745
note: `issue_56125` could also refer to the unresolved item imported here
58-
--> $DIR/issue-56125.rs:21:9
46+
--> $DIR/issue-56125.rs:19:9
5947
|
6048
LL | use empty::issue_56125; //~ ERROR unresolved import `empty::issue_56125`
6149
| ^^^^^^^^^^^^^^^^^^
6250
= help: use `self::issue_56125` to refer to this unresolved item unambiguously
6351

64-
error: aborting due to 6 previous errors
52+
error: aborting due to 4 previous errors
6553

66-
Some errors occurred: E0432, E0433, E0659.
54+
Some errors occurred: E0432, E0659.
6755
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)