-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Dart ignores non-nullability of variables and function parameters. #39678
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
Comments
Thanks for your feedback, the non nullability language feature is still being implemented in the Dart VM and is not fully functional yet. |
Quick status update on the CFE. As of c85979a the CFE emits compile-time errors (or warnings, depending on the mode) for lines Click to expand$ cat /tmp/asdf.dart
class A {
void test(int i) { print(i); }
}
main() {
int i;
i = null;
print(i);
A().test(1);
A().test(null);
}
$ sdk/bin/dart pkg/front_end/tool/fasta.dart compile --dump-ir -o /dev/null --enable-experiment=non-nullable --force-nnbd-checks --nnbd-strong /tmp/asdf.dart
Running: .../sdk/bin/dart --enable-asserts .../pkg/front_end/tool/_fasta/compile.dart --dump-ir -o /dev/null --enable-experiment=non-nullable --force-nnbd-checks --nnbd-strong /tmp/asdf.dart
/tmp/asdf.dart:7:7: Error: A value of type 'Null?' can't be assigned to a variable of type 'int'.
i = null;
^
/tmp/asdf.dart:10:12: Error: The argument type 'Null?' can't be assigned to the parameter type 'int'.
A().test(null);
^
library;
//
// Problems in library:
//
// /tmp/asdf.dart:7:7: Error: A value of type 'Null?' can't be assigned to a variable of type 'int'.
// i = null;
// ^
//
// /tmp/asdf.dart:10:12: Error: The argument type 'Null?' can't be assigned to the parameter type 'int'.
// A().test(null);
// ^
//
import self as self;
import "dart:core" as core;
class A extends core::Object {
synthetic constructor •() → self::A
: super core::Object::•()
;
method test(core::int i) → void {
core::print(i);
}
}
static method main() → dynamic {
core::int i;
i = let final<BottomType> #t1 = invalid-expression "/tmp/asdf.dart:7:7: Error: A value of type 'Null?' can't be assigned to a variable of type 'int'.\n i = null;\n ^" in null as{TypeError} core::int;
core::print(i);
new self::A::•().{self::A::test}(1);
new self::A::•().{self::A::test}(let final<BottomType> #t2 = invalid-expression "/tmp/asdf.dart:10:12: Error: The argument type 'Null?' can't be assigned to the parameter type 'int'.\n A().test(null);\n ^" in null as{TypeError} core::int);
} To compile in weak mode, remove flag Click to expand$ sdk/bin/dart pkg/front_end/tool/fasta.dart compile --dump-ir -o /dev/null --enable-experiment=non-nullable --force-nnbd-checks /tmp/asdf.dart
Running: .../sdk/bin/dart --enable-asserts .../pkg/front_end/tool/_fasta/compile.dart --dump-ir -o /dev/null --enable-experiment=non-nullable --force-nnbd-checks /tmp/asdf.dart
/tmp/asdf.dart:7:3: Warning: Assigning value of type 'Null?' to a variable of type 'int'.
i = null;
^
/tmp/asdf.dart:10:12: Warning: Assigning value of type 'Null?' to a variable of type 'int'.
A().test(null);
^
library;
//
// Problems in library:
//
// /tmp/asdf.dart:7:3: Warning: Assigning value of type 'Null?' to a variable of type 'int'.
// i = null;
// ^
//
// /tmp/asdf.dart:10:12: Warning: Assigning value of type 'Null?' to a variable of type 'int'.
// A().test(null);
// ^
//
import self as self;
import "dart:core" as core;
class A extends core::Object {
synthetic constructor •() → self::A
: super core::Object::•()
;
method test(core::int i) → void {
core::print(i);
}
}
static method main() → dynamic {
core::int i;
i = null;
core::print(i);
new self::A::•().{self::A::test}(1);
new self::A::•().{self::A::test}(null);
} |
Removing the front-end label as this should be considered a vm issue now. |
@stefantsov Is the flag |
NNBD specific warnings and errors still require the |
Is this still blocking unfork? Can we move this under #40106 ? |
Yes this is not blocking the unfork, I have moved into the Dart VM support for Null safe feature project. |
This requires the --force-nnbd-checks guard to be removed from the front end (please see issue #40980). |
Dart VM version: 2.7.0-dev.2.1 (Mon Dec 2 20:10:59 2019 +0100) on "windows_x64"
The following test example tries to assign
null
values to non-nullable variables (if nnbd is enabled):It throws a compile error with analyzer and prints
null
variable values with dart.As
i
is non-nullable, it seems like dart should throw compile errors for the lines 7 and 10 too.Sample output is:
The text was updated successfully, but these errors were encountered: