Skip to content

Potentially constant expressions not properly handled in string interpolations #19558

Closed
@stereotype441

Description

@stereotype441

The following code:

  class Foo {
    final String s;
    const Foo(String x) : s = "($x)";
  }
  main() {
    const Foo f = const Foo('bar');
    print(f.s);
  }

is rejected by the VM with the error message "initializer expression must be compile time constant."

However, based on my understanding of the language spec, this should be allowed. The spec says:

(in section "Constant Constructors"): "Any expression that appears within the initializer list of a constant constructor must be a potentially constant expression, or a compile-time error occurs. A potentially constant expression is an expression e that would be a valid constant expression if all formal parameters of e’s immediately enclosing constant constructor were treated as compile-time constants that were guaranteed to evaluate to an integer, boolean or string value as required by their immediately enclosing superexpression."

(in section "Constants"): "A constant expression is one of the following: ... A literal string where any interpolated expression is a compile-time constant that evaluates to a numeric, string or boolean value or to null."

Analyzer and dart2js treat the above code as valid.

Activity

iposva-google

iposva-google commented on Jun 19, 2014

@iposva-google
Contributor

Set owner to @mhausner.
Added Accepted label.

DartBot

DartBot commented on Jun 19, 2014

@DartBot

This comment was originally written by @mhausner


Yes. Another incarnation of issue #392. When we compile the const Foo constructor, we don't know whether the call site will pass an actual parameter that makes "($x)" a compile-time const.


Added Duplicate label.
Marked as being merged into #392.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kevmoo@stereotype441@iposva-google@DartBot

        Issue actions

          Potentially constant expressions not properly handled in string interpolations · Issue #19558 · dart-lang/sdk