Skip to content

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

Closed

Description

@bholley

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

  1. 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.

  2. Mark-Simulacrum commented on May 19, 2017

    @Mark-Simulacrum
    Member

    This appears to work today; E-needstest.

  3. 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
  4. 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.

  5. 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
  6. 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() {
    }
  7. 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`
    
  8. added
    A-associated-itemsArea: Associated items (types, constants & functions)
    T-langRelevant to the language team
    on Nov 5, 2019
  9. added a commit that references this issue on Feb 2, 2021
    acd5b4b
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

      No branches or pull requests

      Issue actions