Skip to content

Commit

Permalink
Look for both getter and setter in the hierarchy before looking for e…
Browse files Browse the repository at this point in the history
…xtension.

Change-Id: I7080f5ffb307b9deb9bba453e1d1c1aa253b7cc1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/116489
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and commit-bot@chromium.org committed Sep 10, 2019
1 parent 331271d commit fa099eb
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 243 deletions.
366 changes: 147 additions & 219 deletions pkg/analyzer/lib/src/generated/element_resolver.dart

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/driver_resolution.dart';
Expand Down Expand Up @@ -43,3 +45,25 @@ main() {
]);
}
}

@reflectiveTest
class AssignmentToMethodWithExtensionMethodsTest
extends AssignmentToFinalNoSetterTest {
@override
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);

test_instance_undefined_hasGetter() async {
await assertErrorsInCode('''
extension E on int {
int get foo => 0;
}
f() {
0.foo = 1;
}
''', [
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 53, 3),
]);
}
}
50 changes: 50 additions & 0 deletions pkg/analyzer/test/src/diagnostics/assignment_to_method_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/driver_resolution.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(AssignmentToMethodTest);
defineReflectiveTests(AssignmentToMethodWithExtensionMethodsTest);
});
}

Expand All @@ -27,3 +30,50 @@ f(A a) {
]);
}
}

@reflectiveTest
class AssignmentToMethodWithExtensionMethodsTest
extends AssignmentToMethodTest {
@override
AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);

test_instance_extendedHasMethod_extensionHasSetter() async {
await assertErrorsInCode('''
class C {
void foo() {}
}
extension E on C {
void set foo(int _) {}
}
f(C c) {
c.foo = 0;
}
''', [
error(StaticWarningCode.ASSIGNMENT_TO_METHOD, 87, 5),
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 95, 1),
]);
}

test_this_extendedHasMethod_extensionHasSetter() async {
await assertErrorsInCode('''
class C {
void foo() {}
}
extension E on C {
void set foo(int _) {}
f() {
this.foo = 0;
}
}
''', [
error(StaticWarningCode.ASSIGNMENT_TO_METHOD, 86, 8),
error(StaticTypeWarningCode.INVALID_ASSIGNMENT, 97, 1),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class UndefinedExtensionGetterTest extends DriverResolutionTest {
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);

test_instance_defined() async {
test_override_defined() async {
await assertNoErrorsInCode('''
extension E on String {
int get g => 0;
Expand All @@ -33,7 +33,7 @@ f() {
''');
}

test_instance_withoutSetter() async {
test_override_undefined() async {
await assertErrorsInCode('''
extension E on String {}
f() {
Expand All @@ -44,7 +44,7 @@ f() {
]);
}

test_instance_withSetter() async {
test_override_undefined_hasSetter() async {
await assertErrorsInCode('''
extension E on String {
void set s(int x) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,49 @@ class UndefinedExtensionSetterTest extends DriverResolutionTest {
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);

test_instance_defined() async {
test_override_defined() async {
await assertNoErrorsInCode('''
extension E on String {
void set s(int x) {}
extension E on int {
void set foo(int _) {}
}
f() {
E('a').s = 1;
E(0).foo = 1;
}
''');
}

test_instance_undefined() async {
test_override_undefined() async {
await assertErrorsInCode('''
extension E on String {}
extension E on int {}
f() {
E('a').s = 1;
E(0).foo = 1;
}
''', [
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 40, 1),
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 35, 3),
]);
}

test_override_undefined_hasGetter() async {
await assertErrorsInCode('''
extension E on int {
int get foo => 0;
}
f() {
E(0).foo = 1;
}
''', [
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 56, 3),
]);
}

test_static_undefined() async {
await assertErrorsInCode('''
extension E on Object {}
extension E on int {}
void f() {
E.s = 3;
E.foo = 3;
}
''', [
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 40, 1),
error(CompileTimeErrorCode.UNDEFINED_EXTENSION_SETTER, 37, 3),
]);
}
}
49 changes: 49 additions & 0 deletions pkg/analyzer/test/src/diagnostics/undefined_getter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ class UndefinedGetterWithExtensionMethodsTest extends UndefinedGetterTest {
..contextFeatures = new FeatureSet.forTesting(
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);

test_instance_extendedHasSetter_extensionHasGetter() async {
await assertErrorsInCode('''
class C {
void set foo(int _) {}
}
extension E on C {
int get foo => 0;
f() {
this.foo;
}
}
''', [
error(StaticTypeWarningCode.UNDEFINED_GETTER, 95, 3),
]);
}

test_instance_undefined_hasSetter() async {
await assertErrorsInCode('''
extension E on int {
void set foo(int _) {}
}
f() {
0.foo;
}
''', [
error(StaticTypeWarningCode.UNDEFINED_GETTER, 58, 3),
]);
}

test_instance_withInference() async {
await assertErrorsInCode(r'''
extension E on int {}
Expand All @@ -147,4 +178,22 @@ f(C c) {
error(StaticTypeWarningCode.UNDEFINED_GETTER, 46, 1),
]);
}

test_this_extendedHasSetter_extensionHasGetter() async {
await assertErrorsInCode('''
class C {
void set foo(int _) {}
}
extension E on C {
int get foo => 0;
}
f(C c) {
c.foo;
}
''', [
error(StaticTypeWarningCode.UNDEFINED_GETTER, 93, 3),
]);
}
}
4 changes: 2 additions & 2 deletions pkg/analyzer/test/src/diagnostics/unused_import_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ f() {
test_instance_setter() async {
newFile('/test/lib/lib1.dart', content: r'''
extension E on String {
void set length(int i) {}
void set foo(int i) {}
}
''');
await assertNoErrorsInCode('''
import 'lib1.dart';
f() {
'abc'.length = 2;
'abc'.foo = 2;
}
''');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ void test1() {
c1a.m1;

c1a.m1 = 0;
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_EXTENSION_SETTER
// ^^
// [analyzer] STATIC_WARNING.ASSIGNMENT_TO_FINAL_LOCAL
// [cfe] unspecified

c1a.m2;
// ^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_EXTENSION_GETTER
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
// [cfe] unspecified

c1a.m2 = 0;
Expand All @@ -87,7 +87,7 @@ void test1() {
// [cfe] unspecified

c1b.m1 = 0;
// ^^^^^^
// ^^
// [analyzer] COMPILE_TIME_ERROR.AMBIGUOUS_EXTENSION_MEMBER_ACCESS
// [cfe] unspecified

Expand All @@ -97,7 +97,7 @@ void test1() {
// [cfe] unspecified

c1b.m2 = 0;
// ^^^^^^
// ^^
// [analyzer] COMPILE_TIME_ERROR.AMBIGUOUS_EXTENSION_MEMBER_ACCESS
// [cfe] unspecified
}
Expand All @@ -119,14 +119,14 @@ extension E2 on C2 {

this.m1;
this.m1 = 0;
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_EXTENSION_SETTER
// ^^
// [analyzer] STATIC_WARNING.ASSIGNMENT_TO_FINAL_NO_SETTER
// [cfe] unspecified

this.m2 = 0;
this.m2;
// ^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_EXTENSION_GETTER
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER
// [cfe] unspecified

// Check that `this.mc` refers to `C2.mc`.
Expand Down

0 comments on commit fa099eb

Please sign in to comment.