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 incorrectly warns about returning reference to local #112021

Open
kelbon opened this issue Oct 11, 2024 · 4 comments
Open

clang incorrectly warns about returning reference to local #112021

kelbon opened this issue Oct 11, 2024 · 4 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer coroutines C++20 coroutines incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.)

Comments

@kelbon
Copy link
Contributor

kelbon commented Oct 11, 2024

pipeline ref with warn:

https:/kelbon/kelcoro/actions/runs/11295888979/job/31419583993?pr=31

warning:

kelcoro/include/kelcoro/task.hpp:176:12: warning: returning reference to local temporary object [-Wreturn-stack-address]
  176 |     return [](task t) -> async_task<result_type> { co_return co_await t; }(std::move(*this)).get();
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/kelcoro/kelcoro/tests/test_coroutines.cpp:817:29: note: in instantiation of member function 'dd::task<int &>::get' requested here
  817 |   error_if(&rvo_task_ref(x).get() != &x);
      |                             ^
1 warning generated.

code:

dd::task<int&> rvo_task_ref(int& x) {
  (void)co_await jump_on(TP);
  int*& ret = co_await dd::this_coro::return_place;
  ret = &x;
  co_return dd::rvo;
}

TEST(rvo_tasks) {
  int x = 0;
  error_if(&rvo_task_ref(x).get() != &x);
  return error_count;
}

Description of what happens:

task stores int* in its promise, local variable in TEST goes into coroutine by reference (and reference stored on coroutine frame)
Then, .get creates another coroutine and does get (blocking wait for result), returns int&.

Compiler for some reasons thinks, that reference to 'x' is dangling, seems for me, that its incorrect

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Oct 11, 2024
@shafik shafik added the coroutines C++20 coroutines label Oct 11, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 11, 2024

@llvm/issue-subscribers-coroutines

Author: None (kelbon)

pipeline ref with warn:

https:/kelbon/kelcoro/actions/runs/11295888979/job/31419583993?pr=31

warning:

kelcoro/include/kelcoro/task.hpp:176:12: warning: returning reference to local temporary object [-Wreturn-stack-address]
  176 |     return [](task t) -&gt; async_task&lt;result_type&gt; { co_return co_await t; }(std::move(*this)).get();
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/kelcoro/kelcoro/tests/test_coroutines.cpp:817:29: note: in instantiation of member function 'dd::task&lt;int &amp;&gt;::get' requested here
  817 |   error_if(&amp;rvo_task_ref(x).get() != &amp;x);
      |                             ^
1 warning generated.

code:

dd::task&lt;int&amp;&gt; rvo_task_ref(int&amp; x) {
  (void)co_await jump_on(TP);
  int*&amp; ret = co_await dd::this_coro::return_place;
  ret = &amp;x;
  co_return dd::rvo;
}

TEST(rvo_tasks) {
  int x = 0;
  error_if(&amp;rvo_task_ref(x).get() != &amp;x);
  return error_count;
}

Description of what happens:

task stores int* in its promise, local variable in TEST goes into coroutine by reference (and reference stored on coroutine frame)
Then, .get creates another coroutine and does get (blocking wait for result), returns int&.

Compiler for some reasons thinks, that reference to 'x' is dangling, seems for me, that its incorrect

@shafik shafik added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang Clang issues not falling into any other category labels Oct 11, 2024
@cor3ntin
Copy link
Contributor

Are you able to produce a self-contained example of this issue?

@shafik
Copy link
Collaborator

shafik commented Oct 11, 2024

CC @ChuanqiXu9

@kelbon
Copy link
Contributor Author

kelbon commented Oct 11, 2024

Are you able to produce a self-contained example of this issue?

feel free to git clone project and ctest it, may be minimized by removing all what not task, just im tired to do it

@cor3ntin cor3ntin added the incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer coroutines C++20 coroutines incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.)
Projects
None yet
Development

No branches or pull requests

4 participants