Skip to content

Error With Default Associated Type Pointer #32350

Closed
@KodrAus

Description

@KodrAus

The following used to work in nightly, but doesn't anymore (I'm not sure when it changed):

#![feature(associated_type_defaults)]

pub trait Emitter<'a> {
    type Ctxt: 'a;
    type CtxtBrw: 'a = &'a Self::Ctxt;

    fn get_cx(&'a self) -> Self::CtxtBrw;
}

struct MyCtxt;

struct MyEmitter {
    ctxt: MyCtxt
}

impl <'a> Emitter<'a> for MyEmitter {
    type Ctxt = MyCtxt;

    fn get_cx(&'a self) -> &'a MyCtxt {
        &self.ctxt
    }
}

fn main() {

}

The above returns the following error:

<anon>:19:5: 21:6 error: method `get_cx` has an incompatible type for trait:
 expected associated type,
    found &-ptr [E0053]
<anon>:19     fn get_cx(&'a self) -> &'a MyCtxt {
<anon>:20         &self.ctxt
<anon>:21     }
<anon>:19:5: 21:6 help: see the detailed explanation for E0053
error: aborting due to previous error
playpen: application terminated with error code 101

If I manually implement CtxtBrw then it does compile:

impl <'a> Emitter<'a> for MyEmitter {
    type Ctxt = MyCtxt;
    type CtxtBrw = &'a Self::Ctxt;

    fn get_cx(&'a self) -> &'a MyCtxt {
        &self.ctxt
    }
}

Is this expected behaviour? And if so, what's the rationale so I know if I'm trying to do something I shouldn't :)

Metadata

Metadata

Labels

A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.F-associated_type_defaults`#![feature(associated_type_defaults)]`T-langRelevant to the language team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions