Copy elision when initializing a base class subobject
According to https://github.com/llvm/llvm-project/issues/33864
Copy elision cannot be guaranteed when initializing a base class subobject, because base classes can have different layout than the corresponding complete object type.
But if we slightly modify the code example as follows:
struct Base {
Base(Base&&) = delete;
Base() {}
};
Base make_base() { return {}; }
template<typename F>
struct call_wrapper {
F&& f;
constexpr operator decltype(auto)() && {
return static_cast<F&&>(f)();
}
};
template<typename F>
call_wrapper(F&&) -> call_wrapper<F>;
struct B : public Base {
B() : Base(call_wrapper{make_base}) {} // <<< error in GCC, ok in Clang
};
Then Clang starts accepting it and performing copy elision for base class initialization. It looks like a bug because of the same reason. Please note that GCC keeps rejecting the modified example. Online demo: https://gcc.godbolt.org/z/rMbhdf61v
@llvm/issue-subscribers-clang-frontend
@zygoloid was speaking specifically about guaranteed copy elision but I am not sure if there is room for the implementation to still allow this or not.
If copy elision is allowed for base class initialization, then it leads to known problems. Below is a modified example from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86849
#include <cstring>
struct A {
A() {}
int n;
char k;
};
A f() {
A result;
std::memset(&result, 0, sizeof(A));
return result; // (nrvo)
}
struct B {
char x, y, z;
};
template<typename F>
struct call_wrapper {
F&& f;
constexpr operator decltype(auto)() && {
return static_cast<F&&>(f)();
}
};
template<typename F>
call_wrapper(F&&) -> call_wrapper<F>;
struct X : A, virtual B { // B is in A's tail padding
X() : B{1, 2, 3}, A(call_wrapper{f}) {}
};
int main() {
X x;
return x.x; // should return 1, but returns 0 in Clang
}
Online demo: https://gcc.godbolt.org/z/j4jan41KW
Even simpler example without memset and templates:
struct A {
A() {}
int n;
char k = 0;
};
struct B {
char x, y, z;
};
struct F {
operator A() { return {}; }
};
struct X : A, virtual B { // B is in A's tail padding
X() : B{1, 2, 3}, A(F{}) {}
};
int main() {
return X{}.x; // should return 1, but returns 127 or 0 in Clang depending on optimization settings
}
Online demo: https://gcc.godbolt.org/z/so6dd9eG9
seems start to accept since bbb8a0df7367 due to bisection
8. seems start to accept since bbb8a0d due to bisection
This case is probably different enough it would help to add a new test along w/ the tests added in the PR.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Fedor Chelnokov (Fedr)
But if we slightly modify the code example as follows:
struct Base {
Base(Base&&) = delete;
Base() {}
};
Base make_base() { return {}; }
template<typename F>
struct call_wrapper {
F&& f;
constexpr operator decltype(auto)() && {
return static_cast<F&&>(f)();
}
};
template<typename F>
call_wrapper(F&&) -> call_wrapper<F>;
struct B : public Base {
B() : Base(call_wrapper{make_base}) {} // <<< error in GCC, ok in Clang
};
Then Clang starts accepting it and performing copy elision for base class initialization. It looks like a bug because of the same reason. Please note that GCC keeps rejecting the modified example. Online demo: https://gcc.godbolt.org/z/rMbhdf61v
Opened #232902 with the base class case added next to the GH39319 tests in cxx1z-copy-omission.cpp.