Skip to content

Analyzer: null.foo reports no warning #27273

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
xvrh opened this issue Sep 7, 2016 · 11 comments
Closed

Analyzer: null.foo reports no warning #27273

xvrh opened this issue Sep 7, 2016 · 11 comments
Labels
language-strong-mode-polish legacy-area-analyzer Use area-devexp instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Milestone

Comments

@xvrh
Copy link
Contributor

xvrh commented Sep 7, 2016

The dart analyzer reports no warnings with this code:

main() {
  null.foo;
}

I would expect: "The getter 'foo' is not defined for the class 'Null'."

@zoechi
Copy link
Contributor

zoechi commented Sep 8, 2016

I guess you want to enable strong mode https://www.dartlang.org/guides/language/analysis-options

@zoechi
Copy link
Contributor

zoechi commented Sep 8, 2016

See also #27276

@lrhn
Copy link
Member

lrhn commented Sep 8, 2016

I'm guessing this is what triggered #27276
The problem is that the specification special-cases null to the synthetic bottom type at the type level, so the type Null isn't even part of the analysis here. There is no definition of the capabilities of the bottom type because it doesn't really exist - it's a type, not a class, it doesn't have a superclass to inherit things from, and it's specially designed to be allowed to not implement the superclass methods.

We need to fix this somehow, likely by special casing again to say that calling members of bottom is only allowed if they are also members on Object.

@mit-mit mit-mit added the legacy-area-analyzer Use area-devexp instead. label Sep 14, 2016
@bwilkerson bwilkerson added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). and removed legacy-area-analyzer Use area-devexp instead. labels Sep 14, 2016
@leafpetersen
Copy link
Member

cc @lrhn @eernstg @munificent @floitschG

Should Null allow all methods, or should we forbid this?

@leafpetersen leafpetersen self-assigned this May 12, 2017
@lrhn
Copy link
Member

lrhn commented May 12, 2017

The Null class is a class, it has the same methods as Object.
If this is about the null expression, we have changed that, and it's type is now Null, not bottom.

That doesn't completely avoid the problem because (throw 42).method() tries to call method on the bottom type, but it's unlikely to happen much in practice.

@leafpetersen
Copy link
Member

Currently both strong and non-strong mode allow this code with no errors:

import 'dart:async';

main() {
  Future<Null> f;
  f.then((x) => x.length);
}

Do we view this as an implementation bug? WAI? Or something we need to sort out?

@lrhn
Copy link
Member

lrhn commented May 13, 2017

Non-strong-mode is unsurprising, the static type of (x) => x.length is dynamic->dynamic, so it can't trigger any warnings.

Strong mode is wrong. Inference should make x have type Null and Null doesn't have a length getter. The behavior is not even matching original Dart 1 + inference, because in Dart 1, Null was not bottom. The null literal had static type bottom, but the Null type itself was just a class. If you write:

import 'dart:async';

main() {
  Future<Null> f;
  f.then((Null x) => x.length);
}

you should get a warning in non-strong mode (and probably an error in strong mode).

A quick check in dartpad shows that there is no warning, not even in non-strong mode, so yes, I think we do have an analyzer bug here.

@leafpetersen
Copy link
Member

The new front end implements the correct behavior for this. Re-assigning this as an analyzer bug.

@leafpetersen leafpetersen added legacy-area-analyzer Use area-devexp instead. and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jun 5, 2018
@leafpetersen leafpetersen removed their assignment Jun 5, 2018
@bwilkerson bwilkerson added this to the Dart2.1 milestone Sep 2, 2018
@bwilkerson bwilkerson added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) analyzer-untriaged labels Sep 2, 2018
@bwilkerson bwilkerson modified the milestones: Dart2.1, PostDart2.1 Sep 4, 2018
@aadilmaan aadilmaan modified the milestones: Future, D25 Release Jun 4, 2019
@leafpetersen
Copy link
Member

This seems to be fixed.

@leafpetersen
Copy link
Member

Correction, the original issue still reproduces on recent bleeding edge. Calling a method on something of type Null seems to have been fixed, but in at least some cases, calling a method on the null literal succeeds.

@leafpetersen leafpetersen reopened this Jul 4, 2019
@srawlins
Copy link
Member

This was fixed at some point recently; Dart analyzer 2.5.0 reports:

The getter 'foo' 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
language-strong-mode-polish 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

9 participants