Skip to content

Commit

Permalink
Fix namespacing of classname shape keys for type aliases
Browse files Browse the repository at this point in the history
Summary: Fixes #8195

Reviewed By: fredemmott

Differential Revision: D7958066
  • Loading branch information
oulgen authored and fredemmott committed May 11, 2018
1 parent 950b326 commit 6272f66
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 5 deletions.
9 changes: 6 additions & 3 deletions hphp/hack/src/hhbc/emit_type_constant.ml
Expand Up @@ -74,14 +74,17 @@ let check_shape_key (pos, name) =
then Emit_fatal.raise_fatal_parse
pos "Shape key names may not start with integers"

let shape_field_name = function
let shape_field_name ~namespace = function
| A.SFlit ((_, s) as id) ->
check_shape_key id;
s, false
| A.SFclass_const ((_, id), (_, s)) -> id ^ "::" ^ s, true
| A.SFclass_const (id, (_, s)) ->
let classname, _ = Hhbc_id.Class.elaborate_id namespace id in
let id = Hhbc_id.Class.to_raw_string classname in
id ^ "::" ^ s, true

let rec shape_field_to_pair ~tparams ~namespace sf =
let name, is_class_const = shape_field_name sf.A.sf_name in
let name, is_class_const = shape_field_name ~namespace sf.A.sf_name in
let is_optional = sf.A.sf_optional in
let hint = sf.A.sf_hint in
let class_const =
Expand Down
4 changes: 2 additions & 2 deletions hphp/hack/src/hhbc/hhbc_hhas.ml
Expand Up @@ -1761,9 +1761,9 @@ let add_typedef buf typedef =
B.add_string buf " \"\"\"";
B.add_string buf @@ SS.seq_to_string @@
Emit_adata.adata_to_string_seq ts;
B.add_string buf "\"\"\";\n"
B.add_string buf "\"\"\";"
| None ->
B.add_string buf ";\n"
B.add_string buf ";"

let add_include_region
?path ?doc_root ?search_paths ?include_roots ?(check_paths_exist=true)
Expand Down
58 changes: 58 additions & 0 deletions hphp/test/slow/namespace/alias/namespaced_aliases.php
@@ -0,0 +1,58 @@
<?hh

namespace SomeNS {

enum Foo: string {
VALUE = __CLASS__;
}

enum Bar: string {
VALUE = __CLASS__;
}

enum Baz: string {
VALUE = __CLASS__;
}

}

namespace MyNS {


namespace MySubNS {

enum MyEnum: string {
VALUE = 'Herp';
}

}

use type SomeNS\Foo;
use SomeNS\Bar;

enum MyEnum: string {
FOO = 'Herp';
}

type MyShape = shape(
MyEnum::FOO => string,
);

type T = MyEnum;

type UsesFoo = shape(Foo::VALUE => int);
type UsesBar = shape(Bar::VALUE => int);
type UsesBaz = shape(\SomeNS\Baz::VALUE => int);

type ExplicitRelative = shape(namespace\MyEnum::FOO => int);
type ImplicitRelative = shape(MySubNS\MyEnum::VALUE => int);

var_dump(type_structure(MyShape::class));
var_dump(type_structure(T::class));
var_dump(type_structure(UsesFoo::class));
var_dump(type_structure(UsesBar::class));
var_dump(type_structure(UsesBaz::class));
var_dump(type_structure(ExplicitRelative::class));
var_dump(type_structure(ImplicitRelative::class));

}
92 changes: 92 additions & 0 deletions hphp/test/slow/namespace/alias/namespaced_aliases.php.expect
@@ -0,0 +1,92 @@
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["Herp"]=>
array(1) {
["kind"]=>
int(4)
}
}
["alias"]=>
string(12) "MyNS\MyShape"
}
array(3) {
["kind"]=>
int(18)
["classname"]=>
string(11) "MyNS\MyEnum"
["alias"]=>
string(6) "MyNS\T"
}
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["SomeNS\Foo"]=>
array(1) {
["kind"]=>
int(1)
}
}
["alias"]=>
string(12) "MyNS\UsesFoo"
}
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["SomeNS\Bar"]=>
array(1) {
["kind"]=>
int(1)
}
}
["alias"]=>
string(12) "MyNS\UsesBar"
}
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["SomeNS\Baz"]=>
array(1) {
["kind"]=>
int(1)
}
}
["alias"]=>
string(12) "MyNS\UsesBaz"
}
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["Herp"]=>
array(1) {
["kind"]=>
int(1)
}
}
["alias"]=>
string(21) "MyNS\ExplicitRelative"
}
array(3) {
["kind"]=>
int(14)
["fields"]=>
array(1) {
["Herp"]=>
array(1) {
["kind"]=>
int(1)
}
}
["alias"]=>
string(21) "MyNS\ImplicitRelative"
}

0 comments on commit 6272f66

Please sign in to comment.