Bug 103081 - [ICE] with "using enum"
Summary: [ICE] with "using enum"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.1.0
: P3 normal
Target Milestone: 12.3
Assignee: Patrick Palka
URL:
Keywords: ice-on-valid-code
: 104398 105787 107460 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-11-04 14:30 UTC by M Welinder
Modified: 2022-12-19 16:57 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work: 12.2.1, 13.0
Known to fail: 12.2.0
Last reconfirmed: 2021-11-04 00:00:00


Attachments
testcase originally for bug 107460 (380 bytes, text/plain)
2022-10-31 00:30 UTC, Chris MacGregor
Details

Note You need to log in before you can comment on or make changes to this bug.
Description M Welinder 2021-11-04 14:30:26 UTC
This code triggers an internal compiler error.

------------------------------------------------
enum class Pig { OINK };

struct Hog {
  using enum Pig;
  Hog(Pig) { }
};

template <unsigned>
void pen() {
  (void)Hog(Hog::OINK);
}

void pen() {
  pen<0>();
}
------------------------------------------------


.../gcc/11.1.0/bin/g++  -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -pedantic-errors -Wall -Wno-unknown-pragmas -W -fsigned-char -malign-data=abi -fno-semantic-interposition -gz=zlib-gnu -c -march=westmere -mmmx -mno-3dnow -msse -msse2 -msse3 -mssse3 -mno-sse4a -mcx16 -msahf -mno-movbe -mno-aes -mno-sha -mno-pclmul -mpopcnt -mno-abm -mno-lwp -mno-fma -mno-fma4 -mno-xop -mno-bmi -mno-sgx -mno-bmi2 -mno-tbm -mno-avx -mno-avx2 -msse4.2 -msse4.1 -mno-lzcnt -mno-rtm -mno-hle -mno-rdrnd -mno-f16c -mno-fsgsbase -mno-rdseed -mno-prfchw -mno-adx -mfxsr -mno-xsave -mno-xsaveopt -mno-avx512f -mno-avx512er -mno-avx512cd -mno-avx512pf -mno-prefetchwt1 -mno-clflushopt -mno-xsavec -mno-xsaves -mno-avx512dq -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-avx5124fmaps -mno-avx5124vnniw -mno-clwb -mno-mwaitx -mno-clzero -mno-pku -mno-rdpid --param "l1-cache-size=32" --param "l1-cache-line-size=64" --param "l2-cache-size=12288" -mtune=westmere -std=gnu++20 -Wrestrict -Wdangling-else -Wno-placement-new -Wno-deprecated-declarations -fno-strict-overflow -Wzero-as-null-pointer-constant -Wwrite-strings -Wno-ignored-qualifiers -Wno-array-bounds -Wno-mismatched-new-delete -Wdeprecated-copy -Wdeprecated-copy-dtor -Wredundant-move -m32 -mfpmath=sse -Woverloaded-virtual -fPIC -g -o /home/welinder/using-enum_L111a3.o /home/welinder/using-enum.C
/home/welinder/using-enum.C: In instantiation of ‘void pen() [with unsigned int <anonymous> = 0]’:
/home/welinder/using-enum.C:14:9:   required from here
/home/welinder/using-enum.C:10:18: internal compiler error: in tsubst_copy, at cp/pt.c:16715
   10 |   (void)Hog(Hog::OINK);
      |             ~~~~~^~~~
0x669a60 tsubst_copy
	../../../gcc-11.1.0/gcc/cp/pt.c:16715
0x802d0c tsubst_qualified_id
	../../../gcc-11.1.0/gcc/cp/pt.c:16449
0x8041b6 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19948
0x814e01 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19587
0x814e01 tsubst_tree_list(tree_node*, tree_node*, int, tree_node*)
	../../../gcc-11.1.0/gcc/cp/pt.c:15417
0x8037fa tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:20586
0x804f67 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19587
0x804f67 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19797
0x814e01 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19587
0x814e01 tsubst_tree_list(tree_node*, tree_node*, int, tree_node*)
	../../../gcc-11.1.0/gcc/cp/pt.c:15417
0x8037fa tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:20586
0x804f67 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19587
0x804f67 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19797
0x815114 tsubst_copy_and_build(tree_node*, tree_node*, int, tree_node*, bool, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19587
0x815114 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:19198
0x816181 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:18223
0x816181 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:18268
0x815c4f tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:18223
0x815c4f tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:18589
0x809719 tsubst_expr(tree_node*, tree_node*, int, tree_node*, bool)
	../../../gcc-11.1.0/gcc/cp/pt.c:25909
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Return code for compiling using-enum.C: 1


This is an x86_64 system running OpenSuSE 15.2.
Comment 1 Marek Polacek 2021-11-04 14:51:37 UTC
Confirmed.
Comment 2 Marek Polacek 2021-11-04 14:56:52 UTC
Started with r11-5003.
Comment 3 Richard Biener 2021-11-04 14:57:50 UTC
Confirmed on the GCC 11 branch head and trunk.  clang complains:

> clang++-11 -S t.C -std=c++20
t.C:4:9: error: expected unqualified-id
  using enum Pig;
        ^
t.C:10:18: error: no member named 'OINK' in 'Hog'
  (void)Hog(Hog::OINK);
            ~~~~~^
2 errors generated.
Comment 4 M Welinder 2021-11-04 15:15:27 UTC
That version of clang does not do "using enum" at all.  clang 13 accepts this code, but it has other issues with "using enum".
Comment 5 Andrew Pinski 2022-10-30 18:54:42 UTC
*** Bug 105787 has been marked as a duplicate of this bug. ***
Comment 6 Andrew Pinski 2022-10-30 18:55:11 UTC
*** Bug 104398 has been marked as a duplicate of this bug. ***
Comment 7 Chris MacGregor 2022-10-31 00:30:40 UTC
Created attachment 53800 [details]
testcase originally for bug 107460

Now that bug 107460 (ICE with "using enum" member passed to template function, g++ 11.x-13) is marked a dup of this (indirectly, via bug 105787), attaching the testcase here; hopefully that's helpful.
Comment 8 Andrew Pinski 2022-10-31 00:32:27 UTC
*** Bug 107460 has been marked as a duplicate of this bug. ***
Comment 9 Chris MacGregor 2022-10-31 00:50:00 UTC
With the testcase I just attached, using 13.0.0 20221030 via Godbolt: https://godbolt.org/z/8Y4cr6MxY

<source>: In instantiation of 'Event::Event(EventCat, auto:1) [with auto:1 = int]':
<source>:38:32:   required from here
<source>:30:30: internal compiler error: tree check: expected enumeral_type, have record_type in tsubst_copy, at cp/pt.cc:17024
   30 |         boundsCheck<EventCat>(a_category, kEventCat_Min);  // using "EventCat::kEventCat_Min" instead of just "kEventCat_Min" makes this not fail
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Comment 10 GCC Commits 2022-12-03 15:28:39 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:b3912122c9dfaba6c8229e8f095885f69782ceda

commit r13-4477-gb3912122c9dfaba6c8229e8f095885f69782ceda
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sat Dec 3 10:28:25 2022 -0500

    c++: substituting CONST_DECL_USING_P enumerators [PR103081]
    
    We implement class-scope using enum by injecting clones of the enum's
    CONST_DECLs as fields of the class, for which CONST_DECL_USING_P is
    true, so that qualified lookup naturally finds the enumerators.
    Substitution into such a CONST_DECL currently ICEs however, because we
    assume the DECL_CONTEXT is always the ENUMERAL_TYPE (which has
    TYPE_VALUES) but in this case it's the RECORD_TYPE for the class scope
    (which has TYPE_FIELDS).
    
    Since these CONST_DECLs appear to always be non-dependent, this patch
    fixes this by shortcutting substitution for CONST_DECLs that have
    non-dependent DECL_CONTEXT.  This subsumes the existing (and seemingly
    dead) DECL_NAMESPACE_SCOPE_P early exit test and also benefits
    substitution into ordinary non-dependent CONST_DECLs.
    
            PR c++/103081
    
    gcc/cp/ChangeLog:
    
            * pt.cc (tsubst_copy) <case CONST_DECL>: Generalize
            early exit test for namespace-scope decls to check dependence of
            the enclosing scope instead.  Remove dead early exit test.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/using-enum-10.C: New test.
            * g++.dg/cpp2a/using-enum-10a.C: New test.
Comment 11 Patrick Palka 2022-12-03 15:29:13 UTC
Fixed on trunk so far.
Comment 12 GCC Commits 2022-12-19 16:54:36 UTC
The releases/gcc-12 branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:648db321893acabd06c24da149c09fceab4daeff

commit r12-8995-g648db321893acabd06c24da149c09fceab4daeff
Author: Patrick Palka <ppalka@redhat.com>
Date:   Sat Dec 3 10:28:25 2022 -0500

    c++: substituting CONST_DECL_USING_P enumerators [PR103081]
    
    We implement class-scope using enum by injecting clones of the enum's
    CONST_DECLs as fields of the class, for which CONST_DECL_USING_P is
    true, so that qualified lookup naturally finds the enumerators.
    Substitution into such a CONST_DECL currently ICEs however, because we
    assume the DECL_CONTEXT is always the ENUMERAL_TYPE (which has
    TYPE_VALUES) but in this case it's the RECORD_TYPE for the class scope
    (which has TYPE_FIELDS).
    
    Since these CONST_DECLs appear to always be non-dependent, this patch
    fixes this by shortcutting substitution for CONST_DECLs that have
    non-dependent DECL_CONTEXT.  This subsumes the existing (and seemingly
    dead) DECL_NAMESPACE_SCOPE_P early exit test and also benefits
    substitution into ordinary non-dependent CONST_DECLs.
    
            PR c++/103081
    
    gcc/cp/ChangeLog:
    
            * pt.cc (tsubst_copy) <case CONST_DECL>: Generalize
            early exit test for namespace-scope decls to check dependence of
            the enclosing scope instead.  Remove dead early exit test.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/using-enum-10.C: New test.
            * g++.dg/cpp2a/using-enum-10a.C: New test.
    
    (cherry picked from commit b3912122c9dfaba6c8229e8f095885f69782ceda)
Comment 13 Patrick Palka 2022-12-19 16:56:14 UTC
Fixed for GCC 12.3/13, thanks for the bug report.