Skip to content

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

Closed
@weiznich

Description

@weiznich

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions