Skip to content

Commit 2883186

Browse files
committed
Use multiline Diagnostic for candidate in other module
1 parent c8af93f commit 2883186

10 files changed

+38
-37
lines changed

src/librustc_resolve/lib.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6767
use errors::DiagnosticBuilder;
6868

6969
use std::cell::{Cell, RefCell};
70+
use std::cmp;
7071
use std::fmt;
7172
use std::mem::replace;
7273
use std::rc::Rc;
@@ -3224,7 +3225,7 @@ fn show_candidates(session: &mut DiagnosticBuilder,
32243225
better: bool) {
32253226
// don't show more than MAX_CANDIDATES results, so
32263227
// we're consistent with the trait suggestions
3227-
const MAX_CANDIDATES: usize = 5;
3228+
const MAX_CANDIDATES: usize = 4;
32283229

32293230
// we want consistent results across executions, but candidates are produced
32303231
// by iterating through a hash map, so make sure they are ordered:
@@ -3237,21 +3238,21 @@ fn show_candidates(session: &mut DiagnosticBuilder,
32373238
1 => " is found in another module, you can import it",
32383239
_ => "s are found in other modules, you can import them",
32393240
};
3240-
session.help(&format!("possible {}candidate{} into scope:", better, msg_diff));
3241-
3242-
let count = path_strings.len() as isize - MAX_CANDIDATES as isize + 1;
3243-
for (idx, path_string) in path_strings.iter().enumerate() {
3244-
if idx == MAX_CANDIDATES - 1 && count > 1 {
3245-
session.help(
3246-
&format!(" and {} other candidates", count).to_string(),
3247-
);
3248-
break;
3249-
} else {
3250-
session.help(
3251-
&format!(" `use {};`", path_string).to_string(),
3252-
);
3253-
}
3254-
}
3241+
3242+
let end = cmp::min(MAX_CANDIDATES, path_strings.len());
3243+
session.help(&format!("possible {}candidate{} into scope:{}{}",
3244+
better,
3245+
msg_diff,
3246+
&path_strings[0..end].iter().map(|candidate| {
3247+
format!("\n `use {};`", candidate)
3248+
}).collect::<String>(),
3249+
if path_strings.len() > MAX_CANDIDATES {
3250+
format!("\nand {} other candidates",
3251+
path_strings.len() - MAX_CANDIDATES)
3252+
} else {
3253+
"".to_owned()
3254+
}
3255+
));
32553256
}
32563257

32573258
/// A somewhat inefficient routine to obtain the name of a module.

src/test/ui/resolve/enums-are-namespaced-xc.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0425]: cannot find value `A` in module `namespaced_enums`
55
| ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
66
|
77
= help: possible candidate is found in another module, you can import it into scope:
8-
= help: `use namespaced_enums::Foo::A;`
8+
`use namespaced_enums::Foo::A;`
99

1010
error[E0425]: cannot find function `B` in module `namespaced_enums`
1111
--> $DIR/enums-are-namespaced-xc.rs:18:13
@@ -14,7 +14,7 @@ error[E0425]: cannot find function `B` in module `namespaced_enums`
1414
| ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
1515
|
1616
= help: possible candidate is found in another module, you can import it into scope:
17-
= help: `use namespaced_enums::Foo::B;`
17+
`use namespaced_enums::Foo::B;`
1818

1919
error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
2020
--> $DIR/enums-are-namespaced-xc.rs:21:13
@@ -23,7 +23,7 @@ error[E0422]: cannot find struct, variant or union type `C` in module `namespace
2323
| ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
2424
|
2525
= help: possible candidate is found in another module, you can import it into scope:
26-
= help: `use namespaced_enums::Foo::C;`
26+
`use namespaced_enums::Foo::C;`
2727

2828
error: aborting due to 3 previous errors
2929

src/test/ui/resolve/issue-16058.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ error[E0574]: expected struct, variant or union type, found enum `Result`
55
| ^^^^^^ not a struct, variant or union type
66
|
77
= help: possible better candidates are found in other modules, you can import them into scope:
8-
= help: `use std::fmt::Result;`
9-
= help: `use std::io::Result;`
10-
= help: `use std::thread::Result;`
8+
`use std::fmt::Result;`
9+
`use std::io::Result;`
10+
`use std::thread::Result;`
1111

1212
error: aborting due to previous error
1313

src/test/ui/resolve/issue-17518.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0422]: cannot find struct, variant or union type `E` in this scope
55
| ^ not found in this scope
66
|
77
= help: possible candidate is found in another module, you can import it into scope:
8-
= help: `use SomeEnum::E;`
8+
`use SomeEnum::E;`
99

1010
error: aborting due to previous error
1111

src/test/ui/resolve/issue-21221-1.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ error[E0405]: cannot find trait `Mul` in this scope
55
| ^^^ not found in this scope
66
|
77
= help: possible candidates are found in other modules, you can import them into scope:
8-
= help: `use mul1::Mul;`
9-
= help: `use mul2::Mul;`
10-
= help: `use std::ops::Mul;`
8+
`use mul1::Mul;`
9+
`use mul2::Mul;`
10+
`use std::ops::Mul;`
1111

1212
error[E0412]: cannot find type `Mul` in this scope
1313
--> $DIR/issue-21221-1.rs:72:16
@@ -16,11 +16,11 @@ error[E0412]: cannot find type `Mul` in this scope
1616
| ^^^ not found in this scope
1717
|
1818
= help: possible candidates are found in other modules, you can import them into scope:
19-
= help: `use mul1::Mul;`
20-
= help: `use mul2::Mul;`
21-
= help: `use mul3::Mul;`
22-
= help: `use mul4::Mul;`
23-
= help: and 2 other candidates
19+
`use mul1::Mul;`
20+
`use mul2::Mul;`
21+
`use mul3::Mul;`
22+
`use mul4::Mul;`
23+
and 2 other candidates
2424

2525
error[E0405]: cannot find trait `ThisTraitReallyDoesntExistInAnyModuleReally` in this scope
2626
--> $DIR/issue-21221-1.rs:83:6
@@ -35,7 +35,7 @@ error[E0405]: cannot find trait `Div` in this scope
3535
| ^^^ not found in this scope
3636
|
3737
= help: possible candidate is found in another module, you can import it into scope:
38-
= help: `use std::ops::Div;`
38+
`use std::ops::Div;`
3939

4040
error: cannot continue compilation due to previous error
4141

src/test/ui/resolve/issue-21221-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
55
| ^ not found in this scope
66
|
77
= help: possible candidate is found in another module, you can import it into scope:
8-
= help: `use foo::bar::T;`
8+
`use foo::bar::T;`
99

1010
error: main function not found
1111

src/test/ui/resolve/issue-21221-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `OuterTrait` in this scope
55
| ^^^^^^^^^^ not found in this scope
66
|
77
= help: possible candidate is found in another module, you can import it into scope:
8-
= help: `use issue_21221_3::outer::OuterTrait;`
8+
`use issue_21221_3::outer::OuterTrait;`
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/resolve/issue-21221-4.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
55
| ^ not found in this scope
66
|
77
= help: possible candidate is found in another module, you can import it into scope:
8-
= help: `use issue_21221_4::T;`
8+
`use issue_21221_4::T;`
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/resolve/issue-3907.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type alias `Foo`
55
| ^^^ type aliases cannot be used for traits
66
|
77
= help: possible better candidate is found in another module, you can import it into scope:
8-
= help: `use issue_3907::Foo;`
8+
`use issue_3907::Foo;`
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/span/issue-35987.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type parameter `Add`
55
| ^^^ not a trait
66
|
77
= help: possible better candidate is found in another module, you can import it into scope:
8-
= help: `use std::ops::Add;`
8+
`use std::ops::Add;`
99

1010
error: main function not found
1111

0 commit comments

Comments
 (0)