stan-dev / stan-dev/rstan

expose_stan_functions fails on functions postfixed with `_lp`

Open
#550 56 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Summary:

expose_stan_functions does not work with functions that modify target, which worked previously

Description:

If you try, there is a compiler error

Reproducible Steps:

expose_stan_functions this

functions {
  void foo_lp() {
    target += normal_lpdf(0.5 | 0, 1);
  }
}
Current Output:
In file included from file7d8f62dde969.cpp:1:
In file included from /usr/local/lib/R/site-library/rstan/include/exporter.h:1:
In file included from /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:168:
In file included from /usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:25:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/internal/Exporter.h:31:28: error: no matching constructor for initialization of 'stan::math::accumulator<double>'
                    Exporter( SEXP x ) : t(x){}
                                         ^ ~
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:87:41: note: in instantiation of member function 'Rcpp::traits::Exporter<stan::math::accumulator<double> >::Exporter' requested here
            ::Rcpp::traits::Exporter<T> exporter(x);
                                        ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:152:26: note: in instantiation of function template specialization 'Rcpp::internal::as<stan::math::accumulator<double> >' requested here
        return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
                         ^
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/InputParameter.h:46:49: note: in instantiation of function template specialization 'Rcpp::as<stan::math::accumulator<double> >' requested here
        ReferenceInputParameter(SEXP x_) : obj( as<T>(x_) ){}
                                                ^
file7d8f62dde969.cpp:75:77: note: in instantiation of member function 'Rcpp::ReferenceInputParameter<stan::math::accumulator<double> >::ReferenceInputParameter' requested here
    Rcpp::traits::input_parameter< stan::math::accumulator<double>& >::type lp_accum__(lp_accum__SEXP);
                                                                            ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/mat/fun/accumulator.hpp:25:7: note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'const stan::math::accumulator<double>' for 1st argument
class accumulator {
      ^
/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/mat/fun/accumulator.hpp:33:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
  accumulator() : buf_() {}
  ^

and the relevant part of the generated C++ file is

void foo_lp(double& lp__, stan::math::accumulator<double>& lp_accum__, std::ostream* pstream__);
RcppExport SEXP sourceCpp_1_foo_lp(SEXP lp__SEXP, SEXP lp_accum__SEXP, SEXP pstream__SEXP) {
BEGIN_RCPP
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< double& >::type lp__(lp__SEXP);
    Rcpp::traits::input_parameter< stan::math::accumulator<double>& >::type lp_accum__(lp_accum__SEXP);
    Rcpp::traits::input_parameter< std::ostream* >::type pstream__(pstream__SEXP);
    foo_lp(lp__, lp_accum__, pstream__);
    return R_NilValue;
END_RCPP
}
Expected Output:

None

RStan Version:

develop (2.18) branch from GitHub

R Version:

3.5

Operating System:

Debian

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the expose_stan_functions entry point and inspect the generated C++ wrapper for the foo_lp example, especially conversion of lp_accum__ through Rcpp. Reproduce the compiler error with the supplied Stan function and compare the generated wrapper with the expected no-error behavior. Done means functions that modify target and end in _lp compile through expose_stan_functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.