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 // Downcasting from dynamic to object always succeeds, | |
460 // no coercion needed. | |
461 if (fromT.isDynamic && toT == provider.objectType) { | |
462 return Coercion.identity(toT); | |
463 } | |
464 | |
465 // For now, we always wrap closures. | 459 // For now, we always wrap closures. |
466 if (wrap && fromT is FunctionType && toT is FunctionType) { | 460 if (wrap && fromT is FunctionType && toT is FunctionType) { |
467 return _wrapTo(fromT, toT); | 461 return _wrapTo(fromT, toT); |
468 } | 462 } |
469 | 463 |
470 // For now, reject conversions between function types and | 464 // For now, reject conversions between function types and |
471 // call method objects. We could choose to allow casts here. | 465 // call method objects. We could choose to allow casts here. |
472 // Wrapping a function type to assign it to a call method | 466 // Wrapping a function type to assign it to a call method |
473 // object will never succeed. Wrapping the other way could | 467 // object will never succeed. Wrapping the other way could |
474 // be allowed. | 468 // be allowed. |
(...skipping 12 matching lines...) Expand all Loading... |
487 // Iterable<T> for some concrete T (e.g. Object). These are unrelated | 481 // Iterable<T> for some concrete T (e.g. Object). These are unrelated |
488 // in the restricted system, but List<dynamic> <: Iterable<T> in dart. | 482 // in the restricted system, but List<dynamic> <: Iterable<T> in dart. |
489 if (options.relaxedCasts && fromT.isAssignableTo(toT)) { | 483 if (options.relaxedCasts && fromT.isAssignableTo(toT)) { |
490 return Coercion.cast(fromT, toT); | 484 return Coercion.cast(fromT, toT); |
491 } | 485 } |
492 return Coercion.error(); | 486 return Coercion.error(); |
493 } | 487 } |
494 | 488 |
495 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { | 489 StaticInfo checkAssignment(Expression expr, DartType toT, bool constContext) { |
496 final fromT = getStaticType(expr); | 490 final fromT = getStaticType(expr); |
497 final Coercion c = _coerceTo(fromT, toT, true); | 491 final Coercion c = _coerceTo(fromT, toT, options.wrapClosures); |
498 if (c is Identity) return null; | 492 if (c is Identity) return null; |
499 if (c is CoercionError) return new StaticTypeError(this, expr, toT); | 493 if (c is CoercionError) return new StaticTypeError(this, expr, toT); |
500 if (constContext && !options.allowConstCasts) { | 494 if (constContext && !options.allowConstCasts) { |
501 return new StaticTypeError(this, expr, toT); | 495 return new StaticTypeError(this, expr, toT); |
502 } | 496 } |
503 if (c is Cast) return DownCast.create(this, expr, c); | 497 if (c is Cast) return DownCast.create(this, expr, c); |
504 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); | 498 if (c is Wrapper) return ClosureWrap.create(this, expr, c, toT); |
505 assert(false); | 499 assert(false); |
506 return null; | 500 return null; |
507 } | 501 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 if (element is FunctionElement || element is MethodElement) { | 538 if (element is FunctionElement || element is MethodElement) { |
545 // An original declaration. | 539 // An original declaration. |
546 return false; | 540 return false; |
547 } | 541 } |
548 } | 542 } |
549 | 543 |
550 var ft = t as FunctionType; | 544 var ft = t as FunctionType; |
551 return _anyParameterType(ft, (pt) => pt.isDynamic); | 545 return _anyParameterType(ft, (pt) => pt.isDynamic); |
552 } | 546 } |
553 } | 547 } |
OLD | NEW |