Stop propagating return types for top-level function declarations

R=paulberry@google.com, scheglov@google.com

Review URL: https://codereview.chromium.org//1309243007 .
This commit is contained in:
Brian Wilkerson
2015-09-08 13:14:36 -07:00
parent 4bb2e617db
commit 5efe30d458
3 changed files with 38 additions and 67 deletions
@@ -328,7 +328,9 @@ class StaticTypeAnalyzer extends SimpleAstVisitor<Object> {
node.element as ExecutableElementImpl;
functionElement.returnType =
_computeStaticReturnTypeOfFunctionDeclaration(node);
_recordPropagatedTypeOfFunction(functionElement, function.body);
if (node.parent is FunctionDeclarationStatement) {
_recordPropagatedTypeOfFunction(functionElement, function.body);
}
_recordStaticType(function, functionElement.type);
return null;
}
@@ -2346,6 +2346,41 @@ class A {
''');
}
void test_true_functionExpression() {
_assertMatches(r'''
import 'dart:async';
class A {
Future<int> mmm() async {
return 42;
}
a() {
mmm().then((p1) {
return p1.toString();
}).then((p2) {
print(p2);
});
}
}
''', r'''import 'dart:async';
class A {
Future<int> mmm() async {
return 42;
}
a() {
mmm().then((int p1) {
return p1.toString();
}).then((String p2) {
print(p2);
});
}
}
''');
}
void test_true_functionTypeAlias_list_reorder() {
_assertMatches(
r'''
@@ -13443,72 +13443,6 @@ f1(x) {
typeProvider.stringType);
}
void test_propagatedReturnType_function_hasReturnType_returnsNull() {
String code = r'''
String f() => null;
main() {
var v = f();
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.stringType);
}
void test_propagatedReturnType_function_lessSpecificStaticReturnType() {
String code = r'''
Object f() => 42;
main() {
var v = f();
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.intType);
}
void test_propagatedReturnType_function_moreSpecificStaticReturnType() {
String code = r'''
int f(v) => (v as num);
main() {
var v = f(3);
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.intType);
}
void test_propagatedReturnType_function_noReturnTypeName_blockBody_multipleReturns() {
String code = r'''
f() {
if (true) return 0;
return 1.0;
}
main() {
var v = f();
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.numType);
}
void test_propagatedReturnType_function_noReturnTypeName_blockBody_oneReturn() {
String code = r'''
f() {
var z = 42;
return z;
}
main() {
var v = f();
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.intType);
}
void test_propagatedReturnType_function_noReturnTypeName_expressionBody() {
String code = r'''
f() => 42;
main() {
var v = f();
}''';
_assertPropagatedAssignedType(
code, typeProvider.dynamicType, typeProvider.intType);
}
void test_propagatedReturnType_localFunction() {
String code = r'''
main() {