Skip to content

Typechecking of map literals does not always work in checked mode #221

Closed
@DartBot

Description

@DartBot

This issue was originally filed by [email protected]


If we provide type information when instantiating a new Map, we get runtime type-checking (VM, checked mode):
  var m1 = new Map<String, int>();
  m1["str"] = "hello"; // error: type OneByteString is not assignable to type int
  m1[1] = 2; // error: type Smi is not assignable to type String

I can't get the same behavior if I use Map literals, neither in the literal initializer nor in subsequent []= operations. No exception gets thrown for these two lines:
  var m2 = <String, String>{"a": 0}; // Map literal doesn't type-check values
  m2[2] = 1; // nor does []= (for keys or values)

I understand that Dart restricts Map literal keys to Strings and that those are already checked ("map entry key must be string literal").

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.type-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