Skip to content

Commit 7481994

Browse files
Thomas Jiangfacebook-github-bot
authored andcommitted
Throw error when cannot resolve first argument's type in type_structure
Summary: The following fatals in HHVM and is not caught by Hack. ``` <?hh type MyCoolType = shape( 'a' => int, ... ); <<__EntryPoint>> function main(): void { PHP\var_dump(type_structure(MyCoolType::class, 'TUhOh')); } ``` This diff makes Hack throw an error when we can't properly resolve the type of the first argument to `type_structure`. Previously, we were typing the result of this to be `Tany` and not reporting an error. We should not allow type alias's here at all in part because we cannot support the type alias resolution at HackC time and HHVM does not support resolving type aliases here. Reviewed By: Wilfred Differential Revision: D39483470 fbshipit-source-id: 7bced5c09f5ac0210c752774a566a70d365dce42
1 parent 7323b1c commit 7481994

File tree

10 files changed

+62
-13
lines changed

10 files changed

+62
-13
lines changed

hphp/hack/src/errors/typing_error.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,10 @@ module Primary = struct
20582058
}
20592059
| Assign_during_case of Pos.t
20602060
| Invalid_classname of Pos.t
2061-
| Illegal_type_structure of Pos.t
2061+
| Illegal_type_structure of {
2062+
pos: Pos.t;
2063+
msg: string;
2064+
}
20622065
| Illegal_typeconst_direct_access of Pos.t
20632066
| Wrong_expression_kind_attribute of {
20642067
pos: Pos.t;
@@ -3860,17 +3863,15 @@ module Primary = struct
38603863
lazy [],
38613864
[] )
38623865

3863-
let illegal_type_structure pos =
3866+
let illegal_type_structure pos msg =
38643867
let claim =
38653868
lazy
3866-
(let errmsg = "second argument is not a string" in
3867-
let msg =
3869+
(let msg =
38683870
"The two arguments to `type_structure()` must be:"
38693871
^ "\n - first: `ValidClassname::class` or an object of that class"
38703872
^ "\n - second: a single-quoted string literal containing the name"
3871-
^ " of a type constant of that class"
3872-
^ "\n"
3873-
^ errmsg
3873+
^ " of a type constant of that class\n"
3874+
^ msg
38743875
in
38753876
(pos, msg))
38763877
in
@@ -5796,7 +5797,7 @@ module Primary = struct
57965797
local_variable_modified_twice pos pos_modifieds
57975798
| Assign_during_case pos -> assign_during_case pos
57985799
| Invalid_classname pos -> invalid_classname pos
5799-
| Illegal_type_structure pos -> illegal_type_structure pos
5800+
| Illegal_type_structure { pos; msg } -> illegal_type_structure pos msg
58005801
| Illegal_typeconst_direct_access pos -> illegal_typeconst_direct_access pos
58015802
| Wrong_expression_kind_attribute
58025803
{

hphp/hack/src/errors/typing_error.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,10 @@ module Primary : sig
803803
}
804804
| Assign_during_case of Pos.t
805805
| Invalid_classname of Pos.t
806-
| Illegal_type_structure of Pos.t
806+
| Illegal_type_structure of {
807+
pos: Pos.t;
808+
msg: string;
809+
}
807810
| Illegal_typeconst_direct_access of Pos.t
808811
| Wrong_expression_kind_attribute of {
809812
pos: Pos.t;

hphp/hack/src/typing/typing.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6956,10 +6956,23 @@ and dispatch_call
69566956
((), p1, CIexpr e1_)
69576957
in
69586958
let result = class_const ~incl_tc:true env p (cid, (p, cst)) in
6959+
let () =
6960+
match result with
6961+
| (_, (ty, _, _), _) when Typing_utils.is_any env ty ->
6962+
Errors.add_typing_error
6963+
Typing_error.(
6964+
primary
6965+
@@ Primary.Illegal_type_structure
6966+
{ pos; msg = "Could not resolve the type constant" })
6967+
| _ -> ()
6968+
in
69596969
(result, should_forget_fakes)
69606970
| _ ->
69616971
Errors.add_typing_error
6962-
Typing_error.(primary @@ Primary.Illegal_type_structure pos);
6972+
Typing_error.(
6973+
primary
6974+
@@ Primary.Illegal_type_structure
6975+
{ pos; msg = "Second argument is not a string" });
69636976
let result = expr_error env (Reason.Rwitness pos) e in
69646977
(result, should_forget_fakes))
69656978
| _ -> assert false)

hphp/hack/test/typecheck/inout/pseudofunction_with_signature.php.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ File "pseudofunction_with_signature.php", line 8, characters 15-22:
22
Unexpected `inout` annotation for argument (Typing[4183])
33
File "Shapes.hhi", line 40, characters 17-22:
44
This is a normal parameter (does not have `inout`)
5+
File "pseudofunction_with_signature.php", line 9, characters 3-16:
6+
The two arguments to `type_structure()` must be:
7+
- first: `ValidClassname::class` or an object of that class
8+
- second: a single-quoted string literal containing the name of a type constant of that class
9+
Could not resolve the type constant (Typing[4150])
510
File "pseudofunction_with_signature.php", line 9, characters 28-30:
611
No class constant `T` in `dict` (Typing[4090])
712
File "hackarray.hhi", line 29, characters 22-25:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
File "type_structure4.php", line 4, characters 18-33:
22
Unbound name: `NonExistentClass` (an object type) (Naming[2049])
3+
File "type_structure4.php", line 4, characters 3-16:
4+
The two arguments to `type_structure()` must be:
5+
- first: `ValidClassname::class` or an object of that class
6+
- second: a single-quoted string literal containing the name of a type constant of that class
7+
Could not resolve the type constant (Typing[4150])

hphp/hack/test/typecheck/type_structure/type_structure5.php.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ File "type_structure5.php", line 6, characters 3-16:
22
The two arguments to `type_structure()` must be:
33
- first: `ValidClassname::class` or an object of that class
44
- second: a single-quoted string literal containing the name of a type constant of that class
5-
second argument is not a string (Typing[4150])
5+
Second argument is not a string (Typing[4150])
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
File "type_structure6.php", line 6, characters 3-16:
2+
The two arguments to `type_structure()` must be:
3+
- first: `ValidClassname::class` or an object of that class
4+
- second: a single-quoted string literal containing the name of a type constant of that class
5+
Could not resolve the type constant (Typing[4150])
16
File "type_structure6.php", line 6, characters 28-30:
27
No class constant `T` in `C` (Typing[4090])
3-
File "type_structure6.php", line 3, characters 7-7:
4-
Declaration of `C` is here
8+
File "type_structure6.php", line 3, characters 7-7:
9+
Declaration of `C` is here
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
File "type_structure8.php", line 8, characters 3-16:
2+
The two arguments to `type_structure()` must be:
3+
- first: `ValidClassname::class` or an object of that class
4+
- second: a single-quoted string literal containing the name of a type constant of that class
5+
Could not resolve the type constant (Typing[4150])
16
File "type_structure8.php", line 8, characters 18-20:
27
Was expecting a class, but got a string (Typing[4026])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?hh
2+
3+
type MyTypename = int;
4+
5+
function test(): void {
6+
type_structure(MyTypename::class, 'Nonsense');
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
File "type_structure_typename.php", line 6, characters 3-16:
2+
The two arguments to `type_structure()` must be:
3+
- first: `ValidClassname::class` or an object of that class
4+
- second: a single-quoted string literal containing the name of a type constant of that class
5+
Could not resolve the type constant (Typing[4150])

0 commit comments

Comments
 (0)