dart-lang / dart-lang/language

const_constructor_param_type_mismatch generates poor errors:

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

Description

Consider the following code snippet:

```
void main() {
testWidgets('clipboard summary card ...', (tester) async {
final clipboard = Clipboard.test();
await tester.pumpWidget(const ClipboardSummaryCard( clipboard: clipboard ));
});
}
```

The following code has a problem:
```
const ClipboardSummaryCard( clipboard: clipboard )
```
The issue is that we are passing a non-const value 'clipboard' to the constructor - hence the the leading 'const' keyword is incorrect.

The error this code generates is:

```
A value of type 'Null' can't be assigned to a parameter of type 'Clipboard' in a const constructor.
Try using a subtype, or removing the keyword 'const'.
```

This error makes no sense (particularly when sometimes the 'const' keyword is several lines away from the problem).

It makes me go off looking for a nullable type and since `clipboard` is not nullable I just ignore it as a potential source of the problem.

Rather the error should be something like:

```
Use of the 'const' keyword is invalid when calling a constructor with the non-const value: 'clipboard'. Remove the const keyword from before the call to ClipboardSummaryCard.
```

Now I can easily work out what is happening.

This problem is particularly obvious when using VS-Code and dart-code.

dart-code will helpfully add the 'const' keyword if you initially pass all const values to the ClipboardSummaryCard ctor.
If you then go back and change one of the params to the ctor to a non-const value you get the above error and confusion reigns.

Contributor guide

Open the contributing guide

Research direction

Reproduce the const ClipboardSummaryCard call from the issue and inspect how the current const-constructor diagnostic is selected. Trace the diagnostic path for a non-const argument and compare it with the proposed wording; done means the error identifies the offending value and points to the unnecessary const keyword.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.