Skip to content

Commit 4e816af

Browse files
Michael Thomasfacebook-github-bot
authored andcommitted
Add pattern for contravariant projection on lower bounds
Summary: Adds an additional pattern to replicate the legacy `Contravariant_generic` and `Invariant_generic` reasons. Reviewed By: patriciamckenzie Differential Revision: D63897645 fbshipit-source-id: 5edab60a998f5358f4d0a1e56965c6c45b3285de
1 parent 450ed54 commit 4e816af

File tree

6 files changed

+66
-3
lines changed

6 files changed

+66
-3
lines changed

hphp/hack/src/typing/typing_reason.ml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,6 @@ let rec to_string_help :
20732073
to_string_help prefix solutions r
20742074
| From_witness_locl witness -> [witness_locl_to_string prefix witness]
20752075
| From_witness_decl witness -> [witness_decl_to_string prefix witness]
2076-
| Lower_bound { bound = r; _ }
20772076
| Axiom { next = r; _ }
20782077
| Def (_, r)
20792078
| Prj_one { part = r; _ } ->
@@ -2093,7 +2092,33 @@ let rec to_string_help :
20932092
| _ -> to_string_help prefix solutions into)
20942093
| Solved { solution; of_; in_ = r } ->
20952094
let solutions = Tvid.Map.add of_ solution solutions in
2095+
2096+
to_string_help prefix solutions r
2097+
| Lower_bound
2098+
{
2099+
bound = r;
2100+
of_ = Prj_both { prj = Prj_symm_ctor (_, class_name, _, Dir Contra); _ };
2101+
} ->
20962102
to_string_help prefix solutions r
2103+
@ [
2104+
( p,
2105+
"This type argument to "
2106+
^ (strip_ns class_name |> Markdown_lite.md_codify)
2107+
^ " only allows supertypes (it is contravariant)" );
2108+
]
2109+
| Lower_bound
2110+
{
2111+
bound = r;
2112+
of_ = Prj_both { prj = Prj_symm_ctor (_, class_name, _, Inv _); _ };
2113+
} ->
2114+
to_string_help prefix solutions r
2115+
@ [
2116+
( p,
2117+
"This type argument to "
2118+
^ (strip_ns class_name |> Markdown_lite.md_codify)
2119+
^ " must match exactly (it is invariant)" );
2120+
]
2121+
| Lower_bound { bound = r; _ } -> to_string_help prefix solutions r
20972122
| Prj_both
20982123
{
20992124
sub_prj = r_orig;

hphp/hack/test/extended_reasons/vector_invariant.php.debug.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ File "vector_invariant.php", line 3, character 24 - line 3, character 26:
1717

1818
But got `string`
1919

20+
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
21+
22+
3 | function foo(): Vector<int> {
23+
4 | $x = Vector { 1 , »'2'« };
24+
5 | $y = $x;
25+
6 | $z = $y;
26+
27+
This type argument to `Vector` must match exactly (it is invariant)
28+
2029
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
2130

2231
3 | function foo(): Vector<int> {

hphp/hack/test/extended_reasons/vector_invariant.php.diff.exp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
+++ test
44

5-
@@ -1,6 +1,5 @@
5+
@@ -1,7 +1,7 @@
66

77
error: Typing[4110] Invalid return type [1]
88
-> Expected int [2]
99
--> This type argument to Vector must match exactly (it is invariant) [2]
1010
-> But got string [3]
11+
+-> This type argument to Vector must match exactly (it is invariant) [3]
1112

12-
hphp/hack/test/extended_reasons/vector_invariant.php:8:10
13+
hphp/hack/test/extended_reasons/vector_invariant.php:8:10
14+
1 | <?hh

hphp/hack/test/extended_reasons/vector_invariant.php.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ File "vector_invariant.php", line 3, character 24 - line 3, character 26:
2020

2121
But got `string`
2222

23+
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
24+
25+
3 | function foo(): Vector<int> {
26+
4 | $x = Vector { 1 , »'2'« };
27+
5 | $y = $x;
28+
6 | $z = $y;
29+
30+
This type argument to `Vector` must match exactly (it is invariant)
31+
2332
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
2433

2534
3 | function foo(): Vector<int> {

hphp/hack/test/extended_reasons/vector_invariant.php.terse.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ File "vector_invariant.php", line 3, character 24 - line 3, character 26:
1717

1818
But got `string`
1919

20+
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
21+
22+
3 | function foo(): Vector<int> {
23+
4 | $x = Vector { 1 , »'2'« };
24+
5 | $y = $x;
25+
6 | $z = $y;
26+
27+
This type argument to `Vector` must match exactly (it is invariant)
28+
2029
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
2130

2231
3 | function foo(): Vector<int> {

hphp/hack/test/extended_reasons/vector_invariant.php.verbose.exp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ File "vector_invariant.php", line 3, character 24 - line 3, character 26:
2020

2121
But got `string`
2222

23+
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
24+
25+
3 | function foo(): Vector<int> {
26+
4 | $x = Vector { 1 , »'2'« };
27+
5 | $y = $x;
28+
6 | $z = $y;
29+
30+
This type argument to `Vector` must match exactly (it is invariant)
31+
2332
File "vector_invariant.php", line 4, character 22 - line 4, character 24:
2433

2534
3 | function foo(): Vector<int> {

0 commit comments

Comments
 (0)