Skip to content

Commit

Permalink
Add raw attribute value to attributes
Browse files Browse the repository at this point in the history
Summary:
Attributes contain aribtrary static expressions. Those expressions can be nested and include nodes that we do not want to parse in the fast decl parser.
We can still return the raw string value of the attribute so we could either use it in hack with existing custom parsing or pass it back to higher fidelity parsing via an additional api call.

Reviewed By: patzar

Differential Revision: D56884916

fbshipit-source-id: ecb777782e6517c47ca4f3836c87a3cb71530f62
  • Loading branch information
nt591 authored and facebook-github-bot committed May 17, 2024
1 parent 367c418 commit 6de8271
Show file tree
Hide file tree
Showing 39 changed files with 172 additions and 75 deletions.
1 change: 1 addition & 0 deletions hphp/hack/hhi/ext_decl.hhi
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace HH {
type ExtDeclAttribute = shape(
'name' => string,
?'args' => vec<string>,
?'raw_val' => string,
);

type ExtDeclTypeConstraint = shape(
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/decl/decl_hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and aast_user_attribute_to_decl_user_attribute
| (_, _, Aast.String s) -> Some (Typing_defs_core.String s)
| (_, _, Aast.Int i) -> Some (Typing_defs_core.Int i)
| _ -> None);
ua_raw_val = None;
}

and aast_contexts_to_decl_capability env ctxs default_pos :
Expand Down
4 changes: 2 additions & 2 deletions hphp/hack/src/decl/decl_pos_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ struct
ttc_is_ctx = tc.ttc_is_ctx;
}

and user_attribute { ua_name; ua_params } =
{ ua_name = positioned_id ua_name; ua_params }
and user_attribute { ua_name; ua_params; _ } =
{ ua_name = positioned_id ua_name; ua_params; ua_raw_val = None }

and type_param t =
{
Expand Down
16 changes: 14 additions & 2 deletions hphp/hack/src/decl/direct_decl_smart_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ pub struct UserAttributeNode<'a> {
params: &'a [AttributeParam<'a>],
/// This is only used for __Deprecated attribute message and CIPP parameters
string_literal_param: Option<(&'a Pos<'a>, &'a BStr)>,
raw_val: Option<&'a str>,
}

mod fixed_width_token {
Expand Down Expand Up @@ -1136,6 +1137,7 @@ impl<'a> Node<'a> {
name: Id(_pos, attr_name),
params: [],
string_literal_param: None,
raw_val: None,
}) => attr_name == name,
_ => false,
})
Expand Down Expand Up @@ -1224,6 +1226,7 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> Impl<'a, 'o, 't, S> {
AttributeParam::String(_, s) => UAP::String(s),
AttributeParam::Int(i) => UAP::Int(i),
})),
raw_val: attr.raw_val,
})
}

Expand Down Expand Up @@ -5144,6 +5147,7 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
user_attributes.push(self.alloc(shallow_decl_defs::UserAttribute {
name: (name.0, "__EnumClass"),
params: &[],
raw_val: None,
}));
// Match ordering of attributes produced by the OCaml decl parser (even
// though it's the reverse of the syntactic ordering).
Expand Down Expand Up @@ -5545,9 +5549,9 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
fn make_constructor_call(
&mut self,
name: Self::Output,
_left_paren: Self::Output,
left_paren: Self::Output,
args: Self::Output,
_right_paren: Self::Output,
right_paren: Self::Output,
) -> Self::Output {
let unqualified_name = match self.expect_name(name) {
Some(name) => name,
Expand Down Expand Up @@ -5599,10 +5603,18 @@ impl<'a, 'o, 't, S: SourceTextAllocator<'t, 'a>> FlattenSmartConstructors
AttributeParam::String(pos, s) => Some((pos, s)),
_ => None,
});
let raw_val = self.opts.include_assignment_values.then(|| {
self.str_from_utf8(self.source_text.source_text().sub(
self.get_pos(left_paren).end_offset(),
self.get_pos(right_paren).start_offset() - self.get_pos(left_paren).end_offset(),
))
.trim()
});
Node::Attribute(self.alloc(UserAttributeNode {
name,
params,
string_literal_param,
raw_val,
}))
}

Expand Down
3 changes: 2 additions & 1 deletion hphp/hack/src/decl/shallow_class_diff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ let diff_enum_types

let diff_enum_type_options = diff_options ~diff:diff_enum_types

let user_attribute_name_value { Typing_defs.ua_name = (_, name); ua_params } =
let user_attribute_name_value { Typing_defs.ua_name = (_, name); ua_params; _ }
=
(name, ua_params)

let diff_user_attribute_params
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackc/ffi_bridge/compiler_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ mod ffi {
pub struct ExtDeclAttribute {
name: String,
args: Vec<String>,
raw_val: String,
}

#[derive(Debug, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackc/ffi_bridge/ext_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ fn get_attributes(arr: &[&UserAttribute<'_>], name: &str) -> Vec<ExtDeclAttribut
_ => String::from("__ext_decl_unknown"),
})
.collect(),
raw_val: str_or_empty(t.raw_val),
})
.collect()
}
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackrs/ty/decl/from_oxidized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl<P: Pos> From<&obr::typing_defs::UserAttribute<'_>> for ty::UserAttribute<P>
Self {
name: attr.name.into(),
params: (attr.params.iter()).map(Into::into).collect(),
raw_val: attr.raw_val.map(Into::into),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackrs/ty/decl/to_oxidized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<'a, P: Pos> ToOxidized<'a> for UserAttribute<P> {
arena.alloc(obr::typing_defs::UserAttribute {
name: self.name.to_oxidized(arena),
params: self.params.to_oxidized(arena),
raw_val: self.raw_val.as_deref().to_oxidized(arena),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/hackrs/ty/decl/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ walkable!(UserAttributeParam);
pub struct UserAttribute<P> {
pub name: Positioned<TypeName, P>,
pub params: Box<[UserAttributeParam]>,
pub raw_val: Option<String>,
}

impl<P> UserAttribute<P> {
Expand Down
3 changes: 2 additions & 1 deletion hphp/hack/src/oxidized/gen/typing_defs_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
//
// @generated SignedSource<<99b31f08f72fbca3cf3c2dde0ad1a864>>
// @generated SignedSource<<1a46ecd3a387e94c5e5a3c0b56c9ded9>>
//
// To regenerate this file, run:
// hphp/hack/src/oxidized_regen.sh
Expand Down Expand Up @@ -332,6 +332,7 @@ pub enum UserAttributeParam {
pub struct UserAttribute {
pub name: PosId,
pub params: Vec<UserAttributeParam>,
pub raw_val: Option<String>,
}

#[derive(
Expand Down
8 changes: 6 additions & 2 deletions hphp/hack/src/oxidized_by_ref/decl_visitor/node_impl_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
//
// @generated SignedSource<<80a04c9ccd13f2a2e2bb72eae26ca2a0>>
// @generated SignedSource<<95c4e780d33168fd0966bdf61b4e79ce>>
//
// To regenerate this file, run:
// hphp/hack/src/oxidized_regen.sh
Expand Down Expand Up @@ -418,11 +418,15 @@ impl<'a> Node<'a> for UserAttribute<'a> {
UserAttribute {
name: ref __binding_0,
params: ref __binding_1,
raw_val: ref __binding_2,
} => {
{
__binding_0.accept(v)
}
{ __binding_1.accept(v) }
{
__binding_1.accept(v)
}
{ __binding_2.accept(v) }
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion hphp/hack/src/oxidized_by_ref/gen/typing_defs_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This source code is licensed under the MIT license found in the
// LICENSE file in the "hack" directory of this source tree.
//
// @generated SignedSource<<49b484d8faa77d8808d95f056dbeb6eb>>
// @generated SignedSource<<7998eb73219e1a4d2ccfb0c18911ff03>>
//
// To regenerate this file, run:
// hphp/hack/src/oxidized_regen.sh
Expand Down Expand Up @@ -259,6 +259,8 @@ pub struct UserAttribute<'a> {
pub name: PosId<'a>,
#[serde(deserialize_with = "arena_deserializer::arena", borrow)]
pub params: &'a [UserAttributeParam<'a>],
#[serde(deserialize_with = "arena_deserializer::arena", borrow)]
pub raw_val: Option<&'a str>,
}
impl<'a> TrivialDrop for UserAttribute<'a> {}
arena_deserializer::impl_deserialize_in_arena!(UserAttribute<'arena>);
Expand Down
5 changes: 3 additions & 2 deletions hphp/hack/src/typing/typing_defs_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ let user_attribute_param_to_string = function
type user_attribute = {
ua_name: pos_id;
ua_params: user_attribute_param list;
ua_raw_val: string option;
}
[@@deriving eq, hash, show]

Expand Down Expand Up @@ -970,8 +971,8 @@ let rec ty__compare : type a. ?normalize_lists:bool -> a ty_ -> a ty_ -> int =
| 0 -> String.compare s1 s2
| n -> n
and user_attribute_compare ua1 ua2 =
let { ua_name = name1; ua_params = params1 } = ua1 in
let { ua_name = name2; ua_params = params2 } = ua2 in
let { ua_name = name1; ua_params = params1; _ } = ua1 in
let { ua_name = name2; ua_params = params2; _ } = ua2 in
match String.compare (snd name1) (snd name2) with
| 0 -> List.compare user_attribute_param_compare params1 params2
| n -> n
Expand Down
1 change: 1 addition & 0 deletions hphp/hack/src/typing/typing_defs_core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ val user_attribute_param_to_string : user_attribute_param -> string
type user_attribute = {
ua_name: pos_id;
ua_params: user_attribute_param list;
ua_raw_val: string option;
}
[@@deriving eq, show]

Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/class_const_referencing_enum.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([8:3-24], "__ConsistentConstruct");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/classes_consistent_construct.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([3:3-24], "__ConsistentConstruct");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }));
("\\C2",
Expand Down
4 changes: 3 additions & 1 deletion hphp/hack/test/decl/const_attribute.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
sm_attributes = []; sm_sort_text = None });
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([3:3-10], "__Const"); ua_params = [] }];
[{ Typing_defs_core.ua_name = ([3:3-10], "__Const"); ua_params = [];
ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }));
("\\B",
(Shallow_decl_defs.Class
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/const_misc1.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([3:12-13], "__EnumClass");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type =
(Some { Typing_defs.te_base = (Rhint ([3:14-22]), (Tprim Tarraykey));
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/consts_misc2.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([17:12-21], "__EnumClass");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type =
(Some { Typing_defs.te_base = (Rhint ([17:22-27]), Tmixed);
Expand Down
4 changes: 2 additions & 2 deletions hphp/hack/test/decl/enum_class.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([5:12-13], "__EnumClass");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type =
(Some { Typing_defs.te_base = (Rhint ([5:14-22]), (Tprim Tarraykey));
Expand Down Expand Up @@ -114,7 +114,7 @@
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([11:12-13], "__EnumClass");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type =
(Some { Typing_defs.te_base = (Rhint ([11:14-20]), (Tprim Tstring));
Expand Down
6 changes: 4 additions & 2 deletions hphp/hack/test/decl/enum_user_attributes.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
sc_typeconsts = []; sc_props = []; sc_sprops = [];
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([3:18-21], "\\Bar"); ua_params = [] };
[{ Typing_defs_core.ua_name = ([3:18-21], "\\Bar"); ua_params = [];
ua_raw_val = None };
{ Typing_defs_core.ua_name = ([3:3-6], "\\Foo");
ua_params =
[(Typing_defs_core.String "a"); (Typing_defs_core.String "b")] }
[(Typing_defs_core.String "a"); (Typing_defs_core.String "b")];
ua_raw_val = None }
];
sc_enum_type =
(Some { Typing_defs.te_base = (Rhint ([4:10-16]), (Tprim Tstring));
Expand Down
8 changes: 6 additions & 2 deletions hphp/hack/test/decl/explicit_type_collection.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
sc_typeconsts = []; sc_props = []; sc_sprops = [];
sc_constructor = None; sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([7:3-4], "\\A"); ua_params = [] }];
[{ Typing_defs_core.ua_name = ([7:3-4], "\\A"); ua_params = [];
ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }));
("\\Bar",
(Shallow_decl_defs.Class
Expand All @@ -100,6 +102,8 @@
sc_props = []; sc_sprops = []; sc_constructor = None;
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([12:3-4], "\\A"); ua_params = [] }];
[{ Typing_defs_core.ua_name = ([12:3-4], "\\A"); ua_params = [];
ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]
7 changes: 5 additions & 2 deletions hphp/hack/test/decl/multiple_user_attributes_on_class.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
sc_props = []; sc_sprops = []; sc_constructor = None;
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([6:8-11], "\\Bar"); ua_params = [] };
{ Typing_defs_core.ua_name = ([6:3-6], "\\Foo"); ua_params = [] }];
[{ Typing_defs_core.ua_name = ([6:8-11], "\\Bar"); ua_params = [];
ua_raw_val = None };
{ Typing_defs_core.ua_name = ([6:3-6], "\\Foo"); ua_params = [];
ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]
3 changes: 2 additions & 1 deletion hphp/hack/test/decl/namespace_import.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([18:5-13], "__Sealed");
ua_params = [(Typing_defs_core.Classname "\\NS1\\NS2\\C")] }
ua_params = [(Typing_defs_core.Classname "\\NS1\\NS2\\C")];
ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/php_std_lib.hhi.exp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([6:3-14], "__PHPStdLib");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/soft_reified.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
tp_constraints = []; tp_reified = SoftReified;
tp_user_attributes =
[{ Typing_defs_core.ua_name = ([3:17-23], "__Soft");
ua_params = [] }
ua_params = []; ua_raw_val = None }
]
}
];
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/decl/sort_text_class_attribute.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([2:3-25], "__AutocompleteSortText");
ua_params = [(Typing_defs_core.String "XYZ")] }
ua_params = [(Typing_defs_core.String "XYZ")]; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }));
("\\DEF",
Expand Down
4 changes: 2 additions & 2 deletions hphp/hack/test/decl/sound_dynamic_call_meth_1.bad.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
sc_static_methods = []; sc_methods = [];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([4:3-23], "__SupportDynamicType");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }));
("\\C",
Expand Down Expand Up @@ -166,7 +166,7 @@
];
sc_user_attributes =
[{ Typing_defs_core.ua_name = ([15:3-23], "__SupportDynamicType");
ua_params = [] }
ua_params = []; ua_raw_val = None }
];
sc_enum_type = None; sc_docs_url = None }))
]

0 comments on commit 6de8271

Please sign in to comment.