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

[Bug]: Incomplete type error with vector of unique_ptrs #1798

Closed
equeim opened this issue Nov 1, 2022 · 8 comments
Closed

[Bug]: Incomplete type error with vector of unique_ptrs #1798

equeim opened this issue Nov 1, 2022 · 8 comments

Comments

@equeim
Copy link

equeim commented Nov 1, 2022

Description

This code fails to compile with invalid application of 'sizeof' to an incomplete type 'Opaque' error using NDK r25b:

#include <memory>
#include <vector>

class Opaque;

class Foo {
public:
    Foo(); // defined in another translation unit
    ~Foo(); // defined in another translation unit
private:
    std::vector<std::unique_ptr<Opaque>> ptrs{};
};

void bar() {
    Foo foo{};
}

I believe that this code is legal. GCC and MSVC accept it, as well as Clang/libc++ starting with version 14 (which NDK r25b should based on).
It fails with Clang/libc++ 13, though.

Here is godbolt link: https://godbolt.org/z/YvnK5Wrod
If you change Clang version to 14.0.0 then error disappears.

Upstream bug

No response

Commit to cherry-pick

No response

Affected versions

r25

Canary version

No response

Host OS

Linux

Host OS version

Ubuntu 22.04

Affected ABIs

armeabi-v7a, arm64-v8a, x86, x86_64

@equeim equeim added the bug label Nov 1, 2022
@equeim
Copy link
Author

equeim commented Nov 1, 2022

It seems that godbolt's Clang 14 is built from commit llvm/llvm-project@282c83c which is somewhere between 14.0.0 and 14.0.1. While NDK r25b is on llvm/llvm-project@282c83c which is 14.0.0. Any plans to update to latest LLVM 14 release which already is 14.0.6?

@stephenhines
Copy link
Collaborator

NDK r25b is using clang-r450784d, which doesn't seem to reproduce this error for me. Can you confirm which Clang/NDK you are actually running? Also, Android's 14.0.0 designation is always based on a rolling git revision of upstream LLVM and not the official release version, so 14.0.0 usually refers to something before 14 has actually branched. Still, my version of clang-r450784d shows me:

$ /disk/android_trees/aosp/prebuilts/clang/host/linux-x86/clang-r450784d/bin/clang++ --version
Android (8490178, based on r450784d) clang version 14.0.6 (https://android.googlesource.com/toolchain/llvm-project 4c603efb0cca074e9238af8b4106c30add4418f6)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /disk/android_trees/aosp/prebuilts/clang/host/linux-x86/clang-r450784d/bin

The following command also works for your test file:

$ /disk/android_trees/aosp/prebuilts/clang/host/linux-x86/clang-r450784d/bin/clang++ -stdlib=libc++ -O2 -c t.cpp

@equeim
Copy link
Author

equeim commented Nov 3, 2022

alexey@localhost ~/projects $ ~/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --version
Android (8490178, based on r450784d) clang version 14.0.6 (https://android.googlesource.com/toolchain/llvm-project 4c603efb0cca074e9238af8b4106c30add4418f6)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin

Following command fails for me:

alexey@localhost ~/projects $ ~/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++ -std=c++17 -stdlib=libc++ -O2 -c test.cpp
In file included from test.cpp:1:
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:2423:19: error: invalid application of 'sizeof' to an incomplete type 'Opaque'
    static_assert(sizeof(_Tp) > 0,
                  ^~~~~~~~~~~
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:2682:7: note: in instantiation of member function 'std::default_delete<Opaque>::operator()' requested here
      __ptr_.second()(__tmp);
      ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:2636:19: note: in instantiation of member function 'std::unique_ptr<Opaque>::reset' requested here
  ~unique_ptr() { reset(); }
                  ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:1976:92: note: in instantiation of member function 'std::unique_ptr<Opaque>::~unique_ptr' requested here
    _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
                                                                                           ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:1814:21: note: in instantiation of member function 'std::allocator<std::unique_ptr<Opaque>>::destroy' requested here
                __a.destroy(__p);
                    ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/memory:1640:14: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::unique_ptr<Opaque>>>::__destroy<std::unique_ptr<Opaque>>' requested here
            {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
             ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/vector:426:25: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::unique_ptr<Opaque>>>::destroy<std::unique_ptr<Opaque>>' requested here
        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__soon_to_be_end));
                        ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/vector:369:29: note: in instantiation of member function 'std::__vector_base<std::unique_ptr<Opaque>, std::allocator<std::unique_ptr<Opaque>>>::__destruct_at_end' requested here
    void clear() _NOEXCEPT {__destruct_at_end(__begin_);}
                            ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/vector:463:9: note: in instantiation of member function 'std::__vector_base<std::unique_ptr<Opaque>, std::allocator<std::unique_ptr<Opaque>>>::clear' requested here
        clear();
        ^
/home/alexey/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/c++/v1/vector:495:5: note: in instantiation of member function 'std::__vector_base<std::unique_ptr<Opaque>, std::allocator<std::unique_ptr<Opaque>>>::~__vector_base' requested here
    vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
    ^
test.cpp:11:46: note: in instantiation of member function 'std::vector<std::unique_ptr<Opaque>>::vector' requested here
    std::vector<std::unique_ptr<Opaque>> ptrs{};
                                             ^
test.cpp:4:7: note: forward declaration of 'Opaque'
class Opaque;
      ^
1 error generated.

If I use plain clang++ without specifying a --target option then it succeeds. It won't compile for necessary architectures this way of course.

@DanAlbert
Copy link
Member

If I use plain clang++ without specifying a --target option then it succeeds.

If you omit --target but use --stdlib=libc++, does it work? If so this is just #1530

@rprichard
Copy link
Collaborator

The following command also works for your test file:

$ /disk/android_trees/aosp/prebuilts/clang/host/linux-x86/clang-r450784d/bin/clang++ -stdlib=libc++ -O2 -c t.cpp

I believe that command will use a newer libc++ than what the NDK is using.

I still see the reported problem with NDK r25b:

/x/android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang++ t.cpp -c

I assume this is #1530.

@equeim
Copy link
Author

equeim commented Nov 3, 2022

If I use plain clang++ without specifying a --target option then it succeeds.

If you omit --target but use --stdlib=libc++, does it work? If so this is just #1530

Yes (I have Clang 14.0.5 with libc++ installed on host machine (Fedora 36)).

@stephenhines
Copy link
Collaborator

Ah, I missed that I was using the wrong libc++ as part of my call. This indeed looks like #1530 again.

@DanAlbert
Copy link
Member

Duplicate of #1530

@DanAlbert DanAlbert marked this as a duplicate of #1530 Nov 7, 2022
@DanAlbert DanAlbert closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants