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.3k stars 1.59k forks source link

Unnecessary late initialization stub when assigned to constant expressions #54737

Open parlough opened 10 months ago

parlough commented 10 months ago

A snippet like the following where the late final field is assigned in the declaration to a potentially constant expression ends up using a InitLateFinalInstanceFieldStub. There might be intricacies I'm missing, but can't accessing this field be treated as if accessing the constant value?

class Test {
    late final int value = 3;
}

void main() {
    print(Test().value);
}
parlough commented 1 month ago

Similarly, the InitLateFinalInstanceFieldStub is generated/used when accessing a top-level or static final field that is initialized to a const instantiation in the initialization expression.

class Test {
  static final Test instance = const Test(144); 

  final int value;
  const Test(this.value);
}

final Test otherInstance = const Test(144);

void main() {
  print(Test.instance.value);
  print(otherInstance.value);
}