Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated instantiation of enumerators cause ambiguous references to the same name #124405

Open
thebrandre opened this issue Jan 25, 2025 · 1 comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@thebrandre
Copy link
Contributor

Clang erroneously instantiates the enumerators twice in the case of a prior declaration.
As a consequence, the name lookup in the last line of the following example becomes ambiguous.

template <typename T>
struct S {
    enum E : T;
    enum E : T { X = 5 };
};

auto x = S<char>::X;

Compiler output:

source>:7:19: error: reference to 'X' is ambiguous
    7 | auto x = S<char>::X;
      |          ~~~~~~~~~^
<source>:4:18: note: candidate found by name lookup is 'S<char>::X'
    4 |     enum E : T { X = 5 };
      |                  ^
<source>:4:18: note: candidate found by name lookup is 'S<char>::X'
1 error generated.

Another symptom is that diagnostics during instantiation appear twice in the output if a preceding opaque-enum-declaration exists:

template <typename T>
struct S {
    enum E : T;
    enum E : T { X = 0x7FFFFF00 };
};

template struct S<char>;

Compiler output:

<source>:4:22: warning: implicit conversion from 'int' to 'char' changes value from 2147483392 to 0 [-Wconstant-conversion]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^~~~~~~~~~
<source>:7:17: note: in instantiation of template class 'S<char>' requested here
    7 | template struct S<char>;
      |                 ^
<source>:4:22: error: enumerator value evaluates to 2147483392, which cannot be narrowed to type 'char' [-Wc++11-narrowing]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^
<source>:4:22: warning: implicit conversion from 'int' to 'char' changes value from 2147483392 to 0 [-Wconstant-conversion]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^~~~~~~~~~
<source>:4:22: error: enumerator value evaluates to 2147483392, which cannot be narrowed to type 'char' [-Wc++11-narrowing]
2 warnings and 2 errors generated.

See here on Compiler Explorer

@llvmbot
Copy link
Member

llvmbot commented Jan 25, 2025

@llvm/issue-subscribers-clang-frontend

Author: André Brand (thebrandre)

Clang erroneously instantiates the enumerators twice in the case of a prior declaration. As a consequence, the name lookup in the last line of the following example becomes ambiguous.
template &lt;typename T&gt;
struct S {
    enum E : T;
    enum E : T { X = 5 };
};

auto x = S&lt;char&gt;::X;

Compiler output:

source&gt;:7:19: error: reference to 'X' is ambiguous
    7 | auto x = S&lt;char&gt;::X;
      |          ~~~~~~~~~^
&lt;source&gt;:4:18: note: candidate found by name lookup is 'S&lt;char&gt;::X'
    4 |     enum E : T { X = 5 };
      |                  ^
&lt;source&gt;:4:18: note: candidate found by name lookup is 'S&lt;char&gt;::X'
1 error generated.

Another symptom is that diagnostics during instantiation appear twice in the output if a preceding opaque-enum-declaration exists:

template &lt;typename T&gt;
struct S {
    enum E : T;
    enum E : T { X = 0x7FFFFF00 };
};

template struct S&lt;char&gt;;

Compiler output:

&lt;source&gt;:4:22: warning: implicit conversion from 'int' to 'char' changes value from 2147483392 to 0 [-Wconstant-conversion]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^~~~~~~~~~
&lt;source&gt;:7:17: note: in instantiation of template class 'S&lt;char&gt;' requested here
    7 | template struct S&lt;char&gt;;
      |                 ^
&lt;source&gt;:4:22: error: enumerator value evaluates to 2147483392, which cannot be narrowed to type 'char' [-Wc++11-narrowing]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^
&lt;source&gt;:4:22: warning: implicit conversion from 'int' to 'char' changes value from 2147483392 to 0 [-Wconstant-conversion]
    4 |     enum E : T { X = 0x7FFFFF00 };
      |                      ^~~~~~~~~~
&lt;source&gt;:4:22: error: enumerator value evaluates to 2147483392, which cannot be narrowed to type 'char' [-Wc++11-narrowing]
2 warnings and 2 errors generated.

See here on Compiler Explorer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

3 participants