Hello, why does this code fail to compile?
struct Foo(T){
this(Rhs, this This)(scope Rhs rhs){
}
this(ref scope typeof(this) rhs){
}
}
struct Bar{
Foo!int foo;
}
void main(){
}
error: Segmentation fault (core dumped)
Thread overview |
---|
December 12, 2021 template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) |
December 12, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) What are you trying to accomplish? |
December 12, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote: >On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) What are you trying to accomplish? Something like this:
This work but UniquePtr canno't be inside struct because Segmentation fault. |
December 13, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) Firstly, report the crash with the keyword "ice". Then, there might be something not allowed (in some situations the compiler cant decide which ctor to use) but you cant see it for now. |
December 13, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Sunday, 12 December 2021 at 19:17:53 UTC, vit wrote: >On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote: >On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) What are you trying to accomplish? Something like this:
This work but UniquePtr canno't be inside struct because Segmentation fault. This made your previous snippet work:
Definitely something funky going on behind the scenes. |
December 14, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) The problem is that the compiler will try to generate an inout copy constructor for Bar that looks roughly like this:
The idea behind this lowering is to try and call the copy constructor for Foo object if possible. One issue here is that copy constructors have the same symbol name as constructors (__ctor), so
You will get an error stating that you cannot define both an rvalue constructor and a copy constructor. However, since the constructor is templated it is impossible for the compiler to issue this error before actually instantiating the constructor. I see 2 possible fixes for this: (1) either we rename the copy constructor symbol to __cpCtor so that it does not clash with the normal constructor overload set or (2) when a templated constructor is instantiated, we can check whether it is an rvalue constructor and issue an error if a copy constructor exists. When I implemented the copy constructor I thought that it would better to have the copy constructor on a different overload set than the normal constructors, however, Walter insisted that they have the same name. So, I guess (2) is the way to go. Cheers, |
December 14, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to RazvanN | On Tuesday, 14 December 2021 at 12:04:36 UTC, RazvanN wrote: >On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >[...] The problem is that the compiler will try to generate an inout copy constructor for Bar that looks roughly like this:
The idea behind this lowering is to try and call the copy constructor for Foo object if possible. One issue here is that copy constructors have the same symbol name as constructors (__ctor), so
You will get an error stating that you cannot define both an rvalue constructor and a copy constructor. However, since the constructor is templated it is impossible for the compiler to issue this error before actually instantiating the constructor. I see 2 possible fixes for this: (1) either we rename the copy constructor symbol to __cpCtor so that it does not clash with the normal constructor overload set or (2) when a templated constructor is instantiated, we can check whether it is an rvalue constructor and issue an error if a copy constructor exists. When I implemented the copy constructor I thought that it would better to have the copy constructor on a different overload set than the normal constructors, however, Walter insisted that they have the same name. So, I guess (2) is the way to go. Cheers, Then why did my modification work?
Did my modifications ensure that this is not treated as a copy constructor? |
December 14, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tejas | On Tuesday, 14 December 2021 at 13:02:16 UTC, Tejas wrote: >On Tuesday, 14 December 2021 at 12:04:36 UTC, RazvanN wrote: >[...] Then why did my modification work?
Did my modifications ensure that this is not treated as a copy constructor? Yes, the copy constructor needs to be explicitly defined by passing a ref parameter. Since you are expecting an explicit pointer, the compiler does not see it as a copy constructor and therefore does not try to generate one for Bar. |
December 14, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to vit | On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) |
December 14, 2021 Re: template ctor overload Segmentation fault | ||||
---|---|---|---|---|
| ||||
Posted in reply to RazvanN | On Tuesday, 14 December 2021 at 14:40:00 UTC, RazvanN wrote: >On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: >Hello, why does this code fail to compile?
error: Segmentation fault (core dumped) Thanks, I got it to work for now:
|