Skip to content

Misleading error message when trying to invoke function typed field via null receiver #45552

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

Open
stereotype441 opened this issue Apr 1, 2021 · 0 comments
Labels
legacy-area-front-end Legacy: Use area-dart-model instead.

Comments

@stereotype441
Copy link
Member

The following code produces an error in the CFE (as it should):

class C {
  void Function() foo;
  C(this.foo);
}
f(C? c) {
  c.foo(); // (1)
}

But the error message itself is misleading; it seems to be saying that I'm trying to use c as a function, which I'm definitely not doing:

../../tmp/proj/test.dart:6:5: Error: Can't use an expression of type 'C?' as a function because it's potentially null.
 - 'C' is from '../../tmp/proj/test.dart'.
Try calling using ?.call instead.
  c.foo();
    ^

Note that if you remove the () on line (1) you get a far more reasonable error message; I would have expected this more reasonable error message regardless of whether the () is present:

../../tmp/proj/test.dart:6:5: Error: Property 'foo' cannot be accessed on 'C?' because it is potentially null.
 - 'C' is from '../../tmp/proj/test.dart'.
Try accessing using ?. instead.
  c.foo;
    ^^^
@stereotype441 stereotype441 added the legacy-area-front-end Legacy: Use area-dart-model instead. label Apr 1, 2021
dart-bot pushed a commit that referenced this issue Apr 1, 2021
These tests just cover the CFE functionality.  Once
#44905 is addressed, I'll add
test expectations for the analyzer.

Note: these tests uncovered two unreelated bugs: #45551 and #45552.

Bug: #44897
Change-Id: I465b157afc2cc15bcc5ce083aaa74614313a38a6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193581
Commit-Queue: Paul Berry <[email protected]>
Reviewed-by: Leaf Petersen <[email protected]>
Reviewed-by: Bob Nystrom <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-front-end Legacy: Use area-dart-model instead.
Projects
None yet
Development

No branches or pull requests

1 participant