dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.04k stars 1.55k forks source link

Improve error message for redirecting constructors #3265

Open DartBot opened 12 years ago

DartBot commented 12 years ago

This issue was originally filed by schwa...@gmail.com


REPRODUCING STEPS

class Foo {   var a,b;      Foo(this.a, this.b);   Foo.yo(var aa, var bb) { a = aa; b = bb; print("yo!!"); }       // Both of these result in static errors.   Foo.redirect1(var aa, var bb) : this.Foo(aa, bb);   Foo.redirect2(var aa, var bb) : this.Foo.yo(aa, bb); }

EXPECTED / OBSERVED OUTPUT I don't expect any static errors in this code.

DART / OS VERSIONS Dart Editor build 7905, OS X 10.7.4

DartBot commented 12 years ago

This comment was originally written by sc...@gmail.com


Oops, I misread the spec. There is no bug. Foo should be defined as:

class Foo {   var a,b;      Foo(this.a, this.b);   Foo.yo(var aa, var bb) { a = aa; b = bb; print("yo!!"); }       // Both of these result in static errors.   Foo.redirect1(var aa, var bb) : this(aa, bb);   Foo.redirect2(var aa, var bb) : this.yo(aa, bb); }

kasperl commented 12 years ago

Added Invalid label.

peter-ahe-google commented 12 years ago

dart2js produce these error messages:

fisk.dart:8:34: error: cannot resolve constructor Foo.Foo  Foo.redirect1(var aa, var bb) : this.Foo(aa, bb);                                  ^^^^^^^^^^^^^^^^ Error: Compilation failed.

fisk.dart:9:34: error: only call to 'this' or 'super' constructor allowed  Foo.redirect2(var aa, var bb) : this.Foo.yo(aa, bb);                                  ^^^^^^^^^^^^^^^^^^^ Error: Compilation failed.

Are these messages good enough?

DartBot commented 12 years ago

This comment was originally written by sch...@gmail.com


The messages were unhelpful; to resolve my problem I had to resort to web-search. My responses to the two messages:

"Cannot resolve". "Why not?"

"Can only call 'this' or 'super' constructor" "I thought that's what I was doing"

Ideally, the message would ask "did you mean 'this.yo(aa, bb)'? "

BTW, as others have said on the list, it's very laudable that you're taking such an interest in the developer UX. Thank you! It will be difficult to quantify the benefits, but in the long term it will make the web a much nicer platform.

DartBot commented 12 years ago

This comment was originally written by sc...@gmail.com


Clarification... along with the current messages, it would be great if the compiler additionally asked "did you mean..." in these cases.

peter-ahe-google commented 12 years ago

I'm reopening this to track improving the error message.


Removed Type-Defect, Priority-Medium labels. Added Type-Enhancement, Priority-Low, Area-Dart2JS, Triaged labels.

kasperl commented 12 years ago

Changed the title to: "Improve error message for redirecting constructors".

kasperl commented 11 years ago

Added this to the Later milestone.

kasperl commented 11 years ago

Removed this from the Later milestone.

kasperl commented 11 years ago

Added this to the M2 milestone. Removed Priority-Low label. Added Priority-Medium label.

kasperl commented 11 years ago

Removed Type-Enhancement label. Added Type-Defect label.

peter-ahe-google commented 11 years ago

Removed Priority-Medium label. Added Priority-Low label.

peter-ahe-google commented 11 years ago

Removed this from the M2 milestone. Added this to the Later milestone. Removed Priority-Low label. Added Priority-Medium label.

kasperl commented 11 years ago

Added TriageForM5 label.

kasperl commented 11 years ago

Removed TriageForM5 label.

kasperl commented 10 years ago

Removed this from the Later milestone. Added Oldschool-Milestone-Later label.

kasperl commented 10 years ago

Removed Oldschool-Milestone-Later label.

srawlins commented 10 years ago

Today dart2js successfully compiles both the original script in this issue, and the script in #­1, without any warnings or errors. This can be closed.

johnniwinther commented 10 years ago

The messages still need improvement.

johnniwinther commented 2 years ago

The current response has improved slightly:

issue3265.dart:12:35: Error: Expected an assignment after the field name.
To initialize a field, use the syntax 'name = value'.
  Foo.redirect2(var aa, var bb) : this.Foo.yo(aa, bb);
                                  ^^^^
issue3265.dart:11:35: Error: Couldn't find constructor 'Foo.Foo'.
  Foo.redirect1(var aa, var bb) : this.Foo(aa, bb);
                                  ^^^^
issue3265.dart:12:35: Error: Can't access 'this' in a field initializer.
  Foo.redirect2(var aa, var bb) : this.Foo.yo(aa, bb);
                                  ^^^^