Skip to content

Commit

Permalink
Fix private(set) not being implicitely marked as final
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 5, 2024
1 parent e772b3d commit 0b7c768
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ XP Compiler ChangeLog

## ?.?.? / ????-??-??

* Fixed `private(set)` not being implicitely marked as *final*, see
https://wiki.php.net/rfc/asymmetric-visibility-v2#inheritance
(@thekid)
* Fixed enclosing scopes when using references - @thekid

## 9.3.0 / 2024-08-31
Expand Down
1 change: 1 addition & 0 deletions src/main/php/lang/ast/emit/AsymmetricVisibility.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function emitProperty($result, $property) {
} else if ($modifiers & 0x0001000) {
$checks= [$this->private($property->name, 'modify private(set)')];
$modifiers & MODIFIER_PRIVATE && $modifiers&= ~0x0001000;
$modifiers|= MODIFIER_FINAL;
}

// Emit XP meta information for the reflection API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,31 @@ public function rename() {
$t->newInstance()->rename();
}

#[Test, Values(['private', 'protected'])]
public function reflection($modifier) {
#[Test]
public function protected_set_reflection() {
$t= $this->declare('class %T {
public '.$modifier.'(set) string $fixture= "Test";
public protected(set) string $fixture= "Test";
}');

Assert::equals(
'public '.$modifier.'(set) string $fixture',
'public protected(set) string $fixture',
$t->property('fixture')->toString()
);
}

#[Test, Values(['private', 'protected', 'public'])]
#[Test]
public function private_set_implicitely_final_in_reflection() {
$t= $this->declare('class %T {
public private(set) string $fixture= "Test";
}');

Assert::equals(
'public final private(set) string $fixture',
$t->property('fixture')->toString()
);
}

#[Test, Values(['protected', 'public'])]
public function same_modifier_for_get_and_set($modifier) {
$t= $this->declare('class %T {
'.$modifier.' '.$modifier.'(set) string $fixture= "Test";
Expand All @@ -124,6 +136,18 @@ public function same_modifier_for_get_and_set($modifier) {
);
}

#[Test]
public function private_modifier_for_get_and_set() {
$t= $this->declare('class %T {
private private(set) string $fixture= "Test";
}');

Assert::equals(
'private final string $fixture',
$t->property('fixture')->toString()
);
}

#[Test]
public function interaction_with_hooks() {
$t= $this->declare('class %T {
Expand Down

0 comments on commit 0b7c768

Please sign in to comment.