[Issue 24870] New: cast() ignore when the dot operator is used
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Nov 21 02:14:30 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24870
Issue ID: 24870
Summary: cast() ignore when the dot operator is used
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: issues.dlang at jmdavisProg.com
This code
---
void main()
{
const s = S(42);
T* ptr = cast() s.t;
}
struct S
{
T* t;
this(int i)
{
t = new T(42);
}
}
struct T
{
int i;
}
---
fails to compile:
---
q.d(4): Error: cannot implicitly convert expression `s.t` of type `const(T)*`
to `T*`
---
It's exactly what would happen if the cast() were not there. If I change the
offending line to
---
T* ptr = (cast() s).t;
---
to force the cast to be on s, then the code compiles. So, it would appear that
without parens, the cast() applies to t (which is what I would expect), and
putting the parens around the entire expression has the same result as having
none:
---
T* ptr = (cast() s.t);
---
which is also what I would expect. However, in this case, I wouldn't expect it
to matter one whit whether the cast applied to s or to t. If s becomes mutable,
then its t member will be mutable, and if s is left const and the cast applies
to its t member, then t should still be mutable, and then the resulting pointer
value should be mutable. In either case, the result should be a mutable T*
which should be able to be used to initialize the variable.
Maybe there's some language detail here that I'm missing, and this isn't
actually a bug, but I don't see any reason why using cast() wouldn't work on
s.t, so something about using the . operator seems to be mucking things up.
Note that
---
T* t = cast(T*) s.t;
---
does work, so the issue is specifically with cast().
--
More information about the Digitalmars-d-bugs
mailing list