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.25k stars 1.58k forks source link

Dart does not throw error if function argument parameter extends void #33699

Open iarkh opened 6 years ago

iarkh commented 6 years ago

Dart SDK Version: 2.0.0-dev.55.0 OS: Windows 10

Sample code below declares a [typedef] with parameter which extends [void]. It causes a lot of compiler errors with dartanalyzer and passes without errors with dart:

typedef F<X extends void> = Function(X);
main() {}

I believe both tools should behave in the same way. Dartanalyzer sample output is:

Analyzing test.dart...
  error - Typedefs can't reference themselves directly or recursively via another typedef at test.dart:1:1 - type_alias_cannot_reference_itself
  error - Expected a type name at test.dart:1:21 - expected_type_name
  error - Expected to find '>' at test.dart:1:21 - expected_token
  error - Unexpected text '>' at test.dart:1:25 - unexpected_token
  error - Expected a method, getter, setter or operator declaration at test.dart:1:25 - expected_executable
  error - Invalid generic function type at test.dart:1:25 - invalid_generic_function_type
  error - Expected a method, getter, setter or operator declaration at test.dart:1:40 - expected_executable
  error - Unexpected text ';' at test.dart:1:40 - unexpected_token
8 errors found.

Dart stdout and stderr are empty, no exceptions or warnings there.

eernstg commented 6 years ago

The situation here is similar to the one in #33704, so the remarks I made there apply here as well.

Adjusting the area because it is a compile-time error, and it is the VM that does not emit an error.

iarkh commented 6 years ago

With the recent version (2.1.0-dev.8.0) dart throws an error as expected:

test.dart:1:21: Error: Type 'void' can't be used here because it isn't a return type. Try removing 'void' keyword or replace it with 'var', 'final', or a type. typedef F = Function(X); ^^^^ test.dart:1:21: Error: Type 'void' not found. typedef F = Function(X); ^^^^

The only remaining problem here is that probably the second error message is obsolete: if one error reads that void type cannot be used here, it looks strange that the second one reads that void type cannot be found.