sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Analyzer doesn't warn on duplicate extensions from different part files

Open jellynoone opened this issue 1 year ago • 4 comments
trafficstars

// main.dart
import 'person.dart';

void main(List<String> args) {
  Person().say('Hello');
}
// person.dart
part 'person.helpers.dart';

class Person {}

extension PersonExtension on Person {
  void say(String greeting) {
    print(greeting);
  }
}
// person.helpers.dart
part of 'person.dart';

extension PersonExtension on Person {
  void speak(String mind) {
    print(mind);
  }
}

In this example, the same extension, PersonExtension, is defined in both person.dart and person.helpers.dart files. However, there is no warning. The warning does occur in case both definitions are moved to the same part file.

The call to PersonExtension.say in main.dart is reported by the analyzer as non-existing. However, if we put the same main function in person.dart there is no warning there.

// person.dart
part 'person.helpers.dart';

void main(List<String> args) {
  Person().say('Hello');
}

class Person {}

extension PersonExtension on Person {
  void say(String greeting) {
    print(greeting);
  }
}

This example doesn't produce any analyzer warnings, but running the code fails correctly indicating where the duplicate extensions occur.

jellynoone avatar Aug 26 '24 13:08 jellynoone

Summary: The analyzer fails to detect duplicate extensions defined in separate part files, leading to runtime errors. The analyzer correctly identifies the issue when both extensions are in the same part file, but not when they are in separate part files.

dart-github-bot avatar Aug 26 '24 13:08 dart-github-bot

This should actually be a compile-time error, not just a warning.

eernstg avatar Aug 26 '24 13:08 eernstg

Agree. It's not "the same extension", it's a different declaration with the same name, and that is a compile-time error.

lrhn avatar Aug 26 '24 14:08 lrhn

@fshcheglov I think this is a good issue for you to solve.

scheglov avatar Aug 27 '24 00:08 scheglov

https://dart-review.googlesource.com/c/sdk/+/382907

fshcheglov avatar Sep 01 '24 19:09 fshcheglov