Skip to content

special-casing 'static when winnowing trivial candidates #118965

Open
@aliemjay

Description

@aliemjay

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:

  1. ObjectCandidate(_); no region constraints
  2. ParamCandidate(dyn A + 'static: A); requires '?0 = 'static

for the seecond one:

  1. ObjectCandidate(_); no region constraints
  2. ParamCandidate(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

No one assigned

    Labels

    A-trait-systemArea: Trait systemT-typesRelevant to the types team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions