Skip to content

Commit bcb155b

Browse files
committed
Add test cases for Bugzilla Issue 19408
1 parent ae00eb3 commit bcb155b

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/issue19408.d(43): Error: function `issue19408.Infidel!(S).Infidel.get` is not `nothrow`
5+
fail_compilation/issue19408.d(13): which calls `this(this) { if (this.x > 0) throw new Exception19408; else this.x = 1; }`
6+
fail_compilation/issue19408.d(42): Error: delegate `issue19408.failCompile.__lambda_L42_C18` may throw but is marked as `nothrow`
7+
---
8+
*/
9+
10+
struct Infidel(T)
11+
{
12+
T value;
13+
T get() { return value; } // returns a copy
14+
}
15+
16+
class Exception19408 : Exception
17+
{
18+
this() { super("fail"); }
19+
}
20+
21+
void assertThrown(void delegate() nothrow dg)
22+
{
23+
dg();
24+
}
25+
26+
void failCompile()
27+
{
28+
static struct S
29+
{
30+
int x;
31+
this(this)
32+
{
33+
if (x > 0) throw new Exception19408();
34+
else x = 1;
35+
}
36+
}
37+
38+
S s;
39+
auto sneak = Infidel!S(s);
40+
41+
// shouldn't compile
42+
assertThrown(() nothrow {
43+
auto x = sneak.get();
44+
});
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Infidel(T)
2+
{
3+
T value;
4+
T get() { return value; } // returns a copy
5+
}
6+
7+
void main()
8+
{
9+
static struct S
10+
{
11+
int x;
12+
this(this)
13+
{
14+
if (x > 0) throw new Exception("fail");
15+
else x = 1;
16+
}
17+
}
18+
19+
// passes, cannot nothrow-copy
20+
static assert(!is(typeof(() nothrow { S* x; union U { S x; } U u = U(*x); })));
21+
22+
// passes, copy may throw
23+
static assert(is(typeof(() { S* x; union U { S x; } U u = U(*x); })));
24+
25+
S s;
26+
auto sneak = Infidel!S(s); // won't throw here, s.x was 0
27+
28+
// should pass
29+
static assert(!is(typeof(() nothrow { auto x = sneak.get(); })));
30+
}

0 commit comments

Comments
 (0)