Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast() ignore when the dot operator is used #20550

Open
dlangBugzillaToGithub opened this issue Nov 21, 2024 · 1 comment
Open

cast() ignore when the dot operator is used #20550

dlangBugzillaToGithub opened this issue Nov 21, 2024 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Jonathan M Davis (@jmdavis) reported this on 2024-11-21T02:14:30Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24870

CC List

Description

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().
@dlangBugzillaToGithub
Copy link
Author

elpenguino+D commented on 2024-11-21T02:26:18Z

This is behaving as expected. The dot operator has a higher precedence, and `cast()(const(T*))` == `const(T)*`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant