facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.06k stars 2.99k forks source link

Bug in use type Bar\{Bar}; #9386

Closed abhinav-nav closed 10 months ago

abhinav-nav commented 11 months ago

Title: Bug in use type Bar{Bar};

Description:

The line use type Bar\{Bar}; imports the type Bar\Bar as a named type. However, the type Bar\Bar is actually an abstract class. When the compiler sees the line const Bar\my_type CONFIG = shape('a' => 'b');, it expects the type Bar\my_type to be a concrete type. However, since Bar\Bar is an abstract class, it cannot be a concrete type. This is why the compiler is throwing an error.

Steps to Reproduce:

  1. Run the following code:

php namespace Foo;

use namespace Bar; use type Bar{Bar};

final class FooBar extends Bar { const Bar\my_type CONFIG = shape('a' => 'b'); }

namespace Bar;

abstract class Bar { const my_type CONFIG = shape('a' => 'b'); }

type my_type = shape('a' => string);


2. Run the command `hh_client`.

**Expected Behavior:**

The code should compile without any errors.

**Actual Behavior:**

The code will compile with the following error:

File "/root/foo.hack", line 6, characters 28-30: Some members in class Foo\FooBar are incompatible with those declared in type Bar\Bar (Typing[4348]) File "/root/foo.hack", line 13, characters 9-15: Expected shape('a' => string) File "/root/foo.hack", line 7, characters 10-20: But got Bar\Bar\my_type

Workaround:

The workaround for this bug is to use the line use type Bar\Bar; instead of use type Bar\{Bar};. This line imports the abstract class Bar\Bar as a reference type. This means that the compiler will not try to instantiate the class Bar\Bar as a concrete type.

Suggested Fix:

The suggested fix for this bug is to change the line use type Bar\{Bar}; to use type Bar\Bar;. This will import the abstract class Bar\Bar as a reference type, which will prevent the compiler from throwing an error.

Additional Information:

lexidor commented 10 months ago

9335