Fix clang compilation error
The used structured bindings are only supported with C++20 and even then there is a bug in clang with some older version which crash during compilation. The fix doesn't capture the structured binding.
Merge request reports
Activity
changed milestone to %OCT23
requested review from @fweig
assigned to @fweig
huh, structured bindings are a c++ 17 feature: https://en.cppreference.com/w/cpp/language/structured_binding
We already use these in a number of places. Not sure why this one in particular is causing issues...
I'm kind of puzzled by this because structure bindings have been supported by clang since forever. Do you have the compiler version that crashed by any chance?
I don't have an issue to merge this for a one-time workaround. But a whole bunch of things in algo are designed to be used with structured bindings. So this issue could just keep popping up again and again.
when compiling the following code
#include <tuple> std::tuple<int,int> x() { return {1,2}; } void f() { auto [a, b] = x(); auto l = [&]() { return a+b; }; } int main() { f(); return 1; }
with clang 14 I get the following error
/opt/homebrew/Cellar/llvm@14/14.0.6/bin/clang++ -std=c++17 test.cxx test.cxx:10:16: error: reference to local binding 'a' declared in enclosing function 'f' return a+b; ^ test.cxx:8:11: note: 'a' declared here auto [a, b] = x(); ^ test.cxx:10:18: error: reference to local binding 'b' declared in enclosing function 'f' return a+b; ^ test.cxx:8:14: note: 'b' declared here auto [a, b] = x(); ^ 2 errors generated.
when compiling with clang 16 which supports the structured bindings I get the following warning
/opt/homebrew/Cellar/llvm@16/16.0.6/bin/clang++ -std=c++17 test.cxx test.cxx:10:16: warning: captured structured bindings are a C++20 extension [-Wc++20-extensions] return a+b; ^ test.cxx:8:11: note: 'a' declared here auto [a, b] = x(); ^ test.cxx:10:18: warning: captured structured bindings are a C++20 extension [-Wc++20-extensions] return a+b; ^ test.cxx:8:14: note: 'b' declared here auto [a, b] = x(); ^ 2 warnings generated.
I have no idea why clang and cppreference disagree and I also have no clue why the compilation crashes for me.
I am also fine if the code isn't changed and I patch my version only locally.
as I said I don't understand the issue completely. The original error message was
/tmp/cbmroot/algo/unpack/Unpack.cxx:198:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>' for (size_t i = 0; i < numMs; i++) { ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:35:9: note: in instantiation of function template specialization 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>' requested here ParallelMsLoop(Subsystem::STS, monitor, digiTs.fSts, monitor.fSts, *timeslice, fAlgoSts, 0x20); ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here auto [numMs, sizeBytes] = ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:216:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmStsDigi, cbm::algo::sts::Unpack, cbm::algo::sts::UnpackMonitorData>' for (size_t i = 0; i < numMs; i++) { ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here auto [numMs, sizeBytes] = ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:198:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>' for (size_t i = 0; i < numMs; i++) { ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:40:9: note: in instantiation of function template specialization 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>' requested here ParallelMsLoop(Subsystem::TOF, monitor, digiTs.fTof, monitor.fTof, *timeslice, fAlgoTof, 0x00); ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here auto [numMs, sizeBytes] = ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:216:28: error: reference to local binding 'numMs' declared in enclosing function 'cbm::algo::Unpack::ParallelMsLoop<CbmTofDigi, cbm::algo::tof::Unpack, cbm::algo::tof::UnpackMonitorData>' for (size_t i = 0; i < numMs; i++) { ^ /tmp/cbmroot/algo/unpack/Unpack.cxx:187:11: note: 'numMs' declared here auto [numMs, sizeBytes] = ^
numMs is already used before without an error
std::vector<std::vector<Digi>> msDigis(numMs); // unpacked digis per microslice std::vector<Monitor> monitor(numMs); // unpacking monitoring data per microslice
Whatever is different when using numMs in the loop.
The error is only raised when using numMs in the loop.
Bug already reported in Clang: https://github.com/llvm/llvm-project/issues/56406
Edited by Felix Weiglhoferthanks for your investigation.
added 5 commits
-
a9ffa895...3d61a7c2 - 4 commits from branch
computing:master
- 69e77dce - Fix clang compilation error
-
a9ffa895...3d61a7c2 - 4 commits from branch
enabled an automatic merge when the pipeline for 69e77dce succeeds
added CodeOwners label
mentioned in merge request !1494 (merged)