Skip to content

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

Closed
iarkh opened this issue Dec 6, 2019 · 8 comments
Closed

Dart ignores non-nullability of variables and function parameters. #39678

iarkh opened this issue Dec 6, 2019 · 8 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. closed-duplicate Closed in favor of an existing report NNBD Issues related to NNBD Release
Milestone

Comments

@iarkh
Copy link
Contributor

iarkh commented Dec 6, 2019

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):

class A {
  void test(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.

$> dart --enable-experiment=non-nullable test.dart
null
1
null

@a-siva
Copy link
Contributor

a-siva commented 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.

@a-siva a-siva added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. NNBD Issues related to NNBD Release legacy-area-front-end Legacy: Use area-dart-model instead. labels Dec 7, 2019
@johnniwinther johnniwinther added this to the D28 Release milestone Dec 11, 2019
@chloestefantsova
Copy link
Contributor

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; 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);
}

@johnniwinther johnniwinther removed the legacy-area-front-end Legacy: Use area-dart-model instead. label Jan 13, 2020
@johnniwinther
Copy link
Member

Removing the front-end label as this should be considered a vm issue now.

@a-siva a-siva added the vm-nnbd-unfork-sdk Label for all issues that need to be done before the nnbd sdk can be unforked label Jan 16, 2020
@franklinyow franklinyow modified the milestones: D29 Release, D28 Release Jan 21, 2020
@a-siva
Copy link
Contributor

a-siva commented Feb 6, 2020

@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.

@johnniwinther
Copy link
Member

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.

@alexmarkov alexmarkov removed the vm-nnbd-unfork-sdk Label for all issues that need to be done before the nnbd sdk can be unforked label Feb 25, 2020
@franklinyow
Copy link
Contributor

Is this still blocking unfork? Can we move this under #40106 ?

@a-siva
Copy link
Contributor

a-siva commented 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 a-siva added the closed-duplicate Closed in favor of an existing report label Mar 11, 2020
@a-siva
Copy link
Contributor

a-siva commented 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.

@a-siva a-siva closed this as completed Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. closed-duplicate Closed in favor of an existing report NNBD Issues related to NNBD Release
Projects
None yet
Development

No branches or pull requests

6 participants