Skip to content

Language: nullsafe operator (?->) — short-circuit + error semantics parity #4394

Description

@PurHur

Category

language

Problem

Nullsafe calls (?->) are a PHP 8 language feature with specific short-circuit semantics:

  • if the receiver is null, the entire chain evaluates to null without evaluating arguments or side effects
  • if the receiver is non-null but not an object, Zend raises a type error consistent with normal ->

No open parity issue was found for the nullsafe operator.

php-src reference

  • Zend/zend_compile.c — AST nodes and opcode emission for nullsafe chains
  • Zend/zend_execute.c — runtime short-circuit behavior

Repro (failure today)

Save as repro_nullsafe.php:

<?php
class C { public function f($x) { echo "call\n"; return $x; } }

$c = null;
var_export($c?->f((function(){ echo "arg\n"; return 1; })()));
echo "\n";

$c = new C();
var_export($c?->f(2));
echo "\n";

Run:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro_nullsafe.php
# Zend expected:
# NULL
# call
# 2

php bin/vm.php repro_nullsafe.php
# today: parse/compile failure or wrong evaluation order
'

Scope (this repo)

  • Compiler lowering for nullsafe property/method access and chaining
  • VM execution: argument evaluation must be skipped when receiver is null
  • Ensure correct interaction with ??, isset, and nested expressions

Done when

  • repro_nullsafe.php output matches Zend on VM
  • Side effects in arguments are not executed when receiver is null
  • Error types/messages match Zend for non-null non-object receivers

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions