Update constants_2018/constant_types_test to allow type parameters in 'as'.

Change-Id: I37b0413914d6677e2be53fb0ce98c51f81694edb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142568
Reviewed-by: Leaf Petersen <leafp@google.com>
This commit is contained in:
Konstantin Shcheglov 2020-04-07 06:50:28 +00:00
parent b17e8ca9f7
commit 8a8c2e74d2
3 changed files with 40 additions and 21 deletions

View file

@ -26,18 +26,6 @@ List<AstNode> getNotPotentiallyConstants(
return collector.nodes;
}
/// Return `true` if the [node] is a potentially constant type expression.
bool isPotentiallyConstantTypeExpression(TypeAnnotation node) {
if (node is TypeName) {
var element = node.name.staticElement;
if (element is TypeParameterElement) {
return true;
}
}
return isConstantTypeExpression(node);
}
/// Return `true` if the [node] is a constant type expression.
bool isConstantTypeExpression(TypeAnnotation node) {
if (node is TypeName) {
@ -96,6 +84,18 @@ bool isConstantTypeExpression(TypeAnnotation node) {
return false;
}
/// Return `true` if the [node] is a potentially constant type expression.
bool isPotentiallyConstantTypeExpression(TypeAnnotation node) {
if (node is TypeName) {
var element = node.name.staticElement;
if (element is TypeParameterElement) {
return true;
}
}
return isConstantTypeExpression(node);
}
bool _isConstantTypeName(Identifier name) {
var element = name.staticElement;
if (element is ClassElement || element is GenericTypeAliasElement) {
@ -213,8 +213,14 @@ class _Collector {
}
if (node is IsExpression) {
if (!isConstantTypeExpression(node.type)) {
nodes.add(node.type);
if (isNonNullableByDefault) {
if (!isPotentiallyConstantTypeExpression(node.type)) {
nodes.add(node.type);
}
} else {
if (!isConstantTypeExpression(node.type)) {
nodes.add(node.type);
}
}
collect(node.expression);
return;

View file

@ -187,7 +187,7 @@ var x = a as int;
''', () => _xInitializer(), () => [findNode.simple('a as')]);
}
test_asExpression_notConstType() async {
test_asExpression_typeParameter() async {
await _assertNotConst(r'''
const a = 0;
class A<T> {
@ -283,7 +283,7 @@ var x = a is int;
''', () => _xInitializer(), () => [findNode.simple('a is')]);
}
test_isExpression_notConstType() async {
test_isExpression_typeParameter() async {
await _assertNotConst(r'''
const a = 0;
class A<T> {
@ -908,7 +908,7 @@ var x = 'a';
class PotentiallyConstantWithNullSafetyTest extends PotentiallyConstantTest
with WithNullSafetyMixin {
@override
test_asExpression_notConstType() async {
test_asExpression_typeParameter() async {
await _assertConst(r'''
const a = 0;
class A<T> {
@ -916,6 +916,18 @@ class A<T> {
var x = a as T;
}
}
''', () => _xInitializer());
}
@override
test_isExpression_typeParameter() async {
await _assertConst(r'''
const a = 0;
class A<T> {
m() {
var x = a is T;
}
}
''', () => _xInitializer());
}
}

View file

@ -2,8 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests that only constant types are allowed in some positions,
// not type parameters.
// Tests that in some positions only constant types are allowed, so not
// type parameters. But in other positions potentially constant types are
// allowed, so type parameters.
import "package:expect/expect.dart";
@ -15,11 +16,11 @@ class T<X> {
[];
const T.test2(Object o)
: value = o //
as X //# 02: compile-time error
as X //# 02: ok
;
const T.test3(Object o)
: value = o //
is X //# 03: compile-time error
is X //# 03: ok
;
const T.test4()
: value = null //