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

Convert T::Foo resolution to use where clauses, not bounds #20300

Closed
nikomatsakis opened this issue Dec 29, 2014 · 6 comments · Fixed by #22512
Closed

Convert T::Foo resolution to use where clauses, not bounds #20300

nikomatsakis opened this issue Dec 29, 2014 · 6 comments · Fixed by #22512
Assignees
Labels
A-associated-items Area: Associated items (types, constants & functions)
Milestone

Comments

@nikomatsakis
Copy link
Contributor

Currently referencing an associated type like T::Foo searches the bounds on T rather than the full set of where clauses. This is no good.

@nikomatsakis
Copy link
Contributor Author

Nominating and placing on 1.0 alpha milestone. Ultimately PROBABLY a nice to have, but it's really surprising that this doesn't work.

@nikomatsakis nikomatsakis added the A-associated-items Area: Associated items (types, constants & functions) label Jan 7, 2015
@nikomatsakis nikomatsakis changed the title Convert T::Foo resolution to use where clauses, not bounds` Convert T::Foo resolution to use where clauses, not bounds Jan 8, 2015
@nikomatsakis nikomatsakis modified the milestones: 1.0 beta, 1.0 alpha Jan 9, 2015
@jroesch
Copy link
Member

jroesch commented Jan 10, 2015

cc me

@erickt
Copy link
Contributor

erickt commented Jan 10, 2015

Until this is implemented, you can also use a qualified type path <T as Trait>::Item:

trait Trait {
    type Item;
}

struct Foo;

impl Foo {
    fn foo1<T: Trait>(_x: T::Item) {}

    fn foo2<T>(_x: <T as Trait>::Item) where T: Trait {}
}

struct Bar;

impl Trait for Bar {
    type Item = i32;
}

fn main() {
    Foo::foo1::<Bar>(5i32);
    Foo::foo2::<Bar>(5i32);
}

@carllerche
Copy link
Member

cc me

@ahmedcharles
Copy link
Contributor

Would anyone mind mentoring this?

@nikomatsakis
Copy link
Contributor Author

@ahmedcharles a fix is in progress already, actually, but it turns out to be quite a tricky problem. Much trickier than it appeared at first.

bors added a commit that referenced this issue Feb 25, 2015
…ds, r=nikomatsakis

This is a fix for #20300 though as a side-sweep it fixes a number of stack overflows because it integrates cycle detection into the conversion process. I didn't go through and retest everything.

The tricky part here is that in some cases we have to go find the information we need from the AST -- we can't use the converted form of the where-clauses because we often have to handle something like `T::Item` *while converting the where-clauses themselves*. Since this is also not a fixed-point process we can't just try and keep trying to find the best order. So instead I modified the `AstConv` interface to allow you to request the bounds for a type parameter; we'll then do a secondary scan of the where-clauses to figure out what we need. This may create a cycle in some cases, but we have code to catch that.

Another approach that is NOT taken by this PR would be to "convert" `T::Item` into a form that does not specify what trait it's using. This then kind of defers the problem of picking the trait till later. That might be a good idea, but it would make normalization and everything else much harder, so I'm holding off on that (and hoping to find a better way for handling things like `i32::T`).

This PR also removes "most of" the `bounds` struct from `TypeParameterDef`. Still a little ways to go before `ParamBounds` can be removed entirely -- it's used for supertraits, for example (though those really ought to be converted, I think, to a call to `get_type_parameter_bounds` on `Self` from within the trait definition).

cc @jroesch 

Fixes #20300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants