Skip to content

Commit 02c8663

Browse files
committed
Drive by: Unify checks for function call statements with expressions
1 parent ddf5c6a commit 02c8663

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

src/frontend/Typechecker.ml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,6 @@ let check_expression_of_scalar_or_type cf tenv t e name =
914914

915915
(* -- Statements ------------------------------------------------- *)
916916
(* non returning functions *)
917-
let verify_nrfn_target loc cf id =
918-
if
919-
String.is_suffix id.name ~suffix:"_lp"
920-
&& not
921-
(in_lp_function cf || cf.current_block = Model
922-
|| cf.current_block = TParam)
923-
then Semantic_error.target_plusequals_outside_model_or_logprob loc |> error
924917

925918
let check_nrfn loc tenv id es =
926919
match Env.find tenv id.name with
@@ -961,7 +954,9 @@ let check_nrfn loc tenv id es =
961954
let check_nr_fn_app loc cf tenv id es =
962955
let tes = List.map ~f:(check_expression cf tenv) es in
963956
verify_identifier id;
964-
verify_nrfn_target loc cf id;
957+
verify_fn_target_plus_equals cf loc id;
958+
verify_fn_jacobian_plus_equals cf loc id;
959+
verify_fn_rng cf loc id;
965960
check_nrfn loc tenv id tes
966961

967962
(* target plus-equals / jacobian plus-equals *)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
functions {
2+
// void return type to check function statement, rather than expression
3+
void foo_jacobian() {
4+
jacobian += 1;
5+
}
6+
}
7+
transformed data {
8+
foo_jacobian();
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
functions {
2+
void foo_rng(real x){
3+
print(normal_rng(0,x));
4+
}
5+
}
6+
7+
model {
8+
foo_rng(1.0);
9+
}

test/integration/bad/stanc.expected

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,18 @@ Semantic error in 'err-jacobian-plusequals-scope3.stan', line 14, column 11 to c
10831083
15: }
10841084
-------------------------------------------------
10851085

1086+
The jacobian adjustment can only be applied in the transformed parameters block or in functions ending with _jacobian
1087+
[exit 1]
1088+
$ ../../../../install/default/bin/stanc err-jacobian-plusequals-scope4.stan
1089+
Semantic error in 'err-jacobian-plusequals-scope4.stan', line 8, column 2 to column 17:
1090+
-------------------------------------------------
1091+
6: }
1092+
7: transformed data {
1093+
8: foo_jacobian();
1094+
^
1095+
9: }
1096+
-------------------------------------------------
1097+
10861098
The jacobian adjustment can only be applied in the transformed parameters block or in functions ending with _jacobian
10871099
[exit 1]
10881100
$ ../../../../install/default/bin/stanc err-minus-types.stan
@@ -1245,6 +1257,18 @@ Syntax error in 'err-transformed-params.stan', line 4, column 0 to column 11, pa
12451257
-------------------------------------------------
12461258

12471259
"transformed parameters {", "model {" or "generated quantities {" expected after end of parameters block.
1260+
[exit 1]
1261+
$ ../../../../install/default/bin/stanc err_void_rng_check.stan
1262+
Semantic error in 'err_void_rng_check.stan', line 8, column 4 to column 17:
1263+
-------------------------------------------------
1264+
6:
1265+
7: model {
1266+
8: foo_rng(1.0);
1267+
^
1268+
9: }
1269+
-------------------------------------------------
1270+
1271+
Random number generators are only allowed in transformed data block, generated quantities block or user-defined functions with names ending in _rng.
12481272
[exit 1]
12491273
$ ../../../../install/default/bin/stanc expect_statement_seq_close_brace.stan
12501274
Syntax error in 'expect_statement_seq_close_brace.stan', line 6, column 0 to column 0, parsing error:

test/integration/cli-args/warn-pedantic/stanc.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ Warning in 'jacobian_warning_user.stan', line 5, column 2: Left-hand side of
546546
using jacobian += in the transformed parameters block.
547547
[exit 0]
548548
$ ../../../../../install/default/bin/stanc --warn-pedantic lp_fun.stan
549+
Warning in 'lp_fun.stan', line 10, column 2: Using _lp functions in
550+
transformed parameters is deprecated and will be disallowed in Stan 2.39.
551+
Use an _jacobian function instead, as this allows change of variable
552+
adjustments which are conditionally enabled by the algorithms.
549553
Warning: The parameter y has 2 priors.
550554
[exit 0]
551555
$ ../../../../../install/default/bin/stanc --warn-pedantic missing-prior-false-alarm.stan

test/integration/good/pretty.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3991,6 +3991,10 @@ Warning in 'lp_transformed_param.stan', line 14, column 15: Using _lp
39913991
functions in transformed parameters is deprecated and will be disallowed
39923992
in Stan 2.39. Use an _jacobian function instead, as this allows change of
39933993
variable adjustments which are conditionally enabled by the algorithms.
3994+
Warning in 'lp_transformed_param.stan', line 15, column 2: Using _lp
3995+
functions in transformed parameters is deprecated and will be disallowed
3996+
in Stan 2.39. Use an _jacobian function instead, as this allows change of
3997+
variable adjustments which are conditionally enabled by the algorithms.
39943998
[exit 0]
39953999
$ ../../../../install/default/bin/stanc --auto-format map_rect.stan
39964000
functions {

0 commit comments

Comments
 (0)