Skip to content

Commit 074a3dc

Browse files
committed
Fix namespaces interfering with AT
1 parent 665fbbe commit 074a3dc

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--TEST--
2+
Concrete example of using AT
3+
--CREDITS--
4+
Levi Morrison
5+
--FILE--
6+
<?php declare(strict_types=1);
7+
8+
namespace Sequence;
9+
10+
interface Sequence
11+
{
12+
// No null. This is probably going to be painful, but let's try it.
13+
type Item: object|array|string|float|int|bool;
14+
15+
function next(): ?Item;
16+
17+
/**
18+
* @param callable(Item, Item): Item $f
19+
* @return ?Item
20+
*/
21+
function reduce(callable $f): ?Item;
22+
}
23+
24+
final class StringTablePair
25+
{
26+
public function __construct(
27+
public readonly string $string,
28+
public readonly int $id,
29+
) {}
30+
}
31+
32+
final class StringTableSequence implements Sequence
33+
{
34+
private array $strings;
35+
36+
public function __construct(StringTable $string_table) {
37+
$this->strings = $string_table->to_assoc_array();
38+
}
39+
40+
function next(): ?StringTablePair
41+
{
42+
$key = \array_key_first($this->strings);
43+
if (!isset($key)) {
44+
return null;
45+
}
46+
$value = \array_shift($this->strings);
47+
return new StringTablePair($key, $value);
48+
}
49+
50+
/**
51+
* @param callable(Item, Item): Item $f
52+
* @return ?Item
53+
*/
54+
function reduce(callable $f): ?StringTablePair
55+
{
56+
$reduction = $this->next();
57+
if (!isset($reduction)) {
58+
return null;
59+
}
60+
61+
while (($next = $this->next()) !== null) {
62+
$reduction = $f($reduction, $next);
63+
}
64+
return $reduction;
65+
}
66+
}
67+
68+
final class StringTable
69+
{
70+
private array $strings = ["" => 0];
71+
72+
public function __construct() {}
73+
74+
public function offsetGet(string $offset): int
75+
{
76+
return $this->strings[$offset] ?? throw new \Exception();
77+
}
78+
79+
public function offsetExists(string $offset): bool
80+
{
81+
return \isset($this->strings[$offset]);
82+
}
83+
84+
public function intern(string $str): int
85+
{
86+
return $this->strings[$str]
87+
?? ($this->strings[$str] = \count($this->strings));
88+
}
89+
90+
public function to_sequence(): StringTableSequence
91+
{
92+
return new StringTableSequence($this);
93+
}
94+
95+
public function to_assoc_array(): array {
96+
return $this->strings;
97+
}
98+
}
99+
100+
?>
101+
--EXPECTF--
102+
Fatal error: Declaration of Sequence\StringTableSequence::next(): ?Sequence\StringTablePair must be compatible with Sequence\Sequence::next(): Item<object|array|string|int|float|bool>|null in %s on line %d

Zend/zend_compile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,15 +7027,15 @@ static zend_type zend_compile_single_typename(zend_ast *ast)
70277027
} else {
70287028
const char *correct_name;
70297029
uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7030-
zend_string *class_name = type_name;
7031-
uint32_t flags = 0;
70327030

7031+
if (ce && ce->associated_types && zend_hash_exists(ce->associated_types, type_name)) {
7032+
return (zend_type) ZEND_TYPE_INIT_CLASS(zend_string_copy(type_name), /* allow null */ false, _ZEND_TYPE_ASSOCIATED_BIT);
7033+
}
7034+
7035+
zend_string *class_name = type_name;
70337036
if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
70347037
class_name = zend_resolve_class_name_ast(ast);
70357038
zend_assert_valid_class_name(class_name, "a type name");
7036-
if (ce && ce->associated_types && zend_hash_exists(ce->associated_types, class_name)) {
7037-
flags = _ZEND_TYPE_ASSOCIATED_BIT;
7038-
}
70397039
} else {
70407040
ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
70417041

@@ -7078,7 +7078,7 @@ static zend_type zend_compile_single_typename(zend_ast *ast)
70787078

70797079
class_name = zend_new_interned_string(class_name);
70807080
zend_alloc_ce_cache(class_name);
7081-
return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, flags);
7081+
return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
70827082
}
70837083
}
70847084
}

0 commit comments

Comments
 (0)