Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::uncaught_exception error in vendored catch.h with clang++-20 and C++20 standard #2047

Open
tylermorganwall opened this issue Jan 9, 2025 · 2 comments

Comments

@tylermorganwall
Copy link

Description

The vendored catch.h header in the testthat package references std::uncaught_exception, which has been deprecated in C++17 and removed in C++20. When compiling with clang++-20 and the -std=gnu++20 flag, this leads to a compilation error:

clang++-20 -stdlib=libc++ -std=gnu++20 -I"/opt/R/devel/lib/R/include" -DNDEBUG  -I'/github/home/R/x86_64-pc-linux-gnu-library/4.5/Rcpp/include' -I'/github/home/R/x86_64-pc-linux-gnu-library/4.5/RcppThread/include' -I'/github/home/R/x86_64-pc-linux-gnu-library/4.5/progress/include' -I'/github/home/R/x86_64-pc-linux-gnu-library/4.5/spacefillr/include' -I'/github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include' -I/usr/local/include   -D RAY_REPRODUCE_PERLIN -DSTRICT_R_HEADERS -DRAY_HAS_X11 -DHAS_SSE -DHAS_SSE2 -DRAYSIMD -DRAYSIMDVECOFF -fpic  -O3 -Wall -pedantic -frtti -Wp,-D_FORTIFY_SOURCE=3 -Wall -pedantic  -c test-runner.cpp -o test-runner.o
  In file included from test-runner.cpp:7:
  In file included from /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat.h:1:
  In file included from /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat/testthat.h:72:
  /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat/vendor/catch.h:8382:20: error: no member named 'uncaught_exception' in namespace 'std'; did you mean 'uncaught_exceptions'?
   8382 |         if ( !std::uncaught_exception() ){
        |               ~~~~~^
  /usr/lib/llvm-20/bin/../include/c++/v1/__exception/operations.h:35:31: note: 'uncaught_exceptions' declared here
     35 | _LIBCPP_EXPORTED_FROM_ABI int uncaught_exceptions() _NOEXCEPT;
        |                               ^
  In file included from test-runner.cpp:7:
  In file included from /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat.h:1:
  In file included from /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat/testthat.h:72:
  /github/home/R/x86_64-pc-linux-gnu-library/4.5/testthat/include/testthat/vendor/catch.h:8705:22: error: no member named 'uncaught_exception' in namespace 'std'; did you mean 'uncaught_exceptions'?
   8705 |             if( std::uncaught_exception() )
        |                 ~~~~~^
  /usr/lib/llvm-20/bin/../include/c++/v1/__exception/operations.h:35:31: note: 'uncaught_exceptions' declared here
     35 | _LIBCPP_EXPORTED_FROM_ABI int uncaught_exceptions() _NOEXCEPT;
        |                               ^
  2 errors generated.
  make: *** [/opt/R/devel/lib/R/etc/Makeconf:204: test-runner.o] Error 1
  ERROR: compilation failed for package ‘rayrender’
  * removing ‘/tmp/Rtmp5ZqS0O/pkg-lib2ec7db28e4f/rayrender’

This issue arises in two locations in catch.h:

  • Line 8382: if ( !std::uncaught_exception() ){
  • Line 8705: if( std::uncaught_exception() )

This error was detected by compiling the rayrender package with the rhub clang20 runner.

https://github.com/tylermorganwall/rayrender/actions/runs/12653086666/job/35257420480

Suggested Fix

Replace the calls to std::uncaught_exception() with std::uncaught_exceptions() > 0, which is the recommended replacement in C++17 and later.

Here’s the updated code:

// ScopedMessage::~ScopedMessage
if ( std::uncaught_exceptions() == 0 ){
    getResultCapture().popScopedMessage(m_info);
}

and

// Section::~Section
if( std::uncaught_exceptions() > 0 )
    getResultCapture().sectionEndedEarly( endInfo );
else
    getResultCapture().sectionEnded( endInfo );
@tylermorganwall
Copy link
Author

I have just received a CRAN notice that my package will be taken down unless this issue is fixed by 2025-03-05.

https://www.r-project.org/nosvn/R.check/r-devel-linux-x86_64-fedora-clang/rayrender-00install.html

@tylermorganwall
Copy link
Author

Update for anyone else facing this issue:

I have updated my package to temporarily remove compiled tests by wrapping all calls to Catch2 functions and <testthat.h> (which includes the deprecated C++20 function) in a preprocessor guard.

#ifdef NOT_CRAN
#include <testthat.h>
#include <Rcpp.h>

context("...") {
  ...
}
#endif

However, because testthat includes the following dummy function to ensure the symbol is registered:

# This dummy function definition is included with the package to ensure that
# 'tools::package_native_routine_registration_skeleton()' generates the required
# registration info for the 'run_testthat_tests' symbol.
(function() {
  .Call("run_testthat_tests", FALSE, PACKAGE = "rayrender")
})

I had failures due to it trying to I needed to resolve the run_testthat_tests symbol, which it couldn't because I had to add a guard to test-runner.cpp to prevent it from including <testthat.h> (which contains the symbol R was looking for). So I had to also include a dummy function so that that symbol would resolve as well.

/*
 * Please do not edit this file -- it ensures that your package will export a
 * 'run_testthat_tests()' C routine that can be used to run the Catch unit tests
 * available in your package.
 */
#ifdef NOT_CRAN
#define TESTTHAT_TEST_RUNNER
#include <testthat.h>
#else 

#include <R.h>
#include <Rinternals.h>
extern "C" SEXP run_testthat_tests(SEXP use_xml_sxp) {
  return Rf_ScalarLogical(true);
}
#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant