Skip to content

Associated types bug related to trait objects #30833

Closed
@defyrlt

Description

@defyrlt

Playpen
Relevant code (note that DoesStuff is a trait):

impl Deref for Foobar {
    type Target = DoesStuff;

    fn deref(&self) -> &Self::Target {
        match self {
            &Foobar::Foo(ref e) => {
                let newref = e.clone();
                newref as &DoesStuff // error here
                // newref as &Self::Target // just works..
                // newref // returning without `as` works (code below works too)
            }
            &Foobar::Bar(ref e) => e,
        }
    }
}

Error text:

<anon>:26:26: 26:31 error: cannot infer an appropriate lifetime for pattern due to conflicting requirements [E0495]
<anon>:26             &Foobar::Foo(ref e) => {
                                   ^~~~~
<anon>:27:30: 27:39 note: first, the lifetime cannot outlive the method call at 27:29...
<anon>:27                 let newref = e.clone();
                                       ^~~~~~~~~
<anon>:27:30: 27:31 note: ...so that method receiver is valid for the method call
<anon>:27                 let newref = e.clone();
                                       ^
<anon>:24:38: 34:6 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 24:37...
<anon>:24     fn deref(&self) -> &Self::Target {
<anon>:25         match self {
<anon>:26             &Foobar::Foo(ref e) => {
<anon>:27                 let newref = e.clone();
<anon>:28                 newref as &DoesStuff // error here
<anon>:29                 // newref as &Self::Target // just works..
          ...
<anon>:28:17: 28:37 note: ...so that trait type parameters matches those specified on the impl (expected `core::ops::CoerceUnsized<&DoesStuff + 'static>`, found `core::ops::CoerceUnsized<&DoesStuff + 'static>`)
<anon>:28                 newref as &DoesStuff // error here
                          ^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101

cc @mbrubeck since on the IRC you've told me that it's worthy filing an issue :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions