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

Dart Static Analyzer Fails to Report Missing Constructor Parameters and Freezes on Evaluation (Occurs with Generics and Sealed Classes) #59797

Open
GiancarloCante opened this issue Dec 23, 2024 · 3 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@GiancarloCante
Copy link

Hi, when using the following code snippet, I encountered a bug where the Dart static analyzer does not report an error when not all required parameters are provided to a class constructor.

Additionally, the static analysis seems to freeze when evaluating the code.

However, the error is shown when attempting to compile or run the application in the terminal.

On rare occasions, the static analyzer behaves as expected, showing the corresponding errors.

Case 1:

sealed class BaseClass<A, B, C> {
  const BaseClass({
    required this.a,
    required this.b,
    required this.c,
  });

  final A a;
  final B b;
  final C c;
}

class AClass<A, B, C> extends BaseClass<A, B, C> {
  const AClass({
    required super.a,
    required super.b,
    required super.c,
  });
}

void foo<A, B, C, T extends BaseClass<A, B, C>>(List<T Function()> list) {
  // ...
}

void main() {
  foo<int, int, int, AClass<int, int, int>>([
    () => const AClass(a: 1, b: 2, c: 3),
    () => const AClass(a: 3), // wrong
  ]);
}

Case 2:

sealed class BaseClass<A, B, C> {
  const BaseClass({
    required this.a,
    required this.b,
    required this.c,
  });

  final A a;
  final B b;
  final C c;
}

class AClass<A, B, C> extends BaseClass<A, B, C> {
  const AClass({
    required super.a,
    required super.b,
    required super.c,
  });
}

void foo<A, B, C, T extends BaseClass<A, B, C>>(List<T Function()> list) {
  // ...
}

void main() {
  foo<int, int, int, AClass<int, int, int>>([f1]);
}

AClass<int, int, int> f1() => const AClass(a: 1);

Tested On:

  • Windows with Flutter 3.27.1 with Dart 3.6

  • IDX Platform with the following configuration:

[✓] Flutter (Channel stable, 3.24.3, on IDX GNU/Linux 6.1.112+, locale en_US.UTF-8)
    • Flutter version 3.24.3 on channel stable at /home/user/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

image

  • In the DartPad it works normally.

From what I've noticed, the issue seems to arise when using functions that take generics as parameters in combination with sealed classes. I don't know if it occurs in other cases.

This

  foo<int, int, int, AClass<int, int, int>>([
    // () => const AClass(a: 1, b: 2, c: 3),
    // () => const AClass(a: 3),
  ]);

I would appreciate it if you could tell me what the reason is so I can update my code while it is being fixed.

Thanks

@julemand101
Copy link
Contributor

Sounds like an issue more fitting for the implementation of Dart: https://github.com/dart-lang/sdk/issues

This repository are for discussions and issues related to the Dart Language Specification itself.

@srawlins srawlins transferred this issue from dart-lang/language Dec 23, 2024
@srawlins srawlins added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 23, 2024
@srawlins
Copy link
Member

CC @scheglov this might be an interesting repro of code causing the analyzer to hang.

@scheglov
Copy link
Contributor

I'm not able to reproduce it, neither when I analyze these two examples as unit tests, nor when I copy / paste into a file in my IntelliJ IDE.

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.
Projects
None yet
Development

No branches or pull requests

4 participants