Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Unified Diff: pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart

Issue 2907643003: Correctly infer the return context while compiling a factory constructor. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/front_end/lib/src/fasta/kernel/body_builder.dart ('k') | pkg/front_end/test/fasta/strong.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
diff --git a/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart b/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
index ba00e8dc54ef52149fc20dd93c0598eb665fac4e..b659f7de399f032d15962c983b55dfdade0548af 100644
--- a/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/kernel_shadow_ast.dart
@@ -28,6 +28,27 @@ import 'package:kernel/ast.dart';
import 'package:kernel/frontend/accessors.dart';
import 'package:kernel/type_algebra.dart';
+/// Computes the return type of a factory constructor.
+///
+/// Note that we can't just use `constructor.function.functionType.returnType`,
+/// because that's `dynamic` for factory constructors. TODO(paulberry):
+/// investigate whether this can be changed.
+InterfaceType computeFactoryConstructorReturnType(Procedure constructor) {
+ var returnType = constructor.enclosingClass.thisType;
+ if (constructor.enclosingClass.typeParameters.isNotEmpty) {
+ // target.enclosingClass.typeParameters is not the same as
+ // target.function.functionType.typeParameters, so we have to substitute.
+ returnType = Substitution
+ .fromPairs(
+ constructor.enclosingClass.typeParameters,
+ constructor.function.functionType.typeParameters
+ .map((p) => new TypeParameterType(p))
+ .toList())
+ .substituteType(returnType);
+ }
+ return returnType;
+}
+
List<DartType> getExplicitTypeArguments(Arguments arguments) {
if (arguments is KernelArguments) {
return arguments._hasExplicitTypeArguments ? arguments.types : null;
@@ -356,21 +377,7 @@ class KernelFactoryConstructorInvocation extends StaticInvocation
typeNeeded =
inferrer.listener.constructorInvocationEnter(this, typeContext) ||
typeNeeded;
- var returnType = target.enclosingClass.thisType;
- if (target.enclosingClass.typeParameters.isNotEmpty) {
- // target.enclosingClass.typeParameters is not the same as
- // target.function.functionType.typeParameters, so we have to substitute.
- // TODO(paulberrry): it would be easier if we could just use
- // target.function.functionType.returnType, but that's `dynamic` for
- // factory constructors. Investigate whether this can be changed.
- returnType = Substitution
- .fromPairs(
- target.enclosingClass.typeParameters,
- target.function.functionType.typeParameters
- .map((p) => new TypeParameterType(p))
- .toList())
- .substituteType(returnType);
- }
+ InterfaceType returnType = computeFactoryConstructorReturnType(target);
var inferredType = inferrer.inferInvocation(typeContext, typeNeeded,
fileOffset, target.function.functionType, returnType, arguments);
inferrer.listener.constructorInvocationExit(this, inferredType);
« no previous file with comments | « pkg/front_end/lib/src/fasta/kernel/body_builder.dart ('k') | pkg/front_end/test/fasta/strong.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698