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

Unified Diff: runtime/vm/object.cc

Issue 10695137: Provide better error message when passing wrong number of arguments. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/parser.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/object.cc
===================================================================
--- runtime/vm/object.cc (revision 9601)
+++ runtime/vm/object.cc (working copy)
@@ -3690,26 +3690,71 @@
}
+intptr_t Function::NumberOfImplicitParameters() const {
+ if (kind() == RawFunction::kConstructor) {
+ if (is_static()) {
+ ASSERT(IsFactory());
+ return 1; // Type arguments.
+ } else {
+ ASSERT(IsConstructor());
+ return 2; // Instance, phase.
+ }
+ }
+ if (!is_static()) {
+ return 1; // Receiver.
+ }
+ return 0; // No implicit parameters.
+}
+
+
bool Function::AreValidArgumentCounts(int num_arguments,
- int num_named_arguments) const {
+ int num_named_arguments,
+ String* error_message) const {
if (num_arguments > NumberOfParameters()) {
+ if (error_message != NULL) {
+ const intptr_t kMessageBufferSize = 64;
+ char message_buffer[kMessageBufferSize];
+ // Hide implicit parameters to the user.
+ const intptr_t num_hidden_params = NumberOfImplicitParameters();
+ OS::SNPrint(message_buffer,
+ kMessageBufferSize,
+ "%d passed, %s%d expected",
+ num_arguments - num_hidden_params,
+ num_optional_parameters() > 0 ? "at most " : "",
+ NumberOfParameters() - num_hidden_params);
+ *error_message = String::New(message_buffer);
+ }
return false; // Too many arguments.
}
const int num_positional_args = num_arguments - num_named_arguments;
if (num_positional_args < num_fixed_parameters()) {
+ if (error_message != NULL) {
+ const intptr_t kMessageBufferSize = 64;
+ char message_buffer[kMessageBufferSize];
+ // Hide implicit parameters to the user.
+ const intptr_t num_hidden_params = NumberOfImplicitParameters();
+ OS::SNPrint(message_buffer,
+ kMessageBufferSize,
+ "%d %spassed, %d expected",
+ num_positional_args - num_hidden_params,
+ num_optional_parameters() > 0 ? "positional " : "",
+ num_fixed_parameters() - num_hidden_params);
+ *error_message = String::New(message_buffer);
+ }
return false; // Too few arguments.
}
return true;
}
-// TODO(regis): Return some sort of diagnosis information so that we
-// can improve the error messages generated by the parser.
bool Function::AreValidArguments(int num_arguments,
- const Array& argument_names) const {
+ const Array& argument_names,
+ String* error_message) const {
const int num_named_arguments =
argument_names.IsNull() ? 0 : argument_names.Length();
- if (!AreValidArgumentCounts(num_arguments, num_named_arguments)) {
+ if (!AreValidArgumentCounts(num_arguments,
+ num_named_arguments,
+ error_message)) {
return false;
}
// Verify that all argument names are valid parameter names.
@@ -3729,6 +3774,15 @@
}
}
if (!found) {
+ if (error_message != NULL) {
+ const intptr_t kMessageBufferSize = 64;
+ char message_buffer[kMessageBufferSize];
+ OS::SNPrint(message_buffer,
+ kMessageBufferSize,
+ "no optional formal parameter named '%s'",
+ argument_name.ToCString());
+ *error_message = String::New(message_buffer);
+ }
return false;
}
}
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698