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

Higher-ranked lifetime causes inconsistent behavior with trait bounds. #34256

Open
pierzchalski opened this issue Jun 13, 2016 · 1 comment
Open
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@pierzchalski
Copy link
Contributor

pierzchalski commented Jun 13, 2016

Existing code uses trait bounds to force side-conditions on associated types. For instance, IntoIterator is defined (in part) as follows:

Original snippet from 2016 (no longer syntactically legal)
trait IntoIterator where Self::IntoIter::Item == Self::Item {
    type Item;
    type IntoIter: Iterator;
}
trait IntoIterator {
    type Item;
    type IntoIter: Iterator<Item = Self::Item>;
}

Similarly, we can define a side-condition using higher-ranked lifetimes:

trait BorrowIterator where for<'a> &'a Self: IntoIterator {}

This is necessary when we want to implement a trait for a (non-reference) type T, but want to require trait implementations on &'a T where those trait implementations can refer to the borrow lifetime 'a.

However, the former concrete trait bounds are usable as trait bounds in functions. For instance, the following will compile:

fn needs_iter<T: IntoIterator>(){}

Whereas this will fail:

fn needs_borrow_iter<T: BorrowIterator>(){}

With the error:

error: the trait bound `for<'a> &'a T: std::iter::Iterator` is not satisfied [--explain E0277]
  --> <anon>:19:1
19 |> fn needs_borrow_iter<T: BorrowIterator>()
   |> ^
note: `&'a T` is not an iterator; maybe try calling `.iter()` or a similar method
note: required because of the requirements on the impl of `for<'a> std::iter::IntoIterator` for `&'a T`
note: required by `BorrowIterator`

This is doubly puzzling since T: IntoIterator doesn't require T: Iterator.

See #34142 which shows similar issues with bounds on function definitions.
See #27113 which shows issues with 'outlives' relations in where-clauses.

@Mark-Simulacrum Mark-Simulacrum added A-type-system Area: Type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
@steveklabnik
Copy link
Member

Triage: not aware of any change

@fmease fmease changed the title HKL causes inconsistent behavior with trait bounds. Higher-ranked lifetime causes inconsistent behavior with trait bounds. Sep 3, 2024
@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Sep 3, 2024
@fmease fmease added A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions labels Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants