Skip to content

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

@franklinyow

Description

@franklinyow

@chickenblood commented on Fri 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 commented on Fri 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 commented on Fri Nov 20 2020

It could probably at least be a lint.


@dnfield commented on Fri Nov 20 2020

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


@leafpetersen commented on Fri Nov 20 2020

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 commented on Fri 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 commented on Fri Nov 20 2020

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 commented on Fri 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 commented on Mon 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).


@devoncarew commented on Wed Dec 16 2020

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions