Skip to content

Commit 1d08bd5

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot shorthands: Allow potentially constant DotShorthandPropertyAccesses.
When we test for potentially-constant constants (ie. in asserts), we need to also test whether the identifier of a `DotShorthandPropertyAccess` is constant or not. This CL adds one bit of logic and a few unit tests. The language test `language/dot_shorthands/equality/equality_test` also covers this behavior and is passing. Bug: #59835 Change-Id: I4e3daeb008c7dbadbb71d988012bf2e521df2e84 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/424661 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 26b0c1f commit 1d08bd5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pkg/analyzer/lib/src/dart/constant/potentially_constant.dart

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class _Collector {
7272
return;
7373
}
7474

75+
if (node is DotShorthandPropertyAccess) {
76+
return _identifier(node.propertyName);
77+
}
78+
7579
if (node is StringInterpolation) {
7680
for (var component in node.elements) {
7781
if (component is InterpolationExpression) {

pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart

+49
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,55 @@ DotShorthandPropertyAccess
9797
''');
9898
}
9999

100+
test_const_assert_class() async {
101+
await assertNoErrorsInCode(r'''
102+
class Integer {
103+
static const Integer one = const Integer._(1);
104+
final int integer;
105+
Integer(this.integer);
106+
const Integer._(this.integer);
107+
}
108+
109+
class CAssert {
110+
const CAssert.one(Integer i): assert(i == .one);
111+
}
112+
''');
113+
114+
var node = findNode.singleDotShorthandPropertyAccess;
115+
assertResolvedNodeText(node, r'''
116+
DotShorthandPropertyAccess
117+
period: .
118+
propertyName: SimpleIdentifier
119+
token: one
120+
element: <testLibraryFragment>::@class::Integer::@getter::one#element
121+
staticType: Integer
122+
correspondingParameter: dart:core::<fragment>::@class::Object::@method::==::@parameter::other#element
123+
staticType: Integer
124+
''');
125+
}
126+
127+
test_const_assert_enum() async {
128+
await assertNoErrorsInCode(r'''
129+
enum Color { red, green, blue }
130+
131+
class CAssert {
132+
const CAssert.blue(Color color): assert(color == .blue);
133+
}
134+
''');
135+
136+
var node = findNode.singleDotShorthandPropertyAccess;
137+
assertResolvedNodeText(node, r'''
138+
DotShorthandPropertyAccess
139+
period: .
140+
propertyName: SimpleIdentifier
141+
token: blue
142+
element: <testLibraryFragment>::@enum::Color::@getter::blue#element
143+
staticType: Color
144+
correspondingParameter: dart:core::<fragment>::@class::Object::@method::==::@parameter::other#element
145+
staticType: Color
146+
''');
147+
}
148+
100149
test_const_class() async {
101150
await assertNoErrorsInCode('''
102151
class C {

0 commit comments

Comments
 (0)