rust-lang / rust-lang/rust

instantiating impl generic args with `'empty` is unsound

Open
#161,864 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-higher-ranked A-lifetimes A-trait-system I-unsound P-medium T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

originally found by @carlini as "bug 5" in https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/Reporting.20a.20collection.20of.20soundness.20bugs.20in.20next-solver/near/617737394

use std::fmt::Display;
trait ToObj<'a> {
    fn to_obj(self) -> Box<dyn Display + 'a>;
}
impl<'a, T: Display + 'a> ToObj<'a> for T {
    fn to_obj(self) -> Box<dyn Display + 'a> {
        Box::new(self)
    }
}

trait Super {
    fn yeet(&self);
}
impl<'a> Super for ()
where
    for<'b> &'b str: ToObj<'a>,
{
    fn yeet(&self) {
        let dangling = String::from("hello").as_str().to_obj();
        println!("{dangling}");
    }
}

trait Trait: Super {}
impl Trait for () {}

fn main() {
    let _ = (&() as &dyn Trait).yeet();
}

This incorrectly compiles with both the old and the new solver.

The problem is that lexical region resolution supports 'empty and considers for<'b> 'b: 'a to hold if 'a is 'empty. leak_check and MIR borrowck do not have the notion of 'empty anymore.

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 by reproducing the supplied Rust example and tracing lexical region resolution for the 'empty region. Compare that behavior with leak_check and MIR borrowck, which the issue says lack the same notion. Done means the unsound example is rejected by both the old and new solver paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.