Bug 21415 – catch immutable exceptions breaks immutable

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-11-22T18:46:13Z
Last change time
2023-05-02T11:51:02Z
Keywords
pull
Assigned to
No Owner
Creator
Adam D. Ruppe
See also
https://issues.dlang.org/show_bug.cgi?id=12118

Comments

Comment #0 by destructionator — 2020-11-22T18:46:13Z
--- immutable foo = new Exception("omg"); void main() { import std.stdio; try { //throw foo; throw new Exception("wtf"); } catch(immutable Exception e) { //e.msg = "broken"; writeln("caught ", e); } } --- In that example, it silently casts the mutable Exception to immutable. If you throw foo and catch Exception, it silently casts the immutable Exception to mutable. So the mutability stuff is lost here. (I was exploring throwing immutable static exceptions as a possibility for nogc)
Comment #1 by destructionator — 2020-11-22T20:25:53Z
this might be a druntime bug instead of dmd. but i suspect it is both due to too many void* in the interface layer.
Comment #2 by kozzi11 — 2020-11-23T17:42:07Z
I would say this one is even worse: immutable foo = new Exception("omg"); void main() { import std.stdio; try { throw foo; } catch(Exception e) { e.msg = "broken"; writeln("caught ", e); } }
Comment #3 by destructionator — 2020-11-23T17:48:20Z
Yeah, the immutability being lost destroyed what I was hoping to do with this, so definitely the bigger problem. But it being lost either direction is problematic. Really a "catch-all" would be `catch(const Throwable)`. Const and immutable objects should NOT be caught by a catch mutable. I realize nobody thinks about this but we probably should start. An immutable exception can be used without allocation concerns.
Comment #4 by dlang-bot — 2022-12-16T17:08:18Z
@ntrel created dlang/dmd pull request #14707 "Fix Issue 21415 - catch immutable exceptions breaks immutable" fixing this issue: - Fix Issue 21415 - catch immutable exceptions breaks immutable https://github.com/dlang/dmd/pull/14707
Comment #5 by dlang-bot — 2023-05-02T11:51:02Z
dlang/dmd pull request #14707 "Fix Issue 21415 - catch immutable exceptions breaks immutable" was merged into master: - c3920edb59ec4bd8dbac63169bee1d37529e70d9 by Nick Treleaven: Fix Issue 21415 - catch immutable exceptions breaks immutable https://github.com/dlang/dmd/pull/14707