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

Clang crashes when parsing operator== for an enum parameter with a bool enum base and no enumerator list #56560

Closed
alanzhao1 opened this issue Jul 16, 2022 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@alanzhao1
Copy link
Contributor

llvm commit I have checked out where this is broken (not necessarily the cause): 4a40fa8

Repro steps

test.cpp:

enum Foo : bool;

bool bar(Foo a, Foo b) {
    return a == b;
}
  1. Build Clang with LLVM_ENABLE_ASSERTIONS:BOOL=ON
  2. Run clang++ -c test.cpp -o test

Expected result:
clang should compile the file

Actual result:

clang crashes with the assertion

clang++: /path/to/llvm/repo/llvm-project/clang/lib/AST/Expr.cpp:1836: bool clang::CastExpr::CastConsistency() const: Assertion `!getType()->isBooleanType() && "unheralded conversion to bool"' failed.

The full stacktrace is in the attached stacktrace.txt.

(I had to change the extension of the .cpp file and the .sh file as GitHub doesn't support those file type extensions as attachments)

stacktrace.txt
test-f893c2.cpp.txt
test-f893c2.sh.txt

@alanzhao1 alanzhao1 added the clang Clang issues not falling into any other category label Jul 16, 2022
@alanzhao1
Copy link
Contributor Author

This bug was first observed in Chromium: https://crbug.com/1344641

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" crash Prefer [crash-on-valid] or [crash-on-invalid] and removed clang Clang issues not falling into any other category labels Jul 16, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 16, 2022

@llvm/issue-subscribers-clang-frontend

@alanzhao1
Copy link
Contributor Author

alanzhao1 commented Jul 18, 2022

The failing assert is here:

assert(!getType()->isBooleanType() && "unheralded conversion to bool");

assert(!getType()->isBooleanType() && "unheralded conversion to bool");

The definition of isBooleanType() is:

inline bool Type::isBooleanType() const {
  if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType))
    return BT->getKind() == BuiltinType::Bool;
  return false;
}

Using gdb, we find that BT->getKind() is BuiltinType::Bool for the test file.

In contrast, if we add an enum list to the input (which compiles without crashing):

enum Foo : bool {a, b};

bool bar(Foo a, Foo b) {
    return a == b;
}

gdb shows us that BT->getKind() is BuiltinType::Int.

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" crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

No branches or pull requests

3 participants