Commit 50ba4a8
committed
c++: Diagnose invalid type of bitfield widths in templates [PR125674]
As the first testcase shows, outside of templates or when
the bitfield width is not type dependent, we diagnose it
in grokbitfield:
if (width != error_mark_node)
{
/* The width must be an integer type. */
if (!type_dependent_expression_p (width)
&& !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width)))
error ("width of bit-field %qD has non-integral type %qT", value,
TREE_TYPE (width));
else if (!check_for_bare_parameter_packs (width))
{
/* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
check_bitfield_decl picks it from there later and sets DECL_SIZE
accordingly. */
DECL_BIT_FIELD_REPRESENTATIVE (value) = width;
SET_DECL_C_BIT_FIELD (value);
}
}
Later on in check_bitfield_decl we verify it is a constant expression,
folded into INTEGER_CST, non-negative etc.
But during instantiation, we don't repeat that check, so only call
check_bitfield_decl later on which can sometimes emit different diagnostics
(so e.g.
bit-field ‘D<1.0e+0>::d’ width not an integer constant
instead of
width of bit-field ‘D<N>::d’ has non-integral type ‘double’
) but what the second testcase shows, we can ICE during cxx_constant_value
even before that if the type is even more problematic.
The following patch fixes that by repeating the test from grokbitfield
during tsubst_decl.
2026-06-12 Jakub Jelinek <jakub@redhat.com>
PR c++/125674
* pt.cc (tsubst_decl): Diagnose bit-field widths
with invalid type.
* g++.dg/template/bitfield5.C: New test.
* g++.dg/template/bitfield6.C: New test.
Reviewed-by: Jason Merrill <jason@redhat.com>1 parent fad08bd commit 50ba4a8
3 files changed
Lines changed: 45 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16058 | 16058 | | |
16059 | 16059 | | |
16060 | 16060 | | |
16061 | | - | |
16062 | | - | |
16063 | | - | |
16064 | | - | |
16065 | | - | |
| 16061 | + | |
| 16062 | + | |
| 16063 | + | |
| 16064 | + | |
| 16065 | + | |
| 16066 | + | |
| 16067 | + | |
| 16068 | + | |
| 16069 | + | |
| 16070 | + | |
| 16071 | + | |
| 16072 | + | |
| 16073 | + | |
| 16074 | + | |
| 16075 | + | |
| 16076 | + | |
| 16077 | + | |
| 16078 | + | |
| 16079 | + | |
| 16080 | + | |
16066 | 16081 | | |
16067 | 16082 | | |
16068 | 16083 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
0 commit comments