Skip to content

Analyzer doesn't recognize consts from different files when created with implicit const #32823

@leafpetersen

Description

@leafpetersen

The command line analyzer incorrectly reports an error when the argument of a switch case is a constant from another file created with an implicit const. See the example below. Tested with

dartanalyzer version 2.0.0-dev.43.0

and also with bleeding edge with both of these patched in:
https://dart-review.googlesource.com/c/sdk/+/50120.
https://dart-review.googlesource.com/c/sdk/+/49834.

Error messages are:

  error • Const variables must be initialized with a constant value at /Users/leafp/tmp/ddctest.dart:9:13 • const_initialized_with_non_constant_value
  error • Const variables must be initialized with a constant value at /Users/leafp/tmp/ddctest.dart:10:13 • const_initialized_with_non_constant_value
  error • Case expressions must be constant at /Users/leafp/tmp/ddctest.dart:13:12 • non_constant_case_expression

Note that implicit const created values are recognized as const from the current file, but not from a separate file.

lib.dart

class C {
  const C();
  static const C WARNING = C();
  static const C ERROR = const C();
}

test.dart

import "lib.dart" ;

class D {
  const D();
  static const D WARNING = D();
}

void test(C x, D y) {
  const a = C.WARNING;
  const b = C.WARNING;
  const d = D.WARNING;
  switch (x) {
      case C.WARNING:
        break;
      case C.ERROR:
        break;
      default:
        break;
  }
  switch (y) {
      case D.WARNING:
        break;
      default:
        break;
  }
}
void main() {
  test(C.WARNING, D.WARNING);
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions