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

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

Issue 1021273004: Allow arity checks on dynamic function types (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « lib/src/checker/checker.dart ('k') | test/checker/checker_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 var bound = t.element.bound; 126 var bound = t.element.bound;
127 if (bound == null) { 127 if (bound == null) {
128 bound = provider.dynamicType; 128 bound = provider.dynamicType;
129 } 129 }
130 return _nonnullableTypes.any((DartType p) => isSubTypeOf(p, bound)); 130 return _nonnullableTypes.any((DartType p) => isSubTypeOf(p, bound));
131 } else { 131 } else {
132 return false; 132 return false;
133 } 133 }
134 } 134 }
135 135
136 bool _anyParameterType(FunctionType ft, bool predicate(DartType t)) {
137 for (var parameterType in ft.normalParameterTypes) {
Jacob 2015/03/20 16:41:52 I know this code just moved around but it can be r
vsm 2015/03/20 17:23:11 Done.
138 if (predicate(parameterType)) return true;
139 }
140 for (var parameterType in ft.optionalParameterTypes) {
141 if (predicate(parameterType)) return true;
142 }
143 for (var parameterType in ft.namedParameterTypes.values) {
144 if (predicate(parameterType)) return true;
145 }
146 return false;
147 }
148
136 // TODO(leafp): Revisit this. 149 // TODO(leafp): Revisit this.
137 bool isGroundType(DartType t) { 150 bool isGroundType(DartType t) {
138 if (t is FunctionType) return false;
139 if (t is TypeParameterType) return false; 151 if (t is TypeParameterType) return false;
140 if (t.isDynamic) return true; 152 if (t.isDynamic) return true;
141 153
142 // t must be an InterfaceType. 154 if (t is FunctionType) {
143 var typeArguments = (t as InterfaceType).typeArguments; 155 if (!t.returnType.isDynamic ||
144 for (var typeArgument in typeArguments) { 156 _anyParameterType(t, (pt) => !pt.isDynamic)) {
145 if (!typeArgument.isDynamic) return false; 157 return false;
158 } else {
159 return true;
160 }
146 } 161 }
147 162
148 return true; 163 if (t is InterfaceType) {
164 var typeArguments = t.typeArguments;
165 for (var typeArgument in typeArguments) {
166 if (!typeArgument.isDynamic) return false;
Leaf 2015/03/20 17:32:01 We could presumably allow object here as well?
vsm 2015/03/20 17:39:28 Yes, done. Perhaps we should unify Object and dyn
167 }
168 return true;
169 }
170
171 throw new StateError("Unexpected type");
149 } 172 }
150 173
151 FunctionType getCallMethodType(DartType t) { 174 FunctionType getCallMethodType(DartType t) {
152 if (t is InterfaceType) { 175 if (t is InterfaceType) {
153 ClassElement element = t.element; 176 ClassElement element = t.element;
154 InheritanceManager manager = new InheritanceManager(element.library); 177 InheritanceManager manager = new InheritanceManager(element.library);
155 FunctionType callType = manager.lookupMemberType(t, "call"); 178 FunctionType callType = manager.lookupMemberType(t, "call");
156 return callType; 179 return callType;
157 } 180 }
158 return null; 181 return null;
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 // a dynamic parameter type requires a dynamic call in general. 526 // a dynamic parameter type requires a dynamic call in general.
504 // However, as an optimization, if we have an original definition, we know 527 // However, as an optimization, if we have an original definition, we know
505 // dynamic is reified as Object - in this case a regular call is fine. 528 // dynamic is reified as Object - in this case a regular call is fine.
506 if (call is SimpleIdentifier) { 529 if (call is SimpleIdentifier) {
507 var element = call.staticElement; 530 var element = call.staticElement;
508 if (element is FunctionElement || element is MethodElement) { 531 if (element is FunctionElement || element is MethodElement) {
509 // An original declaration. 532 // An original declaration.
510 return false; 533 return false;
511 } 534 }
512 } 535 }
536
513 var ft = t as FunctionType; 537 var ft = t as FunctionType;
514 for (var parameterType in ft.normalParameterTypes) { 538 if (_anyParameterType(ft, (pt) => pt.isDynamic)) return true;
Jacob 2015/03/20 16:41:52 nit... replace with return _anyParameterType(ft, (
vsm 2015/03/20 17:23:12 Done.
515 if (parameterType.isDynamic) return true;
516 }
517 for (var parameterType in ft.optionalParameterTypes) {
518 if (parameterType.isDynamic) return true;
519 }
520 for (var parameterType in ft.namedParameterTypes.values) {
521 if (parameterType.isDynamic) return true;
522 }
523 return false; 539 return false;
524 } 540 }
525 } 541 }
OLDNEW
« no previous file with comments | « lib/src/checker/checker.dart ('k') | test/checker/checker_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698