[PATCH] D130058: [Clang] Diagnose ill-formed constant expression when setting a non fixed enum to a value outside the range of the enumeration values

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 5 05:15:32 PDT 2022


hokein added a comment.

+1 on downgrading to a warning, so that users can have time to do cleanup on their codebase.

We also see internal breakages caused by this change. It breaks some code using the magic_enum <https://github.com/Neargye/magic_enum> library, a simple repro example:

  #include <iostream>                                                             
  #include "magic_enum.hpp"                                
                                                                                  
  enum Color { Red, Black };                                                      
  int main(int argc, char* argv[]) {                                              
     Color color = Black;                                                          
     switch (color) {                                                              
       case Red:                                                                   
         break;                                                                    
       default:                                                                    
         std::cout << magic_enum::enum_name(color);                                
         break;                                                                    
     }                                                                                                                                                             
     return 0;                                                                     
   }

Errors:

  ./magic_enum/magic_enum.hpp:436:10: note: in instantiation of function template specialization 'magic_enum::detail::values<Color, false, 0, 0UL, 1UL, 2UL, 3UL, 4UL, 5UL, 6UL, 7UL, 8UL, 9UL, 10UL, 11UL, 12UL, 13UL, 14UL, 15UL, 16UL, 17UL, 18UL, 19UL, 20UL, 21UL, 22UL, 23UL, 24UL, 25UL, 26UL, 27UL, 28UL, 29UL, 30UL, 31UL, 32UL, 33UL, 34UL, 35UL, 36UL, 37UL, 38UL, 39UL, 40UL, 41UL, 42UL, 43UL, 44UL, 45UL, 46UL, 47UL, 48UL, 49UL, 50UL, 51UL, 52UL, 53UL, 54UL, 55UL, 56UL, 57UL, 58UL, 59UL, 60UL, 61UL, 62UL, 63UL, 64UL, 65UL, 66UL, 67UL, 68UL, 69UL, 70UL, 71UL, 72UL, 73UL, 74UL, 75UL, 76UL, 77UL, 78UL, 79UL, 80UL, 81UL, 82UL, 83UL, 84UL, 85UL, 86UL, 87UL, 88UL, 89UL, 90UL, 91UL, 92UL, 93UL, 94UL, 95UL, 96UL, 97UL, 98UL, 99UL, 100UL, 101UL, 102UL, 103UL, 104UL, 105UL, 106UL, 107UL, 108UL, 109UL, 110UL, 111UL, 112UL, 113UL, 114UL, 115UL, 116UL, 117UL, 118UL, 119UL, 120UL, 121UL, 122UL, 123UL, 124UL, 125UL, 126UL, 127UL, 128UL>' requested here
    return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{});
           ^
  ./magic_enum/magic_enum.hpp:440:34: note: in instantiation of function template specialization 'magic_enum::detail::values<Color, false, unsigned int>' requested here
  inline constexpr auto values_v = values<E, IsFlags>();
                                   ^
  ./magic_enum/magic_enum.hpp:446:33: note: in instantiation of variable template specialization 'magic_enum::detail::values_v' requested here
  inline constexpr auto count_v = values_v<E, IsFlags>.size();
                                  ^
  ./magic_enum/magic_enum.hpp:567:17: note: in instantiation of variable template specialization 'magic_enum::detail::count_v' requested here
    static_assert(count_v<D, false> > 0, "magic_enum requires enum implementation and valid max and min.");
                  ^
  ./magic_enum/magic_enum.hpp:579:1: note: in instantiation of template class 'magic_enum::detail::enable_if_enum<true, false, Color, std::string_view>' requested here
  using enable_if_enum_t = typename enable_if_enum<std::is_enum_v<std::decay_t<T>>, false, T, R>::type;
  ^
  ./magic_enum/magic_enum.hpp:690:69: note: in instantiation of template type alias 'enable_if_enum_t' requested here
  [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t<E, string_view> {
                                                                      ^
  main.cc:17:20: note: while substituting deduced template arguments into function template 'enum_name' [with E = Color]
        std::cout << magic_enum::enum_name(color);


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130058/new/

https://reviews.llvm.org/D130058



More information about the cfe-commits mailing list