Exposing a function that returns an array of tuples fails to compile
Maintainers usually reply within 1 day
A pull request for this has already been merged.
- #1330 by @jgabry — merged
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- build-system, compilers
Research direction
Start by reproducing the failure with the snippet in the issue, which calls $expose_functions() on a function that returns an array of tuples. The header inst/include/rcpp_tuple_interop.hpp defines the tuple wrap overload, and compile_functions() in R/expose.R sets the include order for the generated code. Done means the tuple-array return compiles and exposes, and the existing tuple-argument behaviour still works.
Written by the indexing model from the issue text.
Description
A Stan function that returns an array of tuples can't be exposed with $expose_functions(), and because every function in the block is compiled together, that means no functions from the model can be exposed.
library(cmdstanr)
mod <- cmdstan_model(write_stan_file("
functions {
array[] tuple(real, real) f(array[] tuple(real, real) x) { return x; }
}
"))
mod$expose_functions()
The only message R shows is "Error 1 occurred building shared library". The compiler output is actually
Rcpp/internal/wrap.h:528:23: error: static assertion failed due to
requirement '!sizeof(std::tuple<double, double>)': cannot convert type
to SEXP
I did some investigating with Claude. Here's its diagnosis, although I haven't verified myself yet that this is correct:
The cause is in
inst/include/rcpp_tuple_interop.hpp. It definesRcpp::wrapforstd::tuple, which works when the tuple is the return type itself (and for nested tuples, where the overload calls itself).
Forstd::vector<std::tuple<...>>Rcpp's own container wrap takes over and calls::Rcpp::wrapper element from inside the Rcpp headers (wrap.h:200,range_wrap_dispatch___generic), where our overload isn't visible yet, so the generic fallback fires its static assertion. It's the same visibility problem that keepsRcpp::List::create(args...)from compiling for nested tuples (see the commit message of #1310).Taking a tuple array as an argument is fine, the
Exporterside handles it. Only returning one fails.Rcpp's documented way to extend
wrapfor a new type is to includeRcppCommon.h, declare the overload, then includeRcpp.h, so the declaration is visible to the templates in the Rcpp headers. The
generated code includesRcpp.hfirst through the other interop headers, so this would need the include order incompile_functions()(R/expose.R) rearranged. Untested.
Tested on macOS with CmdStan 2.40.0, Rcpp 1.1.2, on the v1.0 branch with the fix for #1001 (#1310) in. Pre-existing: the old tuple header failed the same way.
- Dominant language
- R
- Stars
- 164
- Forks
- 69
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 47
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stan-dev/cmdstanr
-
internal-code
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
stan-dev/cmdstanr#506 · 7 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Model methods fail on a reloaded fit when the data was passed in memoryPossibly taken @jgabry claimed this 2 days ago. Openbug
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Maintainers usually reply within 1 day
-
internal-code
Difficulty 4/5 3-5 days Newbie friendliness 48/100
stan-dev/cmdstanr#1314 · 4 comments ·
Maintainers usually reply within 1 day
-
feature
Difficulty 4/5 3-5 days Newbie friendliness 35/100
stan-dev/cmdstanr#1175 · 1 comment · 2 reactions ·
Maintainers usually reply within 1 day
All issues in stan-dev/cmdstanr
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
HelenaLC/spatialdataR#284 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Hackathon: Tier 1
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pharmaverse/logrx#309 ·