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

special-casing 'static when winnowing trivial candidates #118965

Open
aliemjay opened this issue Dec 15, 2023 · 1 comment
Open

special-casing 'static when winnowing trivial candidates #118965

aliemjay opened this issue Dec 15, 2023 · 1 comment
Labels
A-traits Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Comments

@aliemjay
Copy link
Member

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(

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 15, 2023
@aliemjay aliemjay added A-traits Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 15, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 15, 2023
unify query canonicalization mode

Exclude from canonicalization only the static lifetimes that appear in the param env because of rust-lang#118965 . Any other occurrence can be canonicalized safely AFAICT.

r? `@lcnr`
@matthewjasper
Copy link
Contributor

To be clear, the behaviour predates #51042. Prior to that PR, global bounds were simply filtered out of the ParamEnv.

bors added a commit to rust-lang-ci/rust that referenced this issue Jan 9, 2024
unify query canonicalization mode

Exclude from canonicalization only the static lifetimes that appear in the param env because of rust-lang#118965 . Any other occurrence can be canonicalized safely AFAICT.

r? `@lcnr`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

No branches or pull requests

3 participants