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

No error in case of iterating over `void` in for-in loops #57078

Open sgrekhov opened 1 week ago

sgrekhov commented 1 week ago

In the language specification we have:

It is a compile-time error for a for-in statement to have an iterator expression of type T such that Iterator< void > is the most specific instantiation of Iterator that is a superinterface of T, unless the iteration variable has type void. It is a compile-time error for an asynchronous for-in statement to have a stream expression of type T such that Stream< void > is the most specific instantiation of Stream that is a superinterface of T, unless the iteration variable has type void.

But the code below doen't have expected errors nor in the analyzer nor in the CFE.

void main() async {
  List<void> list = <void>[1, 2, 3];
  for (Object? i in list) {
//                  ^^^^
// [analyzer] unspecified
// [cfe] unspecified
  }
  for (dynamic i in list) {
//                  ^^^^
// [analyzer] unspecified
// [cfe] unspecified
  }

  Stream<void> stream = Stream<void>.fromIterable([1, 2, 3]);
  await for (Object? i in stream) {
//                        ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
  }
  await for (dynamic i in stream) {
//                        ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
  }
}
dart-github-bot commented 1 week ago

Summary: The issue reports that the Dart analyzer and CFE do not flag compile-time errors when iterating over a List<void> or Stream<void> using a for-in loop, despite the language specification stating that such iterations should be flagged as errors.

johnniwinther commented 1 week ago

@chloestefantsova Can you take a look at this for both analyzer and CFE? Maybe we need a breaking change request since none of the tools currently report the error.