Skip to content

Language: declare(strict_types=1) — scalar parameter coercion and TypeError parity #9053

Description

@PurHur

Category

language

Problem

PHP’s declare(strict_types=1) changes how scalar parameter type declarations behave at call sites. Zend enforces:

  • With strict_types=1 in the caller file: scalar mismatches raise TypeError (no coercion).
  • With strict_types=0 (default): scalar mismatches may coerce (e.g. '1' to 1) depending on target type.

Parity gap: this compiler must match Zend’s strictness rules (especially “caller controls strictness”), error types/messages, and behavior for int, float, string, bool parameters and return types.

php-src reference

  • Zend/zend_execute.c — argument type verification / coercion paths
  • Zend/zend_compile.c — declare(strict_types=1) compile-time flag propagation
  • Tests: Zend/tests/type_declarations/strict_types/*

Repro

Create 3 files:

callee.php:

<?php
function takesInt(int $x): int { return $x; }

caller_weak.php:

<?php
require __DIR__.'/callee.php';
var_dump(takesInt('1'));

caller_strict.php:

<?php
declare(strict_types=1);
require __DIR__.'/callee.php';
try {
    takesInt('1');
    echo "NO ERROR\n";
} catch (Throwable $e) {
    echo get_class($e), "\n";
}

Run:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php caller_weak.php
php caller_strict.php
php bin/vm.php caller_weak.php
php bin/vm.php caller_strict.php'

Expected (Zend):

  • caller_weak.php prints int(1)
  • caller_strict.php prints TypeError

Scope

  • Compiler: ensure strict_types flag is stored per compilation unit and used at call sites
  • VM: align scalar coercion + TypeError messages with Zend
  • Tests: add test/compliance/cases/language/strict_types_scalar_args.phpt

Done when

  • Repro matches Zend outputs for weak vs strict callers
  • Caller-file strictness controls behavior even when callee is in a different file (Zend parity)
  • Error type is TypeError with close-to-Zend message

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

    IMPORTANTCritical patharea: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