[clang] [clang][Sema] Diagnose exceptions only in non-dependent context in discarded `try/catch/throw` blocks (PR #139859)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed May 14 06:02:50 PDT 2025


erichkeane wrote:

> @Rajveer100 I think instead of looking at the context, we should check if the expression (`Ex`) is instantiation dependent

I don't think we can just check the expression, the 'throw' itself could be completely fine.  Consider:

```
template<typename T> void foo() {
  throw 1;
```
The expression is NOT dependent, but we don't wanna diagnose anyway unless it is instantiated.


> @erichkeane As expected the following case is being accepted, which shouldn't happen, let me know what you recommend to diagnose this:
> 
> ```c++
> template <typename T> void foo() {
>   try {
>   } catch (...) {
>   }
>   throw 1;
> }
> void bar2() { foo<int>(); }
> ```

What we will have to do likely is move the checking for this to some piece of common code that is used by TreeTransform for these two.  I haven't looked at a good place, but you'll probably see `TransformCXXThrowExpr` (and an equiv for `try`) that will call a rebuild/etc type function, which should then call the `build` (or some variety of a check function).

Also note, even in THOSE cases we have to properly check the decl context, since it could be a partial specialization/instantiation, so please make sure those are tested as well!

https://github.com/llvm/llvm-project/pull/139859


More information about the cfe-commits mailing list