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

Side by Side Diff: lib/src/checker/rules.dart

Issue 957013002: Typecheck map and list literals (Closed) Base URL: git@github.com:dart-lang/dart-dev-compiler.git@master
Patch Set: Add flag to reject casts in const contexts Created 5 years, 10 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 library ddc.src.checker.rules; 5 library ddc.src.checker.rules;
6 6
7 import 'package:analyzer/src/generated/ast.dart'; 7 import 'package:analyzer/src/generated/ast.dart';
8 import 'package:analyzer/src/generated/element.dart'; 8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/resolver.dart'; 9 import 'package:analyzer/src/generated/resolver.dart';
10 10
(...skipping 17 matching lines...) Expand all
28 {bool ignoreReturn: false}) => isSubTypeOf(f1, f2); 28 {bool ignoreReturn: false}) => isSubTypeOf(f1, f2);
29 29
30 bool isBoolType(DartType t) => t == provider.boolType; 30 bool isBoolType(DartType t) => t == provider.boolType;
31 bool isDoubleType(DartType t) => t == provider.doubleType; 31 bool isDoubleType(DartType t) => t == provider.doubleType;
32 bool isIntType(DartType t) => t == provider.intType; 32 bool isIntType(DartType t) => t == provider.intType;
33 bool isNumType(DartType t) => t == provider.intType.superclass; 33 bool isNumType(DartType t) => t == provider.intType.superclass;
34 bool isStringType(DartType t) => t == provider.stringType; 34 bool isStringType(DartType t) => t == provider.stringType;
35 bool isNonNullableType(DartType t) => false; 35 bool isNonNullableType(DartType t) => false;
36 bool maybeNonNullableType(DartType t) => false; 36 bool maybeNonNullableType(DartType t) => false;
37 37
38 StaticInfo checkAssignment(Expression expr, DartType t); 38 StaticInfo checkAssignment(Expression expr, DartType t, bool constContext);
39 39
40 DartType getStaticType(Expression expr) => expr.staticType; 40 DartType getStaticType(Expression expr) => expr.staticType;
41 41
42 DartType elementType(Element e); 42 DartType elementType(Element e);
43 43
44 bool isDynamic(DartType t); 44 bool isDynamic(DartType t);
45 bool isDynamicTarget(Expression expr); 45 bool isDynamicTarget(Expression expr);
46 bool isDynamicGet(Expression expr); 46 bool isDynamicGet(Expression expr);
47 bool isDynamicCall(Expression call); 47 bool isDynamicCall(Expression call);
48 } 48 }
49 49
50 class DartRules extends TypeRules { 50 class DartRules extends TypeRules {
51 DartRules(TypeProvider provider) : super(provider); 51 DartRules(TypeProvider provider) : super(provider);
52 52
53 bool isSubTypeOf(DartType t1, DartType t2) { 53 bool isSubTypeOf(DartType t1, DartType t2) {
54 return t1.isSubtypeOf(t2); 54 return t1.isSubtypeOf(t2);
55 } 55 }
56 56
57 bool isAssignable(DartType t1, DartType t2) { 57 bool isAssignable(DartType t1, DartType t2) {
58 return t1.isAssignableTo(t2); 58 return t1.isAssignableTo(t2);
59 } 59 }
60 60
61 StaticInfo checkAssignment(Expression expr, DartType toType) { 61 StaticInfo checkAssignment(
62 Expression expr, DartType toType, bool constContext) {
62 final fromType = getStaticType(expr); 63 final fromType = getStaticType(expr);
63 if (!isAssignable(fromType, toType)) { 64 if (!isAssignable(fromType, toType)) {
64 return new StaticTypeError(this, expr, toType); 65 return new StaticTypeError(this, expr, toType);
65 } 66 }
66 return null; 67 return null;
67 } 68 }
68 69
69 DartType elementType(Element e) { 70 DartType elementType(Element e) {
70 return (e as dynamic).type; 71 return (e as dynamic).type;
71 } 72 }
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 // type system, but allowed in the regular dart type system, since these 449 // type system, but allowed in the regular dart type system, since these
449 // are likely to succeed. The canonical example is List<dynamic> and 450 // are likely to succeed. The canonical example is List<dynamic> and
450 // Iterable<T> for some concrete T (e.g. Object). These are unrelated 451 // Iterable<T> for some concrete T (e.g. Object). These are unrelated
451 // in the restricted system, but List<dynamic> <: Iterable<T> in dart. 452 // in the restricted system, but List<dynamic> <: Iterable<T> in dart.
452 if (options.relaxedCasts && fromT.isAssignableTo(toT)) { 453 if (options.relaxedCasts && fromT.isAssignableTo(toT)) {
453 return Coercion.cast(fromT, toT); 454 return Coercion.cast(fromT, toT);
454 } 455 }
455 return Coercion.error(); 456 return Coercion.error();
456 } 457 }
457 458
458 StaticInfo checkAssignment(Expression expr, DartType toT) { 459 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) {
459 final fromT = getStaticType(expr); 460 final fromT = getStaticType(expr);
460 final Coercion c = _coerceTo(fromT, toT, true); 461 final Coercion c = _coerceTo(fromT, toT, true);
462 if (c is Identity) return null;
461 if (c is CoercionError) return new StaticTypeError(this, expr, toT); 463 if (c is CoercionError) return new StaticTypeError(this, expr, toT);
464 if (constContext && !options.allowConstCasts) {
465 return new StaticTypeError(this, expr, toT);
466 }
462 if (c is Cast) return DownCast.create(this, expr, c); 467 if (c is Cast) return DownCast.create(this, expr, c);
463 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); 468 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT);
464 assert(c is Identity); 469 assert(false);
465 return null; 470 return null;
466 } 471 }
467 472
468 DartType elementType(Element e) { 473 DartType elementType(Element e) {
469 return (e as dynamic).type; 474 return (e as dynamic).type;
470 } 475 }
471 476
472 bool isDynamic(DartType t) => options.ignoreTypes || t.isDynamic; 477 bool isDynamic(DartType t) => options.ignoreTypes || t.isDynamic;
473 478
474 /// Returns `true` if the target expression is dynamic. 479 /// Returns `true` if the target expression is dynamic.
(...skipping 12 matching lines...) Expand all
487 /// Returns `true` if the expression is a dynamic function call or method 492 /// Returns `true` if the expression is a dynamic function call or method
488 /// invocation. 493 /// invocation.
489 bool isDynamicCall(Expression call) { 494 bool isDynamicCall(Expression call) {
490 if (options.ignoreTypes) return true; 495 if (options.ignoreTypes) return true;
491 var t = getStaticType(call); 496 var t = getStaticType(call);
492 // TODO(jmesserly): fix handling of types with `call` methods. These are not 497 // TODO(jmesserly): fix handling of types with `call` methods. These are not
493 // FunctionType, but they also aren't dynamic calls. 498 // FunctionType, but they also aren't dynamic calls.
494 return t.isDynamic || t.isDartCoreFunction || t is! FunctionType; 499 return t.isDynamic || t.isDartCoreFunction || t is! FunctionType;
495 } 500 }
496 } 501 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698