dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

[cfe] Type `void` is allowed in `[]=` operator #57071

Open sgrekhov opened 1 week ago

sgrekhov commented 1 week ago

The code below produce no issues in the analyzer but errors in CFE.

main() {
  Map<void, int> m1 = <void, int>{};
  void key = 0;
  m1[key] = 1; // Error: This expression has type 'void' and can't be used.

  Map<int, void> m2 = <int, void>{};
  void value = 0;
  m2[2] = value; // Error: This expression has type 'void' and can't be used.
}

Acording to the specification key and value of type void in the []= operator are both allowed.

See also #57070.

eernstg commented 1 week ago

Right. It could be argued that even though operator []= is a method with two parameters, the syntax m1[key] = 1 is so different from the regular myFunction(arg1, arg2) that it should be given special treatment.

(For example, e1 == e2 is not treated exactly the same way as an invocation of a method with two arguments.)

However, I don't think there is any particular reason why we would want to make an exception for this case, so I'd recommend that we consider the spec rule "an actual argument of type void can be passed to a parameter of type void" to be applicable to this case as well, and then we shouldn't have the errors.