Skip to content

Commit

Permalink
reword "possible candidate" import suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed May 7, 2020
1 parent 97f3eee commit 9f88d75
Show file tree
Hide file tree
Showing 46 changed files with 112 additions and 106 deletions.
19 changes: 11 additions & 8 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl TypoSuggestion {
/// A free importable items suggested in case of resolution failure.
crate struct ImportSuggestion {
pub did: Option<DefId>,
pub descr: &'static str,
pub path: Path,
}

Expand Down Expand Up @@ -652,7 +653,7 @@ impl<'a> Resolver<'a> {
Res::Def(DefKind::Ctor(..), did) => this.parent(did),
_ => res.opt_def_id(),
};
candidates.push(ImportSuggestion { did, path });
candidates.push(ImportSuggestion { did, descr: res.descr(), path });
}
}
}
Expand Down Expand Up @@ -1445,29 +1446,31 @@ fn find_span_immediately_after_crate_name(
crate fn show_candidates(
err: &mut DiagnosticBuilder<'_>,
// This is `None` if all placement locations are inside expansions
span: Option<Span>,
use_placement_span: Option<Span>,
candidates: &[ImportSuggestion],
better: bool,
found_use: bool,
) {
if candidates.is_empty() {
return;
}

// we want consistent results across executions, but candidates are produced
// by iterating through a hash map, so make sure they are ordered:
let mut path_strings: Vec<_> =
candidates.iter().map(|c| path_names_to_string(&c.path)).collect();
path_strings.sort();
path_strings.dedup();

let better = if better { "better " } else { "" };
let msg_diff = match path_strings.len() {
1 => " is found in another module, you can import it",
_ => "s are found in other modules, you can import them",
let (determiner, kind) = if candidates.len() == 1 {
("this", candidates[0].descr)
} else {
("one of these", "items")
};
let msg = format!("possible {}candidate{} into scope", better, msg_diff);
let instead = if better { " instead" } else { "" };
let msg = format!("consider importing {} {}{}", determiner, kind, instead);

if let Some(span) = span {
if let Some(span) = use_placement_span {
for candidate in &mut path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,10 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
let module_def_id = module.def_id().unwrap();
if module_def_id == def_id {
let path = Path { span: name_binding.span, segments: path_segments };
result = Some((module, ImportSuggestion { did: Some(def_id), path }));
result = Some((
module,
ImportSuggestion { did: Some(def_id), descr: "module", path },
));
} else {
// add the module to the lookup
if seen_modules.insert(module_def_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/class-missing-self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0425]: cannot find function `sleep` in this scope
LL | sleep();
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this function
|
LL | use std::thread::sleep;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/crate-in-paths.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find value `Foo` in this scope
LL | Foo;
| ^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this unit struct
|
LL | use crate::bar::Foo;
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ error[E0425]: cannot find value `Set` in this scope
LL | fn setup() -> Set { Set }
| ^^^ not found in this scope
|
help: possible candidates are found in other modules, you can import them into scope
help: consider importing one of these items
|
LL | use AffixHeart::Set;
|
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/glob-resolve1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find function `fpriv` in this scope
LL | fpriv();
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this function
|
LL | use bar::fpriv;
|
Expand All @@ -15,7 +15,7 @@ error[E0425]: cannot find function `epriv` in this scope
LL | epriv();
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this function
|
LL | use bar::epriv;
|
Expand All @@ -32,7 +32,7 @@ error[E0425]: cannot find value `C` in this scope
LL | C;
| ^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this unit struct
|
LL | use bar::C;
|
Expand All @@ -56,7 +56,7 @@ help: an enum with a similar name exists
|
LL | foo::<B>();
| ^
help: possible candidate is found in another module, you can import it into scope
help: consider importing this enum
|
LL | use bar::A;
|
Expand All @@ -74,7 +74,7 @@ help: an enum with a similar name exists
|
LL | foo::<B>();
| ^
help: possible candidate is found in another module, you can import it into scope
help: consider importing this struct
|
LL | use bar::C;
|
Expand All @@ -92,7 +92,7 @@ help: an enum with a similar name exists
|
LL | foo::<B>();
| ^
help: possible candidate is found in another module, you can import it into scope
help: consider importing this type alias
|
LL | use bar::D;
|
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/hygiene/globs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find function `f` in this scope
LL | f();
| ^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing one of these items
|
LL | use foo::f;
|
Expand All @@ -23,7 +23,7 @@ LL | | }
| |_____- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: possible candidates are found in other modules, you can import them into scope
help: consider importing one of these items
|
LL | use bar::g;
|
Expand All @@ -41,7 +41,7 @@ LL | n!(f);
LL | n!(f);
| ^ not found in this scope
|
= note: possible candidate is found in another module, you can import it into scope:
= note: consider importing one of these items:
foo::f
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -54,7 +54,7 @@ LL | n!(f);
LL | f
| ^ not found in this scope
|
= note: possible candidate is found in another module, you can import it into scope:
= note: consider importing one of these items:
foo::f
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/universal_wrong_bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0404]: expected trait, found derive macro `Debug`
LL | fn wants_debug(g: impl Debug) { }
| ^^^^^ not a trait
|
help: possible better candidate is found in another module, you can import it into scope
help: consider importing this trait instead
|
LL | use std::fmt::Debug;
|
Expand All @@ -15,7 +15,7 @@ error[E0404]: expected trait, found derive macro `Debug`
LL | fn wants_display(g: impl Debug) { }
| ^^^^^ not a trait
|
help: possible better candidate is found in another module, you can import it into scope
help: consider importing this trait instead
|
LL | use std::fmt::Debug;
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-17546.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ error[E0573]: expected type, found variant `Result`
LL | fn new() -> Result<foo::MyEnum, String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a type
|
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use std::fmt::Result;
|
Expand All @@ -42,7 +42,7 @@ error[E0573]: expected type, found variant `Result`
LL | fn new() -> Result<foo::MyEnum, String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a type
|
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use std::fmt::Result;
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-35675.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ error[E0425]: cannot find function, tuple struct or tuple variant `Apple` in thi
LL | Apple(5)
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this tuple variant
|
LL | use Fruit::Apple;
|
Expand All @@ -35,7 +35,7 @@ error[E0425]: cannot find function, tuple struct or tuple variant `Apple` in thi
LL | Apple(5)
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this tuple variant
|
LL | use Fruit::Apple;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-37534.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0404]: expected trait, found derive macro `Hash`
LL | struct Foo<T: ?Hash> { }
| ^^^^ not a trait
|
help: possible better candidate is found in another module, you can import it into scope
help: consider importing this trait instead
|
LL | use std::hash::Hash;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-38293.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0423]: expected function, found module `baz`
LL | baz();
| ^^^ not a function
|
help: possible better candidate is found in another module, you can import it into scope
help: consider importing this function instead
|
LL | use bar::baz;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-42944.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0425]: cannot find function, tuple struct or tuple variant `B` in this sc
LL | B(());
| ^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this tuple struct
|
LL | use foo::B;
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-4366-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0412]: cannot find type `Bar` in this scope
LL | fn sub() -> Bar { 1 }
| ^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this type alias
|
LL | use a::b::Bar;
|
Expand All @@ -15,7 +15,7 @@ error[E0423]: expected function, found module `foo`
LL | foo();
| ^^^ not a function
|
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use foo::foo;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-4366.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find function `foo` in this scope
LL | fn sub() -> isize { foo(); 1 }
| ^^^ not found in this scope
|
help: possible candidates are found in other modules, you can import them into scope
help: consider importing one of these items
|
LL | use foo::foo;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-50599.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find value `LOG10_2` in module `std::f64`
LL | const M: usize = (f64::from(N) * std::f64::LOG10_2) as usize;
| ^^^^^^^ not found in `std::f64`
|
help: possible candidates are found in other modules, you can import them into scope
help: consider importing one of these items
|
LL | use std::f32::consts::LOG10_2;
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lexical-scopes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0574]: expected struct, variant or union type, found type parameter `T`
LL | let t = T { i: 0 };
| ^ not a struct, variant or union type
|
help: possible better candidate is found in another module, you can import it into scope
help: consider importing this struct instead
|
LL | use T;
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/lint/use_suggestion_json.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod foo {
],
"children": [
{
"message": "possible candidates are found in other modules, you can import them into scope",
"message": "consider importing one of these items",
"code": null,
"level": "help",
"spans": [
Expand Down Expand Up @@ -385,7 +385,7 @@ mod foo {
\u001b[0m\u001b[1m\u001b[38;5;12mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m let x: Iter;\u001b[0m
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9mnot found in this scope\u001b[0m
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m
\u001b[0m\u001b[1m\u001b[38;5;14mhelp\u001b[0m\u001b[0m: possible candidates are found in other modules, you can import them into scope\u001b[0m
\u001b[0m\u001b[1m\u001b[38;5;14mhelp\u001b[0m\u001b[0m: consider importing one of these items\u001b[0m
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m
\u001b[0m\u001b[1m\u001b[38;5;12mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0muse std::collections::binary_heap::Iter;\u001b[0m
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/macro-outer-attributes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0425]: cannot find function `bar` in module `a`
LL | a::bar();
| ^^^ not found in `a`
|
help: possible candidate is found in another module, you can import it into scope
help: consider importing this function
|
LL | use b::bar;
|
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/namespace/namespace-mix.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ help: a tuple struct with a similar name exists
|
LL | check(m1::TS);
| ^^
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use m2::S;
|
Expand All @@ -35,7 +35,7 @@ help: a tuple struct with a similar name exists
|
LL | check(xm1::TS);
| ^^
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use m2::S;
|
Expand All @@ -57,7 +57,7 @@ help: a tuple variant with a similar name exists
|
LL | check(m7::TV);
| ^^
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use m8::V;
|
Expand All @@ -79,7 +79,7 @@ help: a tuple variant with a similar name exists
|
LL | check(xm7::TV);
| ^^
help: possible better candidates are found in other modules, you can import them into scope
help: consider importing one of these items instead
|
LL | use m8::V;
|
Expand Down
Loading

0 comments on commit 9f88d75

Please sign in to comment.