Skip to content

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

@aliemjay

Description

@aliemjay
Member

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

Activity

added
A-NLLArea: Non-lexical lifetimes (NLL)
C-bugCategory: This is a bug.
NLL-soundWorking towards the "invalid code does not compile" goal
T-typesRelevant to the types team, which will review and decide on the PR/issue.
on Nov 23, 2022
added a commit that references this issue on Nov 20, 2023
66ac3f1
added a commit that references this issue on Jan 17, 2024
6bf600b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @aliemjay@rustbot

    Issue actions

      user annotations on associated consts can be ill-formed · Issue #104763 · rust-lang/rust