Skip to content

Commit 146fee2

Browse files
authored
Merge pull request #1048 from stan-dev/future-keywords
Warn about future reserved words
2 parents 65b9c6a + c4250b4 commit 146fee2

7 files changed

Lines changed: 81 additions & 28 deletions

File tree

src/frontend/Ast.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ let rec id_of_lvalue {lval; _} =
296296
token before the current statement and all the whitespace between two statements
297297
appears as if it were part of the second statement.
298298
get_first_loc tries to skip the leading whitespace and approximate the location
299-
of the first token in the statement. *)
299+
of the first token in the statement.
300+
TODO: See if $sloc works better than $loc for this
301+
*)
300302

301303
let rec get_loc_expr (e : untyped_expression) =
302304
match e.expr with

src/frontend/Input_warnings.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ let deprecated token (pos, message) =
1919
Middle.Location_span.of_positions_opt begin_pos end_pos
2020
|> Option.value ~default:Middle.Location_span.empty in
2121
add_warning span message
22+
23+
let drop_array_future () =
24+
match !warnings with
25+
| ( _
26+
, "Variable name 'array' will be a reserved word starting in Stan 2.32.0. \
27+
Please rename it!" )
28+
:: tl ->
29+
warnings := tl
30+
| _ -> ()
31+
32+
let future_keyword kwrd version (pos1, pos2) =
33+
add_warning
34+
(Option.value ~default:Middle.Location_span.empty
35+
(Middle.Location_span.of_positions_opt pos1 pos2) )
36+
( "Variable name '" ^ kwrd ^ "' will be a reserved word starting in Stan "
37+
^ version ^ ". Please rename it!" )

src/frontend/Input_warnings.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ val deprecated : string -> Lexing.position * string -> unit
1717

1818
val empty : string -> unit
1919
(** Register that an empty file is being lexxed *)
20+
21+
val future_keyword :
22+
string -> string -> Lexing.position * Lexing.position -> unit
23+
(** Warn on a keyword which will be reserved in the future*)
24+
25+
val drop_array_future : unit -> unit
26+
(** Hack: Remove the most recent warning about array as a future keyword.
27+
Needed due to the {e other} hack of how we currently parse arrays.
28+
*)

src/frontend/parser.mly

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ let rec iterate_n f x = function
2121
| n -> iterate_n f (f x) (n - 1)
2222
let nest_unsized_array basic_type n =
2323
iterate_n (fun t -> UnsizedType.UArray t) basic_type n
24+
25+
(* $sloc and $symbolstartpos generates code using !=, which
26+
Core_kernel considers to be an error.
27+
*)
28+
let (!=) = Stdlib.(!=)
2429
%}
2530

2631
%token FUNCTIONBLOCK DATABLOCK TRANSFORMEDDATABLOCK PARAMETERSBLOCK
@@ -93,7 +98,7 @@ program:
9398
let () =
9499
match (ofb, odb, otdb, opb, otpb, omb, ogb) with
95100
| None, None, None, None, None, None, None ->
96-
Input_warnings.empty (fst $loc).pos_fname
101+
Input_warnings.empty ($startpos).pos_fname
97102
| _ -> ()
98103
in
99104
{ functionblock= ofb
@@ -160,11 +165,19 @@ generated_quantities_block:
160165
identifier:
161166
| id=IDENTIFIER { build_id id $loc }
162167
| TRUNCATE { build_id "T" $loc}
163-
| OFFSET { build_id "offset" $loc}
164-
| MULTIPLIER { build_id "multiplier" $loc}
165-
| LOWER { build_id "lower" $loc}
166-
| UPPER { build_id "upper" $loc}
167-
| ARRAY { build_id "array" $loc}
168+
| id_and_v = future_keyword
169+
{
170+
let id, v = id_and_v in
171+
Input_warnings.future_keyword id.name v $loc;
172+
id
173+
}
174+
175+
future_keyword:
176+
| OFFSET { build_id "offset" $loc, "2.32.0" }
177+
| MULTIPLIER { build_id "multiplier" $loc, "2.32.0" }
178+
| LOWER { build_id "lower" $loc, "2.32.0" }
179+
| UPPER { build_id "upper" $loc, "2.32.0" }
180+
| ARRAY { build_id "array" $loc, "2.32.0" }
168181

169182
decl_identifier:
170183
| id=identifier { id }
@@ -342,6 +355,7 @@ decl(type_rule, rhs):
342355
in
343356
let dims = match dims_opt with
344357
| Some ({expr= Indexed ({expr= Variable {name="array"; _}; _}, ixs); _}) ->
358+
Input_warnings.drop_array_future () ;
345359
(match int_ixs ixs with
346360
| Some sizes -> sizes
347361
| None -> error "Dimensions should be expressions, not multiple or range indexing.")
@@ -358,27 +372,7 @@ decl(type_rule, rhs):
358372
; is_global
359373
}
360374
; smeta= {
361-
loc=
362-
(* From the docs:
363-
We remark that, if the current production has an empty right-hand side,
364-
then $startpos and $endpos are equal, and (by convention) are the end
365-
position of the most recently parsed symbol (that is, the symbol that
366-
happens to be on top of the automaton’s stack when this production is
367-
reduced). If the current production has a nonempty right-hand side,
368-
then $startpos is the same as $startpos($1) and $endpos is the same
369-
as $endpos($n), where n is the length of the right-hand side.
370-
371-
372-
So when dims_opt is empty, it uses the preview token as its startpos,
373-
but that makes the whole declaration think it starts at the previous
374-
token. Sadly, $sloc and $symbolstartpos generates code using !=, which
375-
Core_kernel considers to be an error.
376-
*)
377-
let startpos = match dims_opt with
378-
| None -> $startpos(ty)
379-
| Some _ -> $startpos
380-
in
381-
Location_span.of_positions_exn (startpos, $endpos)
375+
loc= Location_span.of_positions_exn $sloc
382376
}
383377
})
384378
)}

test/integration/bad/lang/stanc.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ Syntax error in 'incomplete.stan', line 2, column 18 to column 19, parsing error
630630

631631
Found an incomplete binary expression - are you missing the right hand side?
632632
$ ../../../../../install/default/bin/stanc incomplete2.stan
633+
Warning in 'incomplete2.stan', line 2, column 9: Variable name 'upper' will
634+
be a reserved word starting in Stan 2.32.0. Please rename it!
633635
Syntax error in 'incomplete2.stan', line 2, column 17 to column 22, parsing error:
634636
-------------------------------------------------
635637
1: transformed data {
@@ -679,6 +681,8 @@ Syntax error in 'incomplete5.stan', line 3, column 0 to column 0, parsing error:
679681

680682
Ill-formed block. Expected a statement, variable declaration, or just "}".
681683
$ ../../../../../install/default/bin/stanc incomplete6.stan
684+
Warning in 'incomplete6.stan', line 2, column 8: Variable name 'upper' will
685+
be a reserved word starting in Stan 2.32.0. Please rename it!
682686
Syntax error in 'incomplete6.stan', line 3, column 16 to column 21, parsing error:
683687
-------------------------------------------------
684688
1: transformed data {

test/integration/bad/new/stanc.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,9 @@ Syntax error in 'ill-formed-statement23.stan', line 1, column 35 to column 39, p
11981198

11991199
Ill-formed statement. Expected statement after "else".
12001200
$ ../../../../../install/default/bin/stanc ill-formed-statement24.stan
1201+
Warning in 'ill-formed-statement24.stan', line 1, column 30: Variable name
1202+
'upper' will be a reserved word starting in Stan 2.32.0. Please rename
1203+
it!
12011204
Syntax error in 'ill-formed-statement24.stan', line 1, column 35 to column 36, parsing error:
12021205
-------------------------------------------------
12031206
1: transformed data { if ( 1 ) ; upper}
@@ -1214,6 +1217,9 @@ Syntax error in 'ill-formed-statement25.stan', line 1, column 34 to column 38, p
12141217

12151218
Ill-formed statement. Expected statement after else.
12161219
$ ../../../../../install/default/bin/stanc ill-formed-statement26.stan
1220+
Warning in 'ill-formed-statement26.stan', line 1, column 29: Variable name
1221+
'upper' will be a reserved word starting in Stan 2.32.0. Please rename
1222+
it!
12171223
Syntax error in 'ill-formed-statement26.stan', line 1, column 34 to column 34, parsing error:
12181224
-------------------------------------------------
12191225
1: transformed data { if ( T) ; upper

test/integration/good/pretty.expected

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,11 @@ Warning in 'deprecated_syntax.stan', line 40, column 2: increment_log_prob(...);
30233023
Warning in 'deprecated_syntax.stan', line 42, column 11: get_lp() function is
30243024
deprecated. It will be removed in Stan 2.32.0. Use target() instead. This
30253025
can be done automatically with stanc --print-canonical
3026+
Warning in 'deprecated_syntax.stan', line 46, column 6: Variable name
3027+
'offset' will be a reserved word starting in Stan 2.32.0. Please rename
3028+
it!
3029+
Warning in 'deprecated_syntax.stan', line 47, column 6: Variable name 'array'
3030+
will be a reserved word starting in Stan 2.32.0. Please rename it!
30263031
Warning in 'deprecated_syntax.stan', line 51, column 2: Comments beginning
30273032
with # are deprecated and this syntax will be removed in Stan 2.32.0. Use
30283033
// to begin line comments; this can be done automatically using stanc
@@ -3942,6 +3947,14 @@ data {
39423947
int offset;
39433948
}
39443949

3950+
Warning in 'identifiers.stan', line 2, column 6: Variable name 'upper' will
3951+
be a reserved word starting in Stan 2.32.0. Please rename it!
3952+
Warning in 'identifiers.stan', line 3, column 6: Variable name 'lower' will
3953+
be a reserved word starting in Stan 2.32.0. Please rename it!
3954+
Warning in 'identifiers.stan', line 4, column 6: Variable name 'multiplier'
3955+
will be a reserved word starting in Stan 2.32.0. Please rename it!
3956+
Warning in 'identifiers.stan', line 5, column 6: Variable name 'offset' will
3957+
be a reserved word starting in Stan 2.32.0. Please rename it!
39453958
$ ../../../../install/default/bin/stanc --auto-format if-else-formatting.stan
39463959
generated quantities {
39473960
// make sure pp_recursive_ifthenelse properly places else
@@ -6289,6 +6302,15 @@ model {
62896302
array[1, 2, 3] real abc;
62906303
}
62916304

6305+
Warning in 'unreserved-array-keyword.stan', line 2, column 24: Variable name
6306+
'array' will be a reserved word starting in Stan 2.32.0. Please rename
6307+
it!
6308+
Warning in 'unreserved-array-keyword.stan', line 2, column 40: Variable name
6309+
'array' will be a reserved word starting in Stan 2.32.0. Please rename
6310+
it!
6311+
Warning in 'unreserved-array-keyword.stan', line 6, column 7: Variable name
6312+
'array' will be a reserved word starting in Stan 2.32.0. Please rename
6313+
it!
62926314
$ ../../../../install/default/bin/stanc --auto-format user-defined-lpdf-fun.stan
62936315
functions {
62946316
real bar_lpmf(int y, real z) {

0 commit comments

Comments
 (0)