Skip to content

Dart produces compile error whereas analyzer does not when trying to add null to null. #36343

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 Mar 26, 2019 · 4 comments
Labels
legacy-area-analyzer Use area-devexp instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@iarkh
Copy link
Contributor

iarkh commented Mar 26, 2019

Dart version: 2.2.1-dev.1.1
OS: Windows 10

The following test example tries to calculate null + null expression:

main() {
	try { null + null;} catch (e) {}
	print("OK");
}

Analyzer passes with this whereas dart unexpectedly produces compile time error.

Sample output is:

$> dartanalyzer test.dart
Analyzing test.dart...
No issues found!
$>
$> dart test.dart
test.dart:2:13: Error: The method '+' isn't defined for the class 'Null'.
Try correcting the name to the name of an existing method, or defining a method named '+'.
try { null + null;} catch (e) {}
$>

Please note if I change the source for the following:

main() {
	try { 1 + null;} catch (e) {}
	print("OK");
}

it works as expected: dart throws runtime exception, and so finally it prints "OK" as expected.

@iarkh
Copy link
Contributor Author

iarkh commented Mar 26, 2019

One more failing example:

main() {
	try {null >> 1;} catch (e) {}
	print("OK");
}

@lrhn
Copy link
Member

lrhn commented Mar 26, 2019

The CFE is correct and the analyzer is wrong. The error is not entirely unexpected.

We changed the static type of null to be Null as part of the Dart 2 changes, rather than bottom as part of the preparation for Dart 2 (CL). This was to prevent the bottom type from being propagated around by type inference (a List<bottom> is a curious beast). We also changed Null to be a proper sub-type of every other type. With non-nullable types, that too will cease to be the case.

A consequence of that is that null + anything is a static type error because null does not have any + method.

The 1 + null succeeds because Null is a subtype of num, so it is a valid argument to int.operator+—which then fails at runtime because int.operator+ doesn't accept null arguments.

@lrhn lrhn added legacy-area-analyzer Use area-devexp instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Mar 26, 2019
@eernstg
Copy link
Member

eernstg commented Mar 26, 2019

Note #27273 and #36113 about missing errors from the analyzer about null and Null.

@srawlins
Copy link
Member

Not sure when this was fixed, but Dart analyzer 2.5.0 now produces the following warning:

The operator '+' isn't defined for the class 'Null'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants