Skip to content

[clang][Interp] Bail out on missing ComparisonCategoryInfo #80131

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

Merged
merged 2 commits into from
Feb 6, 2024

Conversation

tbaederr
Copy link
Contributor

Instead of asserting. This can happen in real-world code.

Instead of asserting. This can happen in real-world code.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Instead of asserting. This can happen in real-world code.


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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/ByteCodeExprGen.cpp (+2-1)
  • (modified) clang/test/Sema/struct-cast.c (+1)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 8188d6f7f5c24..a1c458a09e5ae 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -379,7 +379,8 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
       return true;
     const ComparisonCategoryInfo *CmpInfo =
         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
-    assert(CmpInfo);
+    if (!CmpInfo)
+      return false;
 
     // We need a temporary variable holding our return value.
     if (!Initializing) {
diff --git a/clang/test/Sema/struct-cast.c b/clang/test/Sema/struct-cast.c
index 74d00c42c295e..05e5fa4f92ca7 100644
--- a/clang/test/Sema/struct-cast.c
+++ b/clang/test/Sema/struct-cast.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only %s -verify
+// RUN: %clang_cc1 -fsyntax-only %s -fexperimental-new-constant-interpreter -verify
 // expected-no-diagnostics
 
 struct S {

@tbaederr
Copy link
Contributor Author

tbaederr commented Feb 6, 2024

Ping

@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only %s -verify
// RUN: %clang_cc1 -fsyntax-only %s -fexperimental-new-constant-interpreter -verify
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm really confused what this test has to do with the changes above. The changes relate to three-way comparison operators but this test is a C test file and has no comparison operators involved. Why would this test hit that assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The AST we trip up on is

BinaryOperator 0x52100006ed08 'struct S' '='
|-DeclRefExpr 0x52100006eba0 'struct S' lvalue Var 0x52100006e998 'tmp' 'struct S'
`-CStyleCastExpr 0x52100006ecd8 'struct S' <NoOp>
  `-CallExpr 0x52100006ec88 'struct S'
    `-ImplicitCastExpr 0x52100006ec68 'const struct S (*)(void)' <FunctionToPointerDecay>
      `-DeclRefExpr 0x52100006ebc8 'const struct S (void)' Function 0x52100006e870 'foo' 'const struct S (void)'

IIUC this should be rejected since the assignment doesn't work, but we're asserting that we found CmpInfo. We could of course also add more assertions that we're even in C++ mode when looking for the spaceship operator stuff.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why should this assignment not work? (The code looks valid to me). Oh, wait, you mean this assignment shouldn't be a constant expression? (If so, I agree.)

That said, I see now why we're hitting the assertion on an assignment operator in C; the call to lookupInfoForType() will return nullptr in that case and we need to handle that.

I don't think we want to look for the spaceship operator stuff outside of C++ mode, but that can be done in a follow-up as this is fixing a failing assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, what I mean is, the constant interpreter should return false for this assignment in C.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent, thank you!

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only %s -verify
// RUN: %clang_cc1 -fsyntax-only %s -fexperimental-new-constant-interpreter -verify
Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent, thank you!

@tbaederr tbaederr merged commit cc55af7 into llvm:main Feb 6, 2024
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