Skip to content

Passing &dyn Trait from Rc<dyn Trait> #149857

@rnikander

Description

@rnikander

People seem to think this code should work. But it doesn't.

trait Trait {}
fn foo(_: &dyn Trait) {}
fn bar(rc: std::rc::Rc<dyn Trait>) { foo(&rc) }

The error:

error[E0277]: the trait bound `Rc<(dyn Trait + 'static)>: Trait` is not satisfied
 --> src/main.rs:4:42
  |
4 | fn bar(rc: std::rc::Rc<dyn Trait>) { foo(&rc) }
  |                                          ^^^ the trait `Trait` is not implemented for `Rc<(dyn Trait + 'static)>`
  |

From discussion on discord here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsA-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions