Bug 22759 – ImportC: cannot modify const expression from dereferencing const pointer declared within function.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-10T11:17:38Z
Last change time
2022-11-06T20:13:42Z
Keywords
ImportC, pull, rejects-valid
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-02-10T11:17:38Z
The following fails to compile:
// test.c
static int *do_something(int * const p){
int * const data = p;
*data = 2; // Error: cannot modify `const` expression `*data`
return 0;
}
This was reduced from code in sqlite.
Comment #1 by bugzilla — 2022-02-13T05:29:35Z
Not sure what to do about this, as const is transitive in ImportC.
The solution (although somewhat sacrilegious) on the Compiling-C side is obvious, on the D side not so much.
Comment #4 by dave287091 — 2022-07-06T22:19:23Z
Until D adds head const, couldn’t you do a similar fix as for https://issues.dlang.org/show_bug.cgi?id=22534 ? Just treat declarations of `int * data` and `int * const data` the same in the body of c functions (as long as they are not also static).
Neither the status quo nor ignoring const in this case is correct, but this would allow more correct programs to successfully compile.
Comment #5 by schveiguy — 2022-11-02T15:34:24Z
An idea that just came up on discord -- what if D just pretended C head const was mutable? In practice, you are making a copy anyway, and the implementation isn't actually affected since you might assume that the code would already be compiling on existing C compilers.
Credit to Adam Ruppe for the idea.
So in the example, `int * const p` just becomes `int * p`, along with all the other code pieces.
You can keep the fact that const was present in the AST as a note, in case you need to print something or in case you at some point decide to enforce head-const.
Comment #6 by destructionator — 2022-11-02T15:46:48Z
yeah and then anything below the head const i think can stay transitive, which might reject some valid C code but would avoid allowing invalid D code which i think is a fair compromise.
my gut feeling is this would work in the majority of cases and people can work in explicit casts for the remainder
Comment #7 by schveiguy — 2022-11-02T15:58:04Z
(In reply to dave287091 from comment #4)
> Just treat declarations of
> `int * data` and `int * const data` the same in the body of c functions (as
> long as they are not also static).
apologies, it looks exactly the same as the thing I just suggested! I didn't read this fully.
But yes, I think this is the way forward.
Looking at the referenced issue, I can't see how it's any different actually.
Comment #8 by dlang-bot — 2022-11-06T03:40:20Z
@WalterBright created dlang/dmd pull request #14621 "fix Issue 22759 - ImportC: cannot modify const expression from derefe…" fixing this issue:
- fix Issue 22759 - ImportC: cannot modify const expression from dereferencing const pointer declared within function
https://github.com/dlang/dmd/pull/14621
Comment #9 by dlang-bot — 2022-11-06T20:13:42Z
dlang/dmd pull request #14621 "fix Issue 22759 - ImportC: cannot modify const expression from derefe…" was merged into master:
- 9fbfc7bcb314ce16bfc3e662dbf7fb4ca5a446a4 by Walter Bright:
fix Issue 22759 - ImportC: cannot modify const expression from dereferencing const pointer declared within function
https://github.com/dlang/dmd/pull/14621