Bug 23791 – Rvalue default argument for ref parameter gives misleading error

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-03-17T15:04:04Z
Last change time
2024-01-01T12:31:10Z
Keywords
diagnostic, pull
Assigned to
No Owner
Creator
Paul Backus

Comments

Comment #0 by snarwin+bugzilla — 2023-03-17T15:04:04Z
As of DMD 2.102.2, attempting to compile the following program: --- void f1(ref int n = 123) {} struct S {} void f2(ref S s = S.init) {} --- ...produces the following error messages: --- bug.d(1): Error: cannot modify constant `123` bug.d(4): Error: `S()` is not an lvalue and cannot be modified --- These messages are misleading, because the program is not attempting to modify either `123` or `S()`. The actual cause of the errors is that rvalues cannot be used as default arguments for `ref` parameters. The messages should reflect this.
Comment #1 by dlang-bot — 2023-09-09T12:04:00Z
@ntrel created dlang/dmd pull request #15596 "Fix isLvalue error message" fixing this issue: - Fix isLvalue error message Requiring an lvalue is not the same as requiring modification. Fix Issue 23791 - Rvalue default argument for ref parameter gives misleading error. https://github.com/dlang/dmd/pull/15596
Comment #2 by nick — 2024-01-01T12:31:10Z
Fixed by https://github.com/dlang/dmd/pull/15793: /home/nick/os/rval.d(1): Error: cannot create default argument for `ref` / `out` parameter from constant `123` /home/nick/os/rval.d(4): Error: cannot create default argument for `ref` / `out` parameter from expression `S()` because it is not an lvalue