sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Private forwarding constructors not fully supported

Open johnniwinther opened this issue 5 months ago • 1 comments

Consider this program consisting of two libraries:

// private.dart
import 'private_lib.dart';

mixin M {}
class B = A with M;

// private_lib.dart
import 'private.dart';

class A { A._(); }

class C extends B {
  // Analyzer: error - The class 'B' doesn't have a constructor named '_'.
  C._() : super._();
}

method() {
  A._();
  // Analyzer: error - The method '_' isn't defined for the type 'B'.
  // CFE: Error: Member not found: 'B._'.
  B._(); 
  C._();
}

A Function() tearOffA() => A._;
// Analyzer: error - The getter '_' isn't defined for the type 'B'.
// CFE: Error: Member not found: '_'.
B Function() tearOffB() => B._;
C Function() tearOffC() => C._;

The analyzer reports three errors for B._: the super call, the instantiation and the tear-off. The CFE reports two errors for B._: the instantiation and the tear-off.

As I understand the privacy rules, there should be no errors at all.

@eernstg Do you agree?

johnniwinther avatar Jun 13 '25 10:06 johnniwinther

Checking the specification, forwarding constructors should not be created at all when the constructor isn't accessible to the library that contains the mixin application. In other words, B does not have a forwarding constructor that calls A._.

This is not terribly useful in the concrete example because B is then effectively abstract. However, A could declare other constructors and they could have forwarding constructors in B, so the uselessness only comes up in some special cases.

eernstg avatar Jun 13 '25 10:06 eernstg