Higher-ranked lifetime causes inconsistent behavior with trait bounds. #34256
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.
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)
Similarly, we can define a side-condition using higher-ranked lifetimes:
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:
Whereas this will fail:
With the error:
This is doubly puzzling since
T: IntoIterator
doesn't requireT: Iterator
.See #34142 which shows similar issues with bounds on function definitions.
See #27113 which shows issues with 'outlives' relations in where-clauses.
The text was updated successfully, but these errors were encountered: