Skip to content

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

Open
@chiholai-sanasofthk

Description

@chiholai-sanasofthk

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions