Skip to content

On-demand const verification may be a backwards compatibility hazard #19265

Closed
@huonw

Description

@huonw

E.g. #19244 describes how the following is valid, but uncommenting the line causes the error given below.

struct Wrapper(usize);
const BAD_CONST: usize = Wrapper(42).0;

// type X = [u8; BAD_CONST];

fn main() {}
3 | const BAD_CONST: usize = Wrapper(42).0;
  |                          ^^^^^^^^^^^ unimplemented constant expression: tuple struct constructors

(NB. that specific instance may be fixed in future, but that does not necessarily mean this bug is fixed.)

That is, the const-suitability of the value is checked when used as a constant expression, not at the definition. This could possibly may lead to surprises if rules are tweaked (at least, it could lead to surprises that are not detected). In any case, it is certainly confusing for a const to apparently be an unsupported constant expression.

It seems that we could check the definitions, not just checking them lazily when they are used as const expressions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-future-incompatibilityCategory: Future-incompatibility lintsP-lowLow priorityT-langRelevant to the language 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