Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 202c2d1

Browse files
authored
Merge pull request #2361 from n8sh/issue-19416
Make core.exception.onOutOfMemoryError work in betterC merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents ba3295d + 459dda6 commit 202c2d1

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/core/exception.d

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@
99
*/
1010
module core.exception;
1111

12+
// Compiler lowers final switch default case to this (which is a runtime error)
13+
void __switch_errorT()(string file = __FILE__, size_t line = __LINE__) @trusted
14+
{
15+
// Consider making this a compile time check.
16+
version (D_Exceptions)
17+
throw staticError!SwitchError(file, line, null);
18+
else
19+
assert(0, "No appropriate switch clause found");
20+
}
21+
22+
version (D_BetterC)
23+
{
24+
// When compiling with -betterC we use template functions so if they are
25+
// used the bodies are copied into the user's program so there is no need
26+
// for the D runtime during linking.
27+
28+
// In the future we might want to convert all functions in this module to
29+
// templates even for ordinary builds instead of providing them as an
30+
// extern(C) library.
31+
32+
void onOutOfMemoryError()(void* pretend_sideffect = null) @nogc nothrow pure @trusted
33+
{
34+
assert(0, "Memory allocation failed");
35+
}
36+
alias onOutOfMemoryErrorNoGC = onOutOfMemoryError;
37+
38+
void onInvalidMemoryOperationError()(void* pretend_sideffect = null) @nogc nothrow pure @trusted
39+
{
40+
assert(0, "Invalid memory operation");
41+
}
42+
}
43+
else:
44+
1245
/**
1346
* Thrown on a range error.
1447
*/
@@ -580,16 +613,6 @@ extern (C) void onSwitchError( string file = __FILE__, size_t line = __LINE__ )
580613
assert(0, "No appropriate switch clause found");
581614
}
582615

583-
// Compiler lowers final switch default case to this (which is a runtime error)
584-
void __switch_errorT()(string file = __FILE__, size_t line = __LINE__) @trusted
585-
{
586-
// Consider making this a compile time check.
587-
version (D_Exceptions)
588-
throw staticError!SwitchError(file, line, null);
589-
else
590-
assert(0, "No appropriate switch clause found");
591-
}
592-
593616
/**
594617
* A callback for unicode errors in D. A $(LREF UnicodeException) will be thrown.
595618
*

test/betterc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include ../common.mak
22

3-
TESTS:=test18828
3+
TESTS:=test18828 test19416
44

55
.PHONY: all clean
66
all: $(addprefix $(ROOT)/,$(addsuffix ,$(TESTS)))

test/betterc/src/test19416.d

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*******************************************/
2+
// https://issues.dlang.org/show_bug.cgi?id=19416
3+
4+
import core.stdc.stdlib : malloc, free;
5+
import core.exception : onOutOfMemoryError;
6+
7+
extern(C) void main()
8+
{
9+
auto m = malloc(1);
10+
if (!m)
11+
onOutOfMemoryError();
12+
else
13+
free(m);
14+
}

0 commit comments

Comments
 (0)