Thread overview
[Issue 22759] ImportC: cannot modify const expression from dereferencing const pointer declared within function.
Feb 13, 2022
Walter Bright
Jul 06, 2022
mhh
Nov 02, 2022
Adam D. Ruppe
Nov 06, 2022
Dlang Bot
Nov 06, 2022
Dlang Bot
February 13, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
Not sure what to do about this, as const is transitive in ImportC.

--
July 05, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

--- Comment #2 from dave287091@gmail.com ---
This bug was encountered in this blogpost: https://briancallahan.net/blog/20220704.html (section `Running make`).

--
July 06, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

mhh <maxhaton@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxhaton@gmail.com

--- Comment #3 from mhh <maxhaton@gmail.com> ---
The solution (although somewhat sacrilegious) on the Compiling-C side is obvious, on the D side not so much.

--
July 06, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

--- Comment #4 from dave287091@gmail.com ---
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.

--
November 02, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@gmail.com

--- Comment #5 from Steven Schveighoffer <schveiguy@gmail.com> ---
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.

--
November 02, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

Adam D. Ruppe <destructionator@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator@gmail.com

--- Comment #6 from Adam D. Ruppe <destructionator@gmail.com> ---
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

--
November 02, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

--- Comment #7 from Steven Schveighoffer <schveiguy@gmail.com> ---
(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.

--
November 06, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #8 from Dlang Bot <dlang-bot@dlang.rocks> ---
@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

--
November 06, 2022
https://issues.dlang.org/show_bug.cgi?id=22759

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Dlang Bot <dlang-bot@dlang.rocks> ---
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

--