Skip to content

elab: desugar &arr[i] to arr + i for array-shaped pointers#119

Merged
gebner merged 1 commit into
mainfrom
kimaya/desugar
Jun 5, 2026
Merged

elab: desugar &arr[i] to arr + i for array-shaped pointers#119
gebner merged 1 commit into
mainfrom
kimaya/desugar

Conversation

@KimayaBedarkar

Copy link
Copy Markdown
Collaborator

C defines &E1[E2] to be exactly (E1) + (E2). PAL's emit pass was not honouring that identity: for an _array T* or _arrayptr T* operand, the inner Index node could not produce an lvalue, so the surrounding Ref reported 'cannot produce lvalue for a[i]' (non-fatal) and substituted (admit()). The implicit Array->ArrayPtr coercion at the return site then wrapped that with a hardcoded 0sz offset, silently dropping the actual index from the source.

Fix in elab: when a Ref wraps an Index whose array operand has type Pointer(_, Array | ArrayPtr), rewrite the expression to a BinOp::Add and re-elaborate. BinOp::Add on those pointer kinds is already emitted correctly (array_to_arrayptr / arrayptr_shift with the real offset). True C arrays (FixedArray) are unaffected and continue to use the existing lvalue path.

C defines `&E1[E2]` to be exactly `(E1) + (E2)`. PAL's emit pass was
not honouring that identity: for an `_array T*` or `_arrayptr T*`
operand, the inner Index node could not produce an lvalue, so the
surrounding Ref reported 'cannot produce lvalue for a[i]' (non-fatal)
and substituted `(admit())`. The implicit Array->ArrayPtr coercion at
the return site then wrapped that with a hardcoded `0sz` offset,
silently dropping the actual index from the source.

Fix in elab: when a Ref wraps an Index whose array operand has type
Pointer(_, Array | ArrayPtr), rewrite the expression to a BinOp::Add
and re-elaborate. BinOp::Add on those pointer kinds is already emitted
correctly (array_to_arrayptr / arrayptr_shift with the real offset).
True C arrays (FixedArray) are unaffected and continue to use the
existing lvalue path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gebner gebner merged commit a683ac3 into main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants