# [RFC] [Vote] Short Ternary Assignment Operator

- Thread: https://externals.io/message/91901
- Original archive: https://news-web.php.net/php.internals/91901
- Started by Sara Golemon on 2016-03-24T15:51:07+00:00
- Messages: 9
- RFC: Short Ternary Assignment Operator (Inactive) — https://externals.io/rfc/short_ternary_equal_operator.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #91901 — Sara Golemon — 2016-03-24T15:51:07+00:00

URL: https://externals.io/message/91901#91901

Following on from Midori, I've moved the short-ternary assignment
operator RFC into voting phase.
The implementation for which will be her patch with appropriately
different symbols and tests.
https://wiki.php.net/rfc/short_ternary_equal_operator

-Sara

---

## #91903 — Ryan Pallas — 2016-03-24T15:55:47+00:00

URL: https://externals.io/message/91901#91903 · In reply to: #91901

> Following on from Midori, I've moved the short-ternary assignment
> operator RFC into voting phase.
> The implementation for which will be her patch with appropriately
> different symbols and tests.
> https://wiki.php.net/rfc/short_ternary_equal_operator


I like this as well, +1 for me :) Good luck, hope to see it in 7.1!


>
>
> -Sara

---

## #91904 — Nikita Popov — 2016-03-24T16:45:48+00:00

URL: https://externals.io/message/91901#91904 · In reply to: #91901

> Following on from Midori, I've moved the short-ternary assignment
> operator RFC into voting phase.
> The implementation for which will be her patch with appropriately
> different symbols and tests.
> https://wiki.php.net/rfc/short_ternary_equal_operator
>

To clarify, as the RFC only mentions this by implication:

    unset($x); // Assume $x is undefined
    $x ?:= $y;

This will throw a notice, correct?

Thanks,
Nikita

---

## #91906 — Sara Golemon — 2016-03-24T17:39:40+00:00

URL: https://externals.io/message/91901#91906 · In reply to: #91904

> To clarify, as the RFC only mentions this by implication:
>
>     unset($x); // Assume $x is undefined
>     $x ?:= $y;
>
> This will throw a notice, correct?
>
Yes.  The equivalent short-ternary:

$x = $x ?: $y;

would throw a notice, so the assignment version should (and would) as well.

---

## #91914 — Sara Golemon — 2016-03-24T18:12:32+00:00

URL: https://externals.io/message/91901#91914 · In reply to: #91901

After some discussion (and realizing the referenced implementation
needed more work that a simple replacement of tokens), I've decided
the close this vote, work with Midori on both implementations some
more, and reopen at a later time with a complete implementation
(possibly combined with ??=)


> Following on from Midori, I've moved the short-ternary assignment
> operator RFC into voting phase.
> The implementation for which will be her patch with appropriately
> different symbols and tests.
> https://wiki.php.net/rfc/short_ternary_equal_operator
>
> -Sara

---

## #91934 — Sara Golemon — 2016-03-25T03:25:20+00:00

URL: https://externals.io/message/91901#91934 · In reply to: #91914

> After some discussion (and realizing the referenced implementation
> needed more work that a simple replacement of tokens), I've decided
> the close this vote, work with Midori on both implementations some
> more, and reopen at a later time with a complete implementation
> (possibly combined with ??=)
>
https://github.com/php/php-src/compare/master...sgolemon:short-ternary.coalesce

I've gone from scratch to implement this branch which doesn't
introduce any new opcodes, but it does add a new AST kind which is
compiled into something closely (but not quite) resembling a regular
short ternary.

It cheats slightly by assuming that since child[0] comes from a
`variable` in the parser that zend_compile_var() on it will always
yield IS_CV/IS_VAR, and I've got an assert in to guard on that, but a
second set of eyes would be nice there.

You'll notice there's a new runtime check in ZEND_JMP_SET to handle
the IS_INDIRECT case (which I see resulting from the dim/obj paths.
It's hidden behind an existing check for IS_VAR/IS_CV, so anything
producing a TMP won't hit it.  Hopefully it's not too harsh.

Based on feedback, I hope to add the ??= version of this
implementation as another commit and let Midori unify the two RFCs
into one. (unless someone objects to that)

-Sara

---

## #91948 — Nikita Popov — 2016-03-25T13:44:44+00:00

URL: https://externals.io/message/91901#91948 · In reply to: #91934

> 
> > […]
>
> https://github.com/php/php-src/compare/master...sgolemon:short-ternary.coalesce
>
> I've gone from scratch to implement this branch which doesn't
> introduce any new opcodes, but it does add a new AST kind which is
> compiled into something closely (but not quite) resembling a regular
> short ternary.
>
> It cheats slightly by assuming that since child[0] comes from a
> `variable` in the parser that zend_compile_var() on it will always
> yield IS_CV/IS_VAR, and I've got an assert in to guard on that, but a
> second set of eyes would be nice there.
>
> You'll notice there's a new runtime check in ZEND_JMP_SET to handle
> the IS_INDIRECT case (which I see resulting from the dim/obj paths.
> It's hidden behind an existing check for IS_VAR/IS_CV, so anything
> producing a TMP won't hit it.  Hopefully it's not too harsh.
>
> Based on feedback, I hope to add the ??= version of this
> implementation as another commit and let Midori unify the two RFCs
> into one. (unless someone objects to that)
>

I don't think the current implementation is entirely correct. In
particular, it doesn't look memory-safe to me. You're doing an RW fetch on
the LHS first, then evaluate the RHS expression and then ASSIGN to the
result of the RW fetch. This means you're running user code between the RW
fetch and the assignment to it. The code of the RHS expression may cause a
reallocation of the buffer into which the INDIRECT points, making it a
dangling pointer.

Something like

$a = [false];
$a[0] ?:= ($a[''] = 42);

will probably result in valgrinds.

Nikita

---

## #91950 — Sara Golemon — 2016-03-25T15:49:34+00:00

URL: https://externals.io/message/91901#91950 · In reply to: #91948

> I don't think the current implementation is entirely correct. In particular,
> it doesn't look memory-safe to me. You're doing an RW fetch on the LHS
> first, then evaluate the RHS expression and then ASSIGN to the result of the
> RW fetch. This means you're running user code between the RW fetch and the
> assignment to it. The code of the RHS expression may cause a reallocation of
> the buffer into which the INDIRECT points, making it a dangling pointer.
>
> Something like
>
> $a = [false];
> $a[0] ?:= ($a[''] = 42);
>
> will probably result in valgrinds.
>
Ah, good call.  Back to the drawing board. :)

-Sara

---

## #92237 — Sara Golemon — 2016-04-12T23:08:53+00:00

URL: https://externals.io/message/91901#92237 · In reply to: #91950

Took me awhile to get back around to this problem, but this version feels okay.
https://github.com/php/php-src/compare/master...sgolemon:short-ternary.coalesce

It's less efficient than earlier versions, but no worse than the
current `A = A ?: B` syntax (which is effectively what it does, with
the twist of doing a RW fetch instead of an R fetch, and thus
resolving the unwanted notices).

-Sara
