mode_has_nan gives MSVC warning C4702 (unreachable code)
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend, testing-qa
Hướng nghiên cứu
Cảnh báo đến từ mode_has_nan trong include/boost/math/statistics/detail/single_pass.hpp, nơi return true; nằm sau BOOST_MATH_THROW_EXCEPTION và không thể đạt tới khi dùng MSVC. Hãy đọc hàm đó, sau đó biên dịch một tệp nhỏ gọi boost::math::statistics::mode trên std::vector<double> bằng cl /W4 /WX (hoặc GCC với -Wall -Werror nếu không có MSVC). Hoàn thành khi bản build không có cảnh báo và các bài kiểm thử thống kê đơn biến vẫn đều đạt.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
mode_has_nan on develop (471bef4f) gives MSVC warning C4702 (unreachable code). Pull request #1607 (commit b8c1f01d) added this function. A build that sets warnings as errors stops at this warning. Because of this, the Boost.Decimal test test_boost_math_univariate_stats does not compile with MSVC 14.2, 14.3 and 14.5.
Cause
In mode_has_nan (include/boost/math/statistics/detail/single_pass.hpp:374), a return true; statement follows BOOST_MATH_THROW_EXCEPTION:
if (std::any_of(first, last, [](const Z& z) { return !(z == z); }))
{
BOOST_MATH_THROW_EXCEPTION(std::domain_error("The mode is undefined for data containing a NaN."))
return true;
}
return false;
In the usual (not standalone) configuration, BOOST_MATH_THROW_EXCEPTION calls boost::throw_exception. This function does not return. Thus MSVC shows warning C4702 for the return true; statement.
Reproduction
- Get Boost.Math
developat commit b8c1f01d or later. - Write a file
t.cppthat callsboost::math::statistics::modeon astd::vector<double>. - Compile the file with MSVC:
cl /W4 /WX /EHsc /std:c++17 t.cpp.
Output with MSVC 19.51.36260 for x64:
single_pass.hpp(375) : error C2220: the following warning is treated as an error
single_pass.hpp(375) : warning C4702: unreachable code
The Boost.Decimal CI shows the same error with MSVC 14.2, 14.3 and 14.5, for 32-bit and 64-bit.
Expected result
The file compiles without warnings.
Proposed fix
Keep the result of the search in a variable. Return the variable after the throw. Then no statement follows the throw:
const bool has_nan = std::any_of(first, last, [](const Z& z) { return !(z == z); });
if (has_nan)
{
BOOST_MATH_THROW_EXCEPTION(std::domain_error("The mode is undefined for data containing a NaN."))
}
return has_nan;
In standalone mode with exceptions off, BOOST_MATH_THROW_EXCEPTION expands to nothing. With the fix, the function continues to return true for data that contain a NaN.
We did these tests with the fix:
- MSVC compiles the Boost.Decimal test
test_boost_math_univariate_statswithout warnings, with C++14 and C++latest. The test passes. - The tests
test_univariate_statisticsandtest_univariate_statistics_backwards_compatiblepass with GCC 16. - In standalone mode with
-fno-exceptions,modegives the same results before and after the fix. It gives no mode for data that contain a NaN.
- Ngôn ngữ chính
- C++
- Star
- 378
- Fork
- 271
- Merge trung bình
- 12 giờ 31 phút
- Pull request đã merge (30 ngày)
- 70
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của boostorg/math
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 12/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Reverse-mode autodiff: order-2 tape slows order-1 gradients, rewound tapes keep allocating blocks, rvar copies silently share a nodeCó thể đã có người làm @NAThompson đã nhận 3 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Feature WishlistĐang mởenhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
boostorg/math#303 · 20 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của boostorg/math
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
mw00/project-maya#877 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
llvm/offload-test-suite#1703 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
test(tools): test_invite_answer_wiring scans nothing in a checkout whose path contains tests/Đang mởbacklog bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
EchoTools/nevr-runtime#2154 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
BGI-shenzhen/PopLDdecay#25 ·