Skip to content

Recent analyzer version broke the behavior of code-generators when relying on "invalid types" #52455

@rrousselGit

Description

@rrousselGit

Hello!

Before the recent InvalidType changes, when a code-generator encountered an invalid type, they generated "dynamic". But now, the same generator will generate InvalidType, which is not a valid symbol.

This breaks apps that use possibly two code-generators.
As an example, consider an app which uses two code-generators:

  • one code-generator adds a copyWith to classes
  • one generator emits a CodeGeneratedClass

Then, a user would write:

@CopyWith()
class Example with _$Example { // mixin used to add the copyWith method
  Example(this.value);
  List<CodeGeneratedClass> value;
}

In that case, before the recent InvalidType changes, the generated copyWith would be:

mixin _$Example on Example {
  Example copyWith({
    // "value" is typed as dynamic because at the time of generation,
    // the class "CodeGeneratedClass" does not exist. Therefore dynamic used to be returned.
    List<dynamic> value, 
  }) => ...;
}

That behavior was reasonable. It wasn't perfectly typed, but the generated code worked.
So someone could reasonably write:

void main() {
  Example example = Example([]);
  example = example.coyWith(value: [CodeGeneratedClass()]);
}

But with the recent changes, the generated copyWith is now instead:

mixin _$Example on Example {
  Example copyWith({
    List<InvalidType> value, 
  }) => ...;
}

The problem is that this code is no longer valid. The InvalidType type does not exist, and even if it did, CodeGeneratedClass would not be assignable to it.

As such, the application does not compile anymore

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onanalyzer-apiIssues that impact the public API of the analyzer packagearea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions