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

Misleading note message while trying to use a not object safe method on a trait object #103622

Closed
weiznich opened this issue Oct 27, 2022 · 1 comment · Fixed by #105491
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@weiznich
Copy link
Contributor

Given the following code: Playground

struct PgConnection;

pub trait PgMetadataLookup {
    fn cast_with_callback<R: ?Sized>(&mut self, c: &dyn Fn(&mut Self) -> &mut R) -> &mut R
    where Self: Sized;
    
}

pub trait MultiMetadataLookup {
}

impl PgMetadataLookup for PgConnection {
    fn cast_with_callback<R: ?Sized>(&mut self, c: &dyn Fn(&mut Self) -> &mut R) -> &mut R where Self: Sized{
        c(self)
    }
    
}

impl<T> MultiMetadataLookup for T where T: PgMetadataLookup {
}


fn bar(_a: &mut dyn MultiMetadataLookup) {
    
}

fn foo(conn: &mut PgConnection) -> &mut dyn PgMetadataLookup {
    conn    
}

fn main() {
    let mut conn = PgConnection;
    let mut pg_lookup = foo(&mut conn);
    bar(pg_lookup.cast_with_callback(&|s| s))
}

The current output is:

error: the `cast_with_callback` method cannot be invoked on a trait object
  --> src/main.rs:34:19
   |
5  |     where Self: Sized;
   |                 ----- this has a `Sized` requirement
...
34 |     bar(pg_lookup.cast_with_callback(&|s| s))
   |                   ^^^^^^^^^^^^^^^^^^
   |
   = note: you need `&dyn PgMetadataLookup` instead of `&mut dyn PgMetadataLookup`

Ideally the output should not include a not telling me to use &dyn PgMetadataLookup instead of a &mut dyn PgMetadataLookup as this is misleading. In addition following this suggestion only changes the note of the error message such that it now says "note: you need &mut dyn PgMetadataLookup instead of &dyn PgMetadataLookup" which is also wrong.

@weiznich weiznich added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 27, 2022
@BoxyUwU BoxyUwU self-assigned this Oct 31, 2022
@WaffleLapkin
Copy link
Member

MRE (playground):

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();
}
error: the `function` method cannot be invoked on a trait object
  --> src/main.rs:12:35
   |
4  |     fn function(&mut self) where Self: Sized;
   |                                        ----- this has a `Sized` requirement
...
12 |     (&mut Type as &mut dyn Trait).function();
   |                                   ^^^^^^^^
   |
   = note: you need `&dyn Trait` instead of `&mut dyn Trait`

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Dec 13, 2022
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.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Dec 13, 2022
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.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2022
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.
@bors bors closed this as completed in 40ba1c9 Dec 14, 2022
@BoxyUwU BoxyUwU removed their assignment Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants