From 2f38434d50d4468cca393d6406b4b83d63c66ab1 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Wed, 20 Aug 2025 23:20:47 +0200 Subject: [PATCH] Dont substitute self/parent for anonymous class Fixes GH-18373 --- Zend/tests/gh19304.phpt | 2 +- Zend/zend_compile.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Zend/tests/gh19304.phpt b/Zend/tests/gh19304.phpt index c77fc2d6facc2..47e20af64623a 100644 --- a/Zend/tests/gh19304.phpt +++ b/Zend/tests/gh19304.phpt @@ -15,4 +15,4 @@ try { ?> --EXPECT-- -Cannot assign int to property class@anonymous::$v of type class@anonymous +Cannot assign int to property class@anonymous::$v of type self diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 205023fa69b64..45b9764ad9a77 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -7089,18 +7089,21 @@ static zend_type zend_compile_single_typename(zend_ast *ast) zend_assert_valid_class_name(class_name, "a type name"); } else { ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT); + bool substitute_self_parent = zend_is_scope_known() + && CG(active_class_entry) + && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS); zend_ensure_valid_class_fetch_type(fetch_type); if (fetch_type == ZEND_FETCH_CLASS_SELF) { /* Scope might be unknown for unbound closures and traits */ - if (zend_is_scope_known()) { + if (substitute_self_parent) { class_name = CG(active_class_entry)->name; ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time"); } } else { ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT); /* Scope might be unknown for unbound closures and traits */ - if (zend_is_scope_known()) { + if (substitute_self_parent) { class_name = CG(active_class_entry)->parent_name; ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time"); }