Skip to content

Commit 8c41cc9

Browse files
scheglovcommit-bot@chromium.org
authored and
commit-bot@chromium.org
committed
Issue 47302. Enable type parameters as potentially constant expressions.
Bug: #47302 Change-Id: I95d1b94efe5cc2877d37bc5ac615869233395189 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215014 Reviewed-by: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 62a903f commit 8c41cc9

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ class _Collector {
244244
if (element is MethodElement && element.isStatic) {
245245
return;
246246
}
247+
if (element is TypeParameterElement &&
248+
featureSet.isEnabled(Feature.constructor_tearoffs)) {
249+
return;
250+
}
247251
nodes.add(node);
248252
}
249253

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

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ main() {
1313
defineReflectiveSuite(() {
1414
defineReflectiveTests(IsConstantTypeExpressionTest);
1515
defineReflectiveTests(PotentiallyConstantTest);
16-
defineReflectiveTests(PotentiallyConstantWithoutNullSafetyTest);
1716
});
1817
}
1918

@@ -216,6 +215,18 @@ class A<T> {
216215
''', () => _xInitializer());
217216
}
218217

218+
test_asExpression_typeParameter_29() async {
219+
await _assertNotConst(r'''
220+
// @dart = 2.9
221+
const a = 0;
222+
class A<T> {
223+
m() {
224+
var x = a as T;
225+
}
226+
}
227+
''', () => _xInitializer(), () => [findNode.namedType('T;')]);
228+
}
229+
219230
test_asExpression_typeParameter_nested() async {
220231
await _assertConst(r'''
221232
const a = 0;
@@ -518,6 +529,18 @@ class A<T> {
518529
''', () => _xInitializer());
519530
}
520531

532+
test_isExpression_typeParameter_29() async {
533+
await _assertNotConst(r'''
534+
// @dart = 2.9
535+
const a = 0;
536+
class A<T> {
537+
m() {
538+
var x = a is T;
539+
}
540+
}
541+
''', () => _xInitializer(), () => [findNode.namedType('T;')]);
542+
}
543+
521544
test_isExpression_typeParameter_nested() async {
522545
await _assertConst(r'''
523546
const a = 0;
@@ -1097,6 +1120,25 @@ var x = A;
10971120
''', () => _xInitializer());
10981121
}
10991122

1123+
test_simpleIdentifier_typeParameter_class() async {
1124+
await _assertConst(r'''
1125+
class A<T> {
1126+
final Object f;
1127+
A() : f = T;
1128+
}
1129+
''', () => findNode.simple('T;'));
1130+
}
1131+
1132+
test_simpleIdentifier_typeParameter_class_214() async {
1133+
await _assertNotConst(r'''
1134+
// @dart = 2.14
1135+
class A<T> {
1136+
final Object f;
1137+
A() : f = T;
1138+
}
1139+
''', () => findNode.simple('T;'), () => [findNode.simple('T;')]);
1140+
}
1141+
11001142
test_spreadElement() async {
11011143
await _assertConst(r'''
11021144
const a = [0, 1, 2];
@@ -1162,46 +1204,3 @@ var x = 'a';
11621204
return findNode.variableDeclaration('x = ').initializer!;
11631205
}
11641206
}
1165-
1166-
@reflectiveTest
1167-
class PotentiallyConstantWithoutNullSafetyTest extends PubPackageResolutionTest
1168-
with WithoutNullSafetyMixin {
1169-
test_asExpression_typeParameter() async {
1170-
await _assertNotConst(r'''
1171-
const a = 0;
1172-
class A<T> {
1173-
m() {
1174-
var x = a as T;
1175-
}
1176-
}
1177-
''', () => _xInitializer(), () => [findNode.namedType('T;')]);
1178-
}
1179-
1180-
test_isExpression_typeParameter() async {
1181-
await _assertNotConst(r'''
1182-
const a = 0;
1183-
class A<T> {
1184-
m() {
1185-
var x = a is T;
1186-
}
1187-
}
1188-
''', () => _xInitializer(), () => [findNode.namedType('T;')]);
1189-
}
1190-
1191-
_assertNotConst(String code, AstNode Function() getNode,
1192-
List<AstNode> Function() getNotConstList) async {
1193-
await resolveTestCode(code);
1194-
var node = getNode();
1195-
var notConstList = getNotPotentiallyConstants(
1196-
node,
1197-
featureSet: featureSet,
1198-
);
1199-
1200-
var expectedNotConst = getNotConstList();
1201-
expect(notConstList, unorderedEquals(expectedNotConst));
1202-
}
1203-
1204-
Expression _xInitializer() {
1205-
return findNode.variableDeclaration('x = ').initializer!;
1206-
}
1207-
}

0 commit comments

Comments
 (0)