dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.56k forks source link

Analyzer thinks private getter in mixin is not referenced, even though it is #56532

Closed mkorbel1 closed 2 weeks ago

mkorbel1 commented 3 weeks ago

The below code reproduces the issue:

abstract class Base {
  int get _myGetter;
}

mixin BaseMixin on Base {
  @override
  int get _myGetter => 1;
}

class ImplA extends Base {
  @override
  String toString() => '$_myGetter';

  @override
  int get _myGetter => 2;
}

class ImplB extends ImplA with BaseMixin {}

void main(List<String> arguments) {
  print(ImplB());
}

Running dart analyze highlights this issue:

   info • mixin_override.dart:2:11 • The declaration '_myGetter' isn't referenced. Try removing the declaration of '_myGetter'. • unused_element
   info • mixin_override.dart:7:11 • The declaration '_myGetter' isn't referenced. Try removing the declaration of '_myGetter'. • unused_element

The messages are referring to _myGetter in Base and BaseMixin. However, executing the main function prints 1.

$ dart bin/mixin_override.dart 
1

So clearly the version of _myGetter in BaseMixin is referenced and used. The functionality of the program is as expected, but the analyzer is giving a bad warning that it can be removed from BaseMixin, even though it impacts the execution of the program.

I'm running in WSL2 Ubuntu, Dart SDK (no Flutter)

$ dart info

If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

#### General info

- Dart 3.5.1 (stable) (None) on "linux_x64"
- on linux / Linux 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024
- locale is C.UTF-8

#### Project info

- sdk constraint: '^3.5.1'
- dependencies: 
- dev_dependencies: lints, test

#### Process info

| Memory |  CPU | Elapsed time | Command line                                                                           |
| -----: | ---: | -----------: | -------------------------------------------------------------------------------------- |
| 604 MB | 1.4% |        21:18 | dart language-server --protocol=lsp --client-id=VS-Code-Remote --client-version=3.94.0 |
| 440 MB | 2.2% |        20:42 | dart language-server --protocol=lsp --client-id=VS-Code-Remote --client-version=3.94.0 |
|  82 MB | 0.1% |        21:18 | dart tooling-daemon --machine                                                          |
|  82 MB | 0.0% |        20:42 | dart tooling-daemon --machine                                                          |
dart-github-bot commented 3 weeks ago

Summary: The Dart analyzer incorrectly flags a private getter in a mixin as unused, even though it's overridden and used in a subclass. This leads to a false positive warning, suggesting the getter can be removed, despite impacting program execution.

srawlins commented 2 weeks ago

Good catch. This is a duplicate of https://github.com/dart-lang/sdk/issues/49354.