b75502513a
This CL fixes type inference of closures using '=>' syntax so that
they infer consistently with an equivalent closure defined using block
syntax ('{}').
Fixes #31436.
Change-Id: I5463dd0ba986e9953ebabbc24ce3ced9a7220007
Reviewed-on: https://dart-review.googlesource.com/27084
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
73 lines
2.2 KiB
Dart
73 lines
2.2 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
/*@testedFeatures=inference*/
|
|
library test;
|
|
|
|
void block_test() {
|
|
List<Object> Function() g;
|
|
g = /*@returnType=List<Object>*/ () {
|
|
return /*@typeArgs=Object*/ [3];
|
|
};
|
|
assert(g is List<Object> Function());
|
|
assert(g is! List<int> Function());
|
|
g(). /*@target=List::add*/ add("hello"); // No runtime error
|
|
List<int> l = /*@typeArgs=int*/ [3];
|
|
g = /*@returnType=List<int>*/ () {
|
|
return l;
|
|
};
|
|
assert(g is List<Object> Function());
|
|
assert(g is List<int> Function());
|
|
try {
|
|
g(). /*@target=List::add*/ add("hello"); // runtime error
|
|
throw 'expected a runtime error';
|
|
} on TypeError {}
|
|
Object o = l;
|
|
g = /*@returnType=List<Object>*/ () {
|
|
return o;
|
|
}; // No implicit downcast on the assignment, implicit downcast on the return
|
|
assert(g is List<Object> Function());
|
|
assert(g is! List<int> Function());
|
|
assert(g is! Object Function());
|
|
g(); // No runtime error;
|
|
o = 3;
|
|
try {
|
|
g(); // Failed runtime cast on the return type of f
|
|
throw 'expected a runtime error';
|
|
} on TypeError {}
|
|
}
|
|
|
|
void arrow_test() {
|
|
List<Object> Function() g;
|
|
g = /*@returnType=List<Object>*/ () => /*@typeArgs=Object*/ [3];
|
|
assert(g is List<Object> Function());
|
|
assert(g is! List<int> Function());
|
|
g(). /*@target=List::add*/ add("hello"); // No runtime error
|
|
List<int> l = /*@typeArgs=int*/ [3];
|
|
g = /*@returnType=List<int>*/ () => l;
|
|
assert(g is List<Object> Function());
|
|
assert(g is List<int> Function());
|
|
try {
|
|
g(). /*@target=List::add*/ add("hello"); // runtime error
|
|
throw 'expected a runtime error';
|
|
} on TypeError {}
|
|
Object o = l;
|
|
g = /*@returnType=List<Object>*/ () =>
|
|
o; // No implicit downcast on the assignment, implicit downcast on the return
|
|
assert(g is List<Object> Function());
|
|
assert(g is! List<int> Function());
|
|
assert(g is! Object Function());
|
|
g(); // No runtime error;
|
|
o = 3;
|
|
try {
|
|
g(); // Failed runtime cast on the return type of f
|
|
throw 'expected a runtime error';
|
|
} on TypeError {}
|
|
}
|
|
|
|
main() {
|
|
block_test();
|
|
arrow_test();
|
|
}
|