Skip to content

Commit 2fd4ca5

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
set DecoratedType.returnType for FunctionType
This CL updates creation of DecoratedType to ensure that the return type is set if the DecoratedType's type is FunctionType. Change-Id: I9fd031693f5fd07ea5f0e6e16723b7c9659410ca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107702 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent ca4b6e5 commit 2fd4ca5

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎pkg/nnbd_migration/lib/src/decorated_type.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DecoratedType {
4141
this.namedParameters = const {},
4242
this.typeArguments = const []}) {
4343
assert(node != null);
44+
assert(type is! FunctionType || returnType != null);
4445
}
4546

4647
/// Creates a [DecoratedType] corresponding to the given [element], which is

‎pkg/nnbd_migration/lib/src/edge_builder.dart‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/dart/element/type.dart';
1010
import 'package:analyzer/src/dart/element/handle.dart';
1111
import 'package:analyzer/src/dart/element/inheritance_manager2.dart';
1212
import 'package:analyzer/src/dart/element/member.dart';
13+
import 'package:analyzer/src/dart/element/type.dart';
1314
import 'package:analyzer/src/generated/resolver.dart';
1415
import 'package:analyzer/src/generated/source.dart';
1516
import 'package:meta/meta.dart';
@@ -132,7 +133,8 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType> {
132133
} else {
133134
assert(baseElement.isSetter);
134135
decoratedBaseType = DecoratedType(baseElement.type, _graph.never,
135-
positionalParameters: [decoratedElementType]);
136+
positionalParameters: [decoratedElementType],
137+
returnType: DecoratedType(VoidTypeImpl.instance, _graph.always));
136138
}
137139
} else {
138140
decoratedBaseType = _variables.decoratedElementType(baseElement);
@@ -323,8 +325,19 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType> {
323325
assert(_isSimple(thenType)); // TODO(paulberry)
324326
var elseType = node.elseExpression.accept(this);
325327
assert(_isSimple(elseType)); // TODO(paulberry)
328+
329+
DartType staticType = node.staticType;
330+
DecoratedType returnType;
331+
if (staticType is FunctionType) {
332+
DartType functReturnType = staticType.returnType;
333+
returnType = functReturnType.isDynamic || functReturnType.isVoid
334+
// TODO(danrubel): handle LUB for constituent types
335+
? DecoratedType(functReturnType, _graph.always)
336+
: _variables.decoratedElementType(functReturnType.element);
337+
}
326338
var overallType = DecoratedType(
327-
node.staticType, NullabilityNode.forLUB(thenType.node, elseType.node));
339+
staticType, NullabilityNode.forLUB(thenType.node, elseType.node),
340+
returnType: returnType);
328341
_variables.recordDecoratedExpressionType(node, overallType);
329342
return overallType;
330343
}

‎pkg/nnbd_migration/test/edge_builder_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,25 @@ class C {
836836
decoratedTypeAnnotation('int i').node);
837837
}
838838

839+
test_function_assignment() async {
840+
await analyze('''
841+
class C {
842+
void f1(String message) {}
843+
void f2(String message) {}
844+
}
845+
foo(C c, bool flag) {
846+
Function(String message) out = flag ? c.f1 : c.f2;
847+
out('hello');
848+
}
849+
bar() {
850+
foo(C(), true);
851+
foo(C(), false);
852+
}
853+
''');
854+
var type = decoratedTypeAnnotation('Function(String message)');
855+
expect(type.returnType, isNotNull);
856+
}
857+
839858
test_functionDeclaration_expression_body() async {
840859
await analyze('''
841860
int/*1*/ f(int/*2*/ i) => i/*3*/;

0 commit comments

Comments
 (0)