Skip to content
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

Analyzer issue in case of constant constructor and mixin application #59796

Open
sgrekhov opened this issue Dec 23, 2024 · 2 comments
Open

Analyzer issue in case of constant constructor and mixin application #59796

sgrekhov opened this issue Dec 23, 2024 · 2 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@sgrekhov
Copy link
Contributor

The code below produces an error in the analyzer but works without any issue in VM.

class C {
  const C();
}

mixin M on C {
  static const M instance = MC(); // Analyzer error.  The constructor being called isn't a const constructor.
}

class MC = C with M;

main() {
  print(M.instance); 
}

According to the Dart language specification (12.3 Mixin Application) if C has a constant constructor and M declares no instance variables then default constructor of MC is also a constant constructor. So, I believe, that CFE is right here and the analyzer produces a wrong error. Please confirm.

If Sq is a generative const constructor, and MC does not declare any instance variables, Cq is also a const constructor

Dart SDK version: 3.7.0-264.0.dev (dev) (Wed Dec 18 00:02:57 2024 -0800) on "windows_x64"

@sgrekhov sgrekhov added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 23, 2024
@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 27, 2024
@sgrekhov
Copy link
Contributor Author

Interesting that the static member is important here.

class C {
  const C();
}

mixin M on C {
  static const M instance = const MC(); // Analyzer Error

class MC = C with M;

main() {
  M mc = const MC(); // Analyzer error
  print(mc);
}

But without the static member the current behavior is:

class C {
  const C();
}

mixin M on C {
  //static const M instance = const MC();
}

class MC = C with M;

main() {
  M mc = const MC(); // No error
  print(mc);
}

@lrhn
Copy link
Member

lrhn commented Dec 30, 2024

Can confirm.
The constructor of MC is not a "default constructor", just a normal forwarding constructor of a mixin application.
A mixin application class has a forwarding constructor for each generative constructor of its superclass. If the superclass constructor is const and the mixin declares no instance variables, then the forwarding constructor is also const.

Seems the analyzer drops the const if the mixin declares a static variable too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants