Skip to content

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

Closed

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.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 27, 2022
  2. self-assigned this
    on Oct 31, 2022
  3. WaffleLapkin commented on Oct 31, 2022

    @WaffleLapkin
    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`
    
  4. added 2 commits that reference this issue on Dec 13, 2022
    2dd2b73
    9d75865
  5. added a commit that references this issue on Dec 14, 2022
    49b9907
  6. removed their assignment
    on Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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