-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-trait-systemArea: Trait systemArea: Trait systemT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)The Rustc Trait System Refactor Initiative (-Znext-solver)
Description
The following code compiles (adapted from tests/ui/trivial-bounds/trivial-bounds-object.rs
):
// check-pass
trait A { fn test(&self); }
fn foo(x: &dyn A)
where
dyn A + 'static: A, // Using this bound would lead to a lifetime error.
{
x.test();
}
while this doesn't:
trait A { fn test(&self); }
fn foo<'s>(x: &dyn A)
where
dyn A + 's: A, // Using this bound would lead to a lifetime error.
{
x.test();
//~^ ERROR explicit lifetime required in the type of `x`
}
There are two candidates to satisfy the bound dyn A + '?0: A
:
for the first case:
ObjectCandidate(_)
; no region constraintsParamCandidate(dyn A + 'static: A)
; requires'?0 = 'static
for the seecond one:
ObjectCandidate(_)
; no region constraintsParamCandidate(dyn A + 's: A)
; requires'?0 = 's
The different behavior arises because the param candidate in the first case is considered "global", unlike second case. Global param candidates are dropped in favor of any other candidate. This behavior was introduced by #51042 here:
fn candidate_should_be_dropped_in_favor_of( |
Metadata
Metadata
Assignees
Labels
A-trait-systemArea: Trait systemArea: Trait systemT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)The Rustc Trait System Refactor Initiative (-Znext-solver)