Skip to content

Commit 1582877

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Extension type. Issue 53920. Report conflict with inherited setter, even when there is getter.
Bug: #53920 Change-Id: Ib2d7715a45b4696ce56c200cbbadc60ee8ab3b81 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334640 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 933537b commit 1582877

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

+37-13
Original file line numberDiff line numberDiff line change
@@ -2009,31 +2009,55 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
20092009
for (MethodElement method in enclosingClass.methods) {
20102010
String name = method.name;
20112011

2012-
// find inherited property accessor
2013-
var inherited = _inheritanceManager.getInherited2(
2012+
// find inherited property accessors
2013+
final getter = _inheritanceManager.getInherited2(
20142014
enclosingClass, Name(libraryUri, name));
2015-
inherited ??= _inheritanceManager.getInherited2(
2015+
final setter = _inheritanceManager.getInherited2(
20162016
enclosingClass, Name(libraryUri, '$name='));
20172017

2018-
if (method.isStatic && inherited != null) {
2019-
errorReporter.reportErrorForElement(
2020-
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE, method, [
2021-
enclosingClass.displayName,
2022-
name,
2023-
inherited.enclosingElement.displayName,
2024-
]);
2025-
} else if (inherited is PropertyAccessorElement) {
2026-
// Extension type methods redeclare getters with the same name.
2027-
if (enclosingClass is ExtensionTypeElement && inherited.isGetter) {
2018+
if (method.isStatic) {
2019+
void reportStaticConflict(ExecutableElement inherited) {
2020+
errorReporter.reportErrorForElement(
2021+
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE, method, [
2022+
enclosingClass.displayName,
2023+
name,
2024+
inherited.enclosingElement.displayName,
2025+
]);
2026+
}
2027+
2028+
if (getter != null) {
2029+
reportStaticConflict(getter);
20282030
continue;
20292031
}
2032+
2033+
if (setter != null) {
2034+
reportStaticConflict(setter);
2035+
continue;
2036+
}
2037+
}
2038+
2039+
void reportFieldConflict(PropertyAccessorElement inherited) {
20302040
errorReporter.reportErrorForElement(
20312041
CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, method, [
20322042
enclosingClass.displayName,
20332043
name,
20342044
inherited.enclosingElement.displayName
20352045
]);
20362046
}
2047+
2048+
if (getter is PropertyAccessorElement) {
2049+
if (enclosingClass is ExtensionTypeElement) {
2050+
// Extension type methods redeclare getters with the same name.
2051+
} else {
2052+
reportFieldConflict(getter);
2053+
continue;
2054+
}
2055+
}
2056+
2057+
if (setter is PropertyAccessorElement) {
2058+
reportFieldConflict(setter);
2059+
continue;
2060+
}
20372061
}
20382062

20392063
// getter declared in the enclosing class vs. inherited method

pkg/analyzer/test/src/diagnostics/conflicting_method_and_field_test.dart

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ enum E with M {
8484
]);
8585
}
8686

87+
test_extensionType_field_external() async {
88+
await assertErrorsInCode(r'''
89+
extension type A(int it) {
90+
external int foo;
91+
}
92+
93+
extension type B(int it) implements A {
94+
void foo() {}
95+
}
96+
''', [
97+
error(CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, 97, 3),
98+
]);
99+
}
100+
87101
test_extensionType_getter() async {
88102
await assertNoErrorsInCode(r'''
89103
extension type A(int it) {

0 commit comments

Comments
 (0)