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

Add a metadata to define if warnings should appear or not when "noSuchMethod" is defined #6111

Closed
DartBot opened this issue Oct 20, 2012 · 20 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Oct 20, 2012

This issue was originally filed by @a14n


When "noSuchMethod" is defined, no warning is displayed on an undefined method call.

It would be great if a metadata on "noSuchMethod" could be used to define if a warning should appear or not when method is not defined.

Here is a pattern I'd like to use where I really want a warning on an undefined method :

/// definition of all valid methods
abstract class _Api {
  method1();
  method2();
}

/// all valid method calls are implemented by noSuchMethod and the other ones generate warnings
class Api extends _Api {
  @­warnOnUndefinedMethod
  noSuchMethod(method, args) {
    // implementation for all methods
  }
}

This way, I could declare all valid methods and implement them in a single method.

@dgrove
Copy link
Contributor

dgrove commented Oct 23, 2012

Removed Type-Defect label.
Added Type-Enhancement, Area-Editor, Triaged labels.

@danrubel
Copy link

Added this to the M3 milestone.

@clayberg
Copy link

We have a preference that will turn these warnings on and off globally at the moment. Doing it on a case-by-case basis as suggested here might be something we do in the future.


Set owner to @scheglov.
Removed this from the M3 milestone.
Added this to the Later milestone.

@bwilkerson
Copy link
Member

Set owner to @bwilkerson.

@DartBot
Copy link
Author

DartBot commented Jan 4, 2013

This comment was originally written by @a14n


I tried to use that global option, but having a lot of code dealing with js-interop, Dart editor gives me too much warnings to keep that option on. But without that, I cannot check if only declared methods are called.

I would really liked to use such a case-by-case handling. Please, don't wait too much to give us that feature. I hope "Milestone-Later" does not mean "Milestone-Never".

(in the above code snippet, I should have used "implements" instead of "extends")

@bwilkerson
Copy link
Member

Set owner to @jwren.
Removed Area-Editor label.
Added Area-Analyzer label.

@justinfagnani
Copy link
Contributor

dart2js is reporting these warnings too, so I think this is more of a language issue.


Added C1 label.

@gbracha
Copy link
Contributor

gbracha commented Jul 25, 2013

FWIW, the latest spec draft specifies the effect that a noSuchMethod declaration has on warnings. In particular, it does NOT sanction any wholesale suppression of warnings on uses of the type. What it does specify is that if a class implements same interface I, and a noSuchMethod declaration is present in the class (not inherited) then no warnings about missing members of I will be given.

@DartBot
Copy link
Author

DartBot commented Jul 25, 2013

This comment was originally written by @a14n


When I wrote this issue I was looking for a solution to control when the warnings should appear.
Sometimes I want to use noSuchMethod as a black hole that handles all the methods and there I don't want warnings but other times I want to use noSuchMethod only to provide an implementation of missing members and I'd like to get warnings on calls to undefined methods.

An alternative solution could be to

  • warn on calls of undefined methods if the class doesn't directly contain NSM (but it's inherited)
  • no warn if NSM is present in the class (not inherited)

What do you think ?

@sigmundch
Copy link
Member

Gilad - I got a question about your clarification above. Will these warnings be omitted also for subclasses of the class that implements I?

To be more concrete, here is an example:

  abstract class I {
    i1();
    i2();
  }

  class A implements I {
    noSuchMethod(i) => 0; // "implements" everything in I
  }

  class B extends A {
    a1() => 1;
  }

I was expecting to have no warnings in this code, but in today's implementation we get warnings saying that B doesn't implement 'i1' and 'i2'.

Another very similar situation where we get these extra warnings is when A is an abstract class (which is what my code looks like):

  abstract class I {
    i1();
    i2();
  }

  abstract class A implements I {
    a1();
    noSuchMethod(i) => 0;
  }

  class B extends A {
    a1() => 1;
  }

@jmesserly
Copy link

eek, we hit this in JS interop package. For an example of terrible of how terrible it is, see this:

https://chromiumcodereview.appspot.com/23095010/diff/1/lib/polymer_element.dart

@jwren
Copy link
Member

jwren commented Aug 16, 2013

Set owner to @gbracha.
Removed Area-Analyzer label.
Added Area-Language label.

@gbracha
Copy link
Contributor

gbracha commented Aug 16, 2013

There is a plan to add an annotation to deal with this situation. It is is temporarily named @­Proxy, but we'll codify it in a standardized way in not too long.

@lrhn
Copy link
Member

lrhn commented Oct 29, 2013

The annotation is currently called "@Proxy". It will soon be in the core library. It should be specified for milestone 8 if possible.


Removed this from the Later milestone.
Added this to the M8 milestone.
Removed Priority-Medium label.
Added Priority-High label.

@justinfagnani
Copy link
Contributor

@Proxy takes care of class A in Siggi's example, but it does not take care of class B. I've had to deal with code where noSuchMethod is present in a base class, but has to be added to a subclass just to quite the warnings.

In js-interop there's a MagicProxy base class that implements noSuchMethod, and it's used like this:

class PersonProxy extends MagicProxy implements Person {
  // A sacrifice to the analyzer gods
  noSuchMethod(i) => super.noSuchMethod(i);
}

Otherwise warnings about not implementing Person haunt us. The particularly ugly part of this is that i\the requirement to workaround the warnings leaks to users of a library that uses nSM in a base class, you can't just deal with it in a library and ease the pain for them, they have to add the boilerplate themselves.

@sethladd
Copy link
Contributor

What Justin said.

@gbracha
Copy link
Contributor

gbracha commented Oct 29, 2013

Sorry, but @­proxy is supposed to take care of B. It is supposed to silence warnings about unimplemented methods, regardles sof noSuchMethod.

class A is taken care of because of the separate rule about noSuchMethod. As Alexandre said, that is sometimes too broad a brush, but in my view, better too few warnings than too many.

I this doesn't work as I say, we should file a bug against the analyzer.

Tentative spec for this is:

Let C be a concrete class that does not declare its own noSuchMethod() method and is not annotated with a metadata declaration of the form @­proxy, where proxy is declared in dart:core. It is a static warning if the implicit interface of C includes an instance member m of type F and C does not declare or inherit a corresponding instance member m of type F ′ such that F ′ <: F .


Added Accepted label.

@justinfagnani
Copy link
Contributor

I'm sorry Gilad, I wasn't specific enough. The base class has a @­proxy annotation, the subclass doesn't. Adding @­proxy is certainly nicer than adding a noSuchMethod() though.

@gbracha
Copy link
Contributor

gbracha commented Oct 29, 2013

I see. Well, you need to stick @­proxy on each class. It does not effect subclasses. I think that is reasonable behavior.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Oct 29, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@munificent
Copy link
Member

The discussion here sort of veered off from the original feature request.

noSuchMethod() can be cumbersome to work with. In general, we've found it adds a lot of pain to our ahead-of-time compilation story, and often doesn't provide as much value as users would like. For many cases, users are moving to code generation solutions instead of noSuchMethod().

Given that, we aren't planning to spend a lot of effort adding more features around noSuchMethod(), at least not any time soon.

@munificent munificent added the closed-not-planned Closed as we don't intend to take action on the reported issue label Dec 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests