Skip to content

user annotations on associated consts can be ill-formed #104763

Closed
@aliemjay

Description

@aliemjay

The following should fail: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=55b6d108fe6712536a9c7a7abea56bc0

trait Trait {
    const TRAIT: bool;
}

impl<T> Trait for &'static T {
    const TRAIT: bool = true;
}

fn test<T>() {
    <&'static T>::TRAIT;
}

It should require a T: 'static bound on test.

This is unsound but can't currently be exploited because of #98852. A potential exploit would be like:

trait Trait<'a> {
    const TRAIT: fn(&'a str) -> &'static str;
}

impl<'a> Trait<'a> for &'static &'a () { // implies 'a: 'static
    const TRAIT: fn(&'a str) -> &'static str = {
        |x| x // this should pass if we use implied bounds from impl header
    };
}

fn test<T>() {
    let val = <&'static &'_ ()>::TRAIT(&String::new()); // we should error here!
    println!("{}", val);
}

@rustbot label T-types C-bug A-NLL NLL-sound A-borrow-checker
@rustbot claim

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.NLL-soundWorking towards the "invalid code does not compile" goalT-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions