Skip to content

Commit

Permalink
Allow non-abstract type constants from interfaces to be overriden
Browse files Browse the repository at this point in the history
Summary:
The runtime allows this, and it seems to be the desired behavior for now.
Without this diff, HHVM could throw a static-analysis error.

Reviewed By: vassilmladenov

Differential Revision: D38561777

fbshipit-source-id: 10f60e60732c014fe5897afd87bc5b9e918203c0
  • Loading branch information
ottoni authored and facebook-github-bot committed Aug 10, 2022
1 parent 7c9ac8f commit 90d7b5c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
7 changes: 4 additions & 3 deletions hphp/hhbbc/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2515,9 +2515,10 @@ bool build_class_constants(IndexData& index,

if ((cns->cls->attrs & AttrInterface ||
(RO::EvalTraitConstantInterfaceBehavior && (cns->cls->attrs & AttrTrait))) &&
existing->isAbstract) {
// because existing has val, this covers the case where it is
// abstract with default allow incoming to win
(existing->isAbstract || cns->kind == ConstModifiers::Kind::Type)) {
// Because existing has val, this covers the case where it is abstract
// with default allow incoming to win. Also, type constants from
// interfaces may be overridden even if they're not abstract.
} else {
// A constant from an interface or from an included enum collides
// with an existing constant.
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +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
Fatal error: C cannot inherit the type constant T from %rT2|C%r, because it was previously inherited from I1 in %s/test/slow/constants/trait_interface_equivalence.php on line 13
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Fatal error: B cannot inherit the type constant T from T, because it was previously inherited from A in %s/hphp/test/slow/constants/trait_unsoundness_example.php on line 11
Fatal error: B cannot inherit the type constant T from %rT|B%r, because it was previously inherited from A in %s/hphp/test/slow/constants/trait_unsoundness_example.php on line 11
19 changes: 19 additions & 0 deletions hphp/test/slow/hhbbc/type-const-override2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?hh

interface I1 {
abstract const type T1;
}

interface I2 extends I1 {
const type T1 = int;
}

interface I3 extends I2 {
const type T1 = int;
}

abstract class C implements I3 {}

<<__EntryPoint>> function main() {
var_dump(C::class);
}
1 change: 1 addition & 0 deletions hphp/test/slow/hhbbc/type-const-override2.php.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
string(1) "C"

0 comments on commit 90d7b5c

Please sign in to comment.