Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 08, 2020 Casting between Structs | ||||
---|---|---|---|---|
| ||||
I reduced an issue in my project to this code snippet: struct Struct1 { } struct Struct2 { } void func1(const Struct1); void func2() { Struct2 s; func1(cast(Struct1)s); } DMD compiles this just fine but according to the spec this should not compile. The spec (https://dlang.org/spec/expression.html#CastExpression section 8) says that "Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: S(v)", however, if I write Struct1(s) in this example, I get a compile error. Is the spec wrong or is this a problem in the DMD compiler? P.S. this snippet causes LDC to segfault (https://github.com/ldc-developers/ldc/issues/3314), but it's not clear whether it should be made to work like DMD, or if they should conform to the spec and make this a compile error |
February 08, 2020 Re: Casting between Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On Saturday, 8 February 2020 at 17:36:17 UTC, Jonathan Marler wrote:
> I reduced an issue in my project to this code snippet:
>
> struct Struct1 { }
> struct Struct2 { }
>
> void func1(const Struct1);
> void func2()
> {
> Struct2 s;
> func1(cast(Struct1)s);
> }
>
>
> DMD compiles this just fine but according to the spec this should not compile. The spec (https://dlang.org/spec/expression.html#CastExpression section 8) says that "Casting a value v to a struct S, when value is not a struct of the same type, is equivalent to: S(v)", however, if I write Struct1(s) in this example, I get a compile error.
>
> Is the spec wrong or is this a problem in the DMD compiler?
>
> P.S. this snippet causes LDC to segfault (https://github.com/ldc-developers/ldc/issues/3314), but it's not clear whether it should be made to work like DMD, or if they should conform to the spec and make this a compile error
The spec is wrong in many aspects. It was written then never followed. Not sure if it even gets updated.
This seems like it should be an error. I don't know why you would ever want to do a cast like that, especially with lvalues. And if you did want to there is an easy alternative *cast(S1*)&v.
|
February 09, 2020 Re: Casting between Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to jxel | On Saturday, 8 February 2020 at 19:28:26 UTC, jxel wrote:
> On Saturday, 8 February 2020 at 17:36:17 UTC, Jonathan Marler wrote:
>> [...]
>
> The spec is wrong in many aspects. It was written then never followed. Not sure if it even gets updated.
>
I hope you're wrong about this. Can someone in D leadership please prove this statement wrong by clarifying whether this is a bug in DMD or a missing feature in the spec?
|
February 09, 2020 Re: Casting between Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On Sunday, 9 February 2020 at 01:54:36 UTC, Jonathan Marler wrote: > On Saturday, 8 February 2020 at 19:28:26 UTC, jxel wrote: >> On Saturday, 8 February 2020 at 17:36:17 UTC, Jonathan Marler wrote: >>> [...] >> >> The spec is wrong in many aspects. It was written then never followed. Not sure if it even gets updated. >> > > I hope you're wrong about this. Can someone in D leadership please prove this statement wrong by clarifying whether this is a bug in DMD or a missing feature in the spec? I've created a bug here: "Struct Cast Not Documented or Incorrectly Accepted" https://issues.dlang.org/show_bug.cgi?id=20570 |
February 13, 2020 Re: Casting between Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On 2/8/2020 6:05 PM, Jonathan Marler wrote:
> I've created a bug here: "Struct Cast Not Documented or Incorrectly Accepted" https://issues.dlang.org/show_bug.cgi?id=20570
It shouldn't compile. I'd have to investigate with the implementation.
|
Copyright © 1999-2021 by the D Language Foundation