[Deprecation -> Error] nothrow function contracts that throw (#16801)

This commit is contained in:
Nicholas Wilson 2024-08-26 16:36:18 +08:00 committed by GitHub
parent a271bd1847
commit 0834c750aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 11 deletions

View file

@ -0,0 +1,17 @@
An error is now issued for `in`/`out` contracts of `nothrow` functions that may throw
This used to issue a deprecation, it is now an error:
```
void test() nothrow
in
{
throw new Exception(null); // Error: `in` contract may throw but function is marked as `nothrow`
}
out
{
throw new Exception(null); // Error: `out` contract may throw but function is marked as `nothrow`
}
do
{
}
```

View file

@ -1009,14 +1009,11 @@ private extern(C++) final class Semantic3Visitor : Visitor
// BUG: verify that all in and ref parameters are read
freq = freq.statementSemantic(sc2);
// @@@DEPRECATED_2.111@@@ - pass `isnothrow` instead of `false` to print a more detailed error msg`
const blockExit = freq.blockExit(funcdecl, null);
if (blockExit & BE.throw_)
{
if (isnothrow)
// @@@DEPRECATED_2.111@@@
// Deprecated in 2.101, can be made an error in 2.111
deprecation(funcdecl.loc, "`%s`: `in` contract may throw but function is marked as `nothrow`",
error(funcdecl.loc, "`%s`: `in` contract may throw but function is marked as `nothrow`",
funcdecl.toPrettyChars());
else if (funcdecl.nothrowInprocess)
f.isnothrow = false;
@ -1056,14 +1053,11 @@ private extern(C++) final class Semantic3Visitor : Visitor
fens = fens.statementSemantic(sc2);
// @@@DEPRECATED_2.111@@@ - pass `isnothrow` instead of `false` to print a more detailed error msg`
const blockExit = fens.blockExit(funcdecl, null);
if (blockExit & BE.throw_)
{
if (isnothrow)
// @@@DEPRECATED_2.111@@@
// Deprecated in 2.101, can be made an error in 2.111
deprecation(funcdecl.loc, "`%s`: `out` contract may throw but function is marked as `nothrow`",
error(funcdecl.loc, "`%s`: `out` contract may throw but function is marked as `nothrow`",
funcdecl.toPrettyChars());
else if (funcdecl.nothrowInprocess)
f.isnothrow = false;

View file

@ -1,9 +1,8 @@
// REQUIRED_ARGS: -de
/*
TEST_OUTPUT:
---
fail_compilation/fail13123.d(10): Deprecation: `fail13123.test`: `in` contract may throw but function is marked as `nothrow`
fail_compilation/fail13123.d(10): Deprecation: `fail13123.test`: `out` contract may throw but function is marked as `nothrow`
fail_compilation/fail13123.d(9): Error: `fail13123.test`: `in` contract may throw but function is marked as `nothrow`
fail_compilation/fail13123.d(9): Error: `fail13123.test`: `out` contract may throw but function is marked as `nothrow`
---
*/