Skip to content

[Clang] Remove redundant check for scoped enums in shift operators #152865

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tinnamchoi
Copy link

This check, introduced in 0bf3140, has not been necessary since 21673c4

// C99 6.5.7p2: Each of the operands shall have integer type.
if (!lex->getType()->hasIntegerRepresentation() ||
!rex->getType()->hasIntegerRepresentation())
return InvalidOperands(Loc, lex, rex);
// C++0x: Don't allow scoped enums. FIXME: Use something better than
// hasIntegerRepresentation() above instead of this.
if (isScopedEnumerationType(lex->getType()) ||
isScopedEnumerationType(rex->getType())) {
return InvalidOperands(Loc, lex, rex);
}

bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
BT->getKind() <= BuiltinType::Int128;
if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
// Incomplete enum types are not treated as integer types.
// FIXME: In C++, enum types are never integer types.
return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
return false;
}
bool Type::hasIntegerRepresentation() const {
if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isIntegerType();
else
return isIntegerType();
}

This check, introduced in 0bf3140, has not been necessary since 21673c4
Copy link

github-actions bot commented Aug 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-clang

Author: Timothy Choi (tinnamchoi)

Changes

This check, introduced in 0bf3140, has not been necessary since 21673c4

// C99 6.5.7p2: Each of the operands shall have integer type.
if (!lex->getType()->hasIntegerRepresentation() ||
!rex->getType()->hasIntegerRepresentation())
return InvalidOperands(Loc, lex, rex);
// C++0x: Don't allow scoped enums. FIXME: Use something better than
// hasIntegerRepresentation() above instead of this.
if (isScopedEnumerationType(lex->getType()) ||
isScopedEnumerationType(rex->getType())) {
return InvalidOperands(Loc, lex, rex);
}

bool Type::isIntegerType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
return BT->getKind() >= BuiltinType::Bool &&
BT->getKind() <= BuiltinType::Int128;
if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
// Incomplete enum types are not treated as integer types.
// FIXME: In C++, enum types are never integer types.
return ET->getDecl()->isComplete() && !ET->getDecl()->isScoped();
return false;
}
bool Type::hasIntegerRepresentation() const {
if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
return VT->getElementType()->isIntegerType();
else
return isIntegerType();
}


Full diff: https://github.com/llvm/llvm-project/pull/152865.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (-6)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6793d6da85cb1..c805ef205beba 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11747,12 +11747,6 @@ QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS,
       !RHSType->hasIntegerRepresentation())
     return InvalidOperands(Loc, LHS, RHS);
 
-  // C++0x: Don't allow scoped enums. FIXME: Use something better than
-  // hasIntegerRepresentation() above instead of this.
-  if (isScopedEnumerationType(LHSType) ||
-      isScopedEnumerationType(RHSType)) {
-    return InvalidOperands(Loc, LHS, RHS);
-  }
   DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType);
 
   // "The type of the result is that of the promoted left operand."

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is fine because we don’t treat scoped enums as integer types, but we don’t seem to have any tests for this, so could you add some to make sure that we do reject shifts where one (or both) operands are scoped enums (in C and C++).

@tinnamchoi
Copy link
Author

What would the tests in C look like? Scoped enums don't exist in C.

Other than that, the tests to be introduced in #152698 should cover this, running locally seems fine.

0 << E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:10-[[@LINE-5]]:10}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"
0 >> E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:10-[[@LINE-2]]:10}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:10-[[@LINE-5]]:10}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:17-[[@LINE-6]]:17}:")"

a <<= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:11-[[@LINE-2]]:11}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:18-[[@LINE-3]]:18}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:11-[[@LINE-5]]:11}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"
a >>= E_int::e; // expected-error {{invalid operands to binary expression ('int' and 'GH24265::E_int')}}
// cxx11-20-note@-1 {{no implicit conversion for scoped enum; consider casting to underlying type}}
// CXX20: fix-it:{{.*}}:{[[@LINE-2]]:11-[[@LINE-2]]:11}:"static_cast<int>("
// CXX20: fix-it:{{.*}}:{[[@LINE-3]]:18-[[@LINE-3]]:18}:")"
// cxx23-note@-4 {{no implicit conversion for scoped enum; consider using std::to_underlying}}
// CXX23: fix-it:{{.*}}:{[[@LINE-5]]:11-[[@LINE-5]]:11}:"std::to_underlying("
// CXX23: fix-it:{{.*}}:{[[@LINE-6]]:18-[[@LINE-6]]:18}:")"

b <<= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}
b >>= 0; // expected-error {{invalid operands to binary expression ('E_int' and 'int')}}

@Sirraide
Copy link
Member

What would the tests in C look like? Scoped enums don't exist in C.

Yeah, ignore that; I was already tired yesterday and for some reason thought C had them too (it doesn’t; what it does have now is enums with a fixed underlying type, which is probably what I was thinking of)

Other than that, the tests to be introduced in #152698 should cover this, running locally seems fine.

I mean, it might honestly make more sense then to just integrate this change into that pr seeing as this one is pretty small and you’re basically already refactoring how checking around scoped enums works in the other one.

@tinnamchoi
Copy link
Author

I prefer keeping my PRs atomic. All of my changes in that PR are necessary towards the primary goal of that PR, whereas this change wouldn't be. If I allowed myself to make refactors whenever I'm working on a feature it would introduce quite a bit of noise, which seems like a bad habit to have.

@Sirraide
Copy link
Member

I prefer keeping my PRs atomic. All of my changes in that PR are necessary towards the primary goal of that PR, whereas this change wouldn't be. If I allowed myself to make refactors whenever I'm working on a feature it would introduce quite a bit of noise, which seems like a bad habit to have.

I mean, altenratively we could just wait until the other pr is merged and then merge this one afterwards; I just don’t like the idea of making a non-obvious change if there are no tests at all for it (unless we already have test that I missed but I couldn’t find any).

@tinnamchoi
Copy link
Author

Agreed, let's wait for that then.

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" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants