Skip to content

[clang] Clang must diagnose the use of [[lifetimebound]] annotation in void-returning functions #107556

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

Closed
usx95 opened this issue Sep 6, 2024 · 3 comments · Fixed by #113460 or #114203
Assignees
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

Comments

@usx95
Copy link
Contributor

usx95 commented Sep 6, 2024

https://godbolt.org/z/rcPnrWrr7

#include <string>
struct S {
    void setS(const std::string& s [[clang::lifetimebound]]) { this->s = &s; }
    const std::string* s;
};

void foo() {
    S s;
    s.setS(std::string());
}

Lifetime analysis is currently not defined for parameters annotated with [[lifetimebound]] in void-returning function.
Current analysis only enforces that the annotated parameter outlives the return value. With no return value, this analysis is silently disabled.

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

llvmbot commented Sep 6, 2024

@llvm/issue-subscribers-clang-frontend

Author: Utkarsh Saxena (usx95)

https://godbolt.org/z/rcPnrWrr7
#include &lt;string&gt;
struct S {
    void setS(const std::string&amp; s [[clang::lifetimebound]]) { this-&gt;s = &amp;s; }
    const std::string* s;
};

void foo() {
    S s;
    s.setS(std::string());
}

Lifetime analysis is currently not defined for parameters annotated with [[lifetimebound]] in void-returning function.
Current analysis only enforces that the annotated parameter outlives the return value. With no return value, this analysis is silently disabled.

@MitalAshok
Copy link
Contributor

This is at least known about:

// FIXME: Should we diagnose a void return type?
void voidreturn(int &param [[clang::lifetimebound]]);

However, what about dependent void:

template<typename T>
std::add_lvalue_reference_t<const T> get_value(cached_function<T>& f [[clang::lifetimebound]]) {
  if (!f.has_value()) f.call_and_cache();
  return f.cached_value();
}

For T = void? The current behaviour of no extra analysis seems consistent

@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Sep 6, 2024
@changkhothuychung
Copy link
Contributor

Can I pick this up? Can I have some more pointers what need to be done for this issue?

@hokein hokein added the clang:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr) label Sep 25, 2024
@bricknerb bricknerb self-assigned this Oct 23, 2024
bricknerb added a commit to bricknerb/llvm-project that referenced this issue Oct 25, 2024
bricknerb added a commit that referenced this issue Oct 25, 2024
…n a function parameter while the function returns void (#113460)

Fixes: #107556
bricknerb added a commit to bricknerb/llvm-project that referenced this issue Oct 30, 2024
…n a function implicit object parameter while the function returns void

Fixes: llvm#107556
bricknerb added a commit to bricknerb/llvm-project that referenced this issue Oct 30, 2024
…n a function implicit object parameter while the function returns void

Fixes: llvm#107556
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this issue Nov 4, 2024
…n a function parameter while the function returns void (llvm#113460)

Fixes: llvm#107556
bricknerb added a commit that referenced this issue Nov 7, 2024
…n a function implicit object parameter while the function returns void (#114203)

Fixes: #107556
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:memory-safety Issue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)
Projects
None yet
7 participants