com php-src: Do not make scalar type hints reserved words, merely reserve class name: Zend/tests/typehints/scalar_reserved1.phpt
Zend/tests/typehints/scalar_reserved2.phpt Zend/tests/t ypehints/scalar_reserved3.phpt Zend/tests/typehints/sca lar_reserved4.phpt
Zend/tests/typehints/scalar_reserv ed5.phpt Zend/tests/typehints/scalar_reserved6.phpt Zend/zend_compile.c Zend/zend_language_parser.y
Zend/zend_language_scanner.l ext/reflection/tests/Re flecti

From: Date: Tue, 10 Feb 2015 15:48:05 +0000
Subject: com php-src: Do not make scalar type hints reserved words, merely reserve class name: Zend/tests/typehints/scalar_reserved1.phpt
Zend/tests/typehints/scalar_reserved2.phpt Zend/tests/t ypehints/scalar_reserved3.phpt Zend/tests/typehints/sca lar_reserved4.phpt
Zend/tests/typehints/scalar_reserv ed5.phpt Zend/tests/typehints/scalar_reserved6.phpt Zend/zend_compile.c Zend/zend_language_parser.y
Zend/zend_language_scanner.l ext/reflection/tests/Re flecti
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-85735@lists.php.net to get a copy of this message
Commit: 75a225d0d5eb38d8aba789cbe50ec9ae812592f2 Author: Andrea Faulds <ajf@ajf.me> Sat, 27 Dec 2014 17:47:33 +0000 Parents: c3c0f531a2c1436a8061f393ab0f328f63bbb203 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=75a225d0d5eb38d8aba789cbe50ec9ae812592f2 Log: Do not make scalar type hints reserved words, merely reserve class name Changed paths: A Zend/tests/typehints/scalar_reserved1.phpt A Zend/tests/typehints/scalar_reserved2.phpt A Zend/tests/typehints/scalar_reserved3.phpt A Zend/tests/typehints/scalar_reserved4.phpt A Zend/tests/typehints/scalar_reserved5.phpt A Zend/tests/typehints/scalar_reserved6.phpt M Zend/zend_compile.c M Zend/zend_language_parser.y M Zend/zend_language_scanner.l M ext/reflection/tests/ReflectionMethod_basic1.phpt M ext/reflection/tests/ReflectionMethod_basic2.phpt M ext/reflection/tests/ReflectionMethod_basic3.phpt M ext/reflection/tests/ReflectionMethod_basic4.phpt M ext/reflection/tests/ReflectionMethod_getModifiers_basic.phpt

diff --git a/Zend/tests/typehints/scalar_reserved1.phpt b/Zend/tests/typehints/scalar_reserved1.phpt new file mode 100644 index 0000000..8a94030 --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved1.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (1) +--FILE-- +<?php + +class integer {} +--EXPECTF-- +Fatal error: Cannot declare class integer because integer is a type name in %s on line %d diff --git a/Zend/tests/typehints/scalar_reserved2.phpt b/Zend/tests/typehints/scalar_reserved2.phpt new file mode 100644 index 0000000..aa08b9a --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved2.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (2) +--FILE-- +<?php + +class int {} +--EXPECTF-- +Fatal error: Cannot declare class int because int is a type name in %s on line %d diff --git a/Zend/tests/typehints/scalar_reserved3.phpt b/Zend/tests/typehints/scalar_reserved3.phpt new file mode 100644 index 0000000..9843f18 --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved3.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (3) +--FILE-- +<?php + +class float {} +--EXPECTF-- +Fatal error: Cannot declare class float because float is a type name in %s on line %d diff --git a/Zend/tests/typehints/scalar_reserved4.phpt b/Zend/tests/typehints/scalar_reserved4.phpt new file mode 100644 index 0000000..13eb8fb --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved4.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (4) +--FILE-- +<?php + +class string {} +--EXPECTF-- +Fatal error: Cannot declare class string because string is a type name in %s on line %d diff --git a/Zend/tests/typehints/scalar_reserved5.phpt b/Zend/tests/typehints/scalar_reserved5.phpt new file mode 100644 index 0000000..6722f0d --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved5.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (5) +--FILE-- +<?php + +class boolean {} +--EXPECTF-- +Fatal error: Cannot declare class boolean because boolean is a type name in %s on line %d diff --git a/Zend/tests/typehints/scalar_reserved6.phpt b/Zend/tests/typehints/scalar_reserved6.phpt new file mode 100644 index 0000000..fcc6a11 --- /dev/null +++ b/Zend/tests/typehints/scalar_reserved6.phpt @@ -0,0 +1,8 @@ +--TEST-- +Scalar type hint names cannot be used as class, trait or interface names (6) +--FILE-- +<?php + +class bool {} +--EXPECTF-- +Fatal error: Cannot declare class bool because bool is a type name in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index bdd0c17..6d792d2 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -32,6 +32,22 @@ #include "zend_language_scanner.h" #include "zend_inheritance.h" +struct _scalar_typehint_info { + const char* name; + const size_t name_len; + const zend_uchar type; +}; + +static const struct _scalar_typehint_info scalar_typehints[] = { + {"int", sizeof("int") - 1, IS_LONG}, + {"integer", sizeof("integer") - 1, IS_LONG}, + {"float", sizeof("float") - 1, IS_DOUBLE}, + {"string", sizeof("string") - 1, IS_STRING}, + {"bool", sizeof("bool") - 1, _IS_BOOL}, + {"boolean", sizeof("boolean") - 1, _IS_BOOL}, + {NULL, 0, IS_UNDEF} +}; + #define SET_NODE(target, src) do { \ target ## _type = (src)->op_type; \ if ((src)->op_type == IS_CONST) { \ @@ -3949,7 +3965,17 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_ } } else { zend_string *class_name = zend_ast_get_str(type_ast); + const struct _scalar_typehint_info *info = &scalar_typehints[0]; + while (info->name) { + if (class_name->len == info->name_len + && zend_binary_strcasecmp(class_name->val, info->name_len, info->name, info->name_len) == 0) { + arg_info->type_hint = info->type; + goto done; + } + info++; + } + if (zend_is_const_default_class_ref(type_ast)) { class_name = zend_resolve_class_name_ast(type_ast); } else { @@ -3959,9 +3985,15 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_ arg_info->type_hint = IS_OBJECT; arg_info->class_name = class_name; +done: if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) { + if (arg_info->class_name) { zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " "with a class type hint can only be NULL"); + } else if (Z_TYPE(default_node.u.constant) != arg_info->type_hint) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with a %s type hint can only be %s or NULL", class_name->val, class_name->val); + } } } } @@ -4558,6 +4590,7 @@ void zend_compile_class_decl(zend_ast *ast) /* {{{ */ zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry)); zend_op *opline; znode declare_node, extends_node; + const struct _scalar_typehint_info *info = &scalar_typehints[0]; if (CG(active_class_entry)) { zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested"); @@ -4589,6 +4622,14 @@ void zend_compile_class_decl(zend_ast *ast) /* {{{ */ "because the name is already in use", name->val); } + while (info->name) { + if (lcname->len == info->name_len && strcmp(lcname->val, info->name) == 0) { + zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s " + "because %s is a type name", name->val, info->name); + } + info++; + } + name = zend_new_interned_string(name); lcname = zend_new_interned_string(lcname); diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index bfb7787..3671ead 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -82,7 +82,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %left '*' '/' '%' %right '!' %nonassoc T_INSTANCEOF -%right '~' T_INC T_DEC T_DOUBLE_CAST T_STRING_CAST T_OBJECT_CAST cast_operator '@' +%right '~' T_INC T_DEC T_INT_CAST T_DOUBLE_CAST T_STRING_CAST T_ARRAY_CAST T_OBJECT_CAST T_BOOL_CAST T_UNSET_CAST '@' %right T_POW %right '[' %nonassoc T_NEW T_CLONE @@ -136,13 +136,13 @@ static YYSIZE_T zend_yytnamerr(char*, const char*); %token T_INSTANCEOF "instanceof (T_INSTANCEOF)" %token T_INC "++ (T_INC)" %token T_DEC "-- (T_DEC)" -%token T_INT_TYPE "int (T_INT_TYPE)" -%token T_FLOAT_TYPE "float (T_FLOAT_TYPE)" -%token T_STRING_TYPE "string (T_STRING_TYPE)" -%token T_BOOL_TYPE "bool (T_BOOL_TYPE)" +%token T_INT_CAST "(int) (T_INT_CAST)" %token T_DOUBLE_CAST "(double) (T_DOUBLE_CAST)" %token T_STRING_CAST "(string) (T_STRING_CAST)" +%token T_ARRAY_CAST "(array) (T_ARRAY_CAST)" %token T_OBJECT_CAST "(object) (T_OBJECT_CAST)" +%token T_BOOL_CAST "(bool) (T_BOOL_CAST)" +%token T_UNSET_CAST "(unset) (T_UNSET_CAST)" %token T_NEW "new (T_NEW)" %token T_CLONE "clone (T_CLONE)" %token T_EXIT "exit (T_EXIT)" @@ -573,15 +573,9 @@ optional_type: ; type: - T_ARRAY { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); } - | T_CALLABLE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); } - | T_INT_TYPE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_LONG); } - | T_FLOAT_TYPE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_DOUBLE); } - | T_STRING_TYPE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_STRING); } - | T_BOOL_TYPE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, _IS_BOOL); } - | T_ARRAY { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); } - | T_CALLABLE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); } - | name { $$ = $1; } + | T_ARRAY { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); } + | T_CALLABLE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); } + | name { $$ = $1; } ; return_type: @@ -863,27 +857,13 @@ expr_without_variable: | expr T_COALESCE expr { $$ = zend_ast_create(ZEND_AST_COALESCE, $1, $3); } | internal_functions_in_yacc { $$ = $1; } - | '(' T_INT_TYPE ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(IS_LONG, $4); } - | '(' T_FLOAT_TYPE ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(IS_DOUBLE, $4); } - | '(' T_STRING_TYPE ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(IS_STRING, $4); } - | '(' T_BOOL_TYPE ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(_IS_BOOL, $4); } - | '(' T_ARRAY ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(IS_ARRAY, $4); } - | '(' T_UNSET ')' expr - %prec cast_operator - { $$ = zend_ast_create_cast(IS_NULL, $4); } + | T_INT_CAST expr { $$ = zend_ast_create_cast(IS_LONG, $2); } | T_DOUBLE_CAST expr { $$ = zend_ast_create_cast(IS_DOUBLE, $2); } | T_STRING_CAST expr { $$ = zend_ast_create_cast(IS_STRING, $2); } + | T_ARRAY_CAST expr { $$ = zend_ast_create_cast(IS_ARRAY, $2); } | T_OBJECT_CAST expr { $$ = zend_ast_create_cast(IS_OBJECT, $2); } + | T_BOOL_CAST expr { $$ = zend_ast_create_cast(_IS_BOOL, $2); } + | T_UNSET_CAST expr { $$ = zend_ast_create_cast(IS_NULL, $2); } | T_EXIT exit_expr { $$ = zend_ast_create(ZEND_AST_EXIT, $2); } | '@' expr { $$ = zend_ast_create(ZEND_AST_SILENCE, $2); } | scalar { $$ = $1; } diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index 9c87c18..0081192 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -1309,32 +1309,32 @@ NEWLINE ("\r"|"\n"|"\r\n") return T_VAR; } -<ST_IN_SCRIPTING>("int"|"integer") { - return T_INT_TYPE; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("int"|"integer"){TABS_AND_SPACES}")" { + return T_INT_CAST; } -<ST_IN_SCRIPTING>"float" { - return T_FLOAT_TYPE; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("real"|"double"|"float"){TABS_AND_SPACES}")" { + return T_DOUBLE_CAST; } -<ST_IN_SCRIPTING>"string" { - return T_STRING_TYPE; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("string"|"binary"){TABS_AND_SPACES}")" { + return T_STRING_CAST; } -<ST_IN_SCRIPTING>("bool"|"boolean") { - return T_BOOL_TYPE; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"array"{TABS_AND_SPACES}")" { + return T_ARRAY_CAST; } -<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("real"|"double"){TABS_AND_SPACES}")" { - return T_DOUBLE_CAST; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" { + return T_OBJECT_CAST; } -<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("binary"){TABS_AND_SPACES}")" { - return T_STRING_CAST; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("bool"|"boolean"){TABS_AND_SPACES}")" { + return T_BOOL_CAST; } -<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}"object"{TABS_AND_SPACES}")" { - return T_OBJECT_CAST; +<ST_IN_SCRIPTING>"("{TABS_AND_SPACES}("unset"){TABS_AND_SPACES}")" { + return T_UNSET_CAST; } <ST_IN_SCRIPTING>"eval" { diff --git a/ext/reflection/tests/ReflectionMethod_basic1.phpt b/ext/reflection/tests/ReflectionMethod_basic1.phpt index aeabcc6..75ab957 100644 --- a/ext/reflection/tests/ReflectionMethod_basic1.phpt +++ b/ext/reflection/tests/ReflectionMethod_basic1.phpt @@ -48,7 +48,7 @@ class TestClass class DerivedClass extends TestClass {} interface TestInterface { - public function inter(); + public function int(); } reflectMethod("DerivedClass", "foo"); @@ -56,7 +56,7 @@ reflectMethod("TestClass", "stat"); reflectMethod("TestClass", "priv"); reflectMethod("TestClass", "prot"); reflectMethod("DerivedClass", "prot"); -reflectMethod("TestInterface", "inter"); +reflectMethod("TestInterface", "int"); reflectMethod("ReflectionProperty", "__construct"); reflectMethod("TestClass", "__destruct"); @@ -208,7 +208,7 @@ bool(false) ********************************** ********************************** -Reflecting on method TestInterface::inter() +Reflecting on method TestInterface::int() isFinal(): diff --git a/ext/reflection/tests/ReflectionMethod_basic2.phpt b/ext/reflection/tests/ReflectionMethod_basic2.phpt index 8bc7705..c91af67 100644 --- a/ext/reflection/tests/ReflectionMethod_basic2.phpt +++ b/ext/reflection/tests/ReflectionMethod_basic2.phpt @@ -36,7 +36,7 @@ class TestClass class DerivedClass extends TestClass {} interface TestInterface { - public function inter(); + public function int(); } reflectMethod("DerivedClass", "foo"); @@ -44,7 +44,7 @@ reflectMethod("TestClass", "stat"); reflectMethod("TestClass", "priv"); reflectMethod("TestClass", "prot"); reflectMethod("DerivedClass", "prot"); -reflectMethod("TestInterface", "inter"); +reflectMethod("TestInterface", "int"); reflectMethod("ReflectionProperty", "__construct"); reflectMethod("TestClass", "__destruct"); @@ -131,16 +131,16 @@ string(%d) "Method [ <user, inherits TestClass> protected method prot ] { ********************************** ********************************** -Reflecting on method TestInterface::inter() +Reflecting on method TestInterface::int() __toString(): -string(%d) "Method [ <user> abstract public method inter ] { +string(%d) "Method [ <user> abstract public method int ] { @@ %s 36 - 36 } " export(): -string(%d) "Method [ <user> abstract public method inter ] { +string(%d) "Method [ <user> abstract public method int ] { @@ %s 36 - 36 } " diff --git a/ext/reflection/tests/ReflectionMethod_basic3.phpt b/ext/reflection/tests/ReflectionMethod_basic3.phpt index ad6c663..7b65927 100644 --- a/ext/reflection/tests/ReflectionMethod_basic3.phpt +++ b/ext/reflection/tests/ReflectionMethod_basic3.phpt @@ -38,7 +38,7 @@ class TestClass class DerivedClass extends TestClass {} interface TestInterface { - public function inter(); + public function int(); } reflectMethod("DerivedClass", "foo"); @@ -46,7 +46,7 @@ reflectMethod("TestClass", "stat"); reflectMethod("TestClass", "priv"); reflectMethod("TestClass", "prot"); reflectMethod("DerivedClass", "prot"); -reflectMethod("TestInterface", "inter"); +reflectMethod("TestInterface", "int"); reflectMethod("ReflectionProperty", "__construct"); reflectMethod("TestClass", "__destruct"); @@ -124,11 +124,11 @@ bool(true) ********************************** ********************************** -Reflecting on method TestInterface::inter() +Reflecting on method TestInterface::int() getName(): -string(5) "inter" +string(3) "int" isInternal(): bool(false) diff --git a/ext/reflection/tests/ReflectionMethod_basic4.phpt b/ext/reflection/tests/ReflectionMethod_basic4.phpt index a0c2ddc..82672e4 100644 --- a/ext/reflection/tests/ReflectionMethod_basic4.phpt +++ b/ext/reflection/tests/ReflectionMethod_basic4.phpt @@ -42,7 +42,7 @@ class TestClass class DerivedClass extends TestClass {} interface TestInterface { - public function inter(); + public function int(); } reflectMethod("DerivedClass", "foo"); @@ -50,7 +50,7 @@ reflectMethod("TestClass", "stat"); reflectMethod("TestClass", "priv"); reflectMethod("TestClass", "prot"); reflectMethod("DerivedClass", "prot"); -reflectMethod("TestInterface", "inter"); +reflectMethod("TestInterface", "int"); reflectMethod("ReflectionProperty", "__construct"); reflectMethod("TestClass", "__destruct"); @@ -127,7 +127,7 @@ int(34) ********************************** ********************************** -Reflecting on method TestInterface::inter() +Reflecting on method TestInterface::int() getFileName(): diff --git a/ext/reflection/tests/ReflectionMethod_getModifiers_basic.phpt b/ext/reflection/tests/ReflectionMethod_getModifiers_basic.phpt index bb43be0..72baa53 100644 --- a/ext/reflection/tests/ReflectionMethod_getModifiers_basic.phpt +++ b/ext/reflection/tests/ReflectionMethod_getModifiers_basic.phpt @@ -60,7 +60,7 @@ class TestClass class DerivedClass extends TestClass {} interface TestInterface { - public function inter(); + public function int(); public function __clone(); } @@ -222,7 +222,7 @@ Modifiers for method TestClass::__autoload(): 0x08010100 -Modifiers for method TestInterface::inter(): +Modifiers for method TestInterface::int(): 0x08000102
« previous php.cvs (#85735) next »