PHANTOM
🇮🇳 IN
Skip to content

Commit a3f6573

Browse files
Kenneth Lihhvm-bot
authored andcommitted
Implement new attribute syntax
Summary: This diff implements the new syntax for attributes (`@__Memoize` instead of `<<__Memoize>>`), gated behind the `allow_new_attribute_syntax` switch. This also disables the legacy soft typehint syntax due to the conflicting use of the `@` sign. Reviewed By: vassilmladenov Differential Revision: D16094168 fbshipit-source-id: 324e8b18355a551a8625e952f5b3c16f789e8b32
1 parent cc1a696 commit a3f6573

File tree

75 files changed

+1262
-335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1262
-335
lines changed

hphp/hack/src/facts/facts_smart_constructors.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,15 @@ module SC = struct
223223
let make_classish_body _left_brace elements _right_brace st =
224224
st, elements
225225

226-
let make_attribute_specification _left_double_angle attributes _right_double_angle st =
226+
let make_old_attribute_specification _left_double_angle attributes _right_double_angle st =
227227
st, attributes
228228

229+
let make_attribute_specification attributes st =
230+
st, attributes
231+
232+
let make_attribute _at attribute st =
233+
st, attribute
234+
229235
let make_constructor_call class_type _left_paren argument_list _right_paren st =
230236
st, ListItem (class_type, argument_list)
231237

hphp/hack/src/facts/flatten_smart_constructors.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,15 @@ module WithOp(Op : Op_S) = struct
156156
let make_variadic_parameter arg0 arg1 arg2 state =
157157
if Op.is_zero arg0 && Op.is_zero arg1 && Op.is_zero arg2 then state, Op.zero
158158
else state, Op.flatten [arg0; arg1; arg2]
159-
let make_attribute_specification arg0 arg1 arg2 state =
159+
let make_old_attribute_specification arg0 arg1 arg2 state =
160160
if Op.is_zero arg0 && Op.is_zero arg1 && Op.is_zero arg2 then state, Op.zero
161161
else state, Op.flatten [arg0; arg1; arg2]
162+
let make_attribute_specification arg0 state =
163+
if Op.is_zero arg0 then state, Op.zero
164+
else state, Op.flatten [arg0]
165+
let make_attribute arg0 arg1 state =
166+
if Op.is_zero arg0 && Op.is_zero arg1 then state, Op.zero
167+
else state, Op.flatten [arg0; arg1]
162168
let make_inclusion_expression arg0 arg1 state =
163169
if Op.is_zero arg0 && Op.is_zero arg1 then state, Op.zero
164170
else state, Op.flatten [arg0; arg1]

hphp/hack/src/hackfmt/hack_format.ml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ let rec t (env: Env.t) (node: Syntax.t) : Doc.t =
170170
| Syntax.NullableTypeSpecifier _
171171
| Syntax.LikeTypeSpecifier _
172172
| Syntax.SoftTypeSpecifier _
173-
| Syntax.AttributizedSpecifier _
174173
| Syntax.ListItem _
175174
| Syntax.PocketAtomExpression _
176175
| Syntax.PocketIdentifierExpression _
@@ -763,7 +762,7 @@ let rec t (env: Env.t) (node: Syntax.t) : Doc.t =
763762
parameter_name = name;
764763
parameter_default_value = default } ->
765764
Concat [
766-
t env attr;
765+
handle_attribute_spec env attr ~always_split:false;
767766
when_present attr space;
768767
t env visibility;
769768
when_present visibility space;
@@ -809,11 +808,21 @@ let rec t (env: Env.t) (node: Syntax.t) : Doc.t =
809808
right_da;
810809
Newline;
811810
]
812-
| Syntax.AttributeSpecification {
813-
attribute_specification_left_double_angle = left_da;
814-
attribute_specification_attributes = attrs;
815-
attribute_specification_right_double_angle = right_da; } ->
816-
transform_argish env ~allow_trailing:false left_da attrs right_da
811+
| Syntax.OldAttributeSpecification _
812+
| Syntax.AttributeSpecification _ ->
813+
handle_attribute_spec env node ~always_split:true
814+
| Syntax.Attribute { attribute_at = at; attribute_attribute_name = attr } ->
815+
Concat [
816+
t env at;
817+
t env attr;
818+
]
819+
| Syntax.AttributizedSpecifier {
820+
attributized_specifier_attribute_spec = attr_spec;
821+
attributized_specifier_type = attr_type; } ->
822+
Concat [
823+
handle_attribute_spec env attr_spec ~always_split:false;
824+
t env attr_type;
825+
]
817826
| Syntax.InclusionExpression {
818827
inclusion_require = kw;
819828
inclusion_filename = expr; } ->
@@ -2243,6 +2252,22 @@ and separate_with_space_split is_last =
22432252
then Nothing
22442253
else space_split ()
22452254

2255+
and handle_attribute_spec env node ~always_split =
2256+
match Syntax.syntax node with
2257+
| Syntax.OldAttributeSpecification {
2258+
old_attribute_specification_left_double_angle = left_da;
2259+
old_attribute_specification_attributes = attrs;
2260+
old_attribute_specification_right_double_angle = right_da; } ->
2261+
transform_argish env ~allow_trailing:false left_da attrs right_da
2262+
| Syntax.AttributeSpecification { attribute_specification_attributes = attrs } ->
2263+
handle_possible_list env ~after_each:(fun is_last ->
2264+
if always_split then Newline
2265+
else if is_last then Concat [Space; SplitWith Cost.Moderate]
2266+
else Space
2267+
) attrs
2268+
| Syntax.Missing -> Nothing
2269+
| _ -> failwith "Attribute specification expected"
2270+
22462271
and handle_lambda_body env node =
22472272
match Syntax.syntax node with
22482273
| Syntax.CompoundStatement {

hphp/hack/src/hh_parse.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module FullFidelityParseArgs = struct
7070
enable_class_level_where_clauses : bool;
7171
disable_legacy_soft_typehints : bool;
7272
disable_outside_dollar_str_interp : bool;
73+
allow_new_attribute_syntax : bool;
7374
}
7475

7576
let make
@@ -103,6 +104,7 @@ module FullFidelityParseArgs = struct
103104
enable_class_level_where_clauses
104105
disable_legacy_soft_typehints
105106
disable_outside_dollar_str_interp
107+
allow_new_attribute_syntax
106108
= {
107109
full_fidelity_json;
108110
full_fidelity_dot;
@@ -134,6 +136,7 @@ module FullFidelityParseArgs = struct
134136
enable_class_level_where_clauses;
135137
disable_legacy_soft_typehints;
136138
disable_outside_dollar_str_interp;
139+
allow_new_attribute_syntax;
137140
}
138141

139142
let parse_args () =
@@ -183,6 +186,7 @@ module FullFidelityParseArgs = struct
183186
let enable_class_level_where_clauses = ref false in
184187
let disable_legacy_soft_typehints = ref false in
185188
let disable_outside_dollar_str_interp = ref false in
189+
let allow_new_attribute_syntax = ref false in
186190
let options = [
187191
(* modes *)
188192
"--full-fidelity-json",
@@ -304,6 +308,9 @@ No errors are filtered out.";
304308
"--disable-outside-dollar-str-interp",
305309
Arg.Set disable_outside_dollar_str_interp,
306310
"Disables ${x} syntax for string interpolation (use {$x} instead)";
311+
"--allow-new-attribute-syntax",
312+
Arg.Set allow_new_attribute_syntax,
313+
"Allow the new @ attribute syntax (disables legacy soft typehints)";
307314
] in
308315
Arg.parse options push_file usage;
309316
let modes = [
@@ -350,6 +357,7 @@ No errors are filtered out.";
350357
!enable_class_level_where_clauses
351358
!disable_legacy_soft_typehints
352359
!disable_outside_dollar_str_interp
360+
!allow_new_attribute_syntax
353361
end
354362

355363
open FullFidelityParseArgs
@@ -376,6 +384,7 @@ let handle_existing_file args filename =
376384
args.disable_legacy_soft_typehints in
377385
let popt = ParserOptions.with_disable_outside_dollar_str_interp popt
378386
args.disable_outside_dollar_str_interp in
387+
let popt = ParserOptions.with_allow_new_attribute_syntax popt args.allow_new_attribute_syntax in
379388

380389
(* Parse with the full fidelity parser *)
381390
let file = Relative_path.create Relative_path.Dummy filename in
@@ -385,6 +394,7 @@ let handle_existing_file args filename =
385394
~disable_lval_as_an_expression:args.disable_lval_as_an_expression
386395
~rust:args.rust
387396
~disable_legacy_soft_typehints:args.disable_legacy_soft_typehints
397+
~allow_new_attribute_syntax:args.allow_new_attribute_syntax
388398
?mode () in
389399
let syntax_tree = SyntaxTree.make ~env source_text in
390400
let editable = SyntaxTransforms.editable_from_positioned syntax_tree in

hphp/hack/src/hh_single_compile.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ let make_popt () =
473473
~rust:(use_rust_parser co)
474474
~disable_legacy_soft_typehints:(disable_legacy_soft_typehints co)
475475
~disable_outside_dollar_str_interp:(disable_outside_dollar_str_interp co)
476+
~allow_new_attribute_syntax:(allow_new_attribute_syntax co)
476477

477478
let process_single_source_unit compiler_options
478479
handle_output handle_exception filename source_text source_root =

hphp/hack/src/hh_single_type_check.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ let parse_options () =
214214
let use_new_type_errors = ref false in
215215
let disable_outside_dollar_str_interp = ref false in
216216
let disable_linter_fixmes = ref false in
217+
let allow_new_attribute_syntax = ref false in
217218
let options = [
218219
"--ai",
219220
Arg.String (set_ai),
@@ -457,6 +458,9 @@ let parse_options () =
457458
"--disable-linter-fixmes",
458459
Arg.Set disable_linter_fixmes,
459460
"Disables HH_FIXME and HH_IGNORE_ERROR for 5000-5999 error codes";
461+
"--allow-new-attribute-syntax",
462+
Arg.Set allow_new_attribute_syntax,
463+
"Allow the new @ attribute syntax (disables legacy soft typehints)";
460464
] in
461465
let options = Arg.align ~limit:25 options in
462466
Arg.parse options (fun fn -> fn_ref := fn::(!fn_ref)) usage;
@@ -500,6 +504,7 @@ let parse_options () =
500504
~use_new_type_errors:!use_new_type_errors
501505
~po_disable_outside_dollar_str_interp:!disable_outside_dollar_str_interp
502506
~disable_linter_fixmes:!disable_linter_fixmes
507+
~po_allow_new_attribute_syntax:!allow_new_attribute_syntax
503508
()
504509
in
505510
let tcopt = {

hphp/hack/src/hhbc/hhbc_options.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type t = {
6262
option_enable_class_level_where_clauses : bool;
6363
option_disable_legacy_soft_typehints : bool;
6464
option_disable_outside_dollar_str_interp : bool;
65+
option_allow_new_attribute_syntax : bool;
6566
}
6667

6768
let default = {
@@ -117,6 +118,7 @@ let default = {
117118
option_enable_class_level_where_clauses = false;
118119
option_disable_legacy_soft_typehints = false;
119120
option_disable_outside_dollar_str_interp = false;
121+
option_allow_new_attribute_syntax = false;
120122
}
121123

122124
let constant_folding o = o.option_constant_folding
@@ -169,6 +171,7 @@ let enable_constant_visibility_modifiers o = o.option_enable_constant_visibility
169171
let enable_class_level_where_clauses o = o.option_enable_class_level_where_clauses
170172
let disable_legacy_soft_typehints o = o.option_disable_legacy_soft_typehints
171173
let disable_outside_dollar_str_interp o = o.option_disable_outside_dollar_str_interp
174+
let allow_new_attribute_syntax o = o.option_allow_new_attribute_syntax
172175

173176
let to_string o =
174177
let dynamic_invokes =
@@ -225,6 +228,7 @@ let to_string o =
225228
; Printf.sprintf "enable_class_level_where_clauses: %B" @@ enable_class_level_where_clauses o
226229
; Printf.sprintf "disable_legacy_soft_typehints: %B" @@ disable_legacy_soft_typehints o
227230
; Printf.sprintf "disable_outside_dollar_str_interp: %B" @@ disable_outside_dollar_str_interp o
231+
; Printf.sprintf "allow_new_attribute_syntax: %B" @@ allow_new_attribute_syntax o
228232
]
229233

230234
let as_bool s =
@@ -340,6 +344,8 @@ let set_option options name value =
340344
{ options with option_disable_legacy_soft_typehints = as_bool value }
341345
| "hhvm.lang.disable_outside_dollar_str_interp" ->
342346
{ options with option_disable_outside_dollar_str_interp = as_bool value }
347+
| "hhvm.lang.allow_new_attribute_syntax" ->
348+
{ options with option_allow_new_attribute_syntax = as_bool value }
343349
| _ -> options
344350

345351
let get_value_from_config_ config key =
@@ -496,6 +502,8 @@ let value_setters = [
496502
fun opts v -> { opts with option_disable_legacy_soft_typehints = (v = 1) });
497503
(set_value "hhvm.hack.lang.disable_outside_dollar_str_interp" get_value_from_config_int @@
498504
fun opts v -> { opts with option_disable_outside_dollar_str_interp = (v = 1) });
505+
(set_value "hhvm.hack.lang.allow_new_attribute_syntax" get_value_from_config_int @@
506+
fun opts v -> { opts with option_allow_new_attribute_syntax = (v = 1) });
499507
]
500508

501509
let extract_config_options_from_json ~init config_json =

hphp/hack/src/options/globalOptions.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type t = {
6969
po_disable_outside_dollar_str_interp : bool;
7070
disable_linter_fixmes: bool;
7171
po_disallowed_decl_fixmes: ISet.t;
72+
po_allow_new_attribute_syntax : bool;
7273
} [@@deriving show]
7374

7475
let tco_experimental_instanceof = "instanceof"
@@ -257,6 +258,7 @@ let default = {
257258
po_disable_outside_dollar_str_interp = false;
258259
disable_linter_fixmes = false;
259260
po_disallowed_decl_fixmes = ISet.of_list [];
261+
po_allow_new_attribute_syntax = false;
260262
}
261263

262264
let make
@@ -320,6 +322,7 @@ let make
320322
?(po_disable_outside_dollar_str_interp = default.po_disable_outside_dollar_str_interp)
321323
?(disable_linter_fixmes = default.disable_linter_fixmes)
322324
?(po_disallowed_decl_fixmes = default.po_disallowed_decl_fixmes)
325+
?(po_allow_new_attribute_syntax = default.po_allow_new_attribute_syntax)
323326
()
324327
= {
325328
tco_safe_array;
@@ -383,6 +386,7 @@ let make
383386
po_disable_outside_dollar_str_interp;
384387
disable_linter_fixmes;
385388
po_disallowed_decl_fixmes;
389+
po_allow_new_attribute_syntax;
386390
}
387391
let tco_safe_array t = t.tco_safe_array
388392
let tco_safe_vector_array t = t.tco_safe_vector_array
@@ -471,6 +475,8 @@ let disable_linter_fixmes t = t.disable_linter_fixmes
471475

472476
let po_disallowed_decl_fixmes t = t.po_disallowed_decl_fixmes
473477

478+
let po_allow_new_attribute_syntax t = t.po_allow_new_attribute_syntax
479+
474480
let setup_pocket_universes env enabled =
475481
let exp_features = env.tco_experimental_features in
476482
let exp_features = if enabled then

hphp/hack/src/options/globalOptions.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ type t = {
279279
(* Set of error codes disallowed in decl positions *)
280280
po_disallowed_decl_fixmes : ISet.t;
281281

282+
(* Switch from <<...>> to @ for attributes *)
283+
po_allow_new_attribute_syntax : bool;
282284
} [@@deriving show]
283285

284286
val make :
@@ -342,6 +344,7 @@ val make :
342344
?po_disable_outside_dollar_str_interp: bool ->
343345
?disable_linter_fixmes : bool ->
344346
?po_disallowed_decl_fixmes: ISet.t ->
347+
?po_allow_new_attribute_syntax : bool ->
345348
unit ->
346349
t
347350

@@ -429,3 +432,4 @@ val use_new_type_errors : t -> bool
429432
val po_disable_outside_dollar_str_interp : t -> bool
430433
val disable_linter_fixmes : t -> bool
431434
val po_disallowed_decl_fixmes : t -> ISet.t
435+
val po_allow_new_attribute_syntax : t -> bool

hphp/hack/src/options/parserOptions.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ let with_enable_constant_visibility_modifiers po b =
3838
{ po with GlobalOptions.po_enable_constant_visibility_modifiers = b }
3939

4040
let disable_legacy_soft_typehints = GlobalOptions.po_disable_legacy_soft_typehints
41-
4241
let with_disable_legacy_soft_typehints po b =
4342
{ po with GlobalOptions.po_disable_legacy_soft_typehints = b }
4443

@@ -47,6 +46,10 @@ let with_disable_outside_dollar_str_interp po b =
4746

4847
let disallowed_decl_fixmes = GlobalOptions.po_disallowed_decl_fixmes
4948

49+
let allow_new_attribute_syntax = GlobalOptions.po_allow_new_attribute_syntax
50+
let with_allow_new_attribute_syntax po b =
51+
{ po with GlobalOptions.po_allow_new_attribute_syntax = b }
52+
5053
let make
5154
~auto_namespace_map
5255
~codegen
@@ -60,6 +63,7 @@ let make
6063
~enable_class_level_where_clauses
6164
~disable_legacy_soft_typehints
6265
~disable_outside_dollar_str_interp
66+
~allow_new_attribute_syntax
6367
= GlobalOptions.{
6468
default with
6569
po_auto_namespace_map = auto_namespace_map;
@@ -74,4 +78,5 @@ let make
7478
po_enable_class_level_where_clauses = enable_class_level_where_clauses;
7579
po_disable_legacy_soft_typehints = disable_legacy_soft_typehints;
7680
po_disable_outside_dollar_str_interp = disable_outside_dollar_str_interp;
81+
po_allow_new_attribute_syntax = allow_new_attribute_syntax;
7782
}

0 commit comments

Comments
 (0)