Skip to content

Improve is_instanceof inference #13238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024

Conversation

iluuu1994
Copy link
Member

When a class is final, it may be treated as !is_instanceof.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modification of the __is_instanceof should be done before the check if it was changed.

diff --git a/Zend/Optimizer/zend_inference.c b/Zend/Optimizer/zend_inference.c
index af6c301fd934..f3988806e739 100644
--- a/Zend/Optimizer/zend_inference.c
+++ b/Zend/Optimizer/zend_inference.c
@@ -2005,8 +2005,13 @@ static uint32_t get_ssa_alias_types(zend_ssa_alias_kind alias) {
 		if (var >= 0) {													\
- 			if (ssa_var_info[var].ce != (_ce) ||                        \
- 			    ssa_var_info[var].is_instanceof != (_is_instanceof)) {  \
-				ssa_var_info[var].ce = (_ce);						    \
-				ssa_var_info[var].is_instanceof = (_is_instanceof);     \
+			zend_class_entry *__ce = (_ce);							\
+			bool __is_instanceof = (_is_instanceof);				\
+			if (__ce && (__ce->ce_flags & ZEND_ACC_FINAL)) {		\
+				__is_instanceof = false;							\
+			}														\
+ 			if (ssa_var_info[var].ce != __ce ||                        \
+ 			    ssa_var_info[var].is_instanceof != __is_instanceof) {  \
+				ssa_var_info[var].ce = __ce;							\
+				ssa_var_info[var].is_instanceof = __is_instanceof;		\
 				if (update_worklist) { 									\
 					add_usages(op_array, ssa, worklist, var);			\
 				}														\

@iluuu1994
Copy link
Member Author

Fair enough. I don't think this should make a difference because the check is based on ce, which would need to change too for the outcome of the check to be different. But your siggestion might be easier to understand.

When a class is final, it may be treated as !is_instanceof.
@iluuu1994 iluuu1994 force-pushed the improve-instanceof-inference branch from 222f218 to e905051 Compare January 29, 2024 10:25
@dstogov
Copy link
Member

dstogov commented Jan 29, 2024

Fair enough. I don't think this should make a difference because the check is based on ce, which would need to change too for the outcome of the check to be different. But your siggestion might be easier to understand.

The old code might never reach the fixed point and repeatable call add_usages().

@iluuu1994
Copy link
Member Author

The old code might never reach the fixed point and repeatable call add_usages().

Yeah, you're right.

@iluuu1994 iluuu1994 merged commit 34e2dc5 into php:master Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants