Rust: Improve diagnostics regarding how static_in_const doesn't work for associated constants

Created on 4 Jan 2017  路  10Comments  路  Source: rust-lang/rust

Example code:

#![feature(associated_consts, static_in_const)]
struct Test;
impl Test {
    const CONST: &str = "Hello, world!";
}

Would default CONST to &'static str if this actually worked for associated constants. The original RFC decided to not do this, so, it makes sense to document the behaviour.

EDIT: the error message should be better, not the docs.

A-diagnostics C-enhancement T-compiler

Most helpful comment

So it seems like 'static is a pretty good default if you don't say otherwise.

Yes, please!

All 10 comments

This is intentional, but it seems like the RFC text wasn't updated to accommodate this detail.

@petrochenkov I feel that if that's the case, then the error message should be improved and the RFC text should be updated.

Doc team triage: tagging as p-low because this is for an unstable feature

Would extending static_in_const to work with associated constants be a controversial move? Is there any reason it shouldn't be done other than not explicitly being spelled out in the RFC?

I personally would be careful to only default to 'static if the impl doesn't have any lifetime parameters. But otherwise it'd make sense imho.

Should this still be P-low considering how this feature got stabilised? @steveklabnik

@clarcharr you're right! We should change the prioritization. I'm going to nominate this to be discussed at the docs meeting today.

Doc team triage: the RFC issue in the reference repo https://github.com/rust-lang-nursery/reference/issues/9 should be tracking adding this to the reference, so that takes care of the doc requirement.

As such, leaving this to diagnostics now. Removing the p-tag because that's for @rust-lang/compiler to decide.

@frewsxcv

Would extending static_in_const to work with associated constants be a controversial move? Is there any reason it shouldn't be done other than not explicitly being spelled out in the RFC?

We decided against it because, in that case, there might be other lifetimes you would want. For example:

trait Foo<'a> {
    const T: &'a str;
}

that said, revisiting this explanation, it feel a bit weak, since the value of an associated constant would still have to be all consisting of static data. So it seems like 'static is a pretty good default if you don't say otherwise.

So it seems like 'static is a pretty good default if you don't say otherwise.

Yes, please!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nikomatsakis picture nikomatsakis  路  259Comments

Leo1003 picture Leo1003  路  898Comments

withoutboats picture withoutboats  路  202Comments

Centril picture Centril  路  382Comments

alexcrichton picture alexcrichton  路  240Comments