From bc0cc28015b12c1c9146b8ea0755a366d127f786 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Mon, 13 May 2019 12:56:22 -0700 Subject: [PATCH] Make `EditableList` covariant This makes `EditableList` a valid subtype of `EditableList`, which is safe as all HHAST node types are immutable. --- src/nodes/EditableList.hack | 2 +- tests/NodeTypesTest.hack | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/NodeTypesTest.hack diff --git a/src/nodes/EditableList.hack b/src/nodes/EditableList.hack index d0c799809..0fc6f5023 100644 --- a/src/nodes/EditableList.hack +++ b/src/nodes/EditableList.hack @@ -11,7 +11,7 @@ namespace Facebook\HHAST; use namespace HH\Lib\{C, Dict, Vec}; -final class EditableList extends EditableNode { +final class EditableList<+Titem as ?EditableNode> extends EditableNode { /** * Use `EditableList::createMaybeEmptyList()` or * `EditableList::createNonEmptyListOrMissing()` instead to be explicit diff --git a/tests/NodeTypesTest.hack b/tests/NodeTypesTest.hack new file mode 100644 index 000000000..89c99d1e8 --- /dev/null +++ b/tests/NodeTypesTest.hack @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + */ + + +namespace Facebook\HHAST; + +final class NodeTypesTest extends TestCase { + public function testListVariance(): void { + $_typecheck = ((?EditableList $_) ==> null)( + (null ?as FunctionDeclarationHeader)?->getParameterListx(), + ); + $_typecheck = ((?EditableList $_) ==> null)( + (null ?as FunctionDeclarationHeader)?->getParameterListx(), + ); + } + + public function testBinaryExpressionOperandsAreExpressions(): void { + $typecheck = (?IExpression $_) ==> null; + $be = null ?as BinaryExpression; + $typecheck($be?->getLeftOperand()); + $typecheck($be?->getRightOperand()); + } +}