Skip to content

~const Drop bounds are broken / wrong / unsound #94803

Closed
@steffahn

Description

@steffahn
#![feature(const_trait_impl)]

fn extend_lifetime(x: &str) -> &'static str {
    f::<S>(x)
}

const fn f<'a, T: Tr<'a> + ~const Drop>(x: &'a str) -> T::Ty {
    // can call g with T: Drop bound, even though
    // T: ~const Drop works for non-Drop-implementing types
    g::<T>(x)
}

#[allow(drop_bounds)]
const fn g<T: Drop>(x: &str) -> <T as Tr<'_>>::Ty {
    x
}

struct S;
trait Tr<'a> {
    type Ty;
}

#[allow(drop_bounds)]
impl<'a, T: Drop> Tr<'a> for T {
    type Ty = &'a str;
}

impl<'a> Tr<'a> for S {
    type Ty = &'static str;
}

fn main() {
    let x = "Hello World".to_owned();
    let s = extend_lifetime(&x);
    drop(x);
    println!("{s}");
}
��)

(playground)

@rustbot label requires-nightly, I-unsound, T-compiler, F-const_trait_impl, A-const-fn

By the way, even if this worked “correctly” (i.e. soundly) I really hate the fact that this feature is using a sort of T: Drop bound; the drop_bounds warning exists for a reason, using the Drop trait in bounds is discouraged for a reason, abusing it as in the feature that allows “T: ~const Drop” makes everything just sooo much more confusing and inconsistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.F-const_trait_impl`#![feature(const_trait_impl)]`I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions