Skip to content

Private field is promotable in spite of noSuchMethod forwarder #53510

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
sgrekhov opened this issue Sep 13, 2023 · 2 comments
Closed

Private field is promotable in spite of noSuchMethod forwarder #53510

sgrekhov opened this issue Sep 13, 2023 · 2 comments
Assignees
Labels
legacy-area-analyzer Use area-devexp instead. legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on

Comments

@sgrekhov
Copy link
Contributor

The following test fails in both analyzer and CFE

class A {
  final int? _x = 42;

  void testA() {
    if (_x != null) {
      _x.isOdd;  // No expected error
//       ^^^^^
// [analyzer] unspecified
// [cfe] unspecified
    }
  }
}

class C {
  void _x();

  dynamic noSuchMethod(Invocation inv) {}
}

main() {
  A a = A();
  if (a._x is int) {
    a._x.isEven;  // No expected error
//       ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
  }
  a.testA();
}

According to the dart-lang/language#2020 (comment) if there is an implicit noSuchMethod forwarder in the same library field should not be promotable

cc @stereotype441

Tested on Dart SDK version: 3.2.0-edge.655687db777cf07cb4d75398f9eaf2d30ab6af44 (be) (Wed Sep 13 02:35:01 2023 +0000) on "macos_x64"

@eernstg
Copy link
Member

eernstg commented Sep 13, 2023

True, this is what the spec-comment says.

However, @stereotype441, perhaps this is overkill? I'd expect that a forwarder for a method would never interfere with the getter of a final instance variable. Perhaps it is enough to cancel the promotion when there is a noSuchMethod forwarder with the same name which is a getter?

@stereotype441
Copy link
Member

Sorry, that comment doesn't reflect what I actually implemented. What I actually implemented was exactly what @eernstg suggested, namely that a forwarder for a method does not interfere with the getter of a final instance variable. Promotion is only cancelled if there is a noSuchMethod forwarder with the same name which is a getter. I've updated that comment accordingly.

So in @sgrekhov's example, no error should be reported. Which matches the behaviour as of b555e71.

I'm assigning this issue to myself as a reminder to update the text in the CHANGELOG to reflect this subtlety.

@stereotype441 stereotype441 self-assigned this Sep 13, 2023
@devoncarew devoncarew added legacy-area-analyzer Use area-devexp instead. legacy-area-front-end Legacy: Use area-dart-model instead. labels Sep 13, 2023
@scheglov scheglov added the P2 A bug or feature request we're likely to work on label Sep 13, 2023
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. legacy-area-front-end Legacy: Use area-dart-model instead. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

5 participants