Skip to content

c migrator: cond ? &a->f : &b->f becomes &raw const (if cond: a.f else: b.f), the address of a temporary copy #2314

Closed

Description

@ehartford

Spec §15.13: &raw takes the address of a place; an if expression is a value, so &raw const (if c: x.f else: y.f) copies the chosen field into a temporary and points at the copy.

Where. lib/std/re/pcre2_substitute.w:592 and :627 (the migrated pcre2 corpus):

with_memcpy((&raw mut gcontext.memctl as *mut u8),
    (&raw const (if (if __param_mcontext == null: 1 else: 0) != 0: (*code).memctl else: (*__param_mcontext).memctl) as *const u8), sizeof[pcre2_memctl]())

The C is memcpy(&gcontext.memctl, (mcontext == NULL)? &code->memctl : &mcontext->memctl, sizeof(gcontext.memctl));. The migrator moved the & out of the conditional. Here the copy happens to read the same bytes, but if the callee kept the pointer it would dangle; the translation changes meaning.

Repro. Source-check pcre2 (any compiler checking lib/std/re from source instead of its bundle, e.g. a stage1 whose bundles are not built yet): warning: \&raw const` requires a place; this expression is not a place` at both sites.

Expected. The migrator emits if c: &raw const (*code).memctl else: &raw const (*mcontext).memctl. The address of each arm stays in its arm, as C wrote it.

Also. That the compiler only warns here, where &raw mut is an error, is worth a look: a pointer to a temporary is never what the source meant.

Found during D114 stage 2 (#2262).

Activity

  1. ehartford commented on Oct 14, 2026

    @ehartford
    ContributorAuthor

    Also red on stage1 (not the release binary): behav_c_facade_pending_producer_warns asserts no warning from with check, and a stage1 built from main (3d03845 and the #2366 branch) prints this warning twice from lib/std/re/pcre2_substitute.w:593 and :628 (&raw const (if … : a.memctl else: b.memctl), the migrated cond ? &a->f : &b->f). The release compiler compiled from the same tree does not print it, so with-stage1 test test/behavior shows a red the battery never sees. behav_1944_comptime_snapshot_memory likewise fails only on stage1 (committed=31590464 of the 33554432 limit).

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce. The migrator now takes the address in each branch, and the re-promoted pcre2 corpus no longer contains &raw const (if.

    $ cat input.c
    void g(struct S *a, struct S *b, int c, int *out) { memcpy(out, c ? &a->f : &b->f, sizeof(int)); }
    $ with migrate input.c -o out.w
    … (if __param_c != 0: ((&raw const (*__param_a).f as *const i32) as *mut i32) else: ((&raw const (*__param_b).f as *const i32) as *mut i32)) …
    $ grep -c "&raw const (if" lib/std/engines/pcre2/pcre2_substitute.w
    0
    
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions