Bug 24241 – Spec disallows missing default arguments

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-11-13T11:11:32Z
Last change time
2023-12-13T11:40:03Z
Keywords
pull
Assigned to
No Owner
Creator
ponce

Attachments

IDFilenameSummaryContent-TypeSize
1899main.dsource filetext/plain135
1900default-arg-issue.pngRepro screenshotimage/png33159

Comments

Comment #0 by aliloko — 2023-11-13T11:11:32Z
Created attachment 1899 source file Consider the following D program: ------------------- main.d -------------- // Here `d` should have a default argument value too. // Will DMD catch it? ubyte lookup_decoding(char c = '/', bool d) { return (c + d) & 255; } void main(string[] args) { lookup_decoding(1, true); } ------------------------------------------ This build OK with DMD 2.104.0 This doesn't build with DMD 2.100.0 Making it very easy to use default argument incorectly.
Comment #1 by aliloko — 2023-11-13T11:12:57Z
Created attachment 1900 Repro screenshot
Comment #2 by b2.temp — 2023-11-13T11:19:50Z
That does not compile if called without the second arg however lookup_decoding(1); I suppose this bug is related to named arg handling ?
Comment #3 by b2.temp — 2023-11-13T11:23:48Z
hence that would not be a bug after all ?
Comment #4 by aliloko — 2023-11-13T14:09:16Z
Oh this is a voluntary change? In practice, what we've had is that code that is produced by people after that change, break build of those that use earlier compilers. It's a breaking change that I feel we could warn about, since it seems named arguments aren't already there?
Comment #5 by aliloko — 2023-11-13T14:11:57Z
It used to be illegal to have default values for arguments not at the end, making this legal is unexpected (and creates issues like this).
Comment #6 by aliloko — 2023-11-13T14:30:34Z
By spec: https://dlang.org/spec/function.html#function-default-args > Default parameters are resolved and semantically checked in the context of the function declaration. and later > If the default value for a parameter is given, all following parameters must also have default values. Pretty much a bug then?
Comment #7 by razvan.nitu1305 — 2023-11-13T14:43:16Z
Yes, this is an intended change as per the named arguments dip: https://github.com/dlang/DIPs/blob/15537f9b36afa48f1c1cd57468e8c77f8f2499dd/DIPs/DIP1030.md . I don't see how this change breaks any existing code. Old code works as it used to work under the newer compiler versions, whereas new code may be written to take advantage of named parameters. The situation that was invoked: "In practice, what we've had is that code that is produced by people after that change, break build of those that use earlier compilers." is true for any other compiler addition. If a library writer uses the newest fanciest features, then it is expected that the users of the said library need to use newer versions of the compiler. > It's a breaking change that I feel we could warn about, since it seems named arguments aren't already there? How would a warning help here since the code is not going to compile anyway? I don't really see anything actionable for this bug report.
Comment #8 by razvan.nitu1305 — 2023-11-13T14:44:02Z
(In reply to ponce from comment #6) > By spec: > https://dlang.org/spec/function.html#function-default-args > > > > Default parameters are resolved and semantically checked in the context of the function declaration. > > and later > > > If the default value for a parameter is given, all following parameters must also have default values. > > > Pretty much a bug then? The spec needs to be updated.
Comment #9 by b2.temp — 2023-11-13T14:46:54Z
sorry about my previous comments, they were more about the general semantics of "default arguments" but you're right, D specs are the reference here.
Comment #10 by aliloko — 2023-11-13T15:55:08Z
It's surprising to me because it seems nothing hints at this rule changing (not even in spec). The new rules are cool and all, and it's easy to see named arguments will hugely improve readability. It's just that if you're not aware of them changing because following D from even a tiny distance, you can miss the changes completely. Are there other changes like this? How can I get up to speed? Under the release changelog, I'm struggling to find a point where this has changed ("if you write code in the new way, then it's breaking old compilers") can be seen.
Comment #11 by aliloko — 2023-11-13T15:57:38Z
> If a library writer uses the newest fanciest features, Well in this case there is no warning that one might use a newer feature, it certainly happens without my awareness.
Comment #12 by aliloko — 2023-11-13T16:32:07Z
Changelog wasn't updated in the end it seems: https://github.com/dlang/dmd/pull/15030
Comment #13 by dlang-bot — 2023-12-13T10:18:59Z
@dkorpel created dlang/dlang.org pull request #3744 "Document named arguments" fixing this issue: - Document named arguments Fix issue 24241 https://github.com/dlang/dlang.org/pull/3744
Comment #14 by dlang-bot — 2023-12-13T11:40:03Z
dlang/dlang.org pull request #3744 "Document named arguments" was merged into master: - 0c4b6008d6090b59f79be8179710c1312a2ff33c by Dennis Korpel: Document named arguments Fix issue 24241 https://github.com/dlang/dlang.org/pull/3744