D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21631 - core.atomic.cas fails to compile with const ifThis (if target is a pointer)
Summary: core.atomic.cas fails to compile with const ifThis (if target is a pointer)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2021-02-12 12:36 UTC by Harry Vennik
Modified: 2022-03-28 12:09 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Harry Vennik 2021-02-12 12:36:47 UTC
The following snippet fails to compile:

-----
shared uint si1 = 45;
shared uint si2 = 38;
shared uint* psi = &si1;

assert((&psi).cas(cast(const) psi, &si2));
-----

Error message:

src/core/atomic.d(295): Error: cannot implicitly convert expression ifThis of type shared(const(uint))* to shared(uint)*


The cas function has multiple overloads, some of which take a pointer to the ifThis, and use that to store the original value found in the target location of the cas operation. In such case, it's obvious the ifThis cannot be const. But the overloads that simply take ifThis by value (like the one used in the snippet above), cannot change ifThis, and thus should allow it to be const.

However, currently this does not work in the case the target type is a pointer to mutable data (or a struct containing such pointer) and ifThis is const.
Comment 1 Dlang Bot 2021-02-12 12:44:32 UTC
@thaven created dlang/druntime pull request #3373 "core.atomic.cas: add 'const' attribute to local variable 'arg1'" fixing this issue:

- Fix Issue 21631 - cas: add 'const' attribute to local variable 'arg1'

https://github.com/dlang/druntime/pull/3373
Comment 2 Dlang Bot 2022-03-28 12:09:26 UTC
dlang/druntime pull request #3373 "core.atomic.cas: add 'const' attribute to local variable 'arg1'" was merged into master:

- b46844a30b751e6abd5eef59d60f99d5ca3844aa by Harry T. Vennik:
  Fix Issue 21631 - cas: add 'const' attribute to local variable 'arg1'

https://github.com/dlang/druntime/pull/3373