You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
eernstg opened this issue
Sep 15, 2020
· 2 comments
Labels
area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).Epictype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
The test language/variance/variance_in_inference_test fails with the CFE and the analyzer, in several locations.
Inference does not produce the right outcome in at least some of these failure locations:
tests/language/variance/variance_in_inference_test.dart:52:66: Error: The argument type 'Contravariant<Middle>' can't be assigned to the parameter type 'Contravariant<Upper>'.
- 'Contravariant' is from 'tests/language/variance/variance_in_inference_test.dart'.
- 'Middle' is from 'tests/language/variance/variance_in_inference_test.dart'.
- 'Upper' is from 'tests/language/variance/variance_in_inference_test.dart'.
var inferredMiddle = inferContraContra(Contravariant<Upper>(), Contravariant<Middle>());
^
- Line 52, column 66: COMPILE_TIME_ERROR.ARGUMENT_TYPE_NOT_ASSIGNABLE
The argument type 'Contravariant<Middle>' can't be assigned to the parameter type 'Contravariant<Upper>'.
The inferred expression is inferContraContra(Contravariant<Upper>(), Contravariant<Middle>()), which allows for T == Middle, but the inference actually chooses Upper, and then there is an error because a Contravariant<Middle> can't be assigned to the resulting parameter type Contravariant<Upper>. The constraints are T <: Upper and T <: Middle, so inference should find the solution T == Middle.
variance_inout_inference_test
Similarly, the test language/variance/variance_inout_inference_test gives rise to an error based on failing inference:
tests/language/variance/variance_inout_inference_test.dart:46:40: Error: The argument type 'Invariant<Middle>' can't be assigned to the parameter type 'Invariant<Upper>'.
- 'Invariant' is from 'tests/language/variance/variance_inout_inference_test.dart'.
- 'Middle' is from 'tests/language/variance/variance_inout_inference_test.dart'.
- 'Upper' is from 'tests/language/variance/variance_inout_inference_test.dart'.
var inferredMiddle4 = inferInvContra(Invariant<Middle>(), Contravariant<Upper>());
^
- Line 46, column 40: COMPILE_TIME_ERROR.ARGUMENT_TYPE_NOT_ASSIGNABLE
The argument type 'Invariant<Middle>' can't be assigned to the parameter type 'Invariant<Upper>'.
variance_in_inference_error_test
A third case with the same issue is language/variance/variance_in_inference_error_test:
variance_in_inference_error_test.dart:32:66: Error: The argument type 'Contravariant<Middle>' can't be assigned to the parameter type 'Contravariant<Upper>'.
- 'Contravariant' is from 'variance_in_inference_error_test.dart'.
- 'Middle' is from 'variance_in_inference_error_test.dart'.
- 'Upper' is from 'variance_in_inference_error_test.dart'.
var inferredMiddle = inferContraContra(Contravariant<Upper>(), Contravariant<Middle>());
^
error • The argument type 'Contravariant<Middle>' can't be assigned to the parameter type 'Contravariant<Upper>'. • variance_in_inference_error_test.dart:32:66 • argument_type_not_assignable
Again, inference should produce the solution T == Middle from the constraints T <: Upper and T <: Middle, but it chooses T == Upper and then complains. The same issue arises in several other locations in this test.
variance_downwards_inference_test
Finally, the test language/variance/variance_downwards_inference_test reports three spurious errors:
variance_downwards_inference_test.dart:42:25: Error: The argument type 'List<num>' can't be assigned to the parameter type 'List<int>'.
- 'List' is from 'dart:core'.
B<int> b = new B(<num>[])..x=2.2;
^
variance_downwards_inference_test.dart:42:32: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
B<int> b = new B(<num>[])..x=2.2;
^
variance_downwards_inference_test.dart:52:38: Error: A value of type 'double' can't be assigned to a variable of type 'int'.
D<int> d = new D(3, (num x) {})..x=2.2;
^
error • Couldn't infer type parameter 'T'.
Tried to infer 'int' for 'T' which doesn't work:
Parameter 'x' declared as 'List<T>'
but argument is 'List<num>'.
The type 'int' was inferred from:
Return type declared as 'B<T>'
used where 'B<int>' is required.
Consider passing explicit type argument(s) to the generic.
• variance_downwards_inference_test.dart:42:18 • could_not_infer
error • The argument type 'List<num>' can't be assigned to the parameter type 'List<int>'. • variance_downwards_inference_test.dart:42:20 • argument_type_not_assignable
error • A value of type 'double' can't be assigned to a variable of type 'int'. • variance_downwards_inference_test.dart:42:32 • invalid_assignment
error • A value of type 'double' can't be assigned to a variable of type 'int'. • variance_downwards_inference_test.dart:52:38 • invalid_assignment
In line 42, the CFE should choose T == num, but it chooses T == int, and the two errors are then implied. T == int is not a solution to the expected constraints, so it seems likely that the generated constraints are not correct. The analyzer chooses int as well, and then detects that it causes errors, and still reports those errors even though they are derived from a choice made during type inference.
In line 52, they should choose T == num, but they actually choose T == int, which causes the reported error. T == int is a solution, but the heuristic for contravariant parameters should choose the most general solution num.
The text was updated successfully, but these errors were encountered:
eernstg
added
area-meta
Cross-cutting, high-level issues (for tracking many other implementation issues, ...).
type-bug
Incorrect behavior (everything from a crash to more subtle misbehavior)
Epic
labels
Sep 15, 2020
After switching over to NNBD, variance inference started failing because
we no longer used variance to subtype match.
This CL adds a null-aware subtype matching fn for the current algorithm.
Bug: #43419
Change-Id: Ifecc58a7537c0656f089cc3f8aca7fd4289be6d2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334721
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).Epictype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
variance_in_inference_test
The test
language/variance/variance_in_inference_test
fails with the CFE and the analyzer, in several locations.Inference does not produce the right outcome in at least some of these failure locations:
The inferred expression is
inferContraContra(Contravariant<Upper>(), Contravariant<Middle>())
, which allows forT == Middle
, but the inference actually choosesUpper
, and then there is an error because aContravariant<Middle>
can't be assigned to the resulting parameter typeContravariant<Upper>
. The constraints areT <: Upper
andT <: Middle
, so inference should find the solutionT == Middle
.variance_inout_inference_test
Similarly, the test
language/variance/variance_inout_inference_test
gives rise to an error based on failing inference:variance_in_inference_error_test
A third case with the same issue is
language/variance/variance_in_inference_error_test
:Again, inference should produce the solution
T == Middle
from the constraintsT <: Upper
andT <: Middle
, but it choosesT == Upper
and then complains. The same issue arises in several other locations in this test.variance_downwards_inference_test
Finally, the test
language/variance/variance_downwards_inference_test
reports three spurious errors:In line 42, the CFE should choose
T == num
, but it choosesT == int
, and the two errors are then implied.T == int
is not a solution to the expected constraints, so it seems likely that the generated constraints are not correct. The analyzer choosesint
as well, and then detects that it causes errors, and still reports those errors even though they are derived from a choice made during type inference.In line 52, they should choose
T == num
, but they actually chooseT == int
, which causes the reported error.T == int
is a solution, but the heuristic for contravariant parameters should choose the most general solutionnum
.The text was updated successfully, but these errors were encountered: