Fix Issue 21415 - catch immutable exceptions breaks immutable (#14707)

* Fix Issue 21415 - catch immutable exceptions breaks immutable

* Disallow catching inout and shared too

* Workaround catch(shared) in eh2.d
This commit is contained in:
Nick Treleaven 2023-05-02 12:50:55 +01:00 committed by GitHub
parent 6afed16ccc
commit 0659c9b577
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View file

@ -4045,6 +4045,13 @@ void catchSemantic(Catch c, Scope* sc)
// reference .object.Throwable
c.type = getThrowable();
}
else if (!c.type.isNaked() && !c.type.isConst())
{
// @@@DEPRECATED_2.113@@@
// Deprecated in 2.103, change into an error & uncomment in 2.113
deprecation(c.loc, "can only catch mutable or const qualified types, not `%s`", c.type.toChars());
//c.errors = true;
}
c.type = c.type.typeSemantic(c.loc, sc);
if (c.type == Type.terror)
{

View file

@ -108,3 +108,17 @@ void test2456b()
catch (Throwable) {} // NG
}
}
/*
TEST_OUTPUT:
---
fail_compilation/fail2456.d(121): Deprecation: can only catch mutable or const qualified types, not `immutable(Exception)`
---
*/
void main() {
try {
throw new Exception("");
} catch (immutable Exception e) {
assert(0);
}
}

View file

@ -72,11 +72,11 @@ int main()
a.test();
Abc.x |= 0x40;
}
catch (shared(Abc) b)
catch (Abc b)
{
Abc.x |= 0x80;
printf("Caught %p, x = x%x\n", b, Abc.x);
assert(a is b);
assert(cast() a is b);
assert(Abc.x == 0xB5);
}
printf("Success!\n");