Skip to content

Commit 483e1b5

Browse files
Michael Thomasmeta-codesync[bot]
authored andcommitted
Add more tests for super constraints
Summary: Adds more tests to the type constants super constraints tests: 1) Simple test to ensure we can return values with types which are subtypes of the declared lower bound 2) Ensure we detect 'inconsistent' bounds on abstract type constants through inheritance 3) Ensure we detect impossible refinements on lower bounds 4) Make sure super bounds interact well with expression dependent types Reviewed By: mheiber Differential Revision: D88078731 fbshipit-source-id: b9236995acc2176be672c336a494b0171e3b460d
1 parent d80b106 commit 483e1b5

10 files changed

Lines changed: 120 additions & 0 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?hh
2+
3+
class AAA {}
4+
class BBB extends AAA {}
5+
6+
// These are deliberately not subtypes of AAA or BBB
7+
class One {}
8+
class Two extends One {}
9+
10+
abstract class Foo extends Bar {
11+
abstract const type Ta as AAA super Two;
12+
}
13+
14+
abstract class Bar {
15+
abstract const type Ta as AAA super One;
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ERROR: File "consistent_bad.php", line 11, characters 23-24:
2+
The constraint on this type constant is inconsistent with its parent (Typing[4110])
3+
File "consistent_bad.php", line 11, characters 39-41:
4+
Expected `Two`
5+
File "consistent_bad.php", line 15, characters 39-41:
6+
But got `One`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?hh
2+
3+
class AAA {}
4+
class BBB extends AAA {}
5+
6+
// These are deliberately not subtypes of AAA or BBB
7+
class One {}
8+
class Two extends One {}
9+
10+
abstract class Foo {
11+
abstract const type Ta as AAA super Two;
12+
}
13+
14+
abstract class Bar extends Foo {
15+
abstract const type Ta as BBB super One;
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No errors
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?hh
2+
3+
class One {}
4+
class Two extends One {}
5+
class Three extends Two {}
6+
7+
abstract class Foo {
8+
abstract const type Ta as One super Two;
9+
10+
abstract public function wibble(): this::Ta;
11+
abstract public function wobble(this::Ta $_): void;
12+
}
13+
14+
function test(Foo $x): void {
15+
$ta = $x->wibble();
16+
$x->wobble($ta);
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No errors
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?hh
2+
3+
class One {}
4+
class Two extends One {}
5+
class Three extends Two {}
6+
7+
abstract class Foo {
8+
abstract const type Ta as One super Two;
9+
}
10+
11+
function ok(Foo with { type Ta as One super Two } $x): void {}
12+
13+
function redundant(Foo with { type Ta as One super Three } $x): void {}
14+
15+
function also_redundant(Foo with { type Ta as One } $x): void {}
16+
17+
function nope(Foo with { type Ta super One } $x): void {}
18+
19+
function test(Foo $x): void {
20+
nope($x);
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ERROR: File "refinements.php", line 20, characters 8-9:
2+
Invalid argument (Typing[4110])
3+
File "refinements.php", line 17, characters 15-44:
4+
Expected a class with `{type Ta super One}`
5+
File "refinements.php", line 19, characters 15-17:
6+
But got `<expr#2> as Foo`
7+
File "refinements.php", line 20, characters 8-9:
8+
where `<expr#2>` is a reference to this expression
9+
File "refinements.php", line 17, characters 15-44:
10+
Expected a class with `{type Ta super One}`
11+
File "refinements.php", line 19, characters 15-17:
12+
But got `Foo`
13+
File "refinements.php", line 17, characters 40-42:
14+
This `super` refinement constraint is violated
15+
File "refinements.php", line 8, characters 39-41:
16+
Expected `Two`
17+
File "refinements.php", line 17, characters 40-42:
18+
But got `One`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?hh
2+
3+
class Huge {}
4+
class Bigly extends Huge {}
5+
class Middling extends Bigly {}
6+
class Smol extends Middling {}
7+
class Tiny extends Smol {}
8+
9+
abstract class Foo {
10+
abstract const type Ta as Huge super Middling;
11+
12+
public function returnMiddling(): this::Ta {
13+
return new Middling();
14+
}
15+
16+
public function returnSmol(): this::Ta {
17+
return new Smol();
18+
}
19+
20+
public function returnTiny(): this::Ta {
21+
return new Tiny();
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No errors

0 commit comments

Comments
 (0)