Skip to content

Type checker doesn't consider trait duality when reasoning about type parameters for associated types #38917

Closed
@bholley

Description

@bholley
Contributor

See the testcase here: https://is.gd/WZh43I

I'd normally run this by a few people before filing an issue, but #rust is quiet on a Saturday night. ;-)

@Manishearth thoughts?

Activity

Manishearth

Manishearth commented on Jan 8, 2017

@Manishearth
Member

Trait resolution basically doesn't know how to figure out that two types are equal in the case of "dual traits".

This is one of the inevitable issues @nox is going to hit in his Servo script bindings refactor 😉

I do wish we had a more formalized notion of type families that could be used to better instruct the compiler in these cases. @nikomatsakis was talking about this in his blog, and I've personally found the need for a family-trait type situation more than once, both when modeling dualities and when modeling generic abstractions over sets of types like you're trying to do.

Mark-Simulacrum

Mark-Simulacrum commented on May 19, 2017

@Mark-Simulacrum
Member

This appears to work today; E-needstest.

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on May 19, 2017
nikomatsakis

nikomatsakis commented on May 25, 2017

@nikomatsakis
Contributor

I don't think it works necessarily -- if you remove the bound that @bholley highlighted, you still get an error.

removed
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on May 25, 2017
nikomatsakis

nikomatsakis commented on May 25, 2017

@nikomatsakis
Contributor

The example that @bholley expected to pass (and I think he is right that it should) is this one:

use std::borrow::Borrow;

trait TNode: Sized {
  type ConcreteElement: TElement<ConcreteNode = Self>;
}

trait TElement: Sized {
    type ConcreteNode: TNode<ConcreteElement = Self>;
}

trait DomTraversal<N: TNode> {
    type BorrowElement: Borrow<N::ConcreteElement>;
}

fn recalc_style_at<E, D>()
    where E: TElement,
          D: DomTraversal<E::ConcreteNode>,
{
}


fn main() {
}
estebank

estebank commented on Nov 5, 2019

@estebank
Contributor

Current output:

error[E0277]: the trait bound `<D as DomTraversal<<E as TElement>::ConcreteNode>>::BorrowElement: std::borrow::Borrow<E>` is not satisfied
  --> src/main.rs:15:1
   |
11 |   trait DomTraversal<N: TNode> {
   |   ---------------------------- required by `DomTraversal`
...
15 | / fn recalc_style_at<E, D>()
16 | |     where E: TElement,
17 | |           D: DomTraversal<E::ConcreteNode>,
   | |                                            - help: consider further restricting the associated type: `, <D as DomTraversal<<E as TElement>::ConcreteNode>>::BorrowElement: std::borrow::Borrow<E>`
18 | | {
19 | | }
   | |_^ the trait `std::borrow::Borrow<E>` is not implemented for `<D as DomTraversal<<E as TElement>::ConcreteNode>>::BorrowElement`

7 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nikomatsakis@bholley@estebank@Manishearth@jonas-schievink

      Issue actions

        Type checker doesn't consider trait duality when reasoning about type parameters for associated types · Issue #38917 · rust-lang/rust