Skip to content

Dart does not recognise nullable value in extension at runtime #44558

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
javiermrz opened this issue Dec 27, 2020 · 8 comments
Closed

Dart does not recognise nullable value in extension at runtime #44558

javiermrz opened this issue Dec 27, 2020 · 8 comments
Labels
legacy-area-analyzer Use area-devexp instead.

Comments

@javiermrz
Copy link

javiermrz commented Dec 27, 2020

The problem is pretty straightforward. I have the following extension:

extension NullableListExtension on List<dynamic>? {
  String getOrEmptyString(int index) =>
      this == null || index >= this!.length ? "" : this![index]!.toString();
}

When editing, compiler shows no error. However, the following warning appears at runtime:

Warning: Operand of null-aware operation '!' has type 'List<dynamic>' which excludes null.
 - 'List' is from 'dart:core'.
      this == null || index >= this!.length ? "" : this![index];

(With ^ symbol pointing to the exclamation in this![index]).
But if I actually were to remove the ! from the this![index], it would ask me to introduce the symbol again (which I think is the correct thing to do).

@lrhn lrhn transferred this issue from dart-lang/language Dec 27, 2020
@lrhn
Copy link
Member

lrhn commented Dec 27, 2020

I never remember whether we can promote this or not, but in either case we should be consistent.

(Also, the code won't work because the empty string is not assignable to T).

@lrhn lrhn added the legacy-area-analyzer Use area-devexp instead. label Dec 27, 2020
@javiermrz
Copy link
Author

javiermrz commented Dec 30, 2020

@irhn yep, that was an error of my own, thanks for pointing it out, instead of T there should be a String there, I will update that. However, the error still persists.

I don't know if this can be promoted either, but when adding the ! the compiler seems to be happy (not at runtime though). It works like a charm though, this is only a warning, but quite annoying.

@javiermrz
Copy link
Author

javiermrz commented Dec 30, 2020

You are right, the error is gone, thanks. Shouldn't the compiler show an error to warn the developer about this?

@leafpetersen
Copy link
Member

I believe that there is a known issue where the analyzer incorrectly promotes this only in extension methods (it is specified never to promote). I can't find the issue we discussed this in at the moment. We had considered fixing this forward by making this promotable everywhere, but this looked to be too long of an arc of work in the CFE, so for the time being this is a known errata. cc @scheglov @johnniwinther

Related:

#39959
#32120

@javiermrz
Copy link
Author

Great then. Thanks all for your fast answers, I'll close this issue as everything was solved. Happy Holidays :)

@scheglov
Copy link
Contributor

scheglov commented Jan 1, 2021

@leafpetersen actually, it worked in the CFE, and required some additional work in the analyzer. IIRC, I prototyped a solution for the analyzer, but at the end we decided that we don't want such promotion. It seems that in the CFE promotion still works though.
image

@leafpetersen
Copy link
Member

@scheglov I'm probably misremembering details. I'm pretty sure that we do want the promotion, but decided that the implementation cost was too high somewhere to do it now (perhaps the CFE only promotes this in extensions, and making it do so in classes was going to be hard?).

@scheglov
Copy link
Contributor

scheglov commented Jan 5, 2021

Ah, yes, it is quite possible that this promotion in CFE works only in extensions because it has an artificial variable for this in extensions.

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.
Projects
None yet
Development

No branches or pull requests

4 participants