-
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.
Ban private final methods on classes
Summary: `final private` methods don't make sense. `final` bans overriding that method, but `private` stops child classes seeing the method. This leads to a typehole from code of the form: ``` class MyParent { private final function foo(): void {} } class MyChild extends MyParent { private function foo(): void {} } ``` HHVM rejects this, because `MyChild` is overriding a final method. hh allowed this, because `MyParent::foo` is private to `MyParent` and shouldn't affect `MyChild`. Ban methods on classes from being both private and final. This is a backwards compatibility break, as it bans `MyParent` even if there are no child classes. This should be an easy fix though: remove `final` from any `private` methods. Note that this still allows `final private` methods on traits. This prevent accidental clobbering of trait methods from the using class. We might want to revisit this in future, as the same type hole exists there (see updated typehole test). Ignore linking failure due to D32058157 breaking LTO builds. Reviewed By: jamesjwu Differential Revision: D28342765 fbshipit-source-id: 53adaa8a0d8a291ca6fd4c62e1f203848201a547
- Loading branch information
1 parent
ec87522
commit c8faa1a
Showing
36 changed files
with
104 additions
and
68 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
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,30 @@ | ||
(* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the "hack" directory of this source tree. | ||
* | ||
*) | ||
|
||
[@@@warning "-33"] | ||
|
||
open Hh_prelude | ||
|
||
[@@@warning "+33"] | ||
|
||
open Aast | ||
|
||
(* Ban `private final` on classes, but not traits. *) | ||
|
||
let handler = | ||
object | ||
inherit Nast_visitor.handler_base | ||
|
||
method! at_method_ env m = | ||
match (env.Nast_check_env.classish_kind, m.m_visibility, m.m_final) with | ||
| (Some Ast_defs.Ctrait, _, _) -> () | ||
| (_, Private, true) -> | ||
let (pos, _) = m.m_name in | ||
Errors.private_and_final pos | ||
| _ -> () | ||
end |
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,4 +1,4 @@ | ||
File "case_sensitive_inheritance8.php", line 18, characters 27-39: | ||
MyA inherits a method named `createBuildID` whose name differs from this one (`createBuildId`) only by case. (Typing[4393]) | ||
File "case_sensitive_inheritance8.php", line 6, characters 26-38: | ||
File "case_sensitive_inheritance8.php", line 6, characters 20-32: | ||
It was inherited from MyTrait as `createBuildI~~D~~`. If you meant to override it, please use the same casing as the inherited method. Otherwise, please choose a different name for the new method |
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,11 @@ | ||
<?hh | ||
|
||
trait MyTrait { | ||
public function foo(): void { $this->bar(); } | ||
final private function bar(): void {} | ||
} | ||
|
||
class MyClass { | ||
use MyTrait; | ||
private function bar(): void {} // should error, clobbering trait internals | ||
} |
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 @@ | ||
File "final_private_trait.php", line 10, characters 20-22: | ||
You cannot override this method (Typing[4070]) | ||
File "final_private_trait.php", line 5, characters 26-28: | ||
It was declared as final |
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
File renamed without changes.
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 @@ | ||
File "private_final_class_meth.php", line 5, characters 26-37: | ||
Class methods cannot be both `private` and `final`. (NastCheck[3097]) |
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,2 +1,2 @@ | ||
File "public_private.php", line 13, characters 23-29: | ||
File "public_private.php", line 13, characters 17-23: | ||
Methods cannot have multiple visibility modifiers (Parsing[1002]) |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?hh | ||
|
||
trait MyTrait { | ||
public function foo(): void { $this->bar(); } | ||
final private function bar(): void {} | ||
} | ||
|
||
class MyClass { | ||
use MyTrait; | ||
} | ||
|
||
class MyChild extends MyClass { | ||
private function bar(): void {} | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.