Skip to content
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

Poor diagnostic message when using ?.[] #42762

Open
bwilkerson opened this issue Jul 19, 2020 · 3 comments
Open

Poor diagnostic message when using ?.[] #42762

bwilkerson opened this issue Jul 19, 2020 · 3 comments
Labels
analyzer-recovery area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@bwilkerson
Copy link
Member

Create a file containing the following (invalid) code:

void f(String? s) {
  s?.[0];
}

The use of ?. before an index operator appears to no longer be supported, but the diagnostic message is very confusing. There is only one message generated (on the reference to s inside the body):

error: An expression whose value can be 'null' must be null-checked before it can be dereferenced. (unchecked_use_of_nullable_value at [nnbd_test_package] lib/test.dart:2)

The message should explain to users that they can't use ?. before [] to null-check s.

@bwilkerson bwilkerson added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-recovery labels Jul 19, 2020
@srawlins srawlins added the P3 A lower priority bug or feature request label Nov 24, 2020
@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 1, 2021
@srawlins srawlins added the contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) label May 20, 2022
@FMorschel
Copy link
Contributor

Today the message is a new one undefined_getter:

The getter '[' isn't defined for the type 'String'.
Try importing the library that defines '[', correcting the name to the name of an existing getter, or defining a getter or field named '['.

Although in this case probably s?[0]; or s![0]; should fix, if we had a getter/method we would see:

image

Maybe those fixes can be reviewed to show on this case and the ?. one could be transformed for this case as it is if you don't add the .:

image


I also noticed that this same fix doesn't show for function calls:

image

Maybe it could be reviewed for that as well (to add ?.call).

@lrhn
Copy link
Member

lrhn commented Sep 19, 2024

Yeah, the new message isn't very good, since you can't define a getter named [.

Still poor diagnostic message.

The CFE says:

main.dart:2:15: Error: Expected an identifier, but got '['.
Try inserting an identifier before '['.
  print(args?.[1]);
              ^

(From DartPad.)
That's ... not bad. You might have meant to say args?.elements[1].
But it could also suggest args?[1], especially when args is a List?.

@FMorschel
Copy link
Contributor

especially when args is a List?.

I would think the analyzer would know when there is an implementation for the [] (or []=) operator, or am I wrong here? That might be better than specifically List?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-recovery area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants