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: phantom analyze issue for enums #49097

Closed
khobotin-elopage opened this issue May 24, 2022 · 4 comments
Closed

Analyzer: phantom analyze issue for enums #49097

khobotin-elopage opened this issue May 24, 2022 · 4 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P1 A high priority bug; for example, a single project is unusable or has many test failures

Comments

@khobotin-elopage
Copy link

khobotin-elopage commented May 24, 2022

The issue appeared after upgrading to Dart 2.17 & Flutter 3

STR:
Create new flutter project and write:

enum MyEnum {
  myValue(type: TextInputType.none);

  const MyEnum({this.type = TextInputType.text});

  final TextInputType type;
}

the code {this.type = TextInputType.text} throws an error The default value of an optional parameter must be constant.

But TextInputType.text is a constant value

static const TextInputType text = TextInputType._(0);

if I run dart analyze I will get

error • lib/main.dart:120:29 • The default value of an optional parameter must be constant. • non_constant_default_value


The most strange part starts when I go to TextInputType.text definition and then back, or just change a file (delete symbol in text and then undo action), the error dissapears. And now dart analyze show

No issues found!

But once I run command Restart Analysis Server, it go back.

It only applicable for enums. This code

class ClassA {
  const ClassA({this.type = TextInputType.text});

  final TextInputType type;
}

doesn't show any errors.

Another strange thing is: if I put together enum and class and set the same TextInputType default value, enum will not show the error (even after analyzer restart). Like this:

enum MyEnum {
  myValue(type: TextInputType.none);

  const MyEnum({this.type = TextInputType.text});

  final TextInputType type;
}

class ClassA {
  const ClassA({this.type = TextInputType.text});

  final TextInputType type;
}

But once I set TextInputType default value for enum other than text, and restart analyzer, error goes back.

Dart SDK version: 2.17.0 (stable) on macos_arm64
Flutter 3.0.1 • channel stable
IDE: VS Code

@eernstg eernstg added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label May 24, 2022
@scheglov scheglov added the P1 A high priority bug; for example, a single project is unusable or has many test failures label May 24, 2022
@scheglov scheglov self-assigned this May 24, 2022
@scheglov
Copy link
Contributor

I can reproduce it:

  solo_test_XXX() async {
    newFile('$testPackageLibPath/a.dart', r'''
class TextInputType {
  static const none = TextInputType(-1);
  static const text = TextInputType(0);

  final int value;
  const TextInputType(this.value);
}
''');
    await assertNoErrorsInCode(r'''
import 'a.dart';

enum MyEnum {
  myValue(type: TextInputType.none);

  const MyEnum({this.type = TextInputType.text});

  final TextInputType type;
}
''');
  }

Produces:

  Found but did not expect:
    CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE [98, 18, "The default value of an optional parameter must be constant.", null]
  
  To accept the current state, expect:
    error(CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE, 98, 18),

@scheglov
Copy link
Contributor

https://dart-review.googlesource.com/c/sdk/+/245909

copybara-service bot pushed a commit that referenced this issue May 25, 2022
Bug: #49097
Change-Id: I9fccbd2f70d94f7d6cfe780b8d4fadf1708eb7a1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245909
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
@scheglov
Copy link
Contributor

I requested a cherry pick for this fix.

@khobotin-elopage
Copy link
Author

Thanks a lot for quick fix, @scheglov ! ❤️

copybara-service bot pushed a commit that referenced this issue Jun 1, 2022
…hods.

Bug: #49097
Change-Id: I9fccbd2f70d94f7d6cfe780b8d4fadf1708eb7a1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245909
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
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. P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

3 participants