Issue 138939
Summary Clang++ rejects try-catch in discarded if constexpr branch with -fno-exceptions
Labels clang
Assignees
Reporter zerhud
    **Summary**:
Clang incorrectly rejects code containing a `try-catch` block inside a discarded `if constexpr` branch when compiled with `-fno-exceptions`. The `try-catch` block should not be evaluated, as it is excluded at compile-time, but Clang emits an error: `cannot use 'try' with exceptions disabled`.

**Steps to Reproduce**:
1. Create a file `test.cpp` with the following code:
 ```cpp
   template<auto enable> void foo(auto&& fnc) {
       if constexpr(enable) try{ fnc(); } catch(...){}
       else fnc();
   }

 int main(int, char**) {
       foo<false>([]{});
   }
   ```
2. Compile with:
   ```bash
   clang++ -std=c++20 -fno-exceptions -c test.cpp
 ```
3. Alternatively, see the issue on [Godbolt](https://godbolt.org/z/3ecnde5z4): https://godbolt.org/z/3ecnde5z4

**Actual Results**:
```
test.cpp:2:25: error: cannot use 'try' with exceptions disabled
    if constexpr(enable) try{ fnc(); } catch(...){}
                         ^
1 error generated.
```

**Expected Results**:
The code should compile successfully, as the `try-catch` block is in a discarded `if constexpr` branch and should not be evaluated.

**Additional Information**:
- The code compiles successfully with GCC
- A not called template function with try-catch block rejected too ([Godbolt](https://godbolt.org/z/4nb3vcYTo))
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to