Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NNBD: Dart throws error when method with nullable required parameter overrides legacy one #41527

Open
iarkh opened this issue Apr 16, 2020 · 1 comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.

Comments

@iarkh
Copy link
Contributor

iarkh commented Apr 16, 2020

Dart VM version: 2.8.0-dev.20.0 (dev) (Fri Apr 3 10:19:55 2020 +0200) on "windows_x64"

In the following source code example method with nullable required parameter override legacy one:

test_out_lib.dart:

// @dart=2.6
class LEGACY_REQUIRED_ARGS {
  void test_default   ({int i = 1}) {}
  void test_nondefault({int i    }) {}
}

test.dart:

import "test_out_lib.dart";

class A extends LEGACY_REQUIRED_ARGS {
  void test_default   ({required int? i}) { print(i); }
  void test_nondefault({required int? i}) { print(i); }
}

main() {
  A().test_default(i: 1);
  A().test_nondefault(i: 1);
}

It passes with analyzer and throws a compile error with dart:

$> dart --enable-experiment=non-nullable test.dart
test.dart:4:39: Error: The required named parameter 'i' in method 'A.test_default' is not required in overridden method 'LEGACY_REQUIRED_ARGS.test_default'.
void test_default ({required int? i}) { print(i); }
^
test_out_lib.dart:3:8: Context: This is the overridden method ('test_default').
void test_default ({int i = 1}) {}
^
test.dart:5:39: Error: The required named parameter 'i' in method 'A.test_nondefault' is not required in overridden method 'LEGACY_REQUIRED_ARGS.test_nondefault'.
void test_nondefault({required int? i}) { print(i); }
^
test_out_lib.dart:4:8: Context: This is the overridden method ('test_nondefault').
void test_nondefault({int i }) {}
^
$> dartanalyzer --enable-experiment=non-nullable test.dart
Analyzing test.dart...
No issues found!

Seems like dart should not throw error here too.
At least, both tools should behave similarly.

@eernstg
Copy link
Member

eernstg commented Apr 16, 2020

It was concluded in dart-lang/language#812 that subtyping is adjusted such that a required named parameter can match a legacy named parameter. For example, void Function({required int i}) <: void* Function({int* i}). So in this case the CFE should accept the program.

A separate issue is the use of default values, cf. dart-lang/language#928.

@eernstg eernstg added area-front-end Use area-front-end for front end / CFE / kernel format related issues. and removed area-front-end Use area-front-end for front end / CFE / kernel format related issues. labels Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.
Projects
None yet
Development

No branches or pull requests

2 participants