Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

No error reported when a generic parameter doesn't meet the requirement of an associated type #78893

Open

Description

@yshui

Code

I tried this code:

trait A {
    type T: Iterator<Item = i32>;
}

trait B {
    fn x(_: impl A<T = impl Send>);
}

I expected to see this happen:

error[E0277]: `impl Send` is not an iterator

Instead, this happened:

Code compiles

Version it worked on

It most recently worked on: 1.48.0-beta.8

Version with regression

rust nightly 2020-11-08 (likely not the earliest)

Activity

  1. added
    regression-from-stable-to-nightly
    on Nov 9, 2020
  2. added
    I-prioritize
    on Nov 9, 2020
  3. jonas-schievink commented on Nov 9, 2020

    @jonas-schievink
    Contributor

    cc @matthewjasper (might be caused by the projection bounds changes)

  4. added
    A-associated-items
    A-trait-system
    T-compiler
    on Nov 9, 2020
  5. changed the title [-]No error reported when an impl Trait doesn't met the requirement of an associated type[/-] [+]No error reported when an impl Trait doesn't meet the requirement of an associated type[/+] on Nov 9, 2020
  6. added
    C-bug
    A-impl-trait
    and removed
    A-impl-trait
    on Nov 9, 2020
  7. camelid commented on Nov 9, 2020

    @camelid
    Member

    Note: not just impl Trait; also occurs with ordinary trait bounds.

  8. changed the title [-]No error reported when an impl Trait doesn't meet the requirement of an associated type[/-] [+]No error reported when a generic parameter doesn't meet the requirement of an associated type[/+] on Nov 9, 2020
  9. camelid commented on Nov 9, 2020

    @camelid
    Member

    searched nightlies: from nightly-2020-10-01 to nightly-2020-11-09
    regressed nightly: nightly-2020-10-07
    searched commits: from a1dfd24 to 98edd1f
    regressed commit: 08e2d46

    bisected with cargo-bisect-rustc v0.6.0

    Host triple: x86_64-apple-darwin
    Reproduce with:

    cargo bisect-rustc --preserve --regress=success --start=2020-10-01 

    Indeed caused by #73905.

  10. added
    P-critical
    and removed
    I-prioritize
    on Nov 10, 2020
  11. 27 remaining items

  12. jackh726 commented on Feb 3, 2022

    @jackh726
    Member

    Ah, I missed that. Relabeled accordingly.

  13. added
    I-types-nominated
    T-types
    and removed on Jul 15, 2022
  14. lcnr commented on Jul 15, 2022

    @lcnr
    Contributor

    discussed during t-types backlog bonanza.

    Going to discuss this in a later meeting to make a final discussion here

  15. jackh726 commented on Jul 29, 2022

    @jackh726
    Member

    Discussed today during t-types meeting.

    We agreed that this should compile, given this also compiles:

    trait B {
        fn x(_: impl A);
    }

    In other words, the bounds specified in the function arg are a convenient way to add new bounds, but don't change the fact that you still must prove the bounds on the trait.

    We think this can be closed with the following tests (or test with revisions):

    1. A test added showing this compiles
    2. A test added that shows you can't call x without proving that T: Iterator<Item = i32>
  16. added
    E-needs-test
    and removed
    P-high
    I-types-nominated
    on Jul 29, 2022
  17. yshui commented on Jul 29, 2022

    @yshui
    ContributorAuthor

    @jackh726 Then shouldn't this also compile with the same logic?

    trait A<T: Iterator<Item = i32>> {
    }
    trait B {
        fn x<T: Send>(_: impl A<T>);
    }

    Why is associated type treated differently from type parameters?

  18. lcnr commented on Jul 29, 2022

    @lcnr
    Contributor

    @yshui we've ended up quickly talking about your question in the t-types meeting as well

    my intuition for this is that bounds on associated types have to be proven by the impl while bounds on generic arguments have to be proven by the users of that impl.

    A bound like T: Trait<Assoc = U> means that T: Trait, and <T as Trait>::Assoc == U must hold.

    The equality implies that all bounds on <T as Trait>::Assoc must also hold for U (as these two are equal), but it never requires it. Both T: Trait and <T as Trait>::Assoc == U do not require U: Iterator<Item = i32>.

    If you have T: Trait<U>, you cannot destruct this into smaller goals, so you have to prove all the requirements for this, including U: Iterator<Item = i32>.

    As mentioned in the meeting itself, this isn't too great of an explanation if you aren't already deeply involved in the trait system, but I hope it was at least somewhat understandable 😅

  19. yshui commented on Jul 29, 2022

    @yshui
    ContributorAuthor

    @lcnr This seems to be tied to the specific implementation of the trait system in rustc. And, at least to me, doesn't feel like an intuitive distinction between the 2 cases.

    i.e. another implementation of the Rust language might come along and implement this differently. (mrustc doesn't report error for either).

    But I guess in the end this isn't that impactful of a change, and I can go with the decision. 👍

  20. lcnr commented on Nov 10, 2025

    @lcnr
    Contributor

    cc https://rust-lang.zulipchat.com/#narrow/channel/144729-t-types/topic/E-mentor.3A.20check.20item.20bounds.20for.20Projection.20where-clause.20WF/with/554691484

    My opinion on this issue has changed since the types team meeting 3 years ago and I think we should try to change our internal implementation here. I still think implying these bounds is fine from a user-standpoint, but think not doing so in the implementation itself is causing a lot of issues

  21. added
    E-hard
    and removed
    E-needs-test
    on Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-associated-itemsA-trait-systemC-bugE-hardT-compilerT-typesregression-from-stable-to-stable

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions