Allow readonly storage and results for untyped constref (Fixes #41766)
Summary
Fixes #41766. Forwarding an element of readonly storage to an untyped constref parameter was rejected as an assignment to a const variable. Permit readonly storage in valid_for_formal_constref, as the untyped const path already does. Accepted function results also need an address: materialize non-reference actuals in push_formal_para for constref as well as const, leaving existing references unchanged.
This does not permit arbitrary scalar literals or arithmetic expressions as untyped actuals. valid_const permits readonly storage and selected expression forms; it is not a blanket constant-expression switch. The earlier title/description overstated that scope.
System
- Operating system: all; tested on Windows.
- Processor architecture: target-independent frontend/codegen change; tested on x86_64 and i386.
- Device: computer.
Current bug behavior
The original readonly-buffer forwarding example fails with Can't assign values to const variable. Allowing the frontend form without the codegen change leaves integer function results without addressable storage.
Behavior after applying this patch
Existing storage retains its address when forwarded. Accepted temporary results are evaluated once and remain alive for the call. Writable var/out parameters still reject readonly actuals.
Tests
tw41766: original buffer/element forwarding regression, now endian-independent and ending withok.tw41766a: direct/forwarded/typed consumers; local, record-field, dynamic-array and typed-constant storage; integer, floating-point and managed-string function results; two simultaneous temporaries and evaluation counts.tw41766b/c: rejection controls for scalar literals and readonly-to-writable forwarding.
Both runtime tests pass at -O-, -O2 and -O3 on x86_64-win64, and at -O3 on i386-win32. Negative tests reject with the intended diagnostics. The unpatched compiler rejects the runtime matrix. Removing only constref materialization from the patched compiler makes the new matrix fail with internal error 200304235, so it covers the second half of the fix independently.
Also proposed to Unleashed as https://github.com/unleashedpascal/compiler/pull/49; that PR waits for the FPC decision.