www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11051] New: Unmatched case in a final switch should throw in both release and non-release mode

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051

           Summary: Unmatched case in a final switch should throw in both
                    release and non-release mode
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



07:14:09 PDT ---
-----
module test;

import std.stdio;

enum E { a }

string get()
{
    auto e = cast(E)123;
    final switch(e)
    {
        case E.a:
            return "foobar";
    }
}

void main()
{
    writeln(get());
}
-----

$ dmd -run test.d
 core.exception.SwitchError test(10): No appropriate switch clause found
$ dmd -release -run test.d
 x ↑
The release switch ends up making get() return garbage. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Letting it throw an error in non-release mode kills some of optimization you
are supposed to have using a final switch, so I am not sure it's a good idea.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051




07:34:50 PDT ---

 Letting it throw an error in non-release mode kills some of optimization you
 are supposed to have using a final switch, so I am not sure it's a good idea.
You mean *in release mode*, right? It currently throws in non-release mode, it doesn't throw in release mode. But what is the above code supposed to do if not throw in both cases? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051





 You mean *in release mode*, right?
Right, sorry.
 It currently throws in non-release mode, it doesn't throw in release mode.
This seems good to me. If you want to convert an untrusted value to an enum safely you should use std.conv.to: import std.stdio, std.conv; enum E { a } string get() { //immutable e = cast(E)123; immutable e = to!E(123); final switch(e) { case E.a: return "foobar"; } } void main() { get.writeln; } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051




07:56:31 PDT ---

 This seems good to me.
No, returning garbage is not good. At the very least I expect a HALT instruction after the final switch for code which should not reach that point. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru



---


 You mean *in release mode*, right?
Right, sorry.
 It currently throws in non-release mode, it doesn't throw in release mode.
This seems good to me. If you want to convert an untrusted value to an enum safely you should use std.conv.to: import std.stdio, std.conv; enum E { a } string get() { //immutable e = cast(E)123; immutable e = to!E(123); final switch(e) { case E.a: return "foobar"; } } void main() { get.writeln; }
No, you will run into memory errors. Either dmd should still throw SwitchError or insert halt instruction. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11051





 Letting it throw an error in non-release mode kills some of optimization you
 are supposed to have using a final switch, so I am not sure it's a good idea.
But an error isn't covered by the nothrow contract, so you wouldn't be able to optimize that anyways. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 16 2013