Skip to content

Commit dfdc7e4

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Report PRIVATE_SETTER.
Change-Id: I823582328b4b9cc63ac734e9ea1c9354175a6e18 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153880 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent b0b957f commit dfdc7e4

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ const List<ErrorCode> errorCodeValues = [
288288
CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT,
289289
CompileTimeErrorCode.PRIVATE_COLLISION_IN_MIXIN_APPLICATION,
290290
CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER,
291+
CompileTimeErrorCode.PRIVATE_SETTER,
291292
CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT,
292293
CompileTimeErrorCode.RECURSIVE_CONSTRUCTOR_REDIRECT,
293294
CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT,

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5608,6 +5608,12 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
56085608
CompileTimeErrorCode('PRIVATE_OPTIONAL_PARAMETER',
56095609
"Named optional parameters can't start with an underscore.");
56105610

5611+
static const CompileTimeErrorCode PRIVATE_SETTER = CompileTimeErrorCode(
5612+
'PRIVATE_SETTER',
5613+
"The setter '{0}' is private and can't be accessed outside of the "
5614+
"library that declares it.",
5615+
correction: "Try making it public.");
5616+
56115617
/**
56125618
* 12.1 Constants: It is a compile-time error if the value of a compile-time
56135619
* constant expression depends on itself.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,8 +1334,15 @@ class ElementResolver extends SimpleAstVisitor<void> {
13341334
ExecutableElement element;
13351335

13361336
var setter = typeReference.getSetter(propertyName.name);
1337-
if (setter != null && setter.isAccessibleIn(_definingLibrary)) {
1337+
if (setter != null) {
13381338
element = setter;
1339+
if (!setter.isAccessibleIn(_definingLibrary)) {
1340+
_errorReporter.reportErrorForNode(
1341+
CompileTimeErrorCode.PRIVATE_SETTER,
1342+
propertyName,
1343+
[propertyName.name, typeReference.name],
1344+
);
1345+
}
13391346
}
13401347

13411348
if (element != null) {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(PrivateSetterTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class PrivateSetterTest extends DriverResolutionTest {
18+
test_typeLiteral_privateField_differentLibrary() async {
19+
newFile('/test/lib/a.dart', content: r'''
20+
class A {
21+
static int _foo = 0;
22+
}
23+
''');
24+
await assertErrorsInCode(r'''
25+
import 'a.dart';
26+
27+
main() {
28+
A._foo = 0;
29+
}
30+
''', [
31+
error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4),
32+
]);
33+
34+
var aImport = findElement.importFind('package:test/a.dart');
35+
assertElement(
36+
findNode.simple('_foo = 0'),
37+
aImport.setter('_foo'),
38+
);
39+
}
40+
41+
test_typeLiteral_privateField_sameLibrary() async {
42+
await assertNoErrorsInCode(r'''
43+
class A {
44+
// ignore:unused_field
45+
static int _foo = 0;
46+
}
47+
48+
main() {
49+
A._foo = 0;
50+
}
51+
''');
52+
}
53+
54+
test_typeLiteral_privateSetter__sameLibrary() async {
55+
await assertNoErrorsInCode(r'''
56+
class A {
57+
static set _foo(int _) {}
58+
}
59+
60+
main() {
61+
A._foo = 0;
62+
}
63+
''');
64+
}
65+
66+
test_typeLiteral_privateSetter_differentLibrary_hasGetter() async {
67+
newFile('/test/lib/a.dart', content: r'''
68+
class A {
69+
static set _foo(int _) {}
70+
71+
static int get _foo => 0;
72+
}
73+
''');
74+
await assertErrorsInCode(r'''
75+
import 'a.dart';
76+
77+
main() {
78+
A._foo = 0;
79+
}
80+
''', [
81+
error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4),
82+
]);
83+
84+
var aImport = findElement.importFind('package:test/a.dart');
85+
assertElement(
86+
findNode.simple('_foo = 0'),
87+
aImport.setter('_foo'),
88+
);
89+
}
90+
91+
test_typeLiteral_privateSetter_differentLibrary_noGetter() async {
92+
newFile('/test/lib/a.dart', content: r'''
93+
class A {
94+
static set _foo(int _) {}
95+
}
96+
''');
97+
await assertErrorsInCode(r'''
98+
import 'a.dart';
99+
100+
main() {
101+
A._foo = 0;
102+
}
103+
''', [
104+
error(CompileTimeErrorCode.PRIVATE_SETTER, 31, 4),
105+
]);
106+
107+
var aImport = findElement.importFind('package:test/a.dart');
108+
assertElement(
109+
findNode.simple('_foo = 0'),
110+
aImport.setter('_foo'),
111+
);
112+
}
113+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ import 'prefix_identifier_not_followed_by_dot_test.dart'
397397
import 'private_collision_in_mixin_application_test.dart'
398398
as private_collision_in_mixin_application;
399399
import 'private_optional_parameter_test.dart' as private_optional_parameter;
400+
import 'private_setter_test.dart' as private_setter;
400401
import 'receiver_of_type_never_test.dart' as receiver_of_type_never;
401402
import 'recursive_compile_time_constant_test.dart'
402403
as recursive_compile_time_constant;
@@ -805,6 +806,7 @@ main() {
805806
prefix_identifier_not_followed_by_dot.main();
806807
private_collision_in_mixin_application.main();
807808
private_optional_parameter.main();
809+
private_setter.main();
808810
receiver_of_type_never.main();
809811
recursive_compile_time_constant.main();
810812
recursive_constructor_redirect.main();

0 commit comments

Comments
 (0)