com php-src: Improved error message for typed class properties with null as default value: Zend/tests/nullable_types/union_nullable_p
roperty_fails.phpt Zend/zend_compile.c
| From: | Nikita Popov | Date: | Tue, 03 Nov 2020 08:28:53 +0000 |
| Subject: | com php-src: Improved error message for typed class properties with null as default value: Zend/tests/nullable_types/union_nullable_p roperty_fails.phpt Zend/zend_compile.c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-114039@lists.php.net to get a copy of this message | ||
Commit: 6d6fadeb0d9923c3dd99a9381bd26a26bf28ccec
Author: Bogdan Ungureanu <bogdanungureanu21@gmail.com> Tue, 3 Nov 2020 00:18:12
+0200
Committer: Nikita Popov <nikita.ppv@gmail.com> Tue, 3 Nov 2020 09:28:53 +0100
Parents: 94938e4ee7b8a8b843b1deb56be1e60b6b74ce0b
Branches: PHP-8.0 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=6d6fadeb0d9923c3dd99a9381bd26a26bf28ccec
Log:
Improved error message for typed class properties with null as default value
Closes GH-6396.
Changed paths:
A Zend/tests/nullable_types/union_nullable_property_fails.phpt
M Zend/zend_compile.c
Diff:
diff --git a/Zend/tests/nullable_types/union_nullable_property_fails.phpt
b/Zend/tests/nullable_types/union_nullable_property_fails.phpt
new file mode 100644
index 00000000000..809f7d3032e
--- /dev/null
+++ b/Zend/tests/nullable_types/union_nullable_property_fails.phpt
@@ -0,0 +1,15 @@
+--TEST--
+Nullable default property error message
+--FILE--
+<?php
+
+class A {
+ public string|int $b = null;
+}
+
+$t = new A;
+$t->b;
+
+?>
+--EXPECTF--
+Fatal error: Default value for property of type string|int may not be null. Use the nullable type
string|int|null to allow null default value in %s on line %d
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 60b0948a478..0fd9c04330b 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -7042,10 +7042,13 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t
flags, z
&& !zend_is_valid_default_value(type, &value_zv)) {
zend_string *str = zend_type_to_string(type);
if (Z_TYPE(value_zv) == IS_NULL) {
+ ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
+ zend_string *nullable_str = zend_type_to_string(type);
+
zend_error_noreturn(E_COMPILE_ERROR,
"Default value for property of type %s may not be null. "
- "Use the nullable type ?%s to allow null default value",
- ZSTR_VAL(str), ZSTR_VAL(str));
+ "Use the nullable type %s to allow null default value",
+ ZSTR_VAL(str), ZSTR_VAL(nullable_str));
} else {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use %s as default value for property %s::$%s of type %s",