Skip to content

Commit 5cdfd41

Browse files
authored
Merge pull request #1061 from stan-dev/var-fresh-fix
Properly verify a variable name isn't in use
2 parents 5b28a17 + d08e179 commit 5cdfd41

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/frontend/Typechecker.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ let verify_name_fresh_var loc tenv name =
110110
if Utils.is_unnormalized_distribution name then
111111
Semantic_error.ident_has_unnormalized_suffix loc name |> error
112112
else if
113-
Env.mem tenv name
114-
&& not (Stan_math_signatures.is_stan_math_function_name name)
113+
List.exists (Env.find tenv name) ~f:(function
114+
| {kind= `StanMath; _} ->
115+
false (* user variables can shadow library names *)
116+
| _ -> true )
115117
then Semantic_error.ident_in_use loc name |> error
116118

117119
(** verify that the variable being declared is previous unused.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
generated quantities {
2+
vector[3] e;
3+
// e is selected because it is also a Stan-math function 'e()'
4+
vector[3] e;
5+
}

test/integration/bad/lang/stanc.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,17 @@ Expected an expression inside "[" and "]" but found a statement. Indexing should
696696
var[1 + i]
697697
not
698698
var[for (n in 1:N) ...]
699+
$ ../../../../../install/default/bin/stanc reused_name.stan
700+
Semantic error in 'reused_name.stan', line 4, column 14 to column 15:
701+
-------------------------------------------------
702+
2: vector[3] e;
703+
3: // e is selected because it is also a Stan-math function 'e()'
704+
4: vector[3] e;
705+
^
706+
5: }
707+
-------------------------------------------------
708+
709+
Identifier 'e' is already in use.
699710
$ ../../../../../install/default/bin/stanc unterminated_comment.stan
700711
Syntax error in 'unterminated_comment.stan', line 4, column -1, lexing error:
701712
-------------------------------------------------

0 commit comments

Comments
 (0)