rust-lang / rust-lang/rust

Item bounds of aliases are unnormalized when collecting free regions for liveness analysis

Open
#160,488 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code

trait Id {
    type SelfType;
}

impl<T> Id for T {
    type SelfType = T;
}

trait Foo {
    type Assoc<'a>: Id<SelfType: 'static>
    // type Assoc<'a>: 'static // This compiles
    where
        Self: 'a;

    fn assoc(&mut self) -> Self::Assoc<'_>;
}

fn overlapping_mut<T>(mut t: T)
where
    T: Foo,
{
    let _a = t.assoc();
    let _b = t.assoc();
}

I expected to see this happen: compiles.

Instead, this happened:

error[E0499]: cannot borrow `t` as mutable more than once at a time
  --> counter_examples/item-bound-liveness.rs:23:13
   |
22 |     let a = t.assoc();
   |             - first mutable borrow occurs here
23 |     let b = t.assoc();
   |             ^ second mutable borrow occurs here
24 | }
   | - first borrow might be used here, when `a` is dropped and runs the destructor for type `<T as Foo>::Assoc<'_>`
Cause

We should but it's difficult to normalize here

related issues: https://github.com/rust-lang/rust/issues/158461 https://github.com/rust-lang/rust/issues/158463

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

Reproduce the failure with counter_examples/item-bound-liveness.rs, then inspect compiler/rustc_trait_selection/src/traits/outlives_for_liveness.rs around line 40 and the linked related issues. Trace how item bounds are collected for liveness analysis; done means the alias bounds are normalized sufficiently for the example to compile without the erroneous overlapping mutable-borrow error.

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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.