Skip to content

Commit 1e1f33f

Browse files
committed
Use more targetted suggestion span for fully qualified path
1 parent acee1f4 commit 1e1f33f

File tree

5 files changed

+34
-28
lines changed

5 files changed

+34
-28
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,19 +734,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
734734
if !impl_candidates.is_empty() && e.span.contains(span)
735735
&& let Some(expr) = exprs.first()
736736
&& let ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
737-
&& let [path_segment] = path.segments
737+
&& let [_] = path.segments
738738
{
739739
let mut eraser = TypeParamEraser(self.tcx);
740740
let candidate_len = impl_candidates.len();
741741
let suggestions = impl_candidates.iter().map(|candidate| {
742742
let candidate = candidate.super_fold_with(&mut eraser);
743-
format!(
744-
"{}::{}({})",
745-
candidate, segment.ident, path_segment.ident
746-
)
743+
vec![
744+
(expr.span.shrink_to_lo(), format!("{}::{}(", candidate, segment.ident)),
745+
if exprs.len() == 1 {
746+
(expr.span.shrink_to_hi().with_hi(e.span.hi()), ")".to_string())
747+
} else {
748+
(expr.span.shrink_to_hi().with_hi(exprs[1].span.lo()), ", ".to_string())
749+
},
750+
]
747751
});
748-
err.span_suggestions(
749-
e.span,
752+
err.multipart_suggestions(
750753
&format!(
751754
"use the fully qualified path for the potential candidate{}",
752755
pluralize!(candidate_len),

src/test/ui/error-codes/E0283.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ LL | let bar = foo_impl.into() * 1u32;
1414
| | |
1515
| | cannot infer type for type parameter `T` declared on the trait `Into`
1616
| this method call resolves to `T`
17-
| help: use the fully qualified path for the potential candidate: `<Impl as Into<u32>>::into(foo_impl)`
1817
|
1918
note: multiple `impl`s satisfying `Impl: Into<_>` found
2019
--> $DIR/E0283.rs:17:1
@@ -24,6 +23,10 @@ LL | impl Into<u32> for Impl {
2423
= note: and another `impl` found in the `core` crate:
2524
- impl<T, U> Into<U> for T
2625
where U: From<T>;
26+
help: use the fully qualified path for the potential candidate
27+
|
28+
LL | let bar = <Impl as Into<u32>>::into(foo_impl) * 1u32;
29+
| ++++++++++++++++++++++++++ ~
2730

2831
error: aborting due to 2 previous errors
2932

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
struct Thing<X>(X);
22

33
trait Method<T> {
4-
fn method(self) -> T;
4+
fn method(self, _: i32) -> T;
55
}
66

77
impl<X> Method<i32> for Thing<X> {
8-
fn method(self) -> i32 { 0 }
8+
fn method(self, _: i32) -> i32 { 0 }
99
}
1010

1111
impl<X> Method<u32> for Thing<X> {
12-
fn method(self) -> u32 { 0 }
12+
fn method(self, _: i32) -> u32 { 0 }
1313
}
1414

1515
fn main() {
1616
let thing = Thing(true);
17-
thing.method();
17+
thing.method(42);
1818
//~^ ERROR type annotations needed
1919
//~| ERROR type annotations needed
2020
}

src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0282]: type annotations needed
22
--> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11
33
|
4-
LL | thing.method();
5-
| ------^^^^^^--
4+
LL | thing.method(42);
5+
| ------^^^^^^----
66
| | |
77
| | cannot infer type for type parameter `T` declared on the trait `Method`
88
| this method call resolves to `T`
99

1010
error[E0283]: type annotations needed
1111
--> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11
1212
|
13-
LL | thing.method();
14-
| ------^^^^^^--
13+
LL | thing.method(42);
14+
| ------^^^^^^----
1515
| | |
1616
| | cannot infer type for type parameter `T` declared on the trait `Method`
1717
| this method call resolves to `T`
@@ -26,10 +26,10 @@ LL | impl<X> Method<u32> for Thing<X> {
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727
help: use the fully qualified path for the potential candidates
2828
|
29-
LL | <Thing<_> as Method<i32>>::method(thing);
30-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31-
LL | <Thing<_> as Method<u32>>::method(thing);
32-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
LL | <Thing<_> as Method<i32>>::method(thing, 42);
30+
| ++++++++++++++++++++++++++++++++++ ~
31+
LL | <Thing<_> as Method<u32>>::method(thing, 42);
32+
| ++++++++++++++++++++++++++++++++++ ~
3333

3434
error: aborting due to 2 previous errors
3535

src/test/ui/traits/issue-77982.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ LL | opts.get(opt.as_ref());
3636
- impl AsRef<str> for String;
3737
help: use the fully qualified path for the potential candidates
3838
|
39-
LL | opts.get(<String as AsRef<OsStr>>::as_ref(opt));
40-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41-
LL | opts.get(<String as AsRef<Path>>::as_ref(opt));
42-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43-
LL | opts.get(<String as AsRef<[u8]>>::as_ref(opt));
44-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45-
LL | opts.get(<String as AsRef<str>>::as_ref(opt));
46-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39+
LL | opts.get(<str as AsRef<Path>>::as_ref(opt));
40+
| +++++++++++++++++++++++++++++ ~
41+
LL | opts.get(<str as AsRef<OsStr>>::as_ref(opt));
42+
| ++++++++++++++++++++++++++++++ ~
43+
LL | opts.get(<str as AsRef<str>>::as_ref(opt));
44+
| ++++++++++++++++++++++++++++ ~
45+
LL | opts.get(<str as AsRef<[u8]>>::as_ref(opt));
46+
| +++++++++++++++++++++++++++++ ~
4747
and 4 other candidates
4848

4949
error[E0283]: type annotations needed

0 commit comments

Comments
 (0)