Skip to content

boost::mpl::integral_c broken in Clang trunk #69

Closed

Description

@jyknight

Clang has recently addressed an issue where it failed to diagnose the UB of casting an out-of-range integer to an enum in a constant expression.

Since undefined behavior is not allowed in a constant expression, Clang now prohibits enum test { zero }; constexpr int g = static_cast<test>(-1); with "error: constexpr variable 'g' must be initialized by a constant expression; note: integer value -1 is outside the valid range of values [0, 1] for this enumeration type"

This change breaks boost::mpl::integral_c, since it does exactly that. Demonstrated with example, below (and on godbolt)

#include <boost/mpl/integral_c.hpp>
enum test { zero, one };
boost::mpl::integral_c<test, zero> m;

Originally discovered with a program that was using the boost::numeric library, which in turn uses mpl like the above example.

#include <boost/numeric/conversion/udt_builtin_mixture.hpp>
boost::numeric::udt_builtin_mixture<int, int> m;

Activity

  1. dabrahams commented on Aug 9, 2022

    @dabrahams
    Contributor

    The fix looks like this.

  2. added a commit that references this issue on Aug 9, 2022
    453e0cf
  3. dabrahams commented on Aug 10, 2022

    @dabrahams
    Contributor

    Actually I'm back to believing there's no true fix if next and prior have to be of type integral_c<T, ...>.

  4. jyknight commented on Aug 16, 2022

    @jyknight
    Author

    Perhaps the correct fix is to somehow cause the next/prior members to be omitted when they're impossible to represent, just like they're omitted for bool? I expect that's possible somehow via some template specialization magic.

    Note that Clang has been changed since a week ago to no longer have this be an unconditional error. Now it's a controllable warning -Wenum-constexpr-conversion, which by default is enabled as an error. However, it's suppressed in system headers, and can be downgraded/disabled in user code as well. So, to see the error in the godbolt example you'll now need to add -Wsystem-headers to the compiler options. (e.g. https://godbolt.org/z/v4PPjW1n1)

    Since most code using boost will be using it as a system header, they'll also not see the error, and anyone else may downgrade the error to a warning or disable it entirely if desired. However, it may become an unconditional error again in some future release of Clang, so it'd still be good to address it.

  5. dabrahams commented on Aug 16, 2022

    @dabrahams
    Contributor

    Perhaps the correct fix is to somehow cause the next/prior members to be omitted when they're impossible to represent, just like they're omitted for bool? I expect that's possible somehow via some template specialization magic.

    Well, that's what I investigated, and my (ill-informed) conclusion is that it's not possible. The problem is that the the only expressions that try to create the representation (static_cast<test>(-1)) are not constant expressions, and they're well-typed. I hope somebody proves me wrong, but I'm certainly out of ideas.

  6. shafik commented on Aug 23, 2022

    @shafik

    If the intent is that the enum should be able to handle values outside the enumerator values then perhaps making it have a fixed underlying type better represents the intent?

  7. dabrahams commented on Aug 23, 2022

    @dabrahams
    Contributor

    I don't think that's the intent, but it is a decent workaround for the problem. I don't believe the intent is actually realizable, but like I said, I no longer really know C++, so I might be wrong.

  8. ecatmur commented on Nov 16, 2022

    @ecatmur
    Contributor

    the static const trick works: https://godbolt.org/z/KrhdzqYbx

    enum test { zero, one };
    template<class E, E a>
    E const k = static_cast<E>(a - 1);
    template<class T, T> struct S {};
    template<class> int f();
    int g() { return f<S<test, k<test, zero>>>(); }

    this means that going all the way back to the EDG implementation/workaround actually "fixes" it: https://godbolt.org/z/8bTerz3WM

    #define __EDG_VERSION__ 242
    #include <boost/mpl/integral_c.hpp>
    enum test { zero, one };
    boost::mpl::integral_c<test, zero> m;
  9. smeenai commented on Oct 16, 2023

    @smeenai

    Was there any progress here? Clang landed llvm/llvm-project#67528 which makes this an error by default even in system headers. We can turn off the warning for now, but that option will be removed in a future Clang release, so this will need to be addressed before then.

  10. carlosgalvezp commented on Nov 24, 2023

    @carlosgalvezp

    Hi! Any progress? What is the ETA for fixing? Next Clang release in about 6 months will turn the warning into a non-suppressable hard error.

  11. kencu commented on Dec 20, 2023

    @kencu

    we are running into this issue now on MacPorts. current boost builds are failing with the now-default compiler, clang-16.

    various patches are being suggested, versions of the ones listed in this PR, but if at all possible, we'd like to go with the one that will be finally integrated into boost.

  12. 14 remaining items

  13. piersh-aetheros commented on Jan 25, 2025

    @piersh-aetheros

    can this change be backported? many distros aren't shipping recent versions of boost yet. for example, RedHat 9 has Clang 18 and boost 1.75.0.

  14. Egorundel commented on Feb 26, 2025

    @Egorundel

    Hello, guys!

    Please, help me.

    I don't understand what the problem is and how to fix it. Attached screenshots of errors.

    Image
    Image
    Image

    I work in Qt Creator. It seems like I’m working with the GCC compiler, but for some reason I’m getting errors due to clang...

    Image

    Here is my version of Boost in the screenshot below (1.74.0):

    Image

    Ubuntu version: 22.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions