Skip to content

Commit 4d0ea4d

Browse files
committed
Fix constant-aware shape keys in exceptions
1 parent 7380e7d commit 4d0ea4d

File tree

8 files changed

+18
-30
lines changed

8 files changed

+18
-30
lines changed

resources/grammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@
558558

559559
foreach ($children as $field) {
560560
if ($field instanceof Node\Stmt\Shape\ExplicitFieldNode) {
561-
$key = $field->getHashString();
561+
$key = $field->getKey();
562562

563563
if (\in_array($key, $explicit, true)) {
564564
throw SemanticException::fromShapeFieldDuplication($key, $field->offset);

resources/grammar/shape-fields.pp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ShapeFieldsList -> {
4343

4444
foreach ($children as $field) {
4545
if ($field instanceof Node\Stmt\Shape\ExplicitFieldNode) {
46-
$key = $field->getHashString();
46+
$key = $field->getKey();
4747

4848
if (\in_array($key, $explicit, true)) {
4949
throw SemanticException::fromShapeFieldDuplication($key, $field->offset);

src/Node/Stmt/Shape/ClassConstMaskFieldNode.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use TypeLang\Parser\Node\Stmt\Attribute\AttributeGroupsListNode;
88
use TypeLang\Parser\Node\Stmt\ClassConstMaskNode;
9+
use TypeLang\Parser\Node\Stmt\ClassConstNode;
910
use TypeLang\Parser\Node\Stmt\TypeStatement;
1011

1112
final class ClassConstMaskFieldNode extends ExplicitFieldNode
@@ -19,12 +20,15 @@ public function __construct(
1920
parent::__construct($of, $optional, $attributes);
2021
}
2122

22-
public function getHashString(): string
23+
public function getKey(): int|string
2324
{
24-
$key = $this->key::class . ':'
25-
. $this->key->class->toString()
25+
$result = $this->key->class->toString()
2626
. '::' . $this->key->constant?->toString();
2727

28-
return \hash('xxh3', $key);
28+
if ($this->key instanceof ClassConstNode) {
29+
return $result;
30+
}
31+
32+
return $result . '*';
2933
}
3034
}

src/Node/Stmt/Shape/ConstMaskFieldNode.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ public function __construct(
1919
parent::__construct($of, $optional, $attributes);
2020
}
2121

22-
public function getHashString(): string
22+
public function getKey(): string
2323
{
24-
$key = $this->key::class . ':'
25-
. $this->key->name->toString();
26-
27-
return \hash('xxh3', $key);
24+
return (string) $this->key;
2825
}
2926
}

src/Node/Stmt/Shape/ExplicitFieldNode.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
abstract class ExplicitFieldNode extends FieldNode
88
{
99
/**
10-
* @deprecated Please use {@see getHashString()} instead
10+
* Returns pretty-printed key value
1111
*/
12-
public function getKey(): int|string
13-
{
14-
return $this->getHashString();
15-
}
16-
17-
/**
18-
* @return non-empty-string
19-
*/
20-
abstract public function getHashString(): string;
12+
abstract public function getKey(): int|string;
2113
}

src/Node/Stmt/Shape/NamedFieldNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function __construct(
2626
parent::__construct($of, $optional, $attributes);
2727
}
2828

29-
public function getHashString(): string
29+
public function getKey(): string
3030
{
31-
return \hash('xxh3', self::class . ':' . $this->key->toString());
31+
return $this->key->toString();
3232
}
3333
}

src/Node/Stmt/Shape/NumericFieldNode.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,4 @@ public function getKey(): int
2323
{
2424
return $this->key->getValue();
2525
}
26-
27-
public function getHashString(): string
28-
{
29-
return \hash('xxh3', self::class . ':' . $this->key->getRawValue());
30-
}
3126
}

src/Node/Stmt/Shape/StringNamedFieldNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function __construct(
1919
parent::__construct($of, $optional, $attributes);
2020
}
2121

22-
public function getHashString(): string
22+
public function getKey(): string
2323
{
24-
return \hash('xxh3', self::class . ':' . $this->key->getValue());
24+
return $this->key->getValue();
2525
}
2626
}

0 commit comments

Comments
 (0)