dart-lang / dart-lang/language

Type-inference acts on dead code (multiple returns)

Open
#1,885 1 comment 0 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

A method with multiple returns of different types returns `Object`, even if the second return is dead code and never gets executed.

```dart
void main() {
final a = (){
return "string";
return 1;
}();

final String b = a;
// Error: A value of type 'Object' can't be assigned to a variable of type 'String'
}
```

The expected behaviour is that the function should return `String` as if the second return wouldn't exist at all
```dart
void main() {
final a = (){
return "string";
// return 1;
}();

final String b = a;
}
```

Dart SDK 2.14.2

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Dart example and tracing how the closure's return type is inferred from multiple returns. Check the language type-inference specification and the relevant implementation entry point; done means the unreachable second return no longer widens the inferred type, while reachable differing returns retain current behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.