Skip to content

Commit 4f67d26

Browse files
committed
Make E0599's label more clear for field which is used like a method.
fixes #127178
1 parent 52f3c71 commit 4f67d26

File tree

66 files changed

+445
-118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+445
-118
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22772277
let call_expr = tcx.hir().expect_expr(tcx.parent_hir_id(expr.hir_id));
22782278

22792279
if let Some(span) = call_expr.span.trim_start(item_name.span) {
2280-
err.span_suggestion(
2280+
err.span_suggestion_verbose(
22812281
span,
22822282
"remove the arguments",
22832283
"",
@@ -3266,7 +3266,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32663266
.copied();
32673267

32683268
if explain {
3269-
err.help("items from traits can only be used if the trait is in scope");
3269+
err.help(
3270+
"items from traits can only be used if the trait is implemented and in scope",
3271+
);
32703272
}
32713273

32723274
let msg = format!(
@@ -3570,7 +3572,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35703572
}
35713573
}
35723574
}
3573-
if self.suggest_valid_traits(err, item_name, valid_out_of_scope_traits, true) {
3575+
if self.suggest_valid_traits(
3576+
err,
3577+
item_name,
3578+
valid_out_of_scope_traits,
3579+
!trait_missing_method,
3580+
) {
35743581
return;
35753582
}
35763583

@@ -3680,7 +3687,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36803687
_ => None,
36813688
};
36823689
if !trait_missing_method {
3683-
err.help(if param_type.is_some() {
3690+
err.span_help(span, if param_type.is_some() {
36843691
"items from traits can only be used if the type parameter is bounded by the trait"
36853692
} else {
36863693
"items from traits can only be used if the trait is implemented and in scope"

tests/ui/associated-consts/associated-const-no-item.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0599]: no associated item named `ID` found for type `i32` in the current
44
LL | const X: i32 = <i32>::ID;
55
| ^^ associated item not found in `i32`
66
|
7-
= help: items from traits can only be used if the trait is implemented and in scope
7+
help: items from traits can only be used if the trait is implemented and in scope
8+
--> $DIR/associated-const-no-item.rs:5:23
9+
|
10+
LL | const X: i32 = <i32>::ID;
11+
| ^^
812
note: `Foo` defines an item `ID`, perhaps you need to implement it
913
--> $DIR/associated-const-no-item.rs:1:1
1014
|

tests/ui/async-await/in-trait/dont-project-to-specializable-projection.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | match fut.as_mut().poll(ctx) {
2929
|
3030
= note: the method is available for `Pin<&mut impl Future<Output = ()>>` here
3131
|
32-
= help: items from traits can only be used if the trait is in scope
32+
= help: items from traits can only be used if the trait is implemented and in scope
3333
help: trait `Future` which provides `poll` is implemented but not in scope; perhaps you want to import it
3434
|
3535
LL + use std::future::Future;

tests/ui/auto-ref-slice-plus-ref.stderr

+20-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0599]: no method named `test_mut` found for struct `Vec<{integer}>` in th
44
LL | a.test_mut();
55
| ^^^^^^^^
66
|
7-
= help: items from traits can only be used if the trait is implemented and in scope
7+
help: items from traits can only be used if the trait is implemented and in scope
8+
--> $DIR/auto-ref-slice-plus-ref.rs:7:7
9+
|
10+
LL | a.test_mut();
11+
| ^^^^^^^^
812
note: `MyIter` defines an item `test_mut`, perhaps you need to implement it
913
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
1014
|
@@ -19,7 +23,11 @@ error[E0599]: no method named `test` found for struct `Vec<{integer}>` in the cu
1923
LL | a.test();
2024
| ^^^^ method not found in `Vec<{integer}>`
2125
|
22-
= help: items from traits can only be used if the trait is implemented and in scope
26+
help: items from traits can only be used if the trait is implemented and in scope
27+
--> $DIR/auto-ref-slice-plus-ref.rs:8:7
28+
|
29+
LL | a.test();
30+
| ^^^^
2331
note: `MyIter` defines an item `test`, perhaps you need to implement it
2432
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
2533
|
@@ -32,7 +40,11 @@ error[E0599]: no method named `test` found for array `[{integer}; 1]` in the cur
3240
LL | ([1]).test();
3341
| ^^^^ method not found in `[{integer}; 1]`
3442
|
35-
= help: items from traits can only be used if the trait is implemented and in scope
43+
help: items from traits can only be used if the trait is implemented and in scope
44+
--> $DIR/auto-ref-slice-plus-ref.rs:10:11
45+
|
46+
LL | ([1]).test();
47+
| ^^^^
3648
note: `MyIter` defines an item `test`, perhaps you need to implement it
3749
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
3850
|
@@ -45,7 +57,11 @@ error[E0599]: no method named `test` found for reference `&[{integer}; 1]` in th
4557
LL | (&[1]).test();
4658
| ^^^^ method not found in `&[{integer}; 1]`
4759
|
48-
= help: items from traits can only be used if the trait is implemented and in scope
60+
help: items from traits can only be used if the trait is implemented and in scope
61+
--> $DIR/auto-ref-slice-plus-ref.rs:11:12
62+
|
63+
LL | (&[1]).test();
64+
| ^^^^
4965
note: `MyIter` defines an item `test`, perhaps you need to implement it
5066
--> $DIR/auto-ref-slice-plus-ref.rs:14:1
5167
|

tests/ui/box/unit/unique-object-noncopyable.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ LL | let _z = y.clone();
1212
which is required by `Box<dyn Foo>: Clone`
1313
`dyn Foo: Clone`
1414
which is required by `Box<dyn Foo>: Clone`
15-
= help: items from traits can only be used if the trait is implemented and in scope
15+
help: items from traits can only be used if the trait is implemented and in scope
16+
--> $DIR/unique-object-noncopyable.rs:24:16
17+
|
18+
LL | let _z = y.clone();
19+
| ^^^^^
1620
= note: the following trait defines an item `clone`, perhaps you need to implement it:
1721
candidate #1: `Clone`
1822

tests/ui/coherence/coherence_inherent.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
44
LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use Lib::TheTrait;

tests/ui/coherence/coherence_inherent_cc.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0599]: no method named `the_fn` found for reference `&TheStruct` in the c
44
LL | s.the_fn();
55
| ^^^^^^ method not found in `&TheStruct`
66
|
7-
= help: items from traits can only be used if the trait is in scope
7+
= help: items from traits can only be used if the trait is implemented and in scope
88
help: trait `TheTrait` which provides `the_fn` is implemented but not in scope; perhaps you want to import it
99
|
1010
LL + use coherence_inherent_cc_lib::TheTrait;

tests/ui/confuse-field-and-method/issue-2392.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
1919
| ------------- method `not_closure` not found for this struct
2020
...
2121
LL | o_closure.not_closure();
22-
| ^^^^^^^^^^^-- help: remove the arguments
23-
| |
24-
| field, not a method
22+
| ^^^^^^^^^^^ field, not a method
23+
|
24+
help: remove the arguments
25+
|
26+
LL - o_closure.not_closure();
27+
LL + o_closure.not_closure;
28+
|
2529

2630
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
2731
--> $DIR/issue-2392.rs:42:12
@@ -86,9 +90,13 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
8690
| ------------- method `not_closure` not found for this struct
8791
...
8892
LL | w.wrap.not_closure();
89-
| ^^^^^^^^^^^-- help: remove the arguments
90-
| |
91-
| field, not a method
93+
| ^^^^^^^^^^^ field, not a method
94+
|
95+
help: remove the arguments
96+
|
97+
LL - w.wrap.not_closure();
98+
LL + w.wrap.not_closure;
99+
|
92100

93101
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
94102
--> $DIR/issue-2392.rs:58:24

tests/ui/copy-a-resource.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | struct Foo {
77
LL | let _y = x.clone();
88
| ^^^^^ method not found in `Foo`
99
|
10-
= help: items from traits can only be used if the trait is implemented and in scope
10+
help: items from traits can only be used if the trait is implemented and in scope
11+
--> $DIR/copy-a-resource.rs:18:16
12+
|
13+
LL | let _y = x.clone();
14+
| ^^^^^
1115
= note: the following trait defines an item `clone`, perhaps you need to implement it:
1216
candidate #1: `Clone`
1317

tests/ui/functions-closures/fn-help-with-err.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ error[E0599]: no method named `bar` found for struct `Arc<{closure@$DIR/fn-help-
1010
LL | arc2.bar();
1111
| ^^^ method not found in `Arc<{closure@fn-help-with-err.rs:18:36}>`
1212
|
13-
= help: items from traits can only be used if the trait is implemented and in scope
13+
help: items from traits can only be used if the trait is implemented and in scope
14+
--> $DIR/fn-help-with-err.rs:19:10
15+
|
16+
LL | arc2.bar();
17+
| ^^^
1418
note: `Bar` defines an item `bar`, perhaps you need to implement it
1519
--> $DIR/fn-help-with-err.rs:5:1
1620
|

tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn f(a: S) {
2626
a.f();
2727
//~^ ERROR the method `f` exists for struct `S`, but its trait bounds were not satisfied
2828
//~| NOTE method cannot be called on `S` due to unsatisfied trait bounds
29+
//~| HELP items from traits can only be used if the trait is implemented and in scope
2930
}
3031

3132
fn main() {}

tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ LL | impl<T: X<Y<i32> = i32>> M for T {}
1414
| ^^^^^^^^^^^^ - -
1515
| |
1616
| unsatisfied trait bound introduced here
17-
= help: items from traits can only be used if the trait is implemented and in scope
17+
help: items from traits can only be used if the trait is implemented and in scope
18+
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:26:7
19+
|
20+
LL | a.f();
21+
| ^
1822
note: `M` defines an item `f`, perhaps you need to implement it
1923
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:8:1
2024
|

tests/ui/higher-ranked/trait-bounds/hrtb-doesnt-borrow-self-1.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ note: the following trait bounds were not satisfied:
1515
|
1616
LL | impl<T> StreamExt for T where for<'a> &'a mut T: Stream {}
1717
| --------- - ^^^^^^ unsatisfied trait bound introduced here
18-
= help: items from traits can only be used if the trait is implemented and in scope
18+
help: items from traits can only be used if the trait is implemented and in scope
19+
--> $DIR/hrtb-doesnt-borrow-self-1.rs:116:22
20+
|
21+
LL | let filter = map.filterx(|x: &_| true);
22+
| ^^^^^^^
1923
note: `StreamExt` defines an item `filterx`, perhaps you need to implement it
2024
--> $DIR/hrtb-doesnt-borrow-self-1.rs:66:1
2125
|

tests/ui/higher-ranked/trait-bounds/hrtb-doesnt-borrow-self-2.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ note: the following trait bounds were not satisfied:
1515
|
1616
LL | impl<T> StreamExt for T where for<'a> &'a mut T: Stream {}
1717
| --------- - ^^^^^^ unsatisfied trait bound introduced here
18-
= help: items from traits can only be used if the trait is implemented and in scope
18+
help: items from traits can only be used if the trait is implemented and in scope
19+
--> $DIR/hrtb-doesnt-borrow-self-2.rs:112:24
20+
|
21+
LL | let count = filter.countx();
22+
| ^^^^^^
1923
note: `StreamExt` defines an item `countx`, perhaps you need to implement it
2024
--> $DIR/hrtb-doesnt-borrow-self-2.rs:66:1
2125
|

tests/ui/hygiene/no_implicit_prelude.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LL | fn f() { ::bar::m!(); }
2222
LL | ().clone()
2323
| ^^^^^
2424
|
25-
= help: items from traits can only be used if the trait is in scope
25+
= help: items from traits can only be used if the trait is implemented and in scope
2626
help: there is a method `clone_from` with a similar name, but with different arguments
2727
--> $SRC_DIR/core/src/clone.rs:LL:COL
2828
= note: this error originates in the macro `::bar::m` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/hygiene/trait_items.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn f() { ::baz::m!(); }
1010
LL | pub macro m() { ().f() }
1111
| ^ method not found in `()`
1212
|
13-
= help: items from traits can only be used if the trait is in scope
13+
= help: items from traits can only be used if the trait is implemented and in scope
1414
= note: this error originates in the macro `::baz::m` (in Nightly builds, run with -Z macro-backtrace for more info)
1515
help: trait `T` which provides `f` is implemented but not in scope; perhaps you want to import it
1616
|

tests/ui/impl-trait/call_method_on_inherent_impl_on_rigid_type.current.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0599]: no method named `my_debug` found for reference `&impl Debug` in th
44
LL | x.my_debug();
55
| ^^^^^^^^ method not found in `&impl Debug`
66
|
7-
= help: items from traits can only be used if the trait is implemented and in scope
7+
help: items from traits can only be used if the trait is implemented and in scope
8+
--> $DIR/call_method_on_inherent_impl_on_rigid_type.rs:16:11
9+
|
10+
LL | x.my_debug();
11+
| ^^^^^^^^
812
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
913
--> $DIR/call_method_on_inherent_impl_on_rigid_type.rs:4:1
1014
|

tests/ui/impl-trait/call_method_on_inherent_impl_ref.current.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | fn my_debug(&self);
77
LL | x.my_debug();
88
| ^^^^^^^^ method not found in `impl Debug`
99
|
10-
= help: items from traits can only be used if the trait is implemented and in scope
10+
help: items from traits can only be used if the trait is implemented and in scope
11+
--> $DIR/call_method_on_inherent_impl_ref.rs:20:11
12+
|
13+
LL | x.my_debug();
14+
| ^^^^^^^^
1115
note: `MyDebug` defines an item `my_debug`, perhaps you need to implement it
1216
--> $DIR/call_method_on_inherent_impl_ref.rs:4:1
1317
|

tests/ui/impl-trait/call_method_without_import.no_import.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | x.fmt(f);
77
|
88
= note: the method is available for `impl Debug` here
99
|
10-
= help: items from traits can only be used if the trait is in scope
10+
= help: items from traits can only be used if the trait is implemented and in scope
1111
help: trait `Debug` which provides `fmt` is implemented but not in scope; perhaps you want to import it
1212
|
1313
LL + use std::fmt::Debug;
@@ -19,7 +19,7 @@ error[E0599]: no method named `fmt` found for mutable reference `&mut impl Debug
1919
LL | x.fmt(f);
2020
| ^^^ method not found in `&mut impl Debug`
2121
|
22-
= help: items from traits can only be used if the trait is in scope
22+
= help: items from traits can only be used if the trait is implemented and in scope
2323
help: the following traits which provide `fmt` are implemented but not in scope; perhaps you want to import one of them
2424
|
2525
LL + use std::fmt::Binary;

tests/ui/impl-trait/issues/issue-21659-show-relevant-trait-impls-3.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | struct Bar;
77
LL | f1.foo(1usize);
88
| ^^^ method not found in `Bar`
99
|
10-
= help: items from traits can only be used if the trait is implemented and in scope
10+
help: items from traits can only be used if the trait is implemented and in scope
11+
--> $DIR/issue-21659-show-relevant-trait-impls-3.rs:20:8
12+
|
13+
LL | f1.foo(1usize);
14+
| ^^^
1115
note: `Foo` defines an item `foo`, perhaps you need to implement it
1216
--> $DIR/issue-21659-show-relevant-trait-impls-3.rs:1:1
1317
|

tests/ui/impl-trait/method-suggestion-no-duplication.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ LL | struct Foo;
77
LL | foo(|s| s.is_empty());
88
| ^^^^^^^^ method not found in `Foo`
99
|
10-
= help: items from traits can only be used if the trait is implemented and in scope
10+
help: items from traits can only be used if the trait is implemented and in scope
11+
--> $DIR/method-suggestion-no-duplication.rs:7:15
12+
|
13+
LL | foo(|s| s.is_empty());
14+
| ^^^^^^^^
1115
= note: the following trait defines an item `is_empty`, perhaps you need to implement it:
1216
candidate #1: `ExactSizeIterator`
1317

tests/ui/impl-trait/no-method-suggested-traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ fn main() {
2222

2323
1u32.method();
2424
//~^ ERROR no method named
25-
//~|items from traits can only be used if the trait is in scope
25+
//~|items from traits can only be used if the trait is implemented and in scope
2626
std::rc::Rc::new(&mut Box::new(&1u32)).method();
27-
//~^items from traits can only be used if the trait is in scope
27+
//~^items from traits can only be used if the trait is implemented and in scope
2828
//~| ERROR no method named `method` found for struct
2929

3030
'a'.method();

0 commit comments

Comments
 (0)