dart-lang / dart-lang/language

[Dart 3] Can't infer types in some cases

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

Description

The compiler and analyzer are unable to infer types in some pretty clear cases. For example:

```
class A { }

class C extends A {}

class B, S> {
B(A x);
}

void main() {
B(C());
}
```

This code results in the following error:
```
lib/main.dart:10:3:
Error: Inferred type argument 'A' doesn't conform to the bound 'A' of the type variable 'T' on 'B'.
- 'A' is from 'package:dartpad_sample/main.dart' ('lib/main.dart').
- 'Object' is from 'dart:core'.
B(C());
^
lib/main.dart:5:9:
Info: This is the type variable whose bound isn't conformed to.
class B, S> {
^
Error: Compilation failed.
```

Analyzer error:
```
Couldn't infer type parameter 'T'.

Tried to infer 'A' for 'T' which doesn't work:
Type parameter 'T' is declared to extend 'A' producing 'A'.

Consider passing explicit type argument(s) to the generic.
```

**Version:** `Dart SDK version: 3.0.0 (stable) (Thu May 4 01:11:00 2023 -0700) on "windows_x64"`

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example in lib/main.dart with Dart SDK 3.0.0, then compare the compiler and analyzer results for the generic type inference case. Done means the shown B(C()) call no longer produces the bound-conformance or inference errors in either tool.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.