Open
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: