https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129203

            Bug ID: 129203
           Summary: Poor diagnostics when failing a requires-expression in
                    a static_assert
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

If I have a concept using a requires expression, and I assert that concept
against a type that fails to meet on of its requirements:

template <class T>
concept Stuff = requires (T t) {
    t.f();
    t.g(0);
};

struct Bad {
    void f();
};

static_assert(Stuff<Bad>);

I get a detailed error message showing exactly which requirement Bad failed to
meet (this is gcc 16.2 with -std=c++26, no other flags):

<source>:22:15: error: static assertion failed
   22 | static_assert(Stuff<Bad>);
      |               ^~~~~~~~~~
  • constraints not satisfied
    • required by the constraints of 'template<class T> concept Stuff'
      <source>:13:9:   
         13 | concept Stuff = requires (T t) {
            |         ^~~~~
    • in requirements with 'T t' [with T = Bad]
      <source>:13:17:   
         13 | concept Stuff = requires (T t) {
            |                 ^~~~~~~~~~~~~~~~
         14 |     t.f();
            |     ~~~~~~       
         15 |     t.g(0);
            |     ~~~~~~~      
         16 | };
            | ~                
    • the required expression 't.g(0)' is invalid
      <source>:15:8:
         15 |     t.g(0);
            |     ~~~^~~
    • set '-fconcepts-diagnostics-depth=' to at least 2 for more detail

However, if, instead, I simply am static_assert-ing that same requires
expression (which I may need to do, for instance, if access matters):

struct Check {
    template <class T>
    static constexpr bool check() {
        static_assert(requires (T t){
            t.f();
            t.g(0);
        });
        return true;
    }
};

static_assert(Check::check<Bad>());

Then the error doesn't tell me which requirement failed
(https://compiler-explorer.com/z/c9scY5W36):

<source>: In instantiation of 'static constexpr bool Check::check() [with T =
Bad]':
<source>:24:32:   required from here
   24 | static_assert(Check::check<Bad>());
      |               ~~~~~~~~~~~~~~~~~^~
<source>:4:23: error: static assertion failed
    4 |         static_assert(requires (T t){
      |                       ^~~~~~~~~~~~~~~
    5 |             t.f();
      |             ~~~~~~     
    6 |             t.g(0);
      |             ~~~~~~~    
    7 |         });
      |         ~              
  • 'false' evaluates to false

As a result, what I have to do is break up every requirement into its own
static assertion. That's not the best from the perspective of having to repeat
the parameters, but even then — for near matches, we still don't get
information about why the requirement wasn't met (e.g. if `Bad::g` existed but
took two ints, or a string, or something). 

Since we don't have member concepts yet, it'd be pretty nice if we were still
able to get good diagnostics in this case without having to resort to
workarounds.

Especially since in this case, if I had a type requirement, it'd be nice to
write

static_assert(requires {
  typename T::thing;
  requires something<typename T::thing>;
  // ...
});

Which already naturally short-circuits. But if I have to split it up, I have to
do it this way:

static_assert(requires { typename T::thing; });
if constexpr (requires { typename T::thing; }) {
  static_assert(something<typename T::thing>);
  // ...
}

Because otherwise if T::thing isn't there, I'll get that error, but then also a
wall of other errors for every other mis-use of T::thing.
  • [Bug c++/129203] New: Poor diag... barry.revzin at gmail dot com via Gcc-bugs

Reply via email to