Skip to content

[clang][Interp] Fix returning nullptr from functions #67229

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 1 commit into from
Oct 10, 2023

Conversation

tbaederr
Copy link
Contributor

isLive() is false for null pointers, so we need to special-case this here.

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

llvmbot commented Sep 23, 2023

@llvm/pr-subscribers-clang

Changes

isLive() is false for null pointers, so we need to special-case this here.


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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/Interp.h (+1-1)
  • (modified) clang/test/AST/Interp/functions.cpp (+7)
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 8453856e526a6b2..71d49a0894f4e35 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -209,7 +209,7 @@ bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
     // FIXME: We could be calling isLive() here, but the emitted diagnostics
     // seem a little weird, at least if the returned expression is of
     // pointer type.
-    if (!Ret.isLive())
+    if (!Ret.isZero() && !Ret.isLive())
       return false;
   }
 
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 331df74d50b3d62..7f03271d152db5c 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -332,3 +332,10 @@ namespace InvalidReclRefs {
   }
 #endif
 }
+
+namespace PtrReturn {
+  constexpr void *a() {
+    return nullptr;
+  }
+  static_assert(a() == nullptr, "");
+}

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

LGTM, besides the comment on the comment.

@tbaederr
Copy link
Contributor Author

tbaederr commented Oct 9, 2023

Ping

isLive() is false for null pointers, so we need to special-case
this here.
@cor3ntin
Copy link
Contributor

@tbaederr Shafik already approved that - still looks good :)

@tbaederr
Copy link
Contributor Author

Yeah sorry for the unnecessary ping :) I'll push as soon as the CI is done.

@tbaederr tbaederr merged commit 17414ea into llvm:main Oct 10, 2023
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.

4 participants