Skip to content

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

Closed

Description

@iarkh

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

Activity

  1. a-siva commented on Dec 7, 2019

    @a-siva
    Contributor

    Thanks for your feedback, the non nullability language feature is still being implemented in the Dart VM and is not fully functional yet.

  2. added
    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
    NNBDIssues related to NNBD Release
    on Dec 7, 2019
  3. added this to the D28 Release milestone on Dec 11, 2019
  4. chloestefantsova commented on Dec 16, 2019

    @chloestefantsova
    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);
    }
  5. johnniwinther commented on Jan 13, 2020

    @johnniwinther
    Member

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

  6. added
    vm-nnbd-unfork-sdkLabel for all issues that need to be done before the nnbd sdk can be unforked
    on Jan 16, 2020
  7. a-siva commented on Feb 6, 2020

    @a-siva
    Contributor

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

  8. johnniwinther commented on Feb 6, 2020

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

  9. removed
    vm-nnbd-unfork-sdkLabel for all issues that need to be done before the nnbd sdk can be unforked
    on Feb 25, 2020
  10. franklinyow commented on Mar 2, 2020

    @franklinyow
    Contributor

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

  11. a-siva commented on Mar 2, 2020

    @a-siva
    Contributor

    Yes this is not blocking the unfork, I have moved into the Dart VM support for Null safe feature project.

  12. a-siva commented on Mar 11, 2020

    @a-siva
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NNBDIssues related to NNBD Releasearea-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.closed-duplicateClosed in favor of an existing report

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions