Skip to content

Commit 15324c7

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Support const implicit instantiation for prefixed identifiers and property access
Bug: #46020 Change-Id: I99bfa6d96b84a41abdd02d41ff6ad7dd5c99789c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215687 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 1951cce commit 15324c7

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
867867
return null;
868868
}
869869
}
870-
// validate prefixed identifier
871-
return _getConstantValue(node, node.staticElement);
870+
// Validate prefixed identifier.
871+
return _getConstantValue(node, node.identifier);
872872
}
873873

874874
@override
@@ -902,7 +902,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
902902
return prefixResult.stringLength(typeSystem);
903903
}
904904
}
905-
return _getConstantValue(node, node.propertyName.staticElement);
905+
return _getConstantValue(node, node.propertyName);
906906
}
907907

908908
@override
@@ -971,7 +971,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
971971
return _instantiateFunctionType(node, value);
972972
}
973973

974-
return _getConstantValue(node, node.staticElement);
974+
return _getConstantValue(node, node);
975975
}
976976

977977
@override
@@ -1171,9 +1171,11 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
11711171
}
11721172

11731173
/// Return the constant value of the static constant represented by the given
1174-
/// [element]. The [node] is the node to be used if an error needs to be
1174+
/// [identifier]. The [node] is the node to be used if an error needs to be
11751175
/// reported.
1176-
DartObjectImpl? _getConstantValue(Expression node, Element? element) {
1176+
DartObjectImpl? _getConstantValue(
1177+
Expression node, SimpleIdentifier identifier) {
1178+
var element = identifier.staticElement;
11771179
element = element?.declaration;
11781180
var variableElement =
11791181
element is PropertyAccessorElement ? element.variable : element;
@@ -1196,7 +1198,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
11961198
if (value == null) {
11971199
return value;
11981200
}
1199-
return _instantiateFunctionType(node, value);
1201+
return _instantiateFunctionType(identifier, value);
12001202
}
12011203
} else if (variableElement is ConstructorElement) {
12021204
return DartObjectImpl(
@@ -1207,11 +1209,12 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
12071209
} else if (variableElement is ExecutableElement) {
12081210
var function = element as ExecutableElement;
12091211
if (function.isStatic) {
1210-
return DartObjectImpl(
1212+
var rawType = DartObjectImpl(
12111213
typeSystem,
1212-
node.typeOrThrow,
1214+
function.type,
12131215
FunctionState(function),
12141216
);
1217+
return _instantiateFunctionType(identifier, rawType);
12151218
}
12161219
} else if (variableElement is ClassElement) {
12171220
var type = variableElement.instantiate(
@@ -1264,10 +1267,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
12641267
/// type-instantiated with those [node]'s tear-off type argument types,
12651268
/// otherwise returns [value].
12661269
DartObjectImpl? _instantiateFunctionType(
1267-
Expression node, DartObjectImpl value) {
1268-
if (node is! SimpleIdentifier) {
1269-
return value;
1270-
}
1270+
SimpleIdentifier node, DartObjectImpl value) {
12711271
var functionElement = value.toFunctionValue();
12721272
if (functionElement is! ExecutableElement) {
12731273
return value;

pkg/analyzer/test/src/dart/constant/evaluation_test.dart

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,48 @@ const void Function(int) g = self.f;
566566
var result = _evaluateConstant('g');
567567
assertType(result.type, 'void Function(int)');
568568
assertElement(result.toFunctionValue(), findElement.topFunction('f'));
569-
_assertTypeArguments(result, null);
569+
_assertTypeArguments(result, ['int']);
570+
}
571+
572+
test_visitPrefixedIdentifier_genericFunction_instantiatedNonIdentifier() async {
573+
await resolveTestCode('''
574+
void f<T>(T a) {}
575+
const b = false;
576+
const g1 = f;
577+
const g2 = f;
578+
const void Function(int) h = b ? g1 : g2;
579+
''');
580+
var result = _evaluateConstant('h');
581+
assertType(result.type, 'void Function(int)');
582+
assertElement(result.toFunctionValue(), findElement.topFunction('f'));
583+
_assertTypeArguments(result, ['int']);
584+
}
585+
586+
test_visitPrefixedIdentifier_genericFunction_instantiatedPrefixed() async {
587+
await resolveTestCode('''
588+
import '' as self;
589+
void f<T>(T a) {}
590+
const g = f;
591+
const void Function(int) h = self.g;
592+
''');
593+
var result = _evaluateConstant('h');
594+
assertType(result.type, 'void Function(int)');
595+
assertElement(result.toFunctionValue(), findElement.topFunction('f'));
596+
_assertTypeArguments(result, ['int']);
597+
}
598+
599+
test_visitPropertyAccess_genericFunction_instantiated() async {
600+
await resolveTestCode('''
601+
import '' as self;
602+
class C {
603+
static void f<T>(T a) {}
604+
}
605+
const void Function(int) g = self.C.f;
606+
''');
607+
var result = _evaluateConstant('g');
608+
assertType(result.type, 'void Function(int)');
609+
assertElement(result.toFunctionValue(), findElement.method('f'));
610+
_assertTypeArguments(result, ['int']);
570611
}
571612

572613
test_visitSimpleIdentifier_className() async {
@@ -583,6 +624,17 @@ class C {}
583624
await resolveTestCode('''
584625
void f<T>(T a) {}
585626
const void Function(int) g = f;
627+
''');
628+
var result = _evaluateConstant('g');
629+
assertType(result.type, 'void Function(int)');
630+
assertElement(result.toFunctionValue(), findElement.topFunction('f'));
631+
_assertTypeArguments(result, ['int']);
632+
}
633+
634+
test_visitSimpleIdentifier_genericFunction_nonGeneric() async {
635+
await resolveTestCode('''
636+
void f(int a) {}
637+
const void Function(int) g = f;
586638
''');
587639
var result = _evaluateConstant('g');
588640
assertType(result.type, 'void Function(int)');

0 commit comments

Comments
 (0)