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

AssignFinal to nullable type doesn't generate the nullable type #315

Open
johanflint opened this issue Mar 28, 2021 · 4 comments
Open

AssignFinal to nullable type doesn't generate the nullable type #315

johanflint opened this issue Mar 28, 2021 · 4 comments
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@johanflint
Copy link

When using code_builder I noticed that if I use assignFinal with a nullable type, it doesn't output the question mark.

Reproduction recipe:

final statement = refer('foo')
    .assignFinal(
        'bar',
        TypeReference((b) => b
          ..symbol = 'String'
          ..isNullable = true))
    .statement;
print(DartFormatter().format('${statement.accept(DartEmitter.scoped(useNullSafetySyntax: true))}'));

Output: final String bar = foo;
Expected: final String? bar = foo;

Is this a bug or am I missing something obvious here? Thanks for the great package, it's a joy to use!

@srawlins
Copy link
Member

Yeah I see a few issues with assignFinal, assignConst, and assignVar. They all use TypeReference.expression, which uses ScopedCode, which only references the symbol in its toString, not the type arguments, nor the nullability. So this:

void main() {
  final statement2 = refer('foo')
      .assignFinal(
          'bar',
          TypeReference((b) => b
            ..symbol = 'List'
            ..types.add(refer('int'))))
      .statement;
  print(DartFormatter().format(
      '${statement2.accept(DartEmitter.scoped())}'));
}

prints final List bar = foo;, omitting the <int> type argument.

@johanflint
Copy link
Author

Ah indeed. I use ...symbol = 'List?' as a workaround, but it feels like cheating :).

chgibb added a commit to hydro-sdk/hydro-sdk that referenced this issue Jun 26, 2021
@Lohann
Copy link

Lohann commented Feb 22, 2023

still waiting for a fix

@natebosch
Copy link
Member

assignFinal and the related APIs are deprecated. It looks like this bug might exist in the replacements too

natebosch added a commit that referenced this issue Mar 7, 2023
Towards #315

Add a skipped test which demonstrates that the nullability of a type
reference is ignored when emitting a variable assignment.
natebosch added a commit that referenced this issue Mar 7, 2023
Towards #315

Add a skipped test which demonstrates that the nullability of a type
reference is ignored when emitting a variable assignment.
@devoncarew devoncarew added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed problem: bug labels Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants