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

Build failure in nostd/span.h #2382

Closed
VivekSubr opened this issue Oct 20, 2023 · 6 comments
Closed

Build failure in nostd/span.h #2382

VivekSubr opened this issue Oct 20, 2023 · 6 comments
Assignees
Labels
bug Something isn't working triage/needs-information Indicates an issue needs more information in order to work on it.

Comments

@VivekSubr
Copy link

VivekSubr commented Oct 20, 2023

Describe your environment
subramaniamv@a7e76fbbc22b:/src/cna$ clang --version
clang version 16.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Steps to reproduce
Install opentelemetry, and build using this compiler... you will get error,

In file included from /usr/include/affirmed/logging/genTracer.h:3:
In file included from /usr/include/affirmed/logging/genSpan.h:4:
In file included from /usr/include/opentelemetry/sdk/trace/tracer.h:11:
In file included from /usr/include/opentelemetry/sdk/instrumentationscope/instrumentation_scope.h:8:
In file included from /usr/include/opentelemetry/common/key_value_iterable_view.h:9:
In file included from /usr/include/opentelemetry/common/key_value_iterable.h:6:
In file included from /usr/include/opentelemetry/common/attribute_value.h:8:
/usr/include/opentelemetry/nostd/span.h:160:64: error: expected body of lambda expression
                                    std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>

Additional context
Looks like compound statements in enable_if is not getting accepted by clang, this patch gets it working ->

diff -urNp opentelemetry-cpp-1.8.1/api/include/opentelemetry/nostd/span.h opentelemetry-cpp-1.8.1-patched/api/include/opentelemetry/nostd/span.h
--- opentelemetry-cpp-1.8.1/api/include/opentelemetry/nostd/span.h	2022-12-04 12:11:21.000000000 -0600
+++ opentelemetry-cpp-1.8.1-patched/api/include/opentelemetry/nostd/span.h	2023-01-27 06:02:20.660992000 -0600
@@ -135,9 +135,7 @@ public:
   }
 
   template <class U,
-            size_t N,
-            typename std::enable_if<N == Extent &&
-                                    std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
+            size_t N>
   span(const span<U, N> &other) noexcept : data_{other.data()}
   {}
 
@@ -216,8 +214,7 @@ public:
   {}
 
   template <class U,
-            size_t N,
-            typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
+            size_t N>
   span(const span<U, N> &other) noexcept : extent_{other.size()}, data_{other.data()}
   {}
@VivekSubr VivekSubr added the bug Something isn't working label Oct 20, 2023
@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Oct 20, 2023
@marcalff marcalff self-assigned this Oct 20, 2023
@marcalff
Copy link
Member

Investigating.

A couple of comments:

The syntax std::is_convertible<U (*)[], T (*)[]> looks legitimate.

This is for example what I have in my /usr/include/c++/12/type_traits header with g++12

  // helper trait for unique_ptr<T[]>, shared_ptr<T[]>, and span<T, N>
  template<typename _ToElementType, typename _FromElementType>
    using __is_array_convertible
      = is_convertible<_FromElementType(*)[], _ToElementType(*)[]>;

Please indicate:

  • which C++ mode is used (C++11/14/17/20/23) when building.
  • the setting for WITH_STL, I assume it is OFF since nostd::span is used

Also, please indicate which .cc file fails to build, and whether:

  • this fails when compiling opentelemetry-cpp itself (in which cc file ?),
  • or if it fails when compiling some application code using opentelemetry-cpp (what is /usr/include/affirmed/logging/genSpan.h ?)

In both cases, show the calling code that uses a span, causing this.

Also, note that the patch is incorrect.

It may build, but is unsafe, and will assign spans of incompatible objects U and size N to a span<T, Extent>, causing runtime crashes.

@marcalff
Copy link
Member

Trying the following code, which is extracted from nostd::span in opentelemetry-cpp:

#include <cstddef>
#include <type_traits>

template <class T, size_t Extent>
class span {
public:
    span(T* data);

  template <class U,
            size_t N,
            typename std::enable_if<N == Extent &&
                                    std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
  span(const span<U, N> &other) noexcept : data_{other.data()}
  {}

private:
  T* data_;
};

int main() {
    int *d1 = new int[10];
    double *d2 = new double[20];
    span<int, 10> s1(d1);
    span<double, 20> s2(d2);

    span<int, 10> s3(s1);

    // Fails as expected
    // span<int, 20> s4(s2);
}

in compiler explorer (https://godbolt.org/) with Clang 16.0.0.

The code builds properly for valid spans (s3), and fails as expected for invalid ones (s4).

@VivekSubr
Copy link
Author

Thanks for the help @marcalff!

"which C++ mode is used (C++11/14/17/20/23) when building."
C++17, we build otel as,

cmake .. -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_CXX_STANDARD=17 \
         -DWITH_STL=ON -DBUILD_SHARED_LIBS=ON -DWITH_OTLP=ON -DWITH_OTLP_GRPC=ON -DBUILD_TESTING=OFF 

Since span is not in C++17, it still uses nostd.

"Also, please indicate which .cc file fails to build, and whether:"
Application code fails when including the header, syntax error

In file included from ../common/pfcpUtils.cc:18:
In file included from /src/cna/cna/cna-pfcp/interface/pfcpLib/common/pfcpUtils.h:26:
In file included from /src/cna/cna/cna-pfcp/interface/pfcpLib/common/pfcpCommon.h:35:
In file included from /src/cna/cna/cna-pfcp/interface/pfcpLib/common/pfcpTracer.h:20:
In file included from /usr/include/affirmed/logging/genTracer.h:3:
In file included from /usr/include/affirmed/logging/genSpan.h:4:
In file included from /usr/include/opentelemetry/sdk/trace/tracer.h:9:
In file included from /usr/include/opentelemetry/sdk/resource/resource.h:6:
In file included from /usr/include/opentelemetry/common/attribute_value.h:8:

In genSpan.h line 4, we have an include for tracer.h, and this class also creates an object of type std::shared_ptrbase::GenSpan... but the compiler throws a syntax error well before this.

@esigo esigo added triage/needs-information Indicates an issue needs more information in order to work on it. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 23, 2023
@VivekSubr
Copy link
Author

@esigo... what information is needed?

@lalitb
Copy link
Member

lalitb commented Oct 27, 2023

@VivekSubr Based on the conversation during the community meeting, and indicated by @marcalff in above comment, the error couldn't be replicated.

@marcalff
Copy link
Member

Closing, can not reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage/needs-information Indicates an issue needs more information in order to work on it.
Projects
None yet
Development

No branches or pull requests

4 participants