Skip to content

Library-private final fields get different instances depending on how the library was imported #32601

Closed
@pulyaevskiy

Description

@pulyaevskiy

I tried to find relevant issues via search but it didn't yield anything similar.

This may be me learning, but I find following behavior really confusing.

For instance, this Dart file:

// lib/src/a.dart
final _a = new A();

class A {}

void doWithA(String from) {
  print('Hash code of A from $from: ${_a.hashCode}');
}

Up until now I assumed that _a would always be the same instance, but it turns out not entirely true and there can actually be multiple instances of _a depending on how import / export statements are organized in a project.

It is a bit verbose to describe in this issue so I created example project here: https://github.com/pulyaevskiy/library-private-issue

Executing dart lib/main.dart there prints something like this for me:

Hash code of A from main: 931022103
Hash code of A from B: 85304837

Combination of export and import statements in that project is probably far from perfect and normally should be cleaned up. However it seems dangerous to even allow such scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions