digitalmars.D.learn - Why this fails when using unittest?
- Machine Code (12/14) Jun 06 2019 outside an unittest, this compiles fine:
- Machine Code (4/20) Jun 06 2019 or better:
- Adam D. Ruppe (7/12) Jun 06 2019 My suspicion is it is trying to access the context of the
- Steven Schveighoffer (4/22) Jun 06 2019 Yes, correct guess. A unittest block is actually a function, so it's
- Machine Code (22/46) Jun 07 2019 Intesting, I also tried to declare it inside a function, that did
- Machine Code (5/29) Jun 07 2019 All this effort is because I do not want unittest code in a
- Adam D. Ruppe (6/8) Jun 07 2019 Well, that part is easy:
- Machine Code (14/22) Jun 07 2019 Thank you! I was about to read version docs to see if I could
- Machine Code (6/21) Jun 07 2019 So it's a CTFE limitation in unittest/local functions(I also
outside an unittest, this compiles fine: struct A { enum A foo = "hehe"; this(string a) { m_a = a; } alias m_a this; string m_a; } but if you wrap this a unittest {} and compile withdmd -unittest -run foo.dthis give the following error:cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.
Jun 06 2019
On Thursday, 6 June 2019 at 17:40:17 UTC, Machine Code wrote:outside an unittest, this compiles fine: struct A { enum A foo = "hehe"; this(string a) { m_a = a; } alias m_a this; string m_a; } but if you wrap this a unittest {} and compile withI this notice albeit a simple cast workaround that:dmd -unittest -run foo.dthis give the following error:cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.enum A foo = cast(A)"hehe";or better:enum foo = A("hehe");but isn't that a bug? what am I missing?
Jun 06 2019
On Thursday, 6 June 2019 at 17:40:17 UTC, Machine Code wrote:outside an unittest, this compiles fine: struct Atry making it `static struct` insteadMy suspicion is it is trying to access the context of the unittest as a hidden paramater to that constructor there... and in an enum, it can't, just stupid compiler didn't think to mention the hidden arg here. my guess.cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.
Jun 06 2019
On 6/6/19 1:49 PM, Adam D. Ruppe wrote:On Thursday, 6 June 2019 at 17:40:17 UTC, Machine Code wrote:Yes, correct guess. A unittest block is actually a function, so it's considered an inner struct with a hidden context pointer. -Steveoutside an unittest, this compiles fine: struct Atry making it `static struct` insteadMy suspicion is it is trying to access the context of the unittest as a hidden paramater to that constructor there... and in an enum, it can't, just stupid compiler didn't think to mention the hidden arg here. my guess.cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.
Jun 06 2019
On Thursday, 6 June 2019 at 21:02:37 UTC, Steven Schveighoffer wrote:On 6/6/19 1:49 PM, Adam D. Ruppe wrote:Intesting, I also tried to declare it inside a function, that did not work either. Is this hidden context pointer a current limitation in CTFE? I've tried to declare the struct at module level and run the functions on static this() to workaround that but to finish, I'd like to eble to run the code in static main() only when unnitest is enabled but so I haven't manged to do that. *Imaginary code* would be: unittest { enum enabled = true; } else { enum enabled = true; } but I was on my mind unittest was similar to static if() but as it's like a function, there are no else let alone acess to enum enabled as true, outside the block but the idea is run (or even only declare) a piece of code (which includes that struct) only if we are an unittest.On Thursday, 6 June 2019 at 17:40:17 UTC, Machine Code wrote:Yes, correct guess. A unittest block is actually a function, so it's considered an inner struct with a hidden context pointer. -Steveoutside an unittest, this compiles fine: struct Atry making it `static struct` insteadMy suspicion is it is trying to access the context of the unittest as a hidden paramater to that constructor there... and in an enum, it can't, just stupid compiler didn't think to mention the hidden arg here. my guess.cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.
Jun 07 2019
On Friday, 7 June 2019 at 16:30:34 UTC, Machine Code wrote:On Thursday, 6 June 2019 at 21:02:37 UTC, Steven Schveighoffer wrote:All this effort is because I do not want unittest code in a release or even debug. So I want to this be invisible anywhere but unittest or at least simulate that wit static if() how I'm tring to do[...]Intesting, I also tried to declare it inside a function, that did not work either. Is this hidden context pointer a current limitation in CTFE? I've tried to declare the struct at module level and run the functions on static this() to workaround that but to finish, I'd like to eble to run the code in static main() only when unnitest is enabled but so I haven't manged to do that. *Imaginary code* would be: unittest { enum enabled = true; } else { enum enabled = true; } but I was on my mind unittest was similar to static if() but as it's like a function, there are no else let alone acess to enum enabled as true, outside the block but the idea is run (or even only declare) a piece of code (which includes that struct) only if we are an unittest.
Jun 07 2019
On Friday, 7 June 2019 at 16:33:13 UTC, Machine Code wrote:All this effort is because I do not want unittest code in a release or even debug.Well, that part is easy: version(unittest) struct Foo {} at any scope is only build when unittests are turned on in this build.
Jun 07 2019
On Friday, 7 June 2019 at 16:41:12 UTC, Adam D. Ruppe wrote:On Friday, 7 June 2019 at 16:33:13 UTC, Machine Code wrote:Thank you! I was about to read version docs to see if I could determine if I was in unittest by versioning. That's exact behavior I was trying to acomplish. I ended up doing this: version(unittest) { struct Foo { } shared static this() { // test code ... } } Act like a unittest {} but I can use CTFE stuff nicely. Thank you all guys.All this effort is because I do not want unittest code in a release or even debug.Well, that part is easy: version(unittest) struct Foo {} at any scope is only build when unittests are turned on in this build.
Jun 07 2019
On Thursday, 6 June 2019 at 17:49:58 UTC, Adam D. Ruppe wrote:On Thursday, 6 June 2019 at 17:40:17 UTC, Machine Code wrote:didn't work eitheroutside an unittest, this compiles fine: struct Atry making it `static struct` insteadSo it's a CTFE limitation in unittest/local functions(I also tried to declare that struct inside a function, but did not work either). right? assuming it's an error, it's going to be fixed anytime soon?My suspicion is it is trying to access the context of the unittest as a hidden paramater to that constructor there... and in an enum, it can't, just stupid compiler didn't think to mention the hidden arg here. my guess.cannot implicitly convert expression "hehe" of type string to AWhy does inside a unittest it doesn't work? the constructor this(string) {} seems to get disabled.
Jun 07 2019