dart-lang / dart-lang/language

Consider using bounds information when computing the greatest closure

Open
#3,572 2 comments 2 reactions 0 assignees View on GitHub
type-inference
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Several issues have been filed calling out the fact that the [greatest closure computation](https://github.com/dart-lang/language/blob/main/resources/type-system/inference.md#type-variable-elimination-least-and-greatest-closure-of-a-type) does not take into account the bounds on type parameters. That is, when computing the greatest closure of a type variable `T`, we always choose `Object?` as the closing type, even if `T` was declared with a more precise bound. This extends to taking the greatest closure of a type with respect to the unknown types `_` as well (this is the common case in the algorithm, and the source of the motivating examples). See #1761 and #3567 for motivating examples for the `_` case. A motivating example for the type variable case would, I believe, be more esoteric.

It seems plausible that we could instead choose to use the bound of `T` to replace `T` when computing the greatest (but not least) closure. Similarly, if we associated with each unknown type `_` the bound of the type variable from which it was produced, we could choose to replace `_` with a more precise type when computing the greatest closure. Assuming this all works out, this would possibly help with examples like the ones referenced above.

cc @dart-lang/language-team

Contributor guide

Open the contributing guide

Research direction

Start with the greatest-closure section of resources/type-system/inference.md, then read issues #1761 and #3567 for the motivating unknown-type cases. Determine whether bounds can safely guide greatest closure for both type variables and unknown types, and validate the result against those examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.