Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type defaults should not be allowed if they don’t satisfy type constraints #28024

Open
nagisa opened this Issue Aug 26, 2015 · 8 comments

Comments

Projects
None yet
7 participants
@nagisa
Copy link
Contributor

nagisa commented Aug 26, 2015

As spotted in this internals thread:

struct Foo;

fn show<T: Debug = Foo>(t: T) {
    println!("{:?}", x)
}

compiles. Lack of early error/warning/lint here might be hiding bugs in API design if the default never actually kicks in inside the library.

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Aug 26, 2015

triage: I-nominated

@alexcrichton alexcrichton added the T-lang label Sep 9, 2015

@nikomatsakis nikomatsakis self-assigned this Oct 1, 2015

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 1, 2015

triage: P-high

Seems like a straightforward oversight in the WF checker.

@rust-highfive rust-highfive added P-high and removed I-nominated labels Oct 1, 2015

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 7, 2015

Hmm, thinking more about this, it's perhaps not as straightforward as I originally thought. It gets into problems much like higher-ranked regions etc.

For example:

fn foo<T,U = T>()
    where U: Clone
{
}

Here, we can't fully check the value of the default ahead of time, because it references another type parameter (T), and we don't know yet whether T: Clone holds or not. I think for maximal consistency with rust-lang/rfcs#1214, though, we would strive to check where possible, and not otherwise.

cc @rust-lang/lang

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 7, 2015

Nominating for discussion.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 7, 2015

I'm also going to lower the triage. It seems unlikely that anyone would intentionally abuse this, and I think we'll be able to fix this without undue anger or breakage out there in the wild.

triage: P-medium

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Jul 14, 2016

Triage: still a problem.

@nikomatsakis Long time since any update. Do you know how to fix this and can mentor?

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Jul 14, 2016

So it's worth noting that this will compile (modulo some minor fixes), but, if you try to actually use show(), it won't work:

struct Foo;

fn show<T: std::fmt::Debug = Foo>(t: T) {
    println!("{:?}", t)
}

fn main() {
    let f = Foo;
    show(f);
}

gives

error: the trait bound `Foo: std::fmt::Debug` is not satisfied [--explain E0277]
 --> <anon>:9:5
9 |>     show(f);
  |>     ^^^^
note: `Foo` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
note: required by `show`
@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jul 28, 2016

In general, type defaults have been kind of stalled; we've been wanting to rethink the design overall, which has led to a lack of motivation to fixing details of the implementation. I wouldn't in any case tackle this bug first -- the overall algorithm isn't really following the RFC, so I'd fix that first. Not sure if there's an issue about that though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.