Skip to content

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

Closed
@iarkh

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

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