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

RFC 1214 problems with bounds on associated type of type parameter #30311

Closed
jld opened this issue Dec 10, 2015 · 7 comments
Closed

RFC 1214 problems with bounds on associated type of type parameter #30311

jld opened this issue Dec 10, 2015 · 7 comments
Labels
A-type-system Area: Type system C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jld
Copy link
Contributor

jld commented Dec 10, 2015

Consider this function:

fn example<O, B: ToOwned<Owned=O>>(_:&B) { }

This gives an RFC 1214 warning about needing to also add a O: Borrow<B> constraint in order to mention ToOwned<Owned=O>, but I don't understand why. As I understand it, this is effectively a type-level function being passed a proof that B: ToOwned, which necessarily includes a proof that <B as ToOwned>::Owned: Borrow<B>. Requiring an additional proof that <B as ToOwned>::Owned == O to instantiate the fn shouldn't weaken that… right?

Indeed, what I'd expected was the opposite: that I could declare where B: ToOwned<Owned=O> and that would be enough to satisfy any O: Borrow<B> obligations within the scope of the binder (e.g., calling HashMap::get) without having to explicitly declare that.

The above example is trivial for simplicity's sake, but the context where I ran into this wasn't: the owned type was an associated type of a type parameter of the impl containing the fn that was parameterized on the borrowed type.

(cc @aturon)

@aturon
Copy link
Member

aturon commented Dec 10, 2015

cc @nikomatsakis

@ghost
Copy link

ghost commented Dec 14, 2015

You run into this on any type which has the cyclic requirements that ToOwned has. You can also do it without an extra type parameter on the function in this contrived example:

trait Example {
    type Param;
}
trait NestedExample: Example where <Self as Example>::Param: Example { }

fn example<T: NestedExample>(_: &T) { }

impl Example for u32 {
    type Param = u32;
}
impl NestedExample for u32 { }

In this case, it appears that the associated type constraints aren't being propagated like they should. Similarly to how trait Eq: PartialEq tells us here that any type with Eq automatically has PartialEq, this example should imply that any type with NestedExample automatically has Example on its associated type.

@jonas-schievink
Copy link
Contributor

Isn't this just an example of "non-supertrait bounds are not elaborated"?

@jld
Copy link
Contributor Author

jld commented Dec 21, 2015

“non-supertrait bounds are not elaborated” seems to be either #20671 or #10950.

Those seem to be… mostly about type parameters. Associated types are either weaker or stronger (depending on which side you're on) than parameters in that there's exactly one per (self type * other parameters) instead of a set, but I don't know if that's significant for this area of typechecking.

@nikomatsakis
Copy link
Contributor

@jonas-schievink wrote:

Isn't this just an example of "non-supertrait bounds are not elaborated"?

@jld replied:

Those seem to be… mostly about type parameters.

Nonetheless, I think @jonas-schievink is largely correct in terms of the underlying issues in the implementation.

@steveklabnik steveklabnik added the A-type-system Area: Type system label Jun 6, 2016
@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jul 24, 2017
@bstrie
Copy link
Contributor

bstrie commented Feb 20, 2020

Triage: this is still relevant, although am I correct in thinking that this would be addressed once the Implied Bounds RFC (#44491) is implemented?

@jonas-schievink
Copy link
Contributor

Closing in favor of #44491 / #20671

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants