Report inference failure on return types of typedefs and generic function types

Change-Id: I9da541785f75c0da5121bb7058d3ad726ff00e12
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/117522
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Sam Rawlins 2019-09-27 06:21:43 +00:00 committed by commit-bot@chromium.org
parent 40cc975932
commit 3637e78014
2 changed files with 65 additions and 0 deletions

View file

@ -343,6 +343,12 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
super.visitFunctionExpression(node);
}
@override
void visitFunctionTypeAlias(FunctionTypeAlias node) {
_checkStrictInferenceReturnType(node.returnType, node, node.name.name);
super.visitFunctionTypeAlias(node);
}
@override
void visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) {
_checkStrictInferenceReturnType(
@ -351,6 +357,25 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
super.visitFunctionTypedFormalParameter(node);
}
@override
void visitGenericFunctionType(GenericFunctionType node) {
// GenericTypeAlias is handled in [visitGenericTypeAlias], where a proper
// name can be reported in any message.
if (node.parent is! GenericTypeAlias) {
_checkStrictInferenceReturnType(node.returnType, node, node.toString());
}
super.visitGenericFunctionType(node);
}
@override
void visitGenericTypeAlias(GenericTypeAlias node) {
if (node.functionType != null) {
_checkStrictInferenceReturnType(
node.functionType.returnType, node, node.name.name);
}
super.visitGenericTypeAlias(node);
}
@override
void visitImportDirective(ImportDirective node) {
_checkForDeprecatedMemberUse(node.uriElement, node);

View file

@ -156,6 +156,22 @@ void f(int callback()) {
''');
}
test_genericFunctionType() async {
await assertErrorsInCode(r'''
Function(int) f = (int n) {
print(n);
};
''', [error(HintCode.INFERENCE_FAILURE_ON_FUNCTION_RETURN_TYPE, 0, 13)]);
}
test_genericFunctionType_withReturnType() async {
await assertNoErrorsInCode(r'''
void Function(int) f = (int n) {
print(n);
};
''');
}
test_localFunction() async {
await assertNoErrorsInCode(r'''
class C {
@ -199,6 +215,30 @@ f() {
test_topLevelFunction_withReturnType() async {
await assertNoErrorsInCode(r'''
dynamic f() => 7;
''');
}
test_typedef_classic() async {
await assertErrorsInCode(r'''
typedef Callback(int i);
''', [error(HintCode.INFERENCE_FAILURE_ON_FUNCTION_RETURN_TYPE, 0, 24)]);
}
test_typedef_classic_withReturnType() async {
await assertNoErrorsInCode(r'''
typedef void Callback(int i);
''');
}
test_typedef_modern() async {
await assertErrorsInCode(r'''
typedef Callback = Function(int i);
''', [error(HintCode.INFERENCE_FAILURE_ON_FUNCTION_RETURN_TYPE, 0, 35)]);
}
test_typedef_modern_withReturnType() async {
await assertNoErrorsInCode(r'''
typedef Callback = void Function(int i);
''');
}
}