Skip to content

Class implementing classes of another packages are not warn if there is a private method #61916

@ValentinVignal

Description

@ValentinVignal

I'm not sure whether this is a valid bug, but consider this:

// a.dart class A { void _privateMethod() {} void method() {} } void doSomethingOnA(A a) { a.method(); a._privateMethod(); } 
// main.dart import 'a.dart'; class B implements A { @override void method() { // Implementation of method } } void main() { final a = A(); doSomethingOnA(a); final b = B(); doSomethingOnA(b); runApp(const MyApp()); }

There is no lints or compilation errors, but at run time:

Tried calling: _privateMethod() #0 B._privateMethod (package:my_app/b.dart:3:7) #1 doSomethingOnA (package:my_app/a.dart:9:5) #2 main (package:my_app/main.dart:9:3) #3 _runMain.<anonymous closure> (dart:ui/hooks.dart:319:23) #4 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:314:19) #5 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:193:12) 

And this doesn't happen if I define B in a.dart:

// a.dart class A { void _privateMethod() {} void method() {} } class B implements A { // <- lint here: Missing concrete implementation of 'class A._privateMethod'. Try implementing the missing method, or make the class  @override void method() { // Implementation of method } } void doSomethingOnA(A a) { a.method(); a._privateMethod(); }
Analyzing a.dart... 0.3s error • a.dart:7:7 • Missing concrete implementation of 'class A._privateMethod'. Try implementing the missing method, or make the class abstract. • non_abstract_class_inherits_abstract_member 1 issue found. 

Is there something we can do to avoid the runtime issue described?
Some ideas:

  • Have the lint in main.dart when B implements A
  • Force a class modifier (final, sealed) when A has private method used in public APIs (doSomethingWithA)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions