Skip to content

Commit cdc91d7

Browse files
jamesjwufredemmott
authored andcommitted
Fix case insensitivity of PHP keywords
Summary: This changes all PHP keywords + a few Hack ones to be case insensitive. Words like "dynamic" and "mixed" which are only valid keywords in PHP will mostly stay case sensitive(I changed a few of them anyway before I decided it was too annoying), but PHP builtins will be case insensitive after this diff. Fixes #8194 Reviewed By: oulgen, alexeyt Differential Revision: D7958203
1 parent 4715270 commit cdc91d7

37 files changed

Lines changed: 52 additions & 170 deletions

hphp/hack/src/hhbc/emit_expression.ml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ let is_special_function env e args =
266266
| A.Id (_, s) ->
267267
begin
268268
let n = List.length args in
269-
match s with
269+
match String.lowercase_ascii s with
270270
| "isset" -> n > 0
271271
| "empty" -> n = 1
272272
| "define" when is_global_namespace env ->
@@ -1657,25 +1657,29 @@ and emit_expr env ?last_pos ?(need_ref=false) (pos, expr_ as expr) =
16571657
| A.Obj_get (expr, prop, nullflavor) ->
16581658
let query_op = if need_ref then QueryOp.Empty else QueryOp.CGet in
16591659
emit_obj_get ~need_ref env pos None query_op expr prop nullflavor
1660-
| A.Call ((_, A.Id (_, "isset")), _, exprs, []) ->
1660+
1661+
| A.Call ((_, A.Id (_, id)), _, exprs, [])
1662+
when String.lowercase_ascii id = "isset" ->
16611663
emit_box_if_necessary pos need_ref @@ emit_call_isset_exprs env pos exprs
1662-
| A.Call ((_, A.Id (_, "empty")), _, [expr], []) ->
1664+
| A.Call ((_, A.Id (_, id)), _, [expr], [])
1665+
when String.lowercase_ascii id = "empty" ->
16631666
emit_box_if_necessary pos need_ref @@ emit_call_empty_expr env pos expr
1664-
| A.Call ((_, A.Id (_, "idx")), _, ([_; _] | [_; _; _] as es), _)
1665-
when not (jit_enable_rename_function ()) ->
1667+
| A.Call ((_, A.Id (_, id)), _, ([_; _] | [_; _; _] as es), _)
1668+
when String.lowercase_ascii id = "idx" && not (jit_enable_rename_function ()) ->
16661669
emit_box_if_necessary pos need_ref @@ emit_idx env pos es
1667-
| A.Call ((_, A.Id (_, "define")), _, [(_, A.String (_, s)); e], _)
1668-
when is_global_namespace env ->
1670+
| A.Call ((_, A.Id (_, id)), _, [(_, A.String (_, s)); e], _)
1671+
when String.lowercase_ascii id = "define" && is_global_namespace env ->
16691672
emit_box_if_necessary pos need_ref @@ emit_define env pos s e
1670-
| A.Call ((_, A.Id (_, "eval")), _, [expr], _) ->
1673+
| A.Call ((_, A.Id (_, id)), _, [expr], _) when String.lowercase_ascii id = "eval" ->
16711674
emit_box_if_necessary pos need_ref @@ emit_eval env pos expr
16721675
| A.Call ((_, A.Id (_, "class_alias")), _, es, _)
16731676
when is_global_namespace env ->
16741677
emit_pos_then pos @@
16751678
emit_box_if_necessary pos need_ref @@ emit_class_alias es
16761679
| A.Call ((_, A.Id (_, "get_class")), _, [], _) ->
16771680
emit_box_if_necessary pos need_ref @@ emit_get_class_no_args ()
1678-
| A.Call ((_, A.Id (_, ("exit" | "die"))), _, es, _) ->
1681+
| A.Call ((_, A.Id (_, s)), _, es, _)
1682+
when (String.lowercase_ascii s = "exit" || String.lowercase_ascii s = "die") ->
16791683
emit_pos_then pos @@
16801684
emit_exit env (List.hd es)
16811685
| A.Call _

hphp/hack/src/hhbc/emit_statement.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ let rec emit_stmt env (pos, st_) =
131131
instr_null;
132132
emit_return ~need_ref:false env;
133133
]
134-
| A.Expr (_, A.Call ((_, A.Id (_, "unset")), _, exprl, [])) ->
134+
| A.Expr (_, A.Call ((_, A.Id (_, s)), _, exprl, []))
135+
when String.lowercase_ascii s = "unset" ->
135136
gather (List.map exprl (emit_unset_expr env))
136137
| A.Return (Some (inner_pos, A.Await e)) ->
137138
gather [

hphp/hack/src/hhbc/emit_type_hint.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let rec hint_to_type_constraint
109109
then TC.make None []
110110
else TC.make (Some "mixed") []
111111

112-
| A.Happly ((_, "void"), []) when kind <> TypeDef ->
112+
| A.Happly ((_, s), []) when String.lowercase_ascii s = "void" && kind <> TypeDef ->
113113
if Emit_env.is_hh_syntax_enabled ()
114114
|| Hhbc_options.php7_scalar_types !Hhbc_options.compiler_options
115115
then TC.make None []

hphp/hack/src/hhbc/hhbc_id.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ module Function = struct
207207
| Some id ->
208208
if List.mem builtins_in_hh id && (Emit_env.is_hh_syntax_enabled ())
209209
then SU.prefix_namespace "HH" id, Some id
210-
else if List.mem builtins_at_top id
210+
else if List.mem builtins_at_top (String.lowercase_ascii id)
211211
then id, None
212212
else fq_id, backoff_id
213213
(* Likewise for top-level, with no namespace *)

hphp/hack/src/parser/full_fidelity_lexer.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,12 +1390,17 @@ let as_case_insensitive_keyword text =
13901390
non-lower versions in our codebase. *)
13911391
let lower = String.lowercase_ascii text in
13921392
match lower with
1393-
| "eval" | "isset" | "unset" | "empty" | "const" | "new"
1394-
| "and" | "or" | "xor" | "as" | "print" | "throw"
1395-
| "true" | "false" | "null" | "array" | "instanceof"
1396-
| "trait" | "class" | "interface" | "using" | "static" | "inout"
1397-
| "self" | "parent" | "__halt_compiler" | "foreach" | "echo"
1398-
| "abstract" | "final" -> lower
1393+
| "__halt_compiler" | "abstract" | "and" | "array" | "as" | "bool" | "break" | "callable"
1394+
| "case" | "catch" | "class" | "clone" | "const" | "continue" | "declare" | "default"
1395+
| "die" | "do" | "echo" | "else" | "elseif" | "empty" | "enddeclare" | "endfor"
1396+
| "endforeach" | "endif" | "endswitch" | "endwhile" | "eval" | "exit" | "extends" | "false"
1397+
| "final" | "finally" | "for" | "foreach" | "function" | "global" | "goto" | "if"
1398+
| "implements" | "include" | "include_once" | "inout" | "instanceof" | "insteadof" | "int"
1399+
| "interface" | "isset" | "list" | "namespace" | "new" | "null" | "or" | "parent"
1400+
| "print" | "private" | "protected" | "public" | "require" | "require_once"
1401+
| "return" | "self" | "static" | "switch" | "throw" | "trait"
1402+
| "try" | "true" | "unset" | "use" | "using" | "var" | "void" | "while"
1403+
| "xor" | "yield" -> lower
13991404
| _ -> text
14001405

14011406
let as_keyword kind lexer =
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
slow/parser/keyword_case/basic.php
2-
slow/parser/keyword_case/class.php
3-
slow/parser/keyword_case/control_flow.php
4-
slow/parser/keyword_case/eval.php
5-
slow/parser/keyword_case/exception.php
6-
slow/parser/keyword_case/hack_async_await.php
7-
slow/parser/keyword_case/hack_containers.php
8-
slow/parser/keyword_case/hack_generics.php
9-
slow/parser/keyword_case/hack_inout.php
10-
slow/parser/keyword_case/hack_type_newtype.php
11-
slow/parser/keyword_case/hack_using.php
12-
slow/parser/keyword_case/hack_xhp.php
13-
slow/parser/keyword_case/include.php
14-
slow/parser/keyword_case/misc.php
15-
slow/parser/keyword_case/namespace.php
16-
slow/parser/keyword_case/old_ends.php
17-
slow/php7_backported/array_destructuring.php
18-
slow/php7_backported/array_destructuring_byref.php
1+
slow/php7_backported/array_destructuring.php
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
<?php
22
function __autoload($name) {
3-
echo("AUTOLOAD '$name'\n");
4-
eval("class $name {}");
3+
echo("AUTOLOAD '$name'\n");
4+
eval("class $name {}");
55
}
66

77
class BASE {
88
}
99

10-
interface INT {
10+
interface CHILD {
1111
}
1212

13-
class A extends BASE implements INT {
13+
class A extends BASE implements CHILD {
1414
}
1515

1616
$a = new A;
1717
var_dump(is_a($a, "B1"));
1818
var_dump(is_a($a, "A"));
1919
var_dump(is_a($a, "BASE"));
20-
var_dump(is_a($a, "INT"));
20+
var_dump(is_a($a, "CHILD"));
2121
var_dump(is_subclass_of($a, "B2"));
2222
var_dump(is_subclass_of($a, "A"));
2323
var_dump(is_subclass_of($a, "BASE"));
24-
var_dump(is_subclass_of($a, "INT"));
25-
24+
var_dump(is_subclass_of($a, "CHILD"));
2625
var_dump(is_subclass_of("X1", "X2"));
2726
?>
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)