Skip to content

[clang] Do not infer lifetimebound for functions with void return type #131997

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 4 commits into from
Mar 24, 2025

Conversation

usx95
Copy link
Contributor

@usx95 usx95 commented Mar 19, 2025

Fixes: #126231
Also found in : microsoft/STL#5271

@usx95 usx95 requested a review from hokein March 19, 2025 10:25
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2025

@llvm/pr-subscribers-clang

Author: Utkarsh Saxena (usx95)

Changes

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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaAttr.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index defdda17c32de..a6e827a421134 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -217,7 +217,7 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
 }
 
 void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
-  if (FD->getNumParams() == 0)
+  if (FD->getNumParams() == 0 || FD->getReturnType()->isVoidType())
     return;
 
   if (unsigned BuiltinID = FD->getBuiltinID()) {

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

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

It would be nice to have have a test for it. I think it should not be too hard to add one.

@ilya-biryukov
Copy link
Contributor

LGTM!
I second the suggestion to add a test. Also see the suggestion to turn this into a warning in the first place: #126231 (comment)

I am not sure why lifetimebound needs to be an error while other attributes like const produce a warning and can be ignored.

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

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

There seem to be some errors missing the attribute tests now (see buildkite failures)

@ilya-biryukov
Copy link
Contributor

ilya-biryukov commented Mar 19, 2025

@usx95 I struggled to come up with a reproducer and couldn't understand why at first.
I think we infer the attribute after we check for this error. This is obviously something we need to fix, but you can get a reproducer like this:

https://gcc.godbolt.org/z/Wf3eqMabE

// RUN: %clang_cc1 -std=c++20 -verify %s

namespace std {
template <class T>
constexpr const T& as_const(T&) noexcept;

// We need two declarations to see the error for some reason *shrug*
template <class T> void as_const(const T&&) noexcept = delete;
template <class T> void as_const(const T&&) noexcept;
}

namespace GH126231 {

void test() {
  int a = 1;
  std::as_const(a);
}
}

@usx95 usx95 requested review from hokein and ilya-biryukov March 20, 2025 09:16
@usx95 usx95 force-pushed the infer-lifetime-void branch from aeff21a to 052bc3f Compare March 20, 2025 09:17
@usx95 usx95 merged commit 65ee281 into llvm:main Mar 24, 2025
6 of 9 checks passed
@cor3ntin cor3ntin added this to the LLVM 20.X Release milestone Apr 1, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Apr 1, 2025
@cor3ntin
Copy link
Contributor

cor3ntin commented Apr 1, 2025

/cherry-pick 65ee281

@llvmbot
Copy link
Member

llvmbot commented Apr 1, 2025

/pull-request #133997

@llvmbot llvmbot moved this from Needs Triage to Done in LLVM Release Status Apr 1, 2025
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 11, 2025
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
Development

Successfully merging this pull request may close these issues.

compile with clang 20.1.0 error: 'lifetimebound' attribute cannot be applied to a....
5 participants