Skip to content

Commit 670a6f1

Browse files
committed
Change wording to avoid being misleading
1 parent 1eb828e commit 670a6f1

File tree

8 files changed

+21
-27
lines changed

8 files changed

+21
-27
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177177
}
178178

179179
MethodError::IllegalSizedBound(candidates, needs_mut, bound_span, self_expr) => {
180-
let msg = format!("the `{}` method cannot be invoked on a trait object", item_name);
180+
let msg = if needs_mut {
181+
with_forced_trimmed_paths!(format!(
182+
"the `{item_name}` method cannot be invoked on `{rcvr_ty}`"
183+
))
184+
} else {
185+
format!("the `{item_name}` method cannot be invoked on a trait object")
186+
};
181187
let mut err = self.sess().struct_span_err(span, &msg);
182-
err.span_label(bound_span, "this has a `Sized` requirement");
188+
if !needs_mut {
189+
err.span_label(bound_span, "this has a `Sized` requirement");
190+
}
183191
if !candidates.is_empty() {
184192
let help = format!(
185193
"{an}other candidate{s} {were} found in the following trait{s}, perhaps \

src/test/ui/illegal-sized-bound/mutability-mismatch-arg.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22
fn test(t: &mut dyn Iterator<Item=&u64>) -> u64 {
3-
*t.min().unwrap() //~ ERROR the `min` method cannot be invoked on a trait object
3+
*t.min().unwrap() //~ ERROR the `min` method cannot be invoked on
44
}
55

66
fn main() {

src/test/ui/illegal-sized-bound/mutability-mismatch-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22
fn test(t: &dyn Iterator<Item=&u64>) -> u64 {
3-
*t.min().unwrap() //~ ERROR the `min` method cannot be invoked on a trait object
3+
*t.min().unwrap() //~ ERROR the `min` method cannot be invoked on
44
}
55

66
fn main() {

src/test/ui/illegal-sized-bound/mutability-mismatch-arg.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error: the `min` method cannot be invoked on a trait object
1+
error: the `min` method cannot be invoked on `&dyn Iterator<Item = &u64>`
22
--> $DIR/mutability-mismatch-arg.rs:3:9
33
|
44
LL | *t.min().unwrap()
55
| ^^^
6-
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
7-
|
8-
= note: this has a `Sized` requirement
96
|
107
help: you need `&mut dyn Iterator<Item = &u64>` instead of `&dyn Iterator<Item = &u64>`
118
|

src/test/ui/illegal-sized-bound/mutability-mismatch.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub trait MutTrait {
44
fn function(&mut self)
55
where
66
Self: Sized;
7-
//~^ this has a `Sized` requirement
87
}
98

109
impl MutTrait for MutType {
@@ -17,7 +16,6 @@ pub trait Trait {
1716
fn function(&self)
1817
where
1918
Self: Sized;
20-
//~^ this has a `Sized` requirement
2119
}
2220

2321
impl Trait for Type {
@@ -26,9 +24,9 @@ impl Trait for Type {
2624

2725
fn main() {
2826
(&MutType as &dyn MutTrait).function();
29-
//~^ ERROR the `function` method cannot be invoked on a trait object
27+
//~^ ERROR the `function` method cannot be invoked on `&dyn MutTrait`
3028
//~| HELP you need `&mut dyn MutTrait` instead of `&dyn MutTrait`
3129
(&mut Type as &mut dyn Trait).function();
32-
//~^ ERROR the `function` method cannot be invoked on a trait object
30+
//~^ ERROR the `function` method cannot be invoked on `&mut dyn Trait`
3331
//~| HELP you need `&dyn Trait` instead of `&mut dyn Trait`
3432
}

src/test/ui/illegal-sized-bound/mutability-mismatch.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
error: the `function` method cannot be invoked on a trait object
2-
--> $DIR/mutability-mismatch.rs:28:33
1+
error: the `function` method cannot be invoked on `&dyn MutTrait`
2+
--> $DIR/mutability-mismatch.rs:26:33
33
|
4-
LL | Self: Sized;
5-
| ----- this has a `Sized` requirement
6-
...
74
LL | (&MutType as &dyn MutTrait).function();
85
| ^^^^^^^^
96
|
107
= help: you need `&mut dyn MutTrait` instead of `&dyn MutTrait`
118

12-
error: the `function` method cannot be invoked on a trait object
13-
--> $DIR/mutability-mismatch.rs:31:35
9+
error: the `function` method cannot be invoked on `&mut dyn Trait`
10+
--> $DIR/mutability-mismatch.rs:29:35
1411
|
15-
LL | Self: Sized;
16-
| ----- this has a `Sized` requirement
17-
...
1812
LL | (&mut Type as &mut dyn Trait).function();
1913
| ^^^^^^^^
2014
|

src/test/ui/suggestions/imm-ref-trait-object.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn test(t: &dyn Iterator<Item=&u64>) -> u64 {
2-
t.min().unwrap() //~ ERROR the `min` method cannot be invoked on a trait object
2+
t.min().unwrap() //~ ERROR the `min` method cannot be invoked on `&dyn Iterator<Item = &u64>`
33
}
44

55
fn main() {

src/test/ui/suggestions/imm-ref-trait-object.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error: the `min` method cannot be invoked on a trait object
1+
error: the `min` method cannot be invoked on `&dyn Iterator<Item = &u64>`
22
--> $DIR/imm-ref-trait-object.rs:2:8
33
|
44
LL | t.min().unwrap()
55
| ^^^
6-
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
7-
|
8-
= note: this has a `Sized` requirement
96
|
107
help: you need `&mut dyn Iterator<Item = &u64>` instead of `&dyn Iterator<Item = &u64>`
118
|

0 commit comments

Comments
 (0)