-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make trait constants behave like interface constants
Summary: Trait constants were added to the language recently to obviate a workaround where traits themselves could not declare constants, but could bring in constants via interfaces. However, the semantics on conflict were to drop the conflicting constant; this is in contrast to interface conflicts, where you get a fatal at class loading time. ``` class A { const int X = 3; } interface I { const string X = "hello"; } class C extends A implements I {} // Fatal runtime error trait TI implements I {} class D extends A { use TI; } // no fatal // by extension trait T { const bool X = false; } class E extends A { use T; } // also no fatal ``` This flag allows the above cases to fatal, and it also fixes the behavior of defaults + makes HHVM align with Hack on member resolution order. ``` abstract class A { abstract const type T = arraykey; } trait T { const type T = int; } class C extends A { use T; } // C::T = int, was arraykey previously. ``` Equivalently, coeffect unsoundness given trait conflicts is resolved ``` abstract class A { abstract const ctx C = []; } trait T { const ctx C = [defaults]; public function f()[this::C]: void { echo "impure"; } } class C extends A { use T; } // runtime used to think (new C())->f() is pure, now it's correctly defaults. ``` The flag also changes HHBBC to insert trait constants before inserting locally declared constants of a class, which resolves a bug with printing the conflict error message and a static analysis failure where the trait constant was expecting to conflict with a shallowly declared constant (see trait_slot_conflict_repo.php). Reviewed By: oulgen Differential Revision: D28887745 fbshipit-source-id: a698e1818925ba17c2608ce34d50dc1400cfabd0
- Loading branch information
1 parent
bed8173
commit 94a20dc
Showing
24 changed files
with
225 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
hhvm.enable_abstract_context_constants=1 | ||
hhvm.typeconst_abstract_default_reflection_is_abstract=1 | ||
hhvm.abstract_context_constant_uninit_access=1 | ||
hhvm.trait_constant_interface_behavior=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
hhvm.enable_abstract_context_constants=1 | ||
hhvm.typeconst_abstract_default_reflection_is_abstract=1 | ||
hhvm.abstract_context_constant_uninit_access=1 | ||
hhvm.trait_constant_interface_behavior=1 |
17 changes: 17 additions & 0 deletions
17
hphp/test/slow/constants/inherited_concrete_wins_class2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?hh | ||
|
||
abstract class A { | ||
const type T = float; | ||
} | ||
trait Tr { | ||
abstract const type T = int; | ||
} | ||
class C extends A { | ||
use Tr; | ||
} | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
// expecting TypeStructureKind::OF_FLOAT = 3 | ||
var_dump(type_structure(C::class, "T")); | ||
} |
4 changes: 4 additions & 0 deletions
4
hphp/test/slow/constants/inherited_concrete_wins_class2.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dict(1) { | ||
["kind"]=> | ||
int(3) | ||
} |
17 changes: 17 additions & 0 deletions
17
hphp/test/slow/constants/inherited_concrete_wins_trait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?hh | ||
|
||
abstract class A { | ||
abstract const type T = int; | ||
} | ||
trait Tr { | ||
const type T = string; | ||
} | ||
class C extends A { | ||
use Tr; | ||
} | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
// expecting TypeStructureKind::OF_STRING = 4 | ||
var_dump(type_structure(C::class, "T")); | ||
} |
4 changes: 4 additions & 0 deletions
4
hphp/test/slow/constants/inherited_concrete_wins_trait.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dict(1) { | ||
["kind"]=> | ||
int(4) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?hh | ||
|
||
class C { | ||
const int X = 3; | ||
} | ||
|
||
trait T { | ||
const string X = "hello"; | ||
} | ||
|
||
class D extends C { | ||
use T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fatal error: D cannot inherit the constant X from T, because it was previously inherited from C in %s/test/slow/constants/trait_constant_conflict.php on line 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?hh | ||
|
||
interface I1 { | ||
const type T = int; | ||
} | ||
|
||
trait T2 { | ||
const type T = string; | ||
} | ||
|
||
// behavior should be equivalent to if T2 was `interface I2` | ||
// and the declaration was `class C implements I1, I2` | ||
class C implements I1 { | ||
use T2; | ||
} |
1 change: 1 addition & 0 deletions
1
hphp/test/slow/constants/trait_interface_equivalence.php.expectf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fatal error: C cannot inherit the type constant T from T2, because it was previously inherited from I1 in %s/test/slow/constants/trait_interface_equivalence.php on line 13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?hh | ||
|
||
interface IA {} | ||
|
||
interface ITr { | ||
const type T = int; | ||
} | ||
trait Tr implements ITr {} | ||
|
||
class C implements IA { | ||
use Tr; | ||
const type T = int; | ||
} | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
echo "main"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?hh | ||
|
||
class C { | ||
const type Ty = int; | ||
} | ||
|
||
trait T { | ||
const type Ty = string; | ||
} | ||
|
||
class D extends C { | ||
use T; | ||
} |
1 change: 1 addition & 0 deletions
1
hphp/test/slow/constants/trait_type_constant_conflict.php.expectf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fatal error: D cannot inherit the type constant Ty from %rT|D%r, because it was previously inherited from C in %s/test/slow/constants/trait_type_constant_conflict.php on line 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?hh | ||
|
||
abstract class A { | ||
const type T as arraykey = arraykey; | ||
} | ||
|
||
trait T { | ||
const type T = int; | ||
} | ||
|
||
final class B extends A { | ||
use T; | ||
public static function cast(mixed $x): int { | ||
return $x is this::T ? $x : 0; | ||
} | ||
} | ||
|
||
<<__EntryPoint>> | ||
function main(): void { | ||
B::cast('break it'); // Error, string expected int | ||
echo "No type hint violation"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No type hint violation |