Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a default lifetime of 'static in associated consts #125190

Open
tmandry opened this issue May 16, 2024 · 5 comments
Open

Use a default lifetime of 'static in associated consts #125190

tmandry opened this issue May 16, 2024 · 5 comments
Assignees
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lifetimes Area: lifetime related C-feature-request Category: A feature request, i.e: not implemented / a PR. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@tmandry
Copy link
Member

tmandry commented May 16, 2024

Expand the interpretation of rust-lang/rfcs#1623 "static_lifetime_in_statics" to include associated consts in traits and trait impls when there are no other lifetimes in scope. This means the following code will compile:

struct MyType;

impl MyType {
    // Warns today (elided_lifetimes_in_associated_constant) but won't after this FCP
    const FOO: &i32 = &4;
}

trait Trait {
    // Errors today (missing lifetime specifier) but won't after this FCP
    const BAR: &i32;
    // You will also be able to provide a default:
    // const BAR: &i32 = &1;
}

impl Trait for MyType {
    // Warns today (elided_lifetimes_in_associated_constant) but won't after this FCP
    const BAR: &i32 = Self::FOO;
}

However, if there are any lifetime parameters on the item declaration or const, we will continue to require an explicit choice of lifetime. We may revisit this decision in the future. Examples are listed in the appendix below.

Rationale: There is a high level of churn suggested by the backreferences to #115010. The primary intention of this FCP is to reduce the churn associated with this change. The lang team discussed #124211 and thought it would be good to decide on what we want in the most obvious (and common) cases, which should also reduce the level of churn significantly.

Appendix: Examples with lifetimes

struct MyType<'a>(PhantomData<&'a ()>);

impl<'a> MyType<'a> {
    // Continues to warn (elided_lifetimes_in_associated_constant), may be a hard error later
    const FOO: &i32 = &4;
}

trait Trait<'a> {
    // Continues to error:
    //const BAR: &i32;
    // (but this is fine)
    const BAR: &'a i32;
}

impl Trait<'static> for i32 {
    // Okay, because there are no lifetime parameters on this item.
    // If the trait lifetime were not 'static, you would still get a
    // "const not compatible with trait" error.
    const BAR: &i32 = &42;
}

impl<'a> Trait<'static> for MyType<'a> {
    // Continues to warn (elided_lifetimes_in_associated_constant), may be a hard error later
    const BAR: &i32 = Self::FOO;
}

struct Thing;
impl Thing {
    // Nightly-only feature, but would also error in this case because of
    // the lifetime parameter on the const impl item:
    const FOO<'a>: &i32 = &4;
}

fn foo<'a>(_: &'a ()) {
    impl Thing {
        // Okay; the lifetime of the function cannot be named here,
        // so it does not affect whether there is a default.
        const BAZ: &i32 = &5;
    }
}
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 16, 2024
@tmandry tmandry added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 16, 2024
@tmandry
Copy link
Member Author

tmandry commented May 16, 2024

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented May 16, 2024

Team member @tmandry has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 16, 2024
@compiler-errors compiler-errors self-assigned this May 16, 2024
@compiler-errors
Copy link
Member

compiler-errors commented May 16, 2024

I can put up a PR to implement this adjustment, pending FCP approval.

@fmease fmease added A-lifetimes Area: lifetime related C-feature-request Category: A feature request, i.e: not implemented / a PR. A-associated-items Area: Associated items such as associated types and consts. labels May 16, 2024
@tmandry tmandry added the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label May 17, 2024
@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels May 23, 2024
@rfcbot
Copy link

rfcbot commented May 23, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@traviscross traviscross removed the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-lifetimes Area: lifetime related C-feature-request Category: A feature request, i.e: not implemented / a PR. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants