Skip to content

VM: Very confusing error on invalid const assignment #33303

Description

@matanlurey

The following code:

import 'package:test/test.dart';

class Provider {
  const Provider();
}

class Module {
  final List<Provider> providers;
  const Module(this.providers);
}

const aGoodModule = const Module(const [
  const Provider(),
]);

const listOfThings = const [
  const [],
  const Provider(),
];

const aBadModule = const Module(listOfThings);

void main() {
  test('$aBadModule should be invalid', () {
    expect(aBadModule.providers, const isInstanceOf<List<Provider>>());
  });
}

... throws the following very scary error message:

Unhandled exception:
'file:///Users/matanl/github/dart.scratch/dart2_const_downcast/test/dart2_const_downcast_test.dart': error: Not a constant expression: _ImmutableList len:2 is not an instance of Type: class 'List', args:[TypeArguments: (@0x11e2dc031 H39565ba6) [Type: class 'Provider']]'file:///Users/matanl/github/dart.scratch/dart2_const_downcast/test/dart2_const_downcast_test.dart': error: line 26 pos 10: Not a constant expression.
  test('$aBadModule should be invalid', () {

... this is an error (listOfThings is not a List<Provider>), but it's very hard to figure out here:

  • _ImmutableList isn't something I wrote
  • This seems to not have the better error messages I've seen elsewhere
  • This seems to leak internals of the VM (TypeArguments: (@0x11e2dc031 H39565ba6)).

I'll file a separate bug on the analyzer/DDC - I wouldn't have gotten that far if the IDE warned me.

Metadata

Metadata

Assignees

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions