Skip to content

Commit 2dd2b73

Browse files
authored
Rollup merge of rust-lang#105491 - sulami:master, r=compiler-errors
Illegal sized bounds: only suggest mutability change if needed In a scenario like ```rust struct Type; pub trait Trait { fn function(&mut self) where Self: Sized; } impl Trait for Type { fn function(&mut self) {} } fn main() { (&mut Type as &mut dyn Trait).function(); } ``` the problem is Sized, not the mutability of self. Thus don't emit the "you need &T instead of &mut T" note, or the other way around, as all it does is just invert the mutability of whatever was supplied. Fixes rust-lang#103622.
2 parents d5a8be4 + 40ba1c9 commit 2dd2b73

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

compiler/rustc_hir_typeck/src/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
209209
ProbeScope::TraitsInScope,
210210
) {
211211
Ok(ref new_pick) if pick.differs_from(new_pick) => {
212-
needs_mut = true;
212+
needs_mut = new_pick.self_ty.ref_mutability() != self_ty.ref_mutability();
213213
}
214214
_ => {}
215215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct MutType;
2+
3+
pub trait MutTrait {
4+
fn function(&mut self)
5+
where
6+
Self: Sized;
7+
//~^ this has a `Sized` requirement
8+
}
9+
10+
impl MutTrait for MutType {
11+
fn function(&mut self) {}
12+
}
13+
14+
struct Type;
15+
16+
pub trait Trait {
17+
fn function(&self)
18+
where
19+
Self: Sized;
20+
//~^ this has a `Sized` requirement
21+
}
22+
23+
impl Trait for Type {
24+
fn function(&self) {}
25+
}
26+
27+
fn main() {
28+
(&MutType as &dyn MutTrait).function();
29+
//~^ ERROR the `function` method cannot be invoked on a trait object
30+
//~| NOTE you need `&mut dyn MutTrait` instead of `&dyn MutTrait`
31+
(&mut Type as &mut dyn Trait).function();
32+
//~^ ERROR the `function` method cannot be invoked on a trait object
33+
//~| NOTE you need `&dyn Trait` instead of `&mut dyn Trait`
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: the `function` method cannot be invoked on a trait object
2+
--> $DIR/mutability-mismatch.rs:28:33
3+
|
4+
LL | Self: Sized;
5+
| ----- this has a `Sized` requirement
6+
...
7+
LL | (&MutType as &dyn MutTrait).function();
8+
| ^^^^^^^^
9+
|
10+
= note: you need `&mut dyn MutTrait` instead of `&dyn MutTrait`
11+
12+
error: the `function` method cannot be invoked on a trait object
13+
--> $DIR/mutability-mismatch.rs:31:35
14+
|
15+
LL | Self: Sized;
16+
| ----- this has a `Sized` requirement
17+
...
18+
LL | (&mut Type as &mut dyn Trait).function();
19+
| ^^^^^^^^
20+
|
21+
= note: you need `&dyn Trait` instead of `&mut dyn Trait`
22+
23+
error: aborting due to 2 previous errors
24+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct MutType;
2+
3+
pub trait MutTrait {
4+
fn function(&mut self)
5+
where
6+
Self: Sized;
7+
//~^ this has a `Sized` requirement
8+
}
9+
10+
impl MutTrait for MutType {
11+
fn function(&mut self) {}
12+
}
13+
14+
struct Type;
15+
16+
pub trait Trait {
17+
fn function(&self)
18+
where
19+
Self: Sized;
20+
//~^ this has a `Sized` requirement
21+
}
22+
23+
impl Trait for Type {
24+
fn function(&self) {}
25+
}
26+
27+
fn main() {
28+
(&mut MutType as &mut dyn MutTrait).function();
29+
//~^ ERROR the `function` method cannot be invoked on a trait object
30+
(&Type as &dyn Trait).function();
31+
//~^ ERROR the `function` method cannot be invoked on a trait object
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: the `function` method cannot be invoked on a trait object
2+
--> $DIR/regular.rs:28:41
3+
|
4+
LL | Self: Sized;
5+
| ----- this has a `Sized` requirement
6+
...
7+
LL | (&mut MutType as &mut dyn MutTrait).function();
8+
| ^^^^^^^^
9+
10+
error: the `function` method cannot be invoked on a trait object
11+
--> $DIR/regular.rs:30:27
12+
|
13+
LL | Self: Sized;
14+
| ----- this has a `Sized` requirement
15+
...
16+
LL | (&Type as &dyn Trait).function();
17+
| ^^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)