You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dart ignores non-nullability of variables and function parameters. #39678
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):
classA {
voidtest(int i) { print(i); }
}
main() {
int i;
i =null;
print(i);
A().test(1);
A().test(null);
}
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:
$> dartanalyzer --enable-experiment=non-nullable test.dart
Analyzing test.dart...
error - A value of type 'Null' can't be assigned to a variable of type 'int'. - test.dart:7:7 - invalid_assignment
error - The argument type 'Null' can't be assigned to the parameter type 'int'. - test.dart:10:12 - argument_type_not_assignable
2 errors found.
As of c85979a the CFE emits compile-time errors (or warnings, depending on the mode) for lines i = null; and A().test(null);. Here's how to test it:
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 --nnbd-strong:
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);
}
@stefantsov Is the flag --force-nnbd-checks required to get these errors, because I checked and the VM does pass the --enable-experiment=non-nullable flag to the front end and it does not seem to report any errors.
NNBD specific warnings and errors still require the --force-nnbd-checks. We plan to remove this flag once VM and DDC can build the nnbd sdk cleanly with warnings/errors enabled.
Activity
a-siva commentedon Dec 7, 2019
Thanks for your feedback, the non nullability language feature is still being implemented in the Dart VM and is not fully functional yet.
chloestefantsova commentedon Dec 16, 2019
Quick status update on the CFE.
As of c85979a the CFE emits compile-time errors (or warnings, depending on the mode) for lines
i = null;andA().test(null);. Here's how to test it: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
--nnbd-strong:Click to expand
johnniwinther commentedon Jan 13, 2020
Removing the front-end label as this should be considered a vm issue now.
a-siva commentedon Feb 6, 2020
@stefantsov Is the flag
--force-nnbd-checksrequired to get these errors, because I checked and the VM does pass the--enable-experiment=non-nullable flagto the front end and it does not seem to report any errors.johnniwinther commentedon Feb 6, 2020
NNBD specific warnings and errors still require the
--force-nnbd-checks. We plan to remove this flag once VM and DDC can build the nnbd sdk cleanly with warnings/errors enabled.franklinyow commentedon Mar 2, 2020
Is this still blocking unfork? Can we move this under #40106 ?
a-siva commentedon Mar 2, 2020
Yes this is not blocking the unfork, I have moved into the Dart VM support for Null safe feature project.
a-siva commentedon Mar 11, 2020
This requires the --force-nnbd-checks guard to be removed from the front end (please see issue #40980).
Closing as a duplicate of that issue.