Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2374 - Implement nullable object return type #2379

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format based on [Keep a Changelog](https://keepachangelog.com)
and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
### Fixed
- Fixed generation of `ARG_INFO` for nullable object (`?object`) [#2374](https://github.com/zephir-lang/zephir/issues/2374)

## [0.16.0] - 2022-03-20
### Added
Expand Down
39 changes: 31 additions & 8 deletions Library/ArgInfoDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,9 @@ public function render(): void

private function richRenderStart(): void
{
if (array_key_exists('object', $this->functionLike->getReturnTypes())) {
$class = 'NULL';

if (1 === count($this->functionLike->getReturnClassTypes())) {
$class = key($this->functionLike->getReturnClassTypes());
$class = escape_class($this->compilationContext->getFullName($class));
}
if (array_key_exists('object', $this->functionLike->getReturnTypes()) && 1 === count($this->functionLike->getReturnClassTypes())) {
$class = key($this->functionLike->getReturnClassTypes());
$class = escape_class($this->compilationContext->getFullName($class));

$this->codePrinter->output(
sprintf(
Expand Down Expand Up @@ -237,6 +233,33 @@ private function richRenderStart(): void
return;
}

if ($this->functionLike->isReturnTypeNullableObject()) {
$this->codePrinter->output('#if PHP_VERSION_ID >= 80000');
$this->codePrinter->output(
sprintf(
'ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(%s, %d, %d, %s)',
$this->name,
(int) $this->returnByRef,
$this->functionLike->getNumberOfRequiredParameters(),
'MAY_BE_NULL|MAY_BE_OBJECT',
)
);
$this->codePrinter->output('#else');
$this->codePrinter->output(
sprintf(
'ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(%s, %d, %d, %s, %d)',
$this->name,
(int) $this->returnByRef,
$this->functionLike->getNumberOfRequiredParameters(),
'IS_OBJECT',
1,
)
);
$this->codePrinter->output('#endif');

return;
}

if (count($this->functionLike->getReturnTypes()) > 1) {
$types = [];
$mayBeTypes = $this->functionLike->getMayBeArgTypes();
Expand Down Expand Up @@ -446,7 +469,7 @@ private function allowNull(array $parameter): bool
return false;
}

if ('null' == $parameter['default']['type']) {
if ('null' === $parameter['default']['type']) {
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2340,6 +2340,16 @@ public function isReturnTypesHintDetermined(): bool
return true;
}

/**
* Checks if method's return type is nullable object `?object`.
*
* @return bool
*/
public function isReturnTypeNullableObject(): bool
{
return count($this->returnTypes) === 2 && isset($this->returnTypes['object']) && isset($this->returnTypes['null']);
}

/**
* Checks if the method have compatible return types.
*
Expand Down
2 changes: 1 addition & 1 deletion Library/Expression/NativeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function compile(array $expression, CompilationContext $compilationContex
*/
$arrayLength = count($expression['left']);
if ($arrayLength >= 33 && function_exists('gmp_nextprime')) {
$arrayLength = gmp_strval(gmp_nextprime($arrayLength - 1));
$arrayLength = (int) gmp_strval(gmp_nextprime($arrayLength - 1));
}

if ($this->expectingVariable && $symbolVariable->getVariantInits() >= 1) {
Expand Down
1 change: 1 addition & 0 deletions ext/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ if test "$PHP_STUB" = "yes"; then
stub/typeoff.zep.c
stub/types/maybe.zep.c
stub/types/mixedtype.zep.c
stub/types/obj.zep.c
stub/unknownclass.zep.c
stub/unsettest.zep.c
stub/usetest.zep.c
Expand Down
2 changes: 1 addition & 1 deletion ext/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (PHP_STUB != "no") {
ADD_SOURCES(configure_module_dirname + "/stub/requires", "external3.zep.c", "stub");
ADD_SOURCES(configure_module_dirname + "/stub/router", "exception.zep.c route.zep.c", "stub");
ADD_SOURCES(configure_module_dirname + "/stub/typehinting", "testabstract.zep.c", "stub");
ADD_SOURCES(configure_module_dirname + "/stub/types", "maybe.zep.c mixedtype.zep.c", "stub");
ADD_SOURCES(configure_module_dirname + "/stub/types", "maybe.zep.c mixedtype.zep.c obj.zep.c", "stub");
ADD_FLAG("CFLAGS_STUB", "/D ZEPHIR_RELEASE /Oi /Ot /Oy /Ob2 /Gs /GF /Gy /GL");
ADD_FLAG("CFLAGS", "/D ZEPHIR_RELEASE /Oi /Ot /Oy /Ob2 /Gs /GF /Gy /GL");
ADD_FLAG("LDFLAGS", "/LTCG");
Expand Down
2 changes: 2 additions & 0 deletions ext/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ zend_class_entry *stub_typeinstances_ce;
zend_class_entry *stub_typeoff_ce;
zend_class_entry *stub_types_maybe_ce;
zend_class_entry *stub_types_mixedtype_ce;
zend_class_entry *stub_types_obj_ce;
zend_class_entry *stub_unknownclass_ce;
zend_class_entry *stub_unsettest_ce;
zend_class_entry *stub_usetest_ce;
Expand Down Expand Up @@ -473,6 +474,7 @@ static PHP_MINIT_FUNCTION(stub)
ZEPHIR_INIT(Stub_Typeoff);
ZEPHIR_INIT(Stub_Types_MayBe);
ZEPHIR_INIT(Stub_Types_MixedType);
ZEPHIR_INIT(Stub_Types_Obj);
ZEPHIR_INIT(Stub_UnknownClass);
ZEPHIR_INIT(Stub_Unsettest);
ZEPHIR_INIT(Stub_UseTest);
Expand Down
1 change: 1 addition & 0 deletions ext/stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
#include "stub/typeoff.zep.h"
#include "stub/types/maybe.zep.h"
#include "stub/types/mixedtype.zep.h"
#include "stub/types/obj.zep.h"
#include "stub/unknownclass.zep.h"
#include "stub/unsettest.zep.h"
#include "stub/usetest.zep.h"
Expand Down
43 changes: 43 additions & 0 deletions ext/stub/types/obj.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions ext/stub/types/obj.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions stub/types/obj.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace Stub\Types;

class Obj
{
public function nullableObjectReturnNull() -> object | null
{
return null;
}

public function nullableObjectReturnObj() -> object | null
{
return new \stdClass();
}
}
29 changes: 29 additions & 0 deletions tests/Extension/Types/ObjTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <team@zephir-lang.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Extension\Types;

use PHPUnit\Framework\TestCase;
use stdClass;
use Stub\Types\Obj;

final class ObjTypeTest extends TestCase
{
public function testIntFalse(): void
{
$class = new Obj();

$this->assertNull($class->nullableObjectReturnNull());
$this->assertInstanceOf(stdClass::class, $class->nullableObjectReturnObj());
}
}