Trying to debug a static_assert(sizeof(foo) == ...) failure can be rather
awkward since there is no easy way to print the value of the sizeof() at
compile-time without another compiler diagnostic involving an array size.
This patch emits additional notes when a static_assert fails for any
UnaryExprOrTypeTraitExpr expression inside the static_assert.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang/lib/Sema/SemaTemplate.cpp | ||
---|---|---|
3655 | ||
clang/test/SemaCXX/static-assert.cpp | ||
233 | Can you also add a test where the unary expression isn't the reason for the failure? e.g., static_assert(is_same_v<float, decltype(sizeof(int))>); Perhaps we'd want to silence the notes if the expression context for the call to sizeof is itself unevaluated? |
This is less useful now that 09117b21890c652994f7ada0229d309b35b44259 / D130894 has landed, but it might still be worth including (e.g. for expressions that include multiple sizeof() values?
From looking at the output in the test cases, the additional diagnostics seem unnecessary to me in almost all cases...?
+1; my observation is that the extra note repeats information because the new note says what the LHS and the RHS of the expression evaluates to. I could see this new note being more useful in a case like: static_assert(sizeof(foo) + sizeof(bar) == sizeof(baz) / 12); where there are multiple sizeof expressions involved and we could note what each constituent part evaluates to, but I'm not certain it's worth the extra complexity to go to those lengths (I'd want to see real world code where this would really help).
Would require significant work to still be useful now that we print the value of expressions (e.g. only print for more complex expressions).