Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
Make EditableList<T> covariant
Browse files Browse the repository at this point in the history
This makes `EditableList<IExpression>` a valid subtype of
`EditableList<EditableNode>`, which is safe as all HHAST node types are
immutable.
  • Loading branch information
fredemmott committed May 13, 2019
1 parent e2f73e4 commit bc0cc28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/nodes/EditableList.hack
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Facebook\HHAST;

use namespace HH\Lib\{C, Dict, Vec};

final class EditableList<Titem as ?EditableNode> extends EditableNode {
final class EditableList<+Titem as ?EditableNode> extends EditableNode {
/**
* Use `EditableList::createMaybeEmptyList()` or
* `EditableList::createNonEmptyListOrMissing()` instead to be explicit
Expand Down
29 changes: 29 additions & 0 deletions tests/NodeTypesTest.hack
Original file line number Diff line number Diff line change
@@ -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<IParameter> $_) ==> null)(
(null ?as FunctionDeclarationHeader)?->getParameterListx(),
);
$_typecheck = ((?EditableList<EditableNode> $_) ==> null)(
(null ?as FunctionDeclarationHeader)?->getParameterListx(),
);
}

public function testBinaryExpressionOperandsAreExpressions(): void {
$typecheck = (?IExpression $_) ==> null;
$be = null ?as BinaryExpression;
$typecheck($be?->getLeftOperand());
$typecheck($be?->getRightOperand());
}
}

0 comments on commit bc0cc28

Please sign in to comment.