psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Exponent operator should not trigger when operands are different type and `strictBinaryOperands` is enabled

Open zerkms opened this issue 4 years ago • 4 comments

$v = 1.5 ** 10;

This code triggers

ERROR: InvalidOperand - src.php:131:22 - Cannot add ints to floats (see https://psalm.dev/058)
            $v = 1.5 ** 10;

while I believe it makes very little sense to demand both operands of ** be of the same type.

zerkms avatar Feb 19 '21 04:02 zerkms

Hey @zerkms, can you reproduce the issue on https://psalm.dev ?

psalm-github-bot[bot] avatar Feb 19 '21 04:02 psalm-github-bot[bot]

Note: requires strict binary operands option to be enabled.

weirdan avatar Feb 19 '21 06:02 weirdan

This is a big issue when attempting to upgrade a project to Psalm 6, since this option is enabled by default. https://psalm.dev/r/a7932278f5

Of course I could suppress all InvalidOperand errors, but I think they would be helpful when the types are something other than float and int.

theodorejb avatar Aug 17 '25 15:08 theodorejb

I found these snippets:

https://psalm.dev/r/a7932278f5
<?php

var_export(1.15 ** 7);
Psalm output (using commit cdceda0):

ERROR: InvalidOperand - 3:12 - Cannot process numeric types together in strict operands mode, please cast explicitly

psalm-github-bot[bot] avatar Aug 17 '25 15:08 psalm-github-bot[bot]