Skip to content

Adding a lint: Extension methods crashed when called on a dynamic object #44270

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

Closed
chickenblood opened this issue Nov 20, 2020 · 10 comments
Closed
Labels
area-tools A meta category for issues that should be addressed by tooling (prefer more concrete areas).

Comments

@chickenblood
Copy link

chickenblood commented Nov 20, 2020

Happens on MacOSX / Linux and reproducible in DartPad.

Dart version: Based on Flutter 1.23.0-18.1.pre Dart SDK 2.10.4

Consider the following code:

class MyClass {
  void foo() => print('foo');
}

extension Bar on MyClass {
  void bar() => print('bar');
}

void main() {
  dynamic val = MyClass();
  val.foo();
  val.bar(); // crashes
}

This compiles fine, but crashes at runtime because the extension method bar() cannot be found. The compiler should either fail with an error, or better still valid code should be generated that invokes the method at runtime.

@dnfield
Copy link
Contributor

dnfield commented Nov 20, 2020

This is by design: https://dart.dev/guides/language/extension-methods#static-types-and-dynamic

I have no idea if or how this could be turned into a compile time error - someone from the language team could better answer that.

@dnfield
Copy link
Contributor

dnfield commented Nov 20, 2020

It could probably at least be a lint.

@dnfield
Copy link
Contributor

dnfield commented Nov 20, 2020

@leafpetersen would know the langauge question, @pq the lint

@leafpetersen
Copy link
Member

For better or worse the contract with dynamic is that the compiler just believes that you know what you're doing. I think this would be a fine candidate for an opt in lint though.

@pq
Copy link
Member

pq commented Nov 20, 2020

Yeah. This seems to fit in a general category of advice that recommends avoiding dynamic almost altogether.

@chickenblood: can you provide some context? Why would you type val dynamic in practice?

@chickenblood
Copy link
Author

I try to avoid dynamic altogether. In this case I was consuming a Flutter API that returns a dynamic type.

https://api.flutter.dev/flutter/widgets/DragTargetMove.html

I did not realize this since most of the callbacks for this class are typed

I plan to file a bug on that API. However, I would still hope that the linter could help us in cases like this. It is often not apparent that an implicit conversion is taking place.

@vsmenon
Copy link
Member

vsmenon commented Nov 20, 2020

There are a couple lints around implicit dynamics. I'm not sure they capture this case. @chickenblood - do you have an example of where the dynamic call is coming from? The dynamic here is the type parameter, so not obvious.

@chickenblood
Copy link
Author

chickenblood commented Nov 23, 2020

Sure. I provided a callback for DragTarget.onMove and in there called details.data.doThing() (doThing() being the extension method).

DragTarget(
    …
    onMove: (details) {
       details.data.doThing();
    }
),

Because details.data is dynamic, the failure occurs.

DragTarget.onAccept and DragTarget.onAcceptWithDetails do not have this problem because the argument is typed.

The lint sounds like a very good idea here (as well as fixing the DragTarget API).

@franklinyow franklinyow added the area-tools A meta category for issues that should be addressed by tooling (prefer more concrete areas). label Dec 16, 2020
@devoncarew
Copy link
Member

@franklinyow - it sounds like the resolution for this issue is for a lint request? If that's the case, I'd update the title to reflect that, and move the issue to the dart-lang/linter repo.

@franklinyow franklinyow changed the title Extension methods crashed when called on a dynamic object Adding a lint: Extension methods crashed when called on a dynamic object Dec 16, 2020
@franklinyow
Copy link
Contributor

Issue moved to dart-lang/linter dart-lang/sdk#58289 via ZenHub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-tools A meta category for issues that should be addressed by tooling (prefer more concrete areas).
Projects
None yet
Development

No branches or pull requests

7 participants