rust-lang / rust-lang/rust

special-casing `'static` when winnowing trivial candidates

Open
#118,965 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system T-types WG-trait-system-refactor
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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:

  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:
https://github.com/rust-lang/rust/blob/604f185fae9a4b0edf7e28f616a0f53880f8f074/compiler/rustc_trait_selection/src/traits/select/mod.rs#L1816

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with tests/ui/trivial-bounds/trivial-bounds-object.rs and reproduce the two examples to compare candidate selection. Then read compiler/rustc_trait_selection/src/traits/select/mod.rs around the referenced line and trace how global parameter candidates are winnowed. Done means the lifetime behavior is corrected for the reported cases with appropriate UI test coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.