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

Side by Side Diff: frog/member.dart

Issue 9264069: Improve frog error reporting when providing too few or too many arguments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/language/src/PrivateFactoryResolutionNegativeTest.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 /** A formal parameter to a [Method]. */ 5 /** A formal parameter to a [Method]. */
6 class Parameter { 6 class Parameter {
7 FormalNode definition; 7 FormalNode definition;
8 Member method; 8 Member method;
9 9
10 String name; 10 String name;
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 var arg = args.getValue(parameters[i].name); 887 var arg = args.getValue(parameters[i].name);
888 if (arg != null && arg.needsConversion(parameters[i].type)) { 888 if (arg != null && arg.needsConversion(parameters[i].type)) {
889 return true; 889 return true;
890 } 890 }
891 } 891 }
892 } 892 }
893 893
894 return false; 894 return false;
895 } 895 }
896 896
897 static String _argCountMsg(int actual, int expected, [bool atLeast=false]) { 897 /** Returns true if any of the parameters are optional. */
898 return 'wrong number of positional arguments, expected ' + 898 bool hasOptionalParameters() {
899 '${atLeast ? "at least " : ""}$expected but found $actual'; 899 return parameters.some((Parameter p) => p.isOptional);
900 }
901
902 String _tooManyArgumentsMsg(int actual, int expected) {
903 return hasOptionalParameters()
904 ? 'too many arguments, expected at most $expected but found $actual'
905 : _wrongArgumentCountMsg(actual, expected);
906 }
907
908 String _tooFewArgumentsMsg(int actual, int expected) {
909 return hasOptionalParameters()
910 ? 'too few arguments, expected at least $expected but found $actual'
911 : _wrongArgumentCountMsg(actual, expected);
912 }
913
914 String _wrongArgumentCountMsg(int actual, int expected) {
915 return 'wrong number of arguments, expected $expected but found $actual';
900 } 916 }
901 917
902 Value _argError(CallingContext context, Node node, Value target, 918 Value _argError(CallingContext context, Node node, Value target,
903 Arguments args, String msg, int argIndex) { 919 Arguments args, String msg, int argIndex) {
904 if (context.showWarnings) { 920 if (context.showWarnings) {
905 SourceSpan span; 921 SourceSpan span;
906 if ((args.nodes == null) || (argIndex >= args.nodes.length)) { 922 if ((args.nodes == null) || (argIndex >= args.nodes.length)) {
907 span = node.span; 923 span = node.span;
908 } else { 924 } else {
909 span = args.nodes[argIndex].span; 925 span = args.nodes[argIndex].span;
(...skipping 19 matching lines...) Expand all
929 * [node] provides a [SourceSpan] for any error messages. 945 * [node] provides a [SourceSpan] for any error messages.
930 */ 946 */
931 Value invoke(CallingContext context, Node node, Value target, 947 Value invoke(CallingContext context, Node node, Value target,
932 Arguments args) { 948 Arguments args) {
933 949
934 var argValues = <Value>[]; 950 var argValues = <Value>[];
935 int bareCount = args.bareCount; 951 int bareCount = args.bareCount;
936 for (int i = 0; i < bareCount; i++) { 952 for (int i = 0; i < bareCount; i++) {
937 var arg = args.values[i]; 953 var arg = args.values[i];
938 if (i >= parameters.length) { 954 if (i >= parameters.length) {
939 var msg = _argCountMsg(args.length, parameters.length); 955 var msg = _tooManyArgumentsMsg(args.length, parameters.length);
940 return _argError(context, node, target, args, msg, i); 956 return _argError(context, node, target, args, msg, i);
941 } 957 }
942 argValues.add(arg.convertTo(context, parameters[i].type)); 958 argValues.add(arg.convertTo(context, parameters[i].type));
943 } 959 }
944 960
945 int namedArgsUsed = 0; 961 int namedArgsUsed = 0;
946 if (bareCount < parameters.length) { 962 if (bareCount < parameters.length) {
947 genParameterValues(context); 963 genParameterValues(context);
948 964
949 for (int i = bareCount; i < parameters.length; i++) { 965 for (int i = bareCount; i < parameters.length; i++) {
950 var param = parameters[i]; 966 var param = parameters[i];
951 var arg = args.getValue(param.name); 967 var arg = args.getValue(param.name);
952 if (arg == null) { 968 if (arg == null) {
953 arg = param.value; 969 arg = param.value;
954 if (arg == null) { 970 if (arg == null) {
955 // TODO(jmesserly): should we be use the actual constant value here? 971 // TODO(jmesserly): should we be use the actual constant value here?
956 arg = new PureStaticValue(param.type, param.definition.span, true); 972 arg = new PureStaticValue(param.type, param.definition.span, true);
957 } 973 }
958 } else { 974 } else {
959 arg = arg.convertTo(context, parameters[i].type); 975 arg = arg.convertTo(context, parameters[i].type);
960 namedArgsUsed++; 976 namedArgsUsed++;
961 } 977 }
962 978
963 if (arg == null || !parameters[i].isOptional) { 979 if (arg == null || !parameters[i].isOptional) {
964 var msg = _argCountMsg(Math.min(i, args.length), i + 1, atLeast:true); 980 var msg = _tooFewArgumentsMsg(Math.min(i, args.length), i + 1);
965 return _argError(context, node, target, args, msg, i); 981 return _argError(context, node, target, args, msg, i);
966 } else { 982 } else {
967 argValues.add(arg); 983 argValues.add(arg);
968 } 984 }
969 } 985 }
970 } 986 }
971 987
972 if (namedArgsUsed < args.nameCount) { 988 if (namedArgsUsed < args.nameCount) {
973 // Find the unused argument name 989 // Find the unused argument name
974 var seen = new Set<String>(); 990 var seen = new Set<String>();
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
1355 } 1371 }
1356 1372
1357 void forEach(void f(Member member)) { 1373 void forEach(void f(Member member)) {
1358 factories.forEach((_, Map constructors) { 1374 factories.forEach((_, Map constructors) {
1359 constructors.forEach((_, Member member) { 1375 constructors.forEach((_, Member member) {
1360 f(member); 1376 f(member);
1361 }); 1377 });
1362 }); 1378 });
1363 } 1379 }
1364 } 1380 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/src/PrivateFactoryResolutionNegativeTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698