Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchMethodError.toString should handle _Type.call() case #27651

Closed
Hixie opened this issue Oct 22, 2016 · 4 comments
Closed

NoSuchMethodError.toString should handle _Type.call() case #27651

Hixie opened this issue Oct 22, 2016 · 4 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core

Comments

@Hixie
Copy link
Contributor

Hixie commented Oct 22, 2016

...or maybe _Type.call() itself should be implemented and throw a NoSuchMethodError with an appropriate message.

This situation occurs when you have code that omits the "new" in a call like this:

  var x = Timer();

Right now it says:

NoSuchMethodError: Class '_Type' has no instance method 'call'.
Receiver: Type: class 'Timer'
Tried calling: call()

This is highly unclear and we've seen experienced Dart engineers in usability studies get mildly confused by it. We should instead say something like:

NoSuchMethodError: Attempted to use type 'Timer' as a function. Since types do not define
a method 'call', this is not possible. Did you intend to call the Timer constructor and
forget the 'new' operator?
Receiver: Type: class 'Timer'
Tried calling: Timer()

See also #27571.

@vsmenon vsmenon added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core labels Oct 24, 2016
@vsmenon
Copy link
Member

vsmenon commented Oct 24, 2016

BTW, this should always be a static error (and a static error during AOT compilation, not a runtime one). Are you not seeing a static error?

@lrhn
Copy link
Member

lrhn commented Oct 24, 2016

The current Dart specification is a little lacking about this particular case.
The syntax C() is an "unqualified invocation". If C is a class name visible in the current scope, then none of the special cases for an unqualified invocation applies (it's not a prefix, nor any kind of function, getter or variable), so it is equivalent to this.C().
Luckily nobody actually implements that (see, e.g.: https://dartpad.dartlang.org/ae8fe2acde8378d00e90dc4c825af7c5).

The implementations are not treating it correctly as a "function expression invocation" either. If they did, it would be rewritten into C.call() which would call a static function named call on C if it's there. That doesn't happen either.

The implementations instead convert it to (C).call() and then fail when the type object doesn't have a call method. There is nothing in the spec documenting the behavior, but it is pretty reasonable, and we should probably adapt the spec to match - or just make it a compile-time error.

That does mean that the spec doesn't make it a compile-time error yet. It should be a warning (and in dartpad, it is: "C is not a function").

@Hixie
Copy link
Contributor Author

Hixie commented Oct 24, 2016

I totally agree that the analyzer should do a better job of reporting this as well, but this issue is specifically about the exception that is thrown.

@fsc8000
Copy link
Contributor

fsc8000 commented Nov 15, 2016

https://codereview.chromium.org/2507493003/ improves the error message: e.g.

Unhandled exception:
NoSuchMethodError: Attempted to use type 'A' as a function. Since types do not define a method 'call', this is not possible. Did you intend to call the A constructor and forget the 'new' operator?
Receiver: Type: class 'A'
Tried calling: A(1, 2, 4)

@fsc8000 fsc8000 self-assigned this Nov 16, 2016
lrhn added a commit that referenced this issue Nov 17, 2016
Fixers issue #27651
BUG= http://dartbug.com/27651
R=eernst@google.com, floitsch@google.com, fschneider@google.com

Review URL: https://codereview.chromium.org/2444843002 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core
Projects
None yet
Development

No branches or pull requests

4 participants