Skip to content

Commit ad1dbd1

Browse files
vassilmladenovfacebook-github-bot
authored andcommitted
Enforce like types in type hint positions
Summary: As part of the spec for sound dynamic, we said we want `~T` to enforce as `T`, a departure from our original approach which had it enforce as mixed (but as D40516837 (9e8c173) shows, wasn't happening for reified generics). This way, changing a declaration from `int` to `~int` doesn't cause the code to lose enforcement. The "enforce as mixed" idea came to mind from trying to replace `mixed/dynamic` with like types, but we're introducing TANY_MARKER to deal with those. Reviewed By: andrewjkennedy Differential Revision: D40516838 fbshipit-source-id: ec2f0b16edb0398af541ece1f181dd9c4a3ad49b
1 parent 201e947 commit ad1dbd1

17 files changed

+117
-18
lines changed

hphp/hack/src/hackc/emitter/emit_type_hint.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ fn hint_to_type_constraint<'arena>(
210210
) -> Result<Constraint<'arena>> {
211211
let Hint(_, hint) = h;
212212
Ok(match &**hint {
213-
Hdynamic | Hlike(_) | Hfun(_) | Hunion(_) | Hintersection(_) | Hmixed => {
214-
Constraint::default()
215-
}
213+
Hdynamic | Hfun(_) | Hunion(_) | Hintersection(_) | Hmixed => Constraint::default(),
216214
Haccess(_, _) => Constraint::make(
217215
Just("".into()),
218216
TypeConstraintFlags::ExtendedHint | TypeConstraintFlags::TypeConstant,
@@ -227,6 +225,7 @@ fn hint_to_type_constraint<'arena>(
227225
t,
228226
TypeConstraintFlags::Soft | TypeConstraintFlags::ExtendedHint,
229227
)?,
228+
Hlike(h) => hint_to_type_constraint(alloc, kind, tparams, skipawaitable, h)?,
230229
Herr | Hany => {
231230
return Err(Error::unrecoverable(
232231
"This should be an error caught in naming",

hphp/test/slow/like_types/awaitable1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Error: async functions must return an Awaitable.
55
*/
6-
async function f(mixed $x): ~Awaitable<int> {
6+
async function f(mixed $x): <<__Soft>> ~Awaitable<int> {
77
return $x;
88
}
99

hphp/test/slow/like_types/awaitable2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?hh
22

33
/**
4-
* Like-types are treated as mixed, allowing any return value.
4+
* Like-types are enforced at their inner type, so this enforces as Awaitable<int>
55
*/
6-
async function f(mixed $x): Awaitable<~int> {
6+
async function f(mixed $x): Awaitable<<<__Soft>> ~int> {
77
return $x;
88
}
99

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
int(1)
2+
3+
Warning: Value returned from async function f() must be of type @int, float given in %s/test/slow/like_types/awaitable2.php on line 7
24
float(1.5)
5+
6+
Warning: Value returned from async function f() must be of type @int, string given in %s/test/slow/like_types/awaitable2.php on line 7
37
string(3) "foo"
8+
9+
Warning: Value returned from async function f() must be of type @int, bool given in %s/test/slow/like_types/awaitable2.php on line 7
410
bool(false)
5-
resource(%d) of type (stream)
11+
12+
Warning: Value returned from async function f() must be of type @int, File given in %s/test/slow/like_types/awaitable2.php on line 7
13+
resource(4) of type (stream)
14+
15+
Warning: Value returned from async function f() must be of type @int, stdClass given in %s/test/slow/like_types/awaitable2.php on line 7
616
object(stdClass) (0) {
717
}
18+
19+
Warning: Value returned from async function f() must be of type @int, HH\vec given in %s/test/slow/like_types/awaitable2.php on line 7
820
vec(3) {
921
int(1)
1022
int(2)
1123
int(3)
1224
}
25+
26+
Warning: Value returned from async function f() must be of type @int, HH\dict given in %s/test/slow/like_types/awaitable2.php on line 7
1327
dict(2) {
1428
["a"]=>
1529
int(1)
1630
["b"]=>
1731
int(2)
18-
}
32+
}

hphp/test/slow/like_types/awaitable3.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?hh
22

33
/**
4-
* For non-async functions, a like-Awaitable acts like any other type; it's
5-
* not enforced.
4+
* For non-async functions, a like-Awaitable acts like any other like type, enforced at the inner type.
65
*/
7-
function f(mixed $x): ~Awaitable<int> {
6+
function f(mixed $x): <<__Soft>> ~Awaitable<int> {
87
return $x;
98
}
109

hphp/test/slow/like_types/awaitable3.php.expectf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
Warning: Value returned from function f() must be of type @HH\Awaitable, int given in %s/test/slow/like_types/awaitable3.php on line 7
12
int(1)
3+
4+
Warning: Value returned from function f() must be of type @HH\Awaitable, float given in %s/test/slow/like_types/awaitable3.php on line 7
25
float(1.5)
6+
7+
Warning: Value returned from function f() must be of type @HH\Awaitable, string given in %s/test/slow/like_types/awaitable3.php on line 7
38
string(3) "foo"
9+
10+
Warning: Value returned from function f() must be of type @HH\Awaitable, bool given in %s/test/slow/like_types/awaitable3.php on line 7
411
bool(false)
12+
13+
Warning: Value returned from function f() must be of type @HH\Awaitable, File given in %s/test/slow/like_types/awaitable3.php on line 7
514
resource(4) of type (stream)
15+
16+
Warning: Value returned from function f() must be of type @HH\Awaitable, stdClass given in %s/test/slow/like_types/awaitable3.php on line 7
617
object(stdClass) (0) {
718
}
19+
20+
Warning: Value returned from function f() must be of type @HH\Awaitable, HH\vec given in %s/test/slow/like_types/awaitable3.php on line 7
821
vec(3) {
922
int(1)
1023
int(2)
1124
int(3)
1225
}
26+
27+
Warning: Value returned from function f() must be of type @HH\Awaitable, HH\dict given in %s/test/slow/like_types/awaitable3.php on line 7
1328
dict(2) {
1429
["a"]=>
1530
int(1)

hphp/test/slow/like_types/parameter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?hh
22

33
/**
4-
* Like-types are treated as mixed, allowing any argument value.
4+
* Like-types are enforced at their inner type.
55
*/
6-
function f(~int $x): void {
6+
function f(<<__Soft>> ~int $x): void {
77
var_dump($x);
88
}
99

hphp/test/slow/like_types/parameter.php.expectf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
int(1)
2+
3+
Warning: Argument 1 to f() must be of type @int, float given in %s/test/slow/like_types/parameter.php on line 13
24
float(1.5)
5+
6+
Warning: Argument 1 to f() must be of type @int, string given in %s/test/slow/like_types/parameter.php on line 14
37
string(3) "foo"
8+
9+
Warning: Argument 1 to f() must be of type @int, bool given in %s/test/slow/like_types/parameter.php on line 15
410
bool(false)
11+
12+
Warning: Argument 1 to f() must be of type @int, File given in %s/test/slow/like_types/parameter.php on line 16
513
resource(4) of type (stream)
14+
15+
Warning: Argument 1 to f() must be of type @int, stdClass given in %s/test/slow/like_types/parameter.php on line 17
616
object(stdClass) (0) {
717
}
18+
19+
Warning: Argument 1 to f() must be of type @int, HH\vec given in %s/test/slow/like_types/parameter.php on line 18
820
vec(3) {
921
int(1)
1022
int(2)
1123
int(3)
1224
}
25+
26+
Warning: Argument 1 to f() must be of type @int, HH\dict given in %s/test/slow/like_types/parameter.php on line 19
1327
dict(2) {
1428
["a"]=>
1529
int(1)

hphp/test/slow/like_types/parameter_alias.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?hh
22

33
/**
4-
* Like-types are treated as mixed, allowing any argument value.
4+
* Like-types are enforced at their inner type.
55
*/
6-
function f(MyAlias $x): void {
6+
function f(<<__Soft>> MyAlias $x): void {
77
var_dump($x);
88
}
99

hphp/test/slow/like_types/parameter_alias.php.expectf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
int(1)
2+
3+
Warning: Argument 1 to f() must be of type @MyAlias, float given in %s/test/slow/like_types/parameter_alias.php on line 15
24
float(1.5)
5+
6+
Warning: Argument 1 to f() must be of type @MyAlias, string given in %s/test/slow/like_types/parameter_alias.php on line 16
37
string(3) "foo"
8+
9+
Warning: Argument 1 to f() must be of type @MyAlias, bool given in %s/test/slow/like_types/parameter_alias.php on line 17
410
bool(false)
11+
12+
Warning: Argument 1 to f() must be of type @MyAlias, File given in %s/test/slow/like_types/parameter_alias.php on line 18
513
resource(4) of type (stream)
14+
15+
Warning: Argument 1 to f() must be of type @MyAlias, stdClass given in %s/test/slow/like_types/parameter_alias.php on line 19
616
object(stdClass) (0) {
717
}
18+
19+
Warning: Argument 1 to f() must be of type @MyAlias, HH\vec given in %s/test/slow/like_types/parameter_alias.php on line 20
820
vec(3) {
921
int(1)
1022
int(2)
1123
int(3)
1224
}
25+
26+
Warning: Argument 1 to f() must be of type @MyAlias, HH\dict given in %s/test/slow/like_types/parameter_alias.php on line 21
1327
dict(2) {
1428
["a"]=>
1529
int(1)

0 commit comments

Comments
 (0)