Skip to content

Passing a non-const value to a const constructor gives a confusing error message #46792

@Hixie

Description

@Hixie

Consider this code:

class Foo {
  const Foo(this.x);
  final int x;
}

void test(int x) {
  const Foo(x);
}


void main() { }

I would expect one error message, namely, "Arguments of a constant creation must be constant expressions" or some variant thereof. However, today we get two errors, and the first one is really confusing:

  error • A value of type 'Null' can't be assigned to a parameter of type 'int' in a const constructor • test.dart:7:13 • const_constructor_param_type_mismatch
  error • Arguments of a constant creation must be constant expressions • test.dart:7:13 • const_with_non_constant_argument

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-uxtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions