[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: https://github.com/dart-lang/sdk/issues/59835 Change-Id: I4e3daeb008c7dbadbb71d988012bf2e521df2e84 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/424661 Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Commit-Queue: Kallen Tu <kallentu@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart b/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart index a21edbf..993e851 100644 --- a/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart +++ b/pkg/analyzer/lib/src/dart/constant/potentially_constant.dart
@@ -72,6 +72,10 @@ return; } + if (node is DotShorthandPropertyAccess) { + return _identifier(node.propertyName); + } + if (node is StringInterpolation) { for (var component in node.elements) { if (component is InterpolationExpression) {
diff --git a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart index 6b6c17a..2d1ab7e 100644 --- a/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart
@@ -97,6 +97,55 @@ '''); } + test_const_assert_class() async { + await assertNoErrorsInCode(r''' +class Integer { + static const Integer one = const Integer._(1); + final int integer; + Integer(this.integer); + const Integer._(this.integer); +} + +class CAssert { + const CAssert.one(Integer i): assert(i == .one); +} +'''); + + var node = findNode.singleDotShorthandPropertyAccess; + assertResolvedNodeText(node, r''' +DotShorthandPropertyAccess + period: . + propertyName: SimpleIdentifier + token: one + element: <testLibraryFragment>::@class::Integer::@getter::one#element + staticType: Integer + correspondingParameter: dart:core::<fragment>::@class::Object::@method::==::@parameter::other#element + staticType: Integer +'''); + } + + test_const_assert_enum() async { + await assertNoErrorsInCode(r''' +enum Color { red, green, blue } + +class CAssert { + const CAssert.blue(Color color): assert(color == .blue); +} +'''); + + var node = findNode.singleDotShorthandPropertyAccess; + assertResolvedNodeText(node, r''' +DotShorthandPropertyAccess + period: . + propertyName: SimpleIdentifier + token: blue + element: <testLibraryFragment>::@enum::Color::@getter::blue#element + staticType: Color + correspondingParameter: dart:core::<fragment>::@class::Object::@method::==::@parameter::other#element + staticType: Color +'''); + } + test_const_class() async { await assertNoErrorsInCode(''' class C {