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

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

Issue 2953503002: Only set types of conditional expressions in strong mode. (Closed)
Patch Set: Created 3 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// This file declares a "shadow hierarchy" of concrete classes which extend 5 /// This file declares a "shadow hierarchy" of concrete classes which extend
6 /// the kernel class hierarchy, adding methods and fields needed by the 6 /// the kernel class hierarchy, adding methods and fields needed by the
7 /// BodyBuilder. 7 /// BodyBuilder.
8 /// 8 ///
9 /// Instances of these classes may be created using the factory methods in 9 /// Instances of these classes may be created using the factory methods in
10 /// `ast_factory.dart`. 10 /// `ast_factory.dart`.
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 406 }
407 } 407 }
408 } 408 }
409 409
410 /// Concrete shadow object representing a conditional expression in kernel form. 410 /// Concrete shadow object representing a conditional expression in kernel form.
411 /// Shadow object for [ConditionalExpression]. 411 /// Shadow object for [ConditionalExpression].
412 class KernelConditionalExpression extends ConditionalExpression 412 class KernelConditionalExpression extends ConditionalExpression
413 implements KernelExpression { 413 implements KernelExpression {
414 KernelConditionalExpression( 414 KernelConditionalExpression(
415 Expression condition, Expression then, Expression otherwise) 415 Expression condition, Expression then, Expression otherwise)
416 : super(condition, then, otherwise, const DynamicType()); 416 : super(condition, then, otherwise, null);
417 417
418 @override 418 @override
419 void _collectDependencies(KernelDependencyCollector collector) { 419 void _collectDependencies(KernelDependencyCollector collector) {
420 // Inference dependencies are the union of the inference dependencies of the 420 // Inference dependencies are the union of the inference dependencies of the
421 // two returned sub-expressions. 421 // two returned sub-expressions.
422 collector.collectDependencies(then); 422 collector.collectDependencies(then);
423 collector.collectDependencies(otherwise); 423 collector.collectDependencies(otherwise);
424 } 424 }
425 425
426 @override 426 @override
427 DartType _inferExpression( 427 DartType _inferExpression(
428 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) { 428 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) {
429 typeNeeded = 429 typeNeeded =
430 inferrer.listener.conditionalExpressionEnter(this, typeContext) || 430 inferrer.listener.conditionalExpressionEnter(this, typeContext) ||
431 typeNeeded; 431 typeNeeded;
432 if (!inferrer.isTopLevel) { 432 if (!inferrer.isTopLevel) {
433 inferrer.inferExpression( 433 inferrer.inferExpression(
434 condition, inferrer.coreTypes.boolClass.rawType, false); 434 condition, inferrer.coreTypes.boolClass.rawType, false);
435 } 435 }
436 DartType thenType = inferrer.inferExpression(then, typeContext, true); 436 DartType thenType = inferrer.inferExpression(then, typeContext, true);
437 DartType otherwiseType = 437 DartType otherwiseType =
438 inferrer.inferExpression(otherwise, typeContext, true); 438 inferrer.inferExpression(otherwise, typeContext, true);
439 DartType type = inferrer.typeSchemaEnvironment 439 DartType type = inferrer.typeSchemaEnvironment
440 .getLeastUpperBound(thenType, otherwiseType); 440 .getLeastUpperBound(thenType, otherwiseType);
441 staticType = type; 441 if (inferrer.strongMode) {
442 staticType = type;
443 }
442 var inferredType = typeNeeded ? type : null; 444 var inferredType = typeNeeded ? type : null;
443 inferrer.listener.conditionalExpressionExit(this, inferredType); 445 inferrer.listener.conditionalExpressionExit(this, inferredType);
444 return inferredType; 446 return inferredType;
445 } 447 }
446 } 448 }
447 449
448 /// Shadow object for [ConstructorInvocation]. 450 /// Shadow object for [ConstructorInvocation].
449 class KernelConstructorInvocation extends ConstructorInvocation 451 class KernelConstructorInvocation extends ConstructorInvocation
450 implements KernelExpression { 452 implements KernelExpression {
451 final Member _initialTarget; 453 final Member _initialTarget;
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 collector.recordNotImmediatelyEvident(fileOffset); 934 collector.recordNotImmediatelyEvident(fileOffset);
933 } 935 }
934 936
935 @override 937 @override
936 DartType _inferExpression( 938 DartType _inferExpression(
937 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) { 939 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) {
938 typeNeeded = inferrer.listener.ifNullEnter(this, typeContext) || typeNeeded; 940 typeNeeded = inferrer.listener.ifNullEnter(this, typeContext) || typeNeeded;
939 // To infer `e0 ?? e1` in context K: 941 // To infer `e0 ?? e1` in context K:
940 // - Infer e0 in context K to get T0 942 // - Infer e0 in context K to get T0
941 var lhsType = inferrer.inferExpression(_lhs, typeContext, true); 943 var lhsType = inferrer.inferExpression(_lhs, typeContext, true);
942 variable.type = lhsType; 944 if (inferrer.strongMode) {
945 variable.type = lhsType;
946 }
943 // - Let J = T0 if K is `_` else K. 947 // - Let J = T0 if K is `_` else K.
944 var rhsContext = typeContext ?? lhsType; 948 var rhsContext = typeContext ?? lhsType;
945 // - Infer e1 in context J to get T1 949 // - Infer e1 in context J to get T1
946 var rhsType = 950 var rhsType =
947 inferrer.inferExpression(_rhs, rhsContext, typeContext == null); 951 inferrer.inferExpression(_rhs, rhsContext, typeContext == null);
948 // - Let T = greatest closure of K with respect to `?` if K is not `_`, else 952 // - Let T = greatest closure of K with respect to `?` if K is not `_`, else
949 // UP(t0, t1) 953 // UP(t0, t1)
950 // - Then the inferred type is T. 954 // - Then the inferred type is T.
951 var inferredType = typeContext == null 955 var inferredType = typeContext == null
952 ? inferrer.typeSchemaEnvironment.getLeastUpperBound(lhsType, rhsType) 956 ? inferrer.typeSchemaEnvironment.getLeastUpperBound(lhsType, rhsType)
953 : greatestClosure(inferrer.coreTypes, typeContext); 957 : greatestClosure(inferrer.coreTypes, typeContext);
954 body.staticType = inferredType; 958 if (inferrer.strongMode) {
959 body.staticType = inferredType;
960 }
955 inferrer.listener.ifNullExit(this, inferredType); 961 inferrer.listener.ifNullExit(this, inferredType);
956 return inferredType; 962 return inferredType;
957 } 963 }
958 } 964 }
959 965
960 /// Concrete shadow object representing an if statement in kernel form. 966 /// Concrete shadow object representing an if statement in kernel form.
961 class KernelIfStatement extends IfStatement implements KernelStatement { 967 class KernelIfStatement extends IfStatement implements KernelStatement {
962 KernelIfStatement(Expression condition, Statement then, Statement otherwise) 968 KernelIfStatement(Expression condition, Statement then, Statement otherwise)
963 : super(condition, then, otherwise); 969 : super(condition, then, otherwise);
964 970
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
1390 @override 1396 @override
1391 DartType _inferExpression( 1397 DartType _inferExpression(
1392 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) { 1398 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) {
1393 var inferredType = inferrer.inferMethodInvocation( 1399 var inferredType = inferrer.inferMethodInvocation(
1394 this, 1400 this,
1395 variable.initializer, 1401 variable.initializer,
1396 fileOffset, 1402 fileOffset,
1397 _desugaredInvocation, 1403 _desugaredInvocation,
1398 false, 1404 false,
1399 typeContext, 1405 typeContext,
1400 true, 1406 typeNeeded || inferrer.strongMode,
1401 receiverVariable: variable); 1407 receiverVariable: variable);
1402 body.staticType = inferredType; 1408 if (inferrer.strongMode) {
1409 body.staticType = inferredType;
1410 }
1403 return inferredType; 1411 return inferredType;
1404 } 1412 }
1405 } 1413 }
1406 1414
1407 /// Concrete shadow object representing a null-aware read from a property. 1415 /// Concrete shadow object representing a null-aware read from a property.
1408 /// 1416 ///
1409 /// A null-aware property get of the form `a?.b` is represented as the kernel 1417 /// A null-aware property get of the form `a?.b` is represented as the kernel
1410 /// expression: 1418 /// expression:
1411 /// 1419 ///
1412 /// let v = a in v == null ? null : v.b 1420 /// let v = a in v == null ? null : v.b
1413 class KernelNullAwarePropertyGet extends Let implements KernelExpression { 1421 class KernelNullAwarePropertyGet extends Let implements KernelExpression {
1414 KernelNullAwarePropertyGet( 1422 KernelNullAwarePropertyGet(
1415 VariableDeclaration variable, ConditionalExpression body) 1423 VariableDeclaration variable, ConditionalExpression body)
1416 : super(variable, body); 1424 : super(variable, body);
1417 1425
1418 @override 1426 @override
1419 ConditionalExpression get body => super.body; 1427 ConditionalExpression get body => super.body;
1420 1428
1421 PropertyGet get _desugaredGet => body.otherwise; 1429 PropertyGet get _desugaredGet => body.otherwise;
1422 1430
1423 @override 1431 @override
1424 void _collectDependencies(KernelDependencyCollector collector) { 1432 void _collectDependencies(KernelDependencyCollector collector) {
1425 // Null aware expressions are not immediately evident. 1433 // Null aware expressions are not immediately evident.
1426 collector.recordNotImmediatelyEvident(fileOffset); 1434 collector.recordNotImmediatelyEvident(fileOffset);
1427 } 1435 }
1428 1436
1429 @override 1437 @override
1430 DartType _inferExpression( 1438 DartType _inferExpression(
1431 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) { 1439 KernelTypeInferrer inferrer, DartType typeContext, bool typeNeeded) {
1432 var inferredType = inferrer.inferPropertyGet(this, variable.initializer, 1440 var inferredType = inferrer.inferPropertyGet(
1433 fileOffset, _desugaredGet, typeContext, true, 1441 this,
1442 variable.initializer,
1443 fileOffset,
1444 _desugaredGet,
1445 typeContext,
1446 typeNeeded || inferrer.strongMode,
1434 receiverVariable: variable); 1447 receiverVariable: variable);
1435 body.staticType = inferredType; 1448 if (inferrer.strongMode) {
1449 body.staticType = inferredType;
1450 }
1436 return inferredType; 1451 return inferredType;
1437 } 1452 }
1438 } 1453 }
1439 1454
1440 /// Concrete shadow object representing a null literal in kernel form. 1455 /// Concrete shadow object representing a null literal in kernel form.
1441 class KernelNullLiteral extends NullLiteral implements KernelExpression { 1456 class KernelNullLiteral extends NullLiteral implements KernelExpression {
1442 @override 1457 @override
1443 void _collectDependencies(KernelDependencyCollector collector) { 1458 void _collectDependencies(KernelDependencyCollector collector) {
1444 // No inference dependencies. 1459 // No inference dependencies.
1445 } 1460 }
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after
2265 } 2280 }
2266 2281
2267 transformChildren(v) { 2282 transformChildren(v) {
2268 return internalError("Internal error: Unsupported operation."); 2283 return internalError("Internal error: Unsupported operation.");
2269 } 2284 }
2270 2285
2271 visitChildren(v) { 2286 visitChildren(v) {
2272 return internalError("Internal error: Unsupported operation."); 2287 return internalError("Internal error: Unsupported operation.");
2273 } 2288 }
2274 } 2289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698