Skip to content

Compilation fails if a single UDF is defined and used for two different tuple types #3356

@nsiccha

Description

@nsiccha

Not sure whether the title is clear. The following compiles:

functions {
int f3(tuple(int, int, int) x) {
    return x.3;
}
int f2(tuple(int, int) x) {
    return x.2;
}
}
transformed data {
    tuple(int, int, int) x3 = (1, 2, 3);
    tuple(int, int) x2 = (1, 2);
    int y3 = f3(x3);
    int y2 = f2(x2);
}

but the below does not, with a probably obscure-to-many g++ error message (reproduced at the end):

functions {
int f(tuple(int, int, int) x) {
    return x.3;
}
int f(tuple(int, int) x) {
    return x.2;
}
}
data {
}
transformed data {
    tuple(int, int, int) x3 = (1, 2, 3);
    tuple(int, int) x2 = (1, 2);
    int y3 = f(x3);
    int y2 = f(x2);
}

IMO, either, both models should compile, or stanc should already complain about the second model.


Output during attempted compilation of the second model (via the latest BridgeStan called from Julia):

┌ Error: Compilation failed!
│ Command: setenv(`make /home/niko/github/nsiccha/mwe/BridgeStan6/crash_model.so`; dir="/home/niko/.bridgestan/bridgestan-2.7.0")
│ stdout: 
│ --- Translating Stan model to C++ code ---
│ ./bin/stanc  --o=/home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp /home/niko/github/nsiccha/mwe/BridgeStan6/crash.stan
│ 
│ --- Compiling C++ code ---
│ g++ -std=c++17 -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -Wno-class-memaccess      -I ./stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I ./stan/src -I ./stan/lib/rapidjson_1.1.0/ -I ./stan/lib/stan_math/ -I ./stan/lib/stan_math/lib/eigen_3.4.0 -I ./stan/lib/stan_math/lib/boost_1.87.0 -I ./stan/lib/stan_math/lib/sundials_6.1.1/include -I ./stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -fPIC -fvisibility=hidden -fvisibility-inlines-hidden    -DBOOST_DISABLE_ASSERTS         -DBRIDGESTAN_EXPORT -DSTAN_OVERRIDE_EIGEN_ASSERT  -c  -x c++ -o /home/niko/github/nsiccha/mwe/BridgeStan6/crash.o /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp
│ rm /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp
│ 
│ stderr: /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp: In constructor ‘crash_model_namespace::crash_model::crash_model(stan::io::var_context&, unsigned int, std::ostream*)’:
│ /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:129:13: error: call of overloaded ‘f(std::tuple<int, int, int>&, std::ostream*&)’ is ambiguous
│   129 |       y3 = f(x3, pstream__);
│       |            ~^~~~~~~~~~~~~~~
│ /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:42:5: note: candidate: ‘int crash_model_namespace::f(const T0__&, std::ostream*) [with T0__ = std::tuple<int, int, int>; stan::require_all_t<stan::math::is_tuple<T0__>, std::is_integral<typename std::decay<typename std::tuple_element<0, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<1, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<2, typename std::decay<_Tp>::type>::type>::type> >* <anonymous> = 0; std::ostream = std::basic_ostream<char>]’
│    42 | int f(const T0__& x, std::ostream* pstream__) {
│       |     ^
│ /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:67:5: note: candidate: ‘int crash_model_namespace::f(const T0__&, std::ostream*) [with T0__ = std::tuple<int, int, int>; stan::require_all_t<stan::math::is_tuple<T0__>, std::is_integral<typename std::decay<typename std::tuple_element<0, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<1, typename std::decay<_Tp>::type>::type>::type> >* <anonymous> = 0; std::ostream = std::basic_ostream<char>]’
│    67 | int f(const T0__& x, std::ostream* pstream__) {
│       |     ^
│ In file included from /usr/include/c++/11/functional:54,
│                  from ./stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Core:85,
│                  from ./stan/lib/stan_math/lib/eigen_3.4.0/Eigen/Dense:1,
│                  from ./stan/lib/stan_math/stan/math/prim/fun/Eigen.hpp:32,
│                  from ./stan/lib/stan_math/stan/math/prim/meta/append_return_type.hpp:4,
│                  from ./stan/lib/stan_math/stan/math/prim/meta.hpp:72,
│                  from ./stan/lib/stan_math/stan/math/rev/core/accumulate_adjoints.hpp:4,
│                  from ./stan/lib/stan_math/stan/math/rev/core.hpp:4,
│                  from ./stan/src/stan/model/model_base.hpp:8,
│                  from ./stan/src/stan/model/model_header.hpp:4,
│                  from /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:2:
│ /usr/include/c++/11/tuple: In instantiation of ‘struct std::tuple_element<0, std::tuple<> >’:
│ /usr/include/c++/11/tuple:1353:12:   recursively required from ‘struct std::tuple_element<1, std::tuple<int> >’
│ /usr/include/c++/11/tuple:1353:12:   required from ‘struct std::tuple_element<2, std::tuple<int, int> >’
│ /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:24:89:   required by substitution of ‘template<class T0__, stan::require_all_t<stan::math::is_tuple<T0__>, std::is_integral<typename std::decay<typename std::tuple_element<0, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<1, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<2, typename std::decay<_Tp>::type>::type>::type> >* <anonymous> > int crash_model_namespace::f(const T0__&, std::ostream*) [with T0__ = std::tuple<int, int>; stan::require_all_t<stan::math::is_tuple<T0__>, std::is_integral<typename std::decay<typename std::tuple_element<0, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<1, typename std::decay<_Tp>::type>::type>::type>, std::is_integral<typename std::decay<typename std::tuple_element<2, typename std::decay<_Tp>::type>::type>::type> >* <anonymous> = <missing>]’
│ /home/niko/github/nsiccha/mwe/BridgeStan6/crash.hpp:133:13:   required from here
│ /usr/include/c++/11/tuple:1371:25: error: static assertion failed: tuple index must be in range
│  1371 |       static_assert(__i < tuple_size<tuple<>>::value,
│       |                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
│ /usr/include/c++/11/tuple:1371:25: note: ‘(0 < ((long unsigned int)std::integral_constant<long unsigned int, 0>::value))’ evaluates to false
│ make: *** [Makefile:77: /home/niko/github/nsiccha/mwe/BridgeStan6/crash.o] Error 1
└ @ Main ~/github/nsiccha/mwe/BridgeStan6/crash.jl:9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions