Skip to content

Commit 469f559

Browse files
committed
[Clang] Add tests for new diagnostics for scoped enums
1 parent ab9d87b commit 469f559

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

clang/test/SemaCXX/enum-scoped.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++11 -verify -triple x86_64-apple-darwin %s
22
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++17 -verify -triple x86_64-apple-darwin %s
3+
// RUN: %clang_cc1 -fsyntax-only -pedantic -std=c++20 -verify -triple x86_64-apple-darwin %s
34

45
enum class E1 {
56
Val1 = 1L
@@ -328,8 +329,10 @@ namespace PR18044 {
328329
int E::*p; // expected-error {{does not point into a class}}
329330
using E::f; // expected-error {{no member named 'f'}}
330331

332+
#if __cplusplus < 202002L
331333
using E::a; // expected-warning {{using declaration naming a scoped enumerator is a C++20 extension}}
332334
E b = a;
335+
#endif
333336
}
334337

335338
namespace test11 {
@@ -367,3 +370,102 @@ S<_Atomic(int)> s; // expected-warning {{'_Atomic' is a C11 extension}}
367370
static_assert(__is_same(__underlying_type(S<_Atomic(long long)>::OhBoy), long long), ""); // expected-warning {{'_Atomic' is a C11 extension}}
368371
// expected-note@-1 {{in instantiation of template class 'GH147736::S<_Atomic(long long)>' requested here}}
369372
}
373+
374+
namespace GH24265 {
375+
enum class E_int { e };
376+
enum class E_long : long { e };
377+
378+
void f() {
379+
E_int::e + E_long::e; // expected-error {{invalid operands to binary expression ('GH24265::E_int' and 'GH24265::E_long')}}
380+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
381+
// expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}
382+
E_int::e + 0; // expected-error {{invalid operands to binary expression ('GH24265::E_int' and 'int')}}
383+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
384+
385+
0 * E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
386+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
387+
0 / E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
388+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
389+
0 % E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
390+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
391+
0 + E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
392+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
393+
0 - E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
394+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
395+
0 << E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
396+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
397+
0 >> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
398+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
399+
400+
#if __cplusplus >= 202002L
401+
0 <=> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
402+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
403+
#endif
404+
405+
0 < E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
406+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
407+
0 > E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
408+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
409+
0 <= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
410+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
411+
0 >= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
412+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
413+
0 == E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
414+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
415+
0 != E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
416+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
417+
0 & E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
418+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
419+
0 ^ E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
420+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
421+
0 | E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
422+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
423+
0 && E_int::e; // expected-error {{value of type 'GH24265::E_int' is not contextually convertible to 'bool'}}
424+
// expected-error@-1 {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
425+
// expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}
426+
0 || E_int::e; // expected-error {{value of type 'GH24265::E_int' is not contextually convertible to 'bool'}}
427+
// expected-error@-1 {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
428+
// expected-note@-2 {{no implicit conversion for scoped enum; consider casting to underlying type}}
429+
430+
int a;
431+
a *= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
432+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
433+
a /= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
434+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
435+
a %= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
436+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
437+
a += E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
438+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
439+
a -= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
440+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
441+
a <<= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
442+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
443+
a >>= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
444+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
445+
a &= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
446+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
447+
a ^= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
448+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
449+
a |= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
450+
// expected-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
451+
452+
// TODO: These do not have the diagnostic yet
453+
E_int b;
454+
b *= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
455+
b /= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
456+
b %= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
457+
b += 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
458+
b -= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
459+
b <<= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
460+
b >>= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
461+
b &= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
462+
b ^= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
463+
b |= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
464+
465+
a = E_int::e; // expected-error {{assigning to 'int' from incompatible type 'GH24265::E_int'}}
466+
b = 0; // expected-error {{assigning to 'E_int' from incompatible type 'int'}}
467+
468+
E_int c = 0; // expected-error {{cannot initialize a variable of type 'E_int' with an rvalue of type 'int'}}
469+
int d = E_int::e; // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'GH24265::E_int'}}
470+
}
471+
}

0 commit comments

Comments
 (0)