Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ unittest
*/
class SwitchError : Error
{
@safe pure nothrow this( string file = __FILE__, size_t line = __LINE__, Throwable next = null )
@safe pure nothrow @nogc this( string file = __FILE__, size_t line = __LINE__, Throwable next = null )
{
super( "No appropriate switch clause found", file, line, next );
}
Expand Down Expand Up @@ -579,6 +579,12 @@ extern (C) void onSwitchError( string file = __FILE__, size_t line = __LINE__ )
throw new SwitchError( file, line, null );
}

// Compiler lowers final switch default case to this (which is a runtime error)
void __switch_errorT()(string file = __FILE__, size_t line = __LINE__) @trusted
{
// Consider making this a compile time check.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have that compile time check for final switch and normal switch always requires a default branch.

I guess this runtime check is only used to verify that a value is not out-of-range, probably caused by an invalid cast. Ping @andralex

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I didn't know final switch also works on non-enumeral values.

throw staticError!SwitchError(file, line, null);
Copy link
Member

@PetarKirov PetarKirov Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind, that we may need to add a if (__ctfe) assert (0, "informative message...") branch, as I'm not sure if staticError would work in CTFE.
I prefer to leave the PR as it is for now, and cross the bridges when we come to them.

}

/**
* A callback for unicode errors in D. A $(LREF UnicodeException) will be thrown.
Expand Down
8 changes: 8 additions & 0 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -3889,6 +3889,14 @@ unittest
testSwitch!dchar;
}

// Compiler lowers final switch default case to this (which is a runtime error)
// Old implementation is in core/exception.d
void __switch_error()(string file = __FILE__, size_t line = __LINE__)
{
import core.exception : __switch_errorT;
__switch_errorT(file, line);
}

// Helper functions

private inout(TypeInfo) getElement(inout TypeInfo value) @trusted pure nothrow
Expand Down