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

Inferenced return type of generator function become nullable, if it contains return; #59669

Open
chiholai-sanasofthk opened this issue Dec 5, 2024 · 2 comments
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@chiholai-sanasofthk
Copy link

chiholai-sanasofthk commented Dec 5, 2024

Dart SDK version: 3.5.4

If a generator function contains return , inferenced return type becomes Iterable<T?>/Stream<T?>, while it should be Iterable<T>/Stream<T>.

example code:

void main() {
  //==================== sync* ===============================

  //✅ inferenced return type is Iterable<int>
  withOutReturnSync() sync* {
    yield 1;
  }

  // ❌ inferenced return type becomes `Iterable<int?>`, expect to be `Iterable<int>`
  withReturnSync() sync* {
    yield 1;
    return;
  }

  //✅ manually specifying the type works fine
  Iterable<int> withReturnSync2() sync* {
    yield 1;
    return;
  }

  //===================== async* =================================

  //✅ inferenced return type is Stream<int>
  withoutReturnASync() async* {
    yield 1;
  }

  // ❌ inferenced return type becomes `Stream<int?>`, expect to be `Stream<int>`
  withReturnASync() async* {
    yield 1;
    return;
  }

  //✅ manually specifying the type works fine
  Stream<int> withReturnASync2() async* {
    yield 1;
    return;
  }
}
@chiholai-sanasofthk chiholai-sanasofthk added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 5, 2024
@lrhn lrhn added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Dec 8, 2024
@bwilkerson
Copy link
Member

@eernstg What is the correct behavior in this case?

@eernstg
Copy link
Member

eernstg commented Dec 17, 2024

Good catch, @chiholai-sanasofthk!

@bwilkerson, I think the feature specification needs to be adjusted.

A local function declaration is subject to return type inference in the same way as a function literal with the trivial context type _ (specified here).

Function literal return type inference is specified here.

It does have the following rule:

For each return; statement in the block, update T to be UP(Null, T).

However, this item is irrelevant for generator functions because it will never cause the returned value to be null, it is just used to detect that the job done by the generator is complete (so we won't add any further elements to the iterable or stream).

I created this spec change proposal: dart-lang/language#4210.

@keertip keertip added analyzer-spec Issues with the analyzer's implementation of the language spec P3 A lower priority bug or feature request labels Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants