Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illegal sized bounds: only suggest mutability change if needed #105491

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ProbeScope::TraitsInScope,
) {
Ok(ref new_pick) if pick.differs_from(new_pick) => {
needs_mut = true;
needs_mut = new_pick.self_ty.ref_mutability() != self_ty.ref_mutability();
}
_ => {}
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/ui/illegal-sized-bound/mutability-mismatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
struct MutType;

pub trait MutTrait {
fn function(&mut self)
where
Self: Sized;
//~^ this has a `Sized` requirement
}

impl MutTrait for MutType {
fn function(&mut self) {}
}

struct Type;

pub trait Trait {
fn function(&self)
where
Self: Sized;
//~^ this has a `Sized` requirement
}

impl Trait for Type {
fn function(&self) {}
}

fn main() {
(&MutType as &dyn MutTrait).function();
//~^ ERROR the `function` method cannot be invoked on a trait object
//~| NOTE you need `&mut dyn MutTrait` instead of `&dyn MutTrait`
(&mut Type as &mut dyn Trait).function();
//~^ ERROR the `function` method cannot be invoked on a trait object
//~| NOTE you need `&dyn Trait` instead of `&mut dyn Trait`
}
24 changes: 24 additions & 0 deletions src/test/ui/illegal-sized-bound/mutability-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error: the `function` method cannot be invoked on a trait object
--> $DIR/mutability-mismatch.rs:28:33
|
LL | Self: Sized;
| ----- this has a `Sized` requirement
...
LL | (&MutType as &dyn MutTrait).function();
| ^^^^^^^^
|
= note: you need `&mut dyn MutTrait` instead of `&dyn MutTrait`

error: the `function` method cannot be invoked on a trait object
--> $DIR/mutability-mismatch.rs:31:35
|
LL | Self: Sized;
| ----- this has a `Sized` requirement
...
LL | (&mut Type as &mut dyn Trait).function();
| ^^^^^^^^
|
= note: you need `&dyn Trait` instead of `&mut dyn Trait`

error: aborting due to 2 previous errors

32 changes: 32 additions & 0 deletions src/test/ui/illegal-sized-bound/regular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
struct MutType;

pub trait MutTrait {
fn function(&mut self)
where
Self: Sized;
//~^ this has a `Sized` requirement
}

impl MutTrait for MutType {
fn function(&mut self) {}
}

struct Type;

pub trait Trait {
fn function(&self)
where
Self: Sized;
//~^ this has a `Sized` requirement
}

impl Trait for Type {
fn function(&self) {}
}

fn main() {
(&mut MutType as &mut dyn MutTrait).function();
//~^ ERROR the `function` method cannot be invoked on a trait object
(&Type as &dyn Trait).function();
//~^ ERROR the `function` method cannot be invoked on a trait object
}
20 changes: 20 additions & 0 deletions src/test/ui/illegal-sized-bound/regular.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: the `function` method cannot be invoked on a trait object
--> $DIR/regular.rs:28:41
|
LL | Self: Sized;
| ----- this has a `Sized` requirement
...
LL | (&mut MutType as &mut dyn MutTrait).function();
| ^^^^^^^^

error: the `function` method cannot be invoked on a trait object
--> $DIR/regular.rs:30:27
|
LL | Self: Sized;
| ----- this has a `Sized` requirement
...
LL | (&Type as &dyn Trait).function();
| ^^^^^^^^

error: aborting due to 2 previous errors