Skip to content

Trait bounds don't work on default associated types #67187

Closed
@pythongirl325

Description

@pythongirl325

Trait bounds fail for default associated types, but not for re-assigned associated types.

The following code does not compile as seen in this playground.

#![feature(associated_type_defaults)]

trait T {
    type Item = ();
}

struct Foo;
impl T for Foo {}

fn f<I: T>() -> I::Item where I::Item: Default {
    Default::default()
}

fn main(){
    f::<Foo>();
}

This gives the error error[E0277]: the trait bound `<Foo as T>::Item: std::default::Default` is not satisfied

But the code does compile if impl T for Foo {} is replaced with

impl T for Foo {
    type Item = ();
}

as seen on this playground

Meta

rustc --version --verbose:

rustc 1.41.0-nightly (59947fcae 2019-12-08)
binary: rustc
commit-hash: 59947fcae6a40df12e33af8c8c7291014b7603e0
commit-date: 2019-12-08
host: x86_64-unknown-linux-gnu
release: 1.41.0-nightly
LLVM version: 9.0

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-compilerRelevant to the compiler 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