sdk
sdk copied to clipboard
Analyzer doesn't warn on duplicate extensions from different part files
// 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.
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.
This should actually be a compile-time error, not just a warning.
Agree. It's not "the same extension", it's a different declaration with the same name, and that is a compile-time error.
@fshcheglov I think this is a good issue for you to solve.
https://dart-review.googlesource.com/c/sdk/+/382907