Skip to content

Commit

Permalink
Merge pull request #2211 from zephir-lang/#1094-fix-nullable-array
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNDRmac authored Apr 15, 2021
2 parents a618a58 + cddef63 commit da7ed5d
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The format based on [Keep a Changelog](http://keepachangelog.com)
and this project adheres to [Semantic Versioning](http://semver.org).

## [Unreleased]

### Fixed
- Fixed nullable array [#1094](https://github.com/zephir-lang/zephir/issues/1094)

## [0.13.2] - 2021-04-10
### Fixed
Expand Down
17 changes: 9 additions & 8 deletions Library/Backends/ZendEngine3/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,6 @@ public function forStatement(Variable $exprVariable, $keyVariable, $variable, $d
$codePrinter->output('if (Z_TYPE_P('.$this->getVariableCode($exprVariable).') == IS_ARRAY) {');
$codePrinter->increaseLevel();

$macro = null;
$reverse = $statement['reverse'] ? 'REVERSE_' : '';

if (isset($keyVariable)) {
Expand Down Expand Up @@ -1074,7 +1073,7 @@ public function forStatement(Variable $exprVariable, $keyVariable, $variable, $d
$codePrinter->decreaseLevel();
}

/*
/**
* Compile statements in the 'for' block
*/
if (isset($statement['statements'])) {
Expand Down Expand Up @@ -1177,29 +1176,31 @@ public function fetchClassEntry($str)
* {@inheritdoc}
*
* @param string $type
* @param CompilationContext $context
* @param CompilationContext $compilationContext
* @param bool $isLocal
*
* @return Variable
*/
public function getScalarTempVariable(
string $type,
CompilationContext $context,
CompilationContext $compilationContext,
$isLocal = true
): Variable {
return $context->symbolTable->getTempNonTrackedVariable($type, $context);
return $compilationContext->symbolTable->getTempNonTrackedVariable($type, $compilationContext);
}

/**
* {@inheritdoc}
* Initialize array
*
* Init empty array or specific size array.
*
* @param Variable $variable
* @param CompilationContext $context
* @param int $size
* @param int|null $size
*
* @return void
*/
public function initArray(Variable $variable, CompilationContext $context, int $size = null)
public function initArray(Variable $variable, CompilationContext $context, ?int $size = null): void
{
$code = $this->getVariableCode($variable);

Expand Down
1 change: 0 additions & 1 deletion Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,6 @@ public function assignDefaultValue(array $parameter, CompilationContext $compila
switch ($parameter['default']['type']) {
case 'null':
$compilationContext->backend->initVar($paramVariable, $compilationContext);
$compilationContext->backend->initArray($paramVariable, $compilationContext, null);
break;

case 'empty-array':
Expand Down
12 changes: 6 additions & 6 deletions Library/Operators/Comparison/ComparisonBaseOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,14 @@ public function compile($expression, CompilationContext $compilationContext)
true
);
switch ($right->getType()) {
case 'string':
case 'null':
$rightStr = 'null' == $right->getType() ? '' : $right->getCode();
return new CompiledExpression('bool', $this->zvalNullOperator.'('.$variableLeftCode.')', $expression['left']);
break;

case 'string':
$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableLeftCode.', "'.$rightStr.'")', $expression['left']);
return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableLeftCode.', "'.$right->getCode().'")', $expression['left']);
break;

case 'variable':
Expand Down Expand Up @@ -523,9 +525,7 @@ public function compile($expression, CompilationContext $compilationContext)
case 'array':
switch ($right->getType()) {
case 'null':
$compilationContext->headersManager->add('kernel/operators');

return new CompiledExpression('bool', $this->zvalStringOperator.'('.$variableCode.', "")', $expression['left']);
return new CompiledExpression('bool', $this->zvalNullOperator.'('.$variableCode.')', $expression['left']);

case 'variable':
$variableRight = $compilationContext->symbolTable->getVariableForRead($right->getCode(), $compilationContext, $expression['left']);
Expand Down
93 changes: 93 additions & 0 deletions ext/stub/arrayaccesstest.zep.c

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

18 changes: 18 additions & 0 deletions ext/stub/arrayaccesstest.zep.h

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

7 changes: 2 additions & 5 deletions ext/stub/flow/switchflow.zep.c

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

5 changes: 0 additions & 5 deletions ext/stub/globals/serverrequestfactory.zep.c

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

1 change: 0 additions & 1 deletion ext/stub/mcall.zep.c

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

28 changes: 28 additions & 0 deletions stub/arrayaccesstest.zep
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,32 @@ class ArrayAccessTest

return in_array(s, arr);
}

public function issue1094Test1(const array items = null) -> bool
{
bool isItemsNULL;

// This syntax do not exist in Zephir...
//let isItemsNULL = null === items;

return isItemsNULL;
}

public function issue1094Test2(const array items = null) -> bool
{
bool isItemsNULL;

let isItemsNULL = items === null;

return isItemsNULL;
}

public function issue1094Test3(const array items = null) -> bool
{
bool isItemsNULL;

let isItemsNULL = is_null(items);

return isItemsNULL;
}
}
17 changes: 17 additions & 0 deletions tests/Extension/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ public function testIssue1155(): void

$this->assertFalse($class->issue1155());
}

public function testIssue1094(): void
{
$class = new \Stub\ArrayAccessTest();

$this->assertFalse($class->issue1094Test1());
$this->assertFalse($class->issue1094Test1([]));
$this->assertFalse($class->issue1094Test1(['test' => 'ok']));

$this->assertTrue($class->issue1094Test2());
$this->assertFalse($class->issue1094Test2([]));
$this->assertFalse($class->issue1094Test2(['test' => 'ok']));

$this->assertTrue($class->issue1094Test3());
$this->assertFalse($class->issue1094Test3([]));
$this->assertFalse($class->issue1094Test3(['test' => 'ok']));
}
}
2 changes: 1 addition & 1 deletion tests/Extension/MCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testArrayParamWithDefaultNullValue(): void
$this->assertNumberOfRequiredParameters(0);

$this->assertSame('array', $this->getMethodFirstParameter()->getType()->getName());
$this->assertSame($this->test->testArrayParamWithDefaultNullValue(), []);
$this->assertNull($this->test->testArrayParamWithDefaultNullValue());
$this->assertSame($this->test->testArrayParamWithDefaultNullValue([1]), [1]);
}

Expand Down

0 comments on commit da7ed5d

Please sign in to comment.