Skip to content

instantiating impl generic args with 'empty is unsound #161864

Description

@lcnr

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions