Skip to content

Adjust function literal return type inference to avoid spurious null#4210

Merged
eernstg merged 2 commits intomainfrom
spec_generator_return_inference_dec24
Aug 21, 2025
Merged

Adjust function literal return type inference to avoid spurious null#4210
eernstg merged 2 commits intomainfrom
spec_generator_return_inference_dec24

Conversation

@eernstg
Copy link
Member

@eernstg eernstg commented Dec 17, 2024

See dart-lang/sdk#59669 where this topic came up. Thanks to @chiholai-sanasofthk for spotting this issue!

This PR changes one item in the list of actions taken during function literal return type inference: A return; statement only adds Null to the return type in cases where the given function literal is a non-generator.

With the current version, Null is added to the return type also in cases like () sync* { yield 1; return; } such that this function literal gets the inferred return type Iterable<int?>. This is an unnecessary loss of typing precision because null is never actually added to the returned iterable. With this update, the inferred return type is Iterable<int>.

@lrhn
Copy link
Member

lrhn commented Jan 5, 2025

Definitely a spec bug, never intended to make any difference to the return type that a generator contains a return.

@eernstg
Copy link
Member Author

eernstg commented Jan 6, 2025

Updated, PTAL.

@leafpetersen
Copy link
Member

What do the two implementations do? I think from the linked issue, at least the analyzer follows the old spec? So this is a breaking change? No objection to the change in principle, but we need to be sure that it gets tracked as a breaking change, and scheduled for implementation as needed.

@eernstg
Copy link
Member Author

eernstg commented Jan 8, 2025

What do the two implementations do? ... the analyzer follows the old spec

Right, the analyzer includes ? in the inferred element type, the CFE does not. This would imply that there is no change in the dynamic semantics, and also that all existing programs where the CFE has been used (that's all programs, I guess?) will pass without errors if the analyzer is updated as proposed here.

Double checking, the following program (based on the current specification) is accepted by the analyzer, but the CFE reports errors as shown:

// ignore_for_file: unused_element

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

  withOutReturnSync() sync* {
    yield 1;
  }
  withOutReturnSync.expectStaticType<Exactly<Iterable<int> Function()>>;

  withReturnSync() sync* {
    yield 1;
    return;
  }
  withReturnSync.expectStaticType<Exactly<Iterable<int?> Function()>>; // Analyzer OK, CFE error.

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

  withoutReturnASync() async* {
    yield 1;
  }
  withoutReturnASync.expectStaticType<Exactly<Stream<int> Function()>>;

  withReturnASync() async* {
    yield 1;
    return;
  }
  withReturnASync.expectStaticType<Exactly<Stream<int?> Function()>>; // Analyzer OK, CFE error.
}

typedef Exactly<X> = X Function(X);

extension<X> on X {
  X expectStaticType<Y extends Exactly<X>>() => this;
}

@eernstg
Copy link
Member Author

eernstg commented Jan 8, 2025

Based on my previous comment, it seems likely to me that nothing will break. So we could run the breaking change process with an attached "but, most likely, nothing will actually break" plus an explanation. Or we could test internally and conclude that it isn't actually breaking if nothing breaks there.

@eernstg
Copy link
Member Author

eernstg commented Mar 5, 2025

I think we should be able to proceed with this one.

Checking again, the following program expects the behavior which is being specified if this PR is landed, and it is compiled and executed without errors by the CFE; however, the analyzer behaves according to the current specification (and would need to be adjusted if and when this is landed).

// ignore_for_file: unused_element

void main() {
  withoutReturnSync() sync* {
    yield 1;
  }
  withoutReturnSync().expectStaticType<Exactly<Iterable<int>>>();

  withReturnSync() sync* {
    yield 1;
    return;
  }
  withReturnSync().expectStaticType<Exactly<Iterable<int>>>(); // The analyzer fails here.

  Iterable<int> withReturnSync2() sync* {
    yield 1;
    return;
  }
  withReturnSync2().expectStaticType<Exactly<Iterable<int>>>();

  withoutReturnAsync() async* {
    yield 1;
  }
  withoutReturnAsync().expectStaticType<Exactly<Stream<int>>>();

  withReturnAsync() async* {
    yield 1;
    return;
  }
  withReturnAsync().expectStaticType<Exactly<Stream<int>>>(); // The analyzer fails here.

  Stream<int> withReturnAsync2() async* {
    yield 1;
    return;
  }
  withReturnAsync2().expectStaticType<Exactly<Stream<int>>>();
}

typedef Exactly<X> = X Function(X);

extension<X> on X {
  X expectStaticType<Y extends Exactly<X>>() => this;
}

Given that the CFE already follows the proposed specification (at compile time and run time), no programs should break if we land this and adjust the analyzer.

@eernstg
Copy link
Member Author

eernstg commented Mar 5, 2025

@dart-lang/language-team, WDYT, can we proceed to land the spec, test the breakage on internal code (no breakage is expected), and adjust the analyzer (if it is true that no breakage is observed)?

@natebosch
Copy link
Member

If the CFE already implements the expected behavior I agree that it should not cause problems to roll this out, and using internal code as a double check sounds good to me.

@eernstg eernstg force-pushed the spec_generator_return_inference_dec24 branch from 15c50c5 to c135c99 Compare March 6, 2025 13:41
@eernstg eernstg force-pushed the spec_generator_return_inference_dec24 branch from c135c99 to b3d397e Compare April 8, 2025 14:25
@eernstg eernstg force-pushed the spec_generator_return_inference_dec24 branch from b3d397e to 60dcd1a Compare May 16, 2025 14:16
@eernstg
Copy link
Member Author

eernstg commented May 20, 2025

@lrhn, do you have further comments on this one?

@eernstg
Copy link
Member Author

eernstg commented Jun 16, 2025

@leafpetersen, I'd like to move on with this change. We could initiate a breaking change process, or we could rely on the fact that the CFE already has the behavior which is specified if the PR is landed. WDYT?

@eernstg
Copy link
Member Author

eernstg commented Jun 27, 2025

@leafpetersen, I'd like to push this one forward. WDYT, do we need a breaking change process?

@eernstg eernstg force-pushed the spec_generator_return_inference_dec24 branch from 60dcd1a to 8298994 Compare July 7, 2025 14:33
@eernstg eernstg force-pushed the spec_generator_return_inference_dec24 branch from 8298994 to ec420a5 Compare July 9, 2025 10:32
Copy link
Member

@leafpetersen leafpetersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! cc @scheglov @johnniwinther @chloestefantsova for any concerns.

@scheglov
Copy link
Contributor

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

@eernstg eernstg merged commit f9c9aa3 into main Aug 21, 2025
3 checks passed
@eernstg eernstg deleted the spec_generator_return_inference_dec24 branch August 21, 2025 07:50
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Aug 21, 2025
… when generator.

Bug: dart-lang/language#4210
Change-Id: I098d80fe36ba6629d8613f2b4e80d4562c3232b5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446183
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Sep 1, 2025
Change-Id: I0731af5cc96757f6beb7ef3e2434d50dfa48c2bb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447800
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants