From: Nikita Popov Date: Mon, 23 Sep 2019 13:48:49 +0000 (+0200) Subject: Canonicalize typed properties error message X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d7ae123648c1a5b6e6ebec440e8a621eaa223543;p=php Canonicalize typed properties error message We have lots of other typed properties related error messages of the form "assign X to typed property Y::$z of type A", so use th same format for the primary message as well. Special-casing things like classes and nullability is not going to scale with future type-system extensions, and I don't think it really adds clarity either. --- diff --git a/Zend/tests/type_declarations/typed_properties_004.phpt b/Zend/tests/type_declarations/typed_properties_004.phpt index 03d9e41aac..2df693aa99 100644 --- a/Zend/tests/type_declarations/typed_properties_004.phpt +++ b/Zend/tests/type_declarations/typed_properties_004.phpt @@ -11,8 +11,8 @@ new class("PHP 7 is better than you, and it knows it ...") { }; ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Typed property class@anonymous::$int must be int, string used in %s:6 +Fatal error: Uncaught TypeError: Cannot assign string to property class@anonymous::$int of type int in %s:%d Stack trace: -#0 %s(2): class@anonymous->__construct('PHP 7 is better...') +#0 %s(%d): class@anonymous->__construct('PHP 7 is better...') #1 {main} - thrown in %s on line 6 + thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_005.phpt b/Zend/tests/type_declarations/typed_properties_005.phpt index b521963000..104f4bd57e 100644 --- a/Zend/tests/type_declarations/typed_properties_005.phpt +++ b/Zend/tests/type_declarations/typed_properties_005.phpt @@ -13,8 +13,8 @@ new class(new Dummy) { }; ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Typed property class@anonymous::$std must be an instance of stdClass, Dummy used in %s:8 +Fatal error: Uncaught TypeError: Cannot assign Dummy to property class@anonymous::$std of type stdClass in %s:%d Stack trace: -#0 %s(4): class@anonymous->__construct(Object(Dummy)) +#0 %s(%d): class@anonymous->__construct(Object(Dummy)) #1 {main} - thrown in %s on line 8 + thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_020.phpt b/Zend/tests/type_declarations/typed_properties_020.phpt index d32d17b1c8..3b16a9d81d 100644 --- a/Zend/tests/type_declarations/typed_properties_020.phpt +++ b/Zend/tests/type_declarations/typed_properties_020.phpt @@ -22,5 +22,5 @@ $foo = new Foo(); var_dump($foo->bar); ?> --EXPECT-- -Typed property Foo::$bar must be int, float used +Cannot assign float to property Foo::$bar of type int int(2) diff --git a/Zend/tests/type_declarations/typed_properties_029.phpt b/Zend/tests/type_declarations/typed_properties_029.phpt index a5418c91a8..9beca10627 100644 --- a/Zend/tests/type_declarations/typed_properties_029.phpt +++ b/Zend/tests/type_declarations/typed_properties_029.phpt @@ -12,7 +12,7 @@ $foo = new Foo; $foo->bar = "1"; ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, string used in %s:9 +Fatal error: Uncaught TypeError: Cannot assign string to property Foo::$bar of type int in %s:%d Stack trace: #0 {main} - thrown in %s on line 9 + thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_030.phpt b/Zend/tests/type_declarations/typed_properties_030.phpt index a835ae5fae..8feda02316 100644 --- a/Zend/tests/type_declarations/typed_properties_030.phpt +++ b/Zend/tests/type_declarations/typed_properties_030.phpt @@ -19,7 +19,7 @@ unset($foo->bar); # ok var_dump($foo->bar); # not okay, __get is nasty ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, string used in %s:16 +Fatal error: Uncaught TypeError: Cannot assign string to property Foo::$bar of type int in %s:%d Stack trace: #0 {main} - thrown in %s on line 16 + thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_034.phpt b/Zend/tests/type_declarations/typed_properties_034.phpt index c3885af39f..79317553ab 100644 --- a/Zend/tests/type_declarations/typed_properties_034.phpt +++ b/Zend/tests/type_declarations/typed_properties_034.phpt @@ -38,7 +38,7 @@ var_dump($foo); ?> --EXPECT-- int(42) -Typed property class@anonymous::$baz must be int, null used +Cannot assign null to property class@anonymous::$baz of type int int(1) int(10) int(10) diff --git a/Zend/tests/type_declarations/typed_properties_038.phpt b/Zend/tests/type_declarations/typed_properties_038.phpt index 44e5d04c7f..94a00d0d8c 100644 --- a/Zend/tests/type_declarations/typed_properties_038.phpt +++ b/Zend/tests/type_declarations/typed_properties_038.phpt @@ -44,7 +44,7 @@ object(class@anonymous)#1 (1) { ["bar"]=> int(%d) } -string(60) "Typed property class@anonymous::$bar must be int, float used" +string(65) "Cannot assign float to property class@anonymous::$bar of type int" object(class@anonymous)#1 (1) { ["bar"]=> int(%d) @@ -54,7 +54,7 @@ object(class@anonymous)#1 (1) { ["bar"]=> int(%d) } -string(60) "Typed property class@anonymous::$bar must be int, float used" +string(65) "Cannot assign float to property class@anonymous::$bar of type int" object(class@anonymous)#1 (1) { ["bar"]=> int(%d) diff --git a/Zend/tests/type_declarations/typed_properties_039.phpt b/Zend/tests/type_declarations/typed_properties_039.phpt index 62413d804c..47ae17f0ab 100644 --- a/Zend/tests/type_declarations/typed_properties_039.phpt +++ b/Zend/tests/type_declarations/typed_properties_039.phpt @@ -25,7 +25,7 @@ var_dump($objs); ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Typed property A::$foo must be int, A used in %s:%d +Fatal error: Uncaught TypeError: Cannot assign A to property A::$foo of type int in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_040.phpt b/Zend/tests/type_declarations/typed_properties_040.phpt index db2b90903c..0b47c92a87 100644 --- a/Zend/tests/type_declarations/typed_properties_040.phpt +++ b/Zend/tests/type_declarations/typed_properties_040.phpt @@ -19,7 +19,7 @@ var_dump($foo->bar); --EXPECTF-- string(3) "bar" -Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, null used in %s:14 +Fatal error: Uncaught TypeError: Cannot assign null to property Foo::$bar of type int in %s:%d Stack trace: #0 {main} - thrown in %s on line 14 + thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_046.phpt b/Zend/tests/type_declarations/typed_properties_046.phpt index 050e64a2a4..d95fe8dc24 100644 --- a/Zend/tests/type_declarations/typed_properties_046.phpt +++ b/Zend/tests/type_declarations/typed_properties_046.phpt @@ -22,8 +22,8 @@ for ($i = 0; $i < 5; $i++) { } } --EXPECT-- -Typed property Foo::$bbb must be int, string used -Typed property Foo::$bbb must be int, string used -Typed property Foo::$bbb must be int, string used -Typed property Foo::$bbb must be int, string used -Typed property Foo::$bbb must be int, string used +Cannot assign string to property Foo::$bbb of type int +Cannot assign string to property Foo::$bbb of type int +Cannot assign string to property Foo::$bbb of type int +Cannot assign string to property Foo::$bbb of type int +Cannot assign string to property Foo::$bbb of type int diff --git a/Zend/tests/type_declarations/typed_properties_047.phpt b/Zend/tests/type_declarations/typed_properties_047.phpt index 0b48cd1cd4..db78143b02 100644 --- a/Zend/tests/type_declarations/typed_properties_047.phpt +++ b/Zend/tests/type_declarations/typed_properties_047.phpt @@ -37,4 +37,4 @@ NULL int(5) NULL Typed property Foo::$foo must not be accessed before initialization -Typed property Foo::$foo must be int or null, string used +Cannot assign string to property Foo::$foo of type ?int diff --git a/Zend/tests/type_declarations/typed_properties_051.phpt b/Zend/tests/type_declarations/typed_properties_051.phpt index 1c8673d0d3..977570b238 100644 --- a/Zend/tests/type_declarations/typed_properties_051.phpt +++ b/Zend/tests/type_declarations/typed_properties_051.phpt @@ -24,4 +24,4 @@ try { ?> --EXPECT-- string(4) "okok" -Typed property A::$a must be string, C used +Cannot assign C to property A::$a of type string diff --git a/Zend/tests/type_declarations/typed_properties_056.phpt b/Zend/tests/type_declarations/typed_properties_056.phpt index 54013f44f2..4d2930cc3b 100644 --- a/Zend/tests/type_declarations/typed_properties_056.phpt +++ b/Zend/tests/type_declarations/typed_properties_056.phpt @@ -19,5 +19,5 @@ var_dump($o->foo); unset($o); ?> --EXPECT-- -Typed property A::$foo must be string, int used +Cannot assign int to property A::$foo of type string string(3) "100" diff --git a/Zend/tests/type_declarations/typed_properties_057.phpt b/Zend/tests/type_declarations/typed_properties_057.phpt index 6b53a42600..ebec0f48bf 100644 --- a/Zend/tests/type_declarations/typed_properties_057.phpt +++ b/Zend/tests/type_declarations/typed_properties_057.phpt @@ -25,7 +25,7 @@ var_dump($o->foo); unset($o); ?> --EXPECT-- -Typed property A::$foo must be string, int used +Cannot assign int to property A::$foo of type string string(3) "100" -Typed property A::$foo must be string, int used +Cannot assign int to property A::$foo of type string string(3) "100" diff --git a/Zend/tests/type_declarations/typed_properties_058.phpt b/Zend/tests/type_declarations/typed_properties_058.phpt index d9283bdbef..a6d65daa4c 100644 --- a/Zend/tests/type_declarations/typed_properties_058.phpt +++ b/Zend/tests/type_declarations/typed_properties_058.phpt @@ -28,5 +28,5 @@ for ($i = 0; $i < 2; $i++) { ?> --EXPECT-- int(5) -Typed property B::$foo must be string, int used -Typed property B::$foo must be string, int used +Cannot assign int to property B::$foo of type string +Cannot assign int to property B::$foo of type string diff --git a/Zend/tests/type_declarations/typed_properties_060.phpt b/Zend/tests/type_declarations/typed_properties_060.phpt index 559349b7a9..5b3c9d0981 100644 --- a/Zend/tests/type_declarations/typed_properties_060.phpt +++ b/Zend/tests/type_declarations/typed_properties_060.phpt @@ -16,7 +16,7 @@ $o->a = "a"; --EXPECTF-- int(1) -Fatal error: Uncaught TypeError: Typed property A::$a must be int, string used in %s:%d +Fatal error: Uncaught TypeError: Cannot assign string to property A::$a of type int in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/type_declarations/typed_properties_068.phpt b/Zend/tests/type_declarations/typed_properties_068.phpt index 7ed58d2a00..dc5cb1b5ff 100644 --- a/Zend/tests/type_declarations/typed_properties_068.phpt +++ b/Zend/tests/type_declarations/typed_properties_068.phpt @@ -69,14 +69,14 @@ int(4) Cannot assign null to reference held by property Foo::$i of type int int(4) int(4) -Typed property Foo::$i must be int, null used +Cannot assign null to property Foo::$i of type int int(4) int(4) string(1) "5" string(1) "5" int(0) int(0) -Typed property Foo::$i must be int, string used +Cannot assign string to property Foo::$i of type int int(0) string(1) "x" Reference with value of type string held by property Foo::$s of type string is not compatible with property Foo::$i of type int diff --git a/Zend/tests/type_declarations/typed_properties_069.phpt b/Zend/tests/type_declarations/typed_properties_069.phpt index 77cfc02487..0f804f8f06 100644 --- a/Zend/tests/type_declarations/typed_properties_069.phpt +++ b/Zend/tests/type_declarations/typed_properties_069.phpt @@ -22,6 +22,6 @@ var_dump(nonNumericStringRef()); ?> --EXPECT-- -Typed property Foo::$i must be int, string used +Cannot assign string to property Foo::$i of type int Typed static property Foo::$i must not be accessed before initialization string(1) "x" diff --git a/Zend/tests/type_declarations/typed_properties_070.phpt b/Zend/tests/type_declarations/typed_properties_070.phpt index dd77c68355..ec2d6bf8c6 100644 --- a/Zend/tests/type_declarations/typed_properties_070.phpt +++ b/Zend/tests/type_declarations/typed_properties_070.phpt @@ -43,7 +43,7 @@ string(2) "11" string(2) "13" string(2) "12" int(1) -Typed property Foo::$i must be int, float used +Cannot assign float to property Foo::$i of type int int(1) -Typed property Foo::$i must be int, string used +Cannot assign string to property Foo::$i of type int int(1) diff --git a/Zend/tests/type_declarations/typed_properties_074.phpt b/Zend/tests/type_declarations/typed_properties_074.phpt index 8e6be312c6..5a84435550 100644 --- a/Zend/tests/type_declarations/typed_properties_074.phpt +++ b/Zend/tests/type_declarations/typed_properties_074.phpt @@ -31,7 +31,7 @@ object(Test)#1 (1) { ["val"]=> uninitialized(int) } -Typed property Test::$val must be int, string used +Cannot assign string to property Test::$val of type int object(Test)#1 (1) { ["prop"]=> &string(1) "x" diff --git a/Zend/tests/type_declarations/typed_properties_075.phpt b/Zend/tests/type_declarations/typed_properties_075.phpt index 5f3943cbee..def86f3695 100644 --- a/Zend/tests/type_declarations/typed_properties_075.phpt +++ b/Zend/tests/type_declarations/typed_properties_075.phpt @@ -45,9 +45,9 @@ var_dump(Foo::$bar); --EXPECT-- string(70) "Cannot increment property Foo::$bar of type int past its maximal value" int(9223372036854775807) -string(48) "Typed property Foo::$bar must be int, float used" +string(53) "Cannot assign float to property Foo::$bar of type int" int(9223372036854775807) string(70) "Cannot increment property Foo::$bar of type int past its maximal value" int(9223372036854775807) -string(48) "Typed property Foo::$bar must be int, float used" +string(53) "Cannot assign float to property Foo::$bar of type int" int(9223372036854775807) diff --git a/Zend/tests/type_declarations/typed_properties_078.phpt b/Zend/tests/type_declarations/typed_properties_078.phpt index 6645970dd4..d8fc67d466 100644 --- a/Zend/tests/type_declarations/typed_properties_078.phpt +++ b/Zend/tests/type_declarations/typed_properties_078.phpt @@ -45,7 +45,7 @@ var_dump($ref instanceof ArrayIterator); --EXPECT-- array(0) { } -string(89) "Typed property class@anonymous::$t must be an instance of Traversable or null, array used" +string(72) "Cannot assign array to property class@anonymous::$t of type ?Traversable" array(0) { } array(1) { diff --git a/Zend/tests/type_declarations/typed_properties_093.phpt b/Zend/tests/type_declarations/typed_properties_093.phpt index f0c7ef51a9..1f4bf4cb8c 100644 --- a/Zend/tests/type_declarations/typed_properties_093.phpt +++ b/Zend/tests/type_declarations/typed_properties_093.phpt @@ -24,7 +24,7 @@ var_dump($test); ?> --EXPECT-- -Typed property Test::$prop must be int, string used +Cannot assign string to property Test::$prop of type int object(Test)#2 (0) { ["prop"]=> uninitialized(int) diff --git a/Zend/tests/type_declarations/typed_properties_095.phpt b/Zend/tests/type_declarations/typed_properties_095.phpt index edbef8dfc7..3f1027f08f 100644 --- a/Zend/tests/type_declarations/typed_properties_095.phpt +++ b/Zend/tests/type_declarations/typed_properties_095.phpt @@ -60,8 +60,8 @@ var_dump(_ZendTestClass::$staticIntProp); ?> --EXPECT-- int(123) -Typed property _ZendTestClass::$intProp must be int, string used -Typed property _ZendTestClass::$classProp must be an instance of stdClass or null, _ZendTestClass used +Cannot assign string to property _ZendTestClass::$intProp of type int +Cannot assign _ZendTestClass to property _ZendTestClass::$classProp of type ?stdClass object(_ZendTestClass)#1 (2) { ["intProp"]=> int(456) @@ -70,8 +70,8 @@ object(_ZendTestClass)#1 (2) { } } int(123) -Typed property _ZendTestClass::$intProp must be int, string used -Typed property _ZendTestClass::$classProp must be an instance of stdClass or null, Test used +Cannot assign string to property _ZendTestClass::$intProp of type int +Cannot assign Test to property _ZendTestClass::$classProp of type ?stdClass object(Test)#4 (2) { ["intProp"]=> int(456) @@ -80,5 +80,5 @@ object(Test)#4 (2) { } } int(123) -Typed property _ZendTestClass::$staticIntProp must be int, string used +Cannot assign string to property _ZendTestClass::$staticIntProp of type int int(456) diff --git a/Zend/tests/type_declarations/typed_properties_096.phpt b/Zend/tests/type_declarations/typed_properties_096.phpt index 32191b241e..83f086d345 100644 --- a/Zend/tests/type_declarations/typed_properties_096.phpt +++ b/Zend/tests/type_declarations/typed_properties_096.phpt @@ -31,7 +31,7 @@ var_dump($test); ?> --EXPECT-- -Typed property Test1::$prop must be an instance of Foobar, int used +Cannot assign int to property Test1::$prop of type Foobar object(Test1)#1 (1) { ["prop"]=> uninitialized(Foobar) diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index cf32ba55ac..1534222e5e 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -899,23 +899,12 @@ ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_i return; } - // TODO Switch to a more standard error message? - type_str = zend_type_to_string(ZEND_TYPE_WITHOUT_NULL(info->type)); - if (ZEND_TYPE_IS_CLASS(info->type)) { - zend_type_error("Typed property %s::$%s must be an instance of %s%s, %s used", - ZSTR_VAL(info->ce->name), - zend_get_unmangled_property_name(info->name), - ZSTR_VAL(type_str), - ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "", - Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property))); - } else { - zend_type_error("Typed property %s::$%s must be %s%s, %s used", - ZSTR_VAL(info->ce->name), - zend_get_unmangled_property_name(info->name), - ZSTR_VAL(type_str), - ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "", - Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property))); - } + type_str = zend_type_to_string(info->type); + zend_type_error("Cannot assign %s to property %s::$%s of type %s", + Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)), + ZSTR_VAL(info->ce->name), + zend_get_unmangled_property_name(info->name), + ZSTR_VAL(type_str)); zend_string_release(type_str); }