OLD | NEW |
---|---|
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 dev_compiler.src.checker.rules; | 5 library dev_compiler.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 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 final r1s = fromT.normalParameterTypes; | 386 final r1s = fromT.normalParameterTypes; |
387 final o1s = fromT.optionalParameterTypes; | 387 final o1s = fromT.optionalParameterTypes; |
388 final n1s = fromT.namedParameterTypes; | 388 final n1s = fromT.namedParameterTypes; |
389 final ret1 = fromT.returnType; | 389 final ret1 = fromT.returnType; |
390 | 390 |
391 final r2s = toT.normalParameterTypes; | 391 final r2s = toT.normalParameterTypes; |
392 final o2s = toT.optionalParameterTypes; | 392 final o2s = toT.optionalParameterTypes; |
393 final n2s = toT.namedParameterTypes; | 393 final n2s = toT.namedParameterTypes; |
394 final ret2 = toT.returnType; | 394 final ret2 = toT.returnType; |
395 | 395 |
396 Coercion ret = _coerceTo(ret1, ret2, true); | 396 Coercion ret = _coerceTo(ret1, ret2, options.wrapClosures); |
397 | 397 |
398 // Reject if one has named and the other has optional | 398 // Reject if one has named and the other has optional |
399 if (n1s.length > 0 && o2s.length > 0) return Coercion.error(); | 399 if (n1s.length > 0 && o2s.length > 0) return Coercion.error(); |
400 if (n2s.length > 0 && o1s.length > 0) return Coercion.error(); | 400 if (n2s.length > 0 && o1s.length > 0) return Coercion.error(); |
401 | 401 |
402 Map<String, Coercion> ns = new Map<String, Coercion>(); | 402 Map<String, Coercion> ns = new Map<String, Coercion>(); |
403 // toT has named parameters | 403 // toT has named parameters |
404 if (n2s.length > 0) { | 404 if (n2s.length > 0) { |
405 // Coerce each named parameter from toT to the expected | 405 // Coerce each named parameter from toT to the expected |
406 // type in fromT (note the contravariance) | 406 // type in fromT (note the contravariance) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 // wrapper coercion is produced using _wrapTo (see above) | 449 // wrapper coercion is produced using _wrapTo (see above) |
450 // Returns the error coercion if the types cannot be coerced | 450 // Returns the error coercion if the types cannot be coerced |
451 // according to our current criteria. | 451 // according to our current criteria. |
452 Coercion _coerceTo(DartType fromT, DartType toT, [bool wrap = false]) { | 452 Coercion _coerceTo(DartType fromT, DartType toT, [bool wrap = false]) { |
453 // We can use anything as void | 453 // We can use anything as void |
454 if (toT.isVoid) return Coercion.identity(toT); | 454 if (toT.isVoid) return Coercion.identity(toT); |
455 | 455 |
456 // fromT <: toT, no coercion needed | 456 // fromT <: toT, no coercion needed |
457 if (isSubTypeOf(fromT, toT)) return Coercion.identity(toT); | 457 if (isSubTypeOf(fromT, toT)) return Coercion.identity(toT); |
458 | 458 |
459 // FIXME(vsm): I don't think we need this any more. | |
Leaf
2015/03/25 21:06:28
Why not?
| |
459 // Downcasting from dynamic to object always succeeds, | 460 // Downcasting from dynamic to object always succeeds, |
460 // no coercion needed. | 461 // no coercion needed. |
461 if (fromT.isDynamic && toT == provider.objectType) { | 462 if (fromT.isDynamic && toT == provider.objectType) { |
462 return Coercion.identity(toT); | 463 return Coercion.identity(toT); |
463 } | 464 } |
464 | 465 |
465 // For now, we always wrap closures. | 466 // For now, we always wrap closures. |
466 if (wrap && fromT is FunctionType && toT is FunctionType) { | 467 if (wrap && fromT is FunctionType && toT is FunctionType) { |
467 return _wrapTo(fromT, toT); | 468 return _wrapTo(fromT, toT); |
468 } | 469 } |
(...skipping 18 matching lines...) Expand all Loading... | |
487 // Iterable<T> for some concrete T (e.g. Object). These are unrelated | 488 // Iterable<T> for some concrete T (e.g. Object). These are unrelated |
488 // in the restricted system, but List<dynamic> <: Iterable<T> in dart. | 489 // in the restricted system, but List<dynamic> <: Iterable<T> in dart. |
489 if (options.relaxedCasts && fromT.isAssignableTo(toT)) { | 490 if (options.relaxedCasts && fromT.isAssignableTo(toT)) { |
490 return Coercion.cast(fromT, toT); | 491 return Coercion.cast(fromT, toT); |
491 } | 492 } |
492 return Coercion.error(); | 493 return Coercion.error(); |
493 } | 494 } |
494 | 495 |
495 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { | 496 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { |
496 final fromT = getStaticType(expr); | 497 final fromT = getStaticType(expr); |
497 final Coercion c = _coerceTo(fromT, toT, true); | 498 final Coercion c = _coerceTo(fromT, toT, options.wrapClosures); |
498 if (c is Identity) return null; | 499 if (c is Identity) return null; |
499 if (c is CoercionError) return new StaticTypeError(this, expr, toT); | 500 if (c is CoercionError) return new StaticTypeError(this, expr, toT); |
500 if (constContext && !options.allowConstCasts) { | 501 if (constContext && !options.allowConstCasts) { |
501 return new StaticTypeError(this, expr, toT); | 502 return new StaticTypeError(this, expr, toT); |
502 } | 503 } |
503 if (c is Cast) return DownCast.create(this, expr, c); | 504 if (c is Cast) return DownCast.create(this, expr, c); |
504 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); | 505 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); |
505 assert(false); | 506 assert(false); |
506 return null; | 507 return null; |
507 } | 508 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
544 if (element is FunctionElement || element is MethodElement) { | 545 if (element is FunctionElement || element is MethodElement) { |
545 // An original declaration. | 546 // An original declaration. |
546 return false; | 547 return false; |
547 } | 548 } |
548 } | 549 } |
549 | 550 |
550 var ft = t as FunctionType; | 551 var ft = t as FunctionType; |
551 return _anyParameterType(ft, (pt) => pt.isDynamic); | 552 return _anyParameterType(ft, (pt) => pt.isDynamic); |
552 } | 553 } |
553 } | 554 } |
OLD | NEW |