Skip to content

Commit 56b0a1f

Browse files
committed
Add warning for unknown annotations
1 parent 142c6f3 commit 56b0a1f

File tree

9 files changed

+57
-1
lines changed

9 files changed

+57
-1
lines changed

src/frontend/Annotations.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
open Ast
2+
open Core
3+
4+
let message a = "Unknown annotation '" ^ a ^ "' will be ignored by the compiler"
5+
6+
let unused_list pred id anns =
7+
List.filter_map
8+
~f:(fun a -> if pred a then None else Some (id.id_loc, message a))
9+
anns
10+
11+
let rec collect_stmt pred (acc : Warnings.t list) {stmt; _} =
12+
match stmt with
13+
| FunDef {annotations; funname; _} ->
14+
acc @ (unused_list pred funname) annotations
15+
| VarDecl {annotations; variables; _} ->
16+
acc @ (unused_list pred (List.hd_exn variables).identifier) annotations
17+
| _ -> fold_statement Fn.const (collect_stmt pred) Fn.const Fn.const acc stmt
18+
19+
let find_unrecognized (pred : string -> bool) (prog : Ast.untyped_program) :
20+
Warnings.t list =
21+
fold_program (collect_stmt pred) [] prog

src/frontend/Annotations.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val find_unrecognized :
2+
(string -> bool) -> Ast.untyped_program -> Warnings.t list

src/stan_math_backend/Transform_Mir.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,8 @@ let map_prog_stmt_lists f (p : ('a, 'b, 'c) Program.t) =
10121012
; transform_inits= f p.transform_inits
10131013
; unconstrain_array= f p.unconstrain_array }
10141014

1015+
let recognized_annotation _ = false
1016+
10151017
let trans_prog (p : Program.Typed.t) =
10161018
(* name mangling of c++ keywords*)
10171019
let rec map_stmt {Stmt.Fixed.pattern; meta} =

src/stan_math_backend/Transform_Mir.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ open Middle
77
val trans_prog : Program.Typed.t -> Program.Typed.t
88
val is_opencl_var : string -> bool
99
val use_opencl : bool ref
10+
val recognized_annotation : string -> bool

src/stanc/stanc.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ let use_file filename =
285285
get_ast_or_exit ?printed_filename filename
286286
~print_warnings:(not !canonicalize_settings.deprecations)
287287
~bare_functions:!bare_functions in
288+
let unused_annotations =
289+
Annotations.find_unrecognized Transform_Mir.recognized_annotation ast in
290+
Warnings.pp_warnings ?printed_filename Fmt.stderr unused_annotations;
288291
Debugging.ast_logger ast;
289292
let typed_ast = type_ast_or_exit ?printed_filename ast in
290293
let canonical_ast =

src/stancjs/stancjs.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ let stan2cpp model_name model_string is_flag_set flag_val includes :
3232
let open Result.Monad_infix in
3333
let result =
3434
ast >>= fun ast ->
35+
let unused_annotations =
36+
Annotations.find_unrecognized Transform_Mir.recognized_annotation ast
37+
in
3538
let typed_ast =
3639
Typechecker.check_program ast
3740
|> Result.map_error ~f:(fun e -> Errors.Semantic_error e) in
3841
typed_ast >>| fun (typed_ast, type_warnings) ->
39-
let warnings = parser_warnings @ type_warnings in
42+
let warnings = parser_warnings @ unused_annotations @ type_warnings in
4043
if is_flag_set "info" then
4144
r.return (Result.Ok (Info.info typed_ast), warnings, []);
4245
let canonicalizer_settings =

test/integration/good/lang/pretty.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ generated quantities {
3535
matrix[34, 10000] a_bit_too_long = rep_matrix(1, 34, 10000);
3636
}
3737

38+
Warning in 'good_annotations.stan', line 2, column 17: Unknown annotation
39+
'foo' will be ignored by the compiler
40+
Warning in 'good_annotations.stan', line 2, column 17: Unknown annotation
41+
'biz' will be ignored by the compiler
42+
Warning in 'good_annotations.stan', line 7, column 19: Unknown annotation
43+
'baz' will be ignored by the compiler
44+
Warning in 'good_annotations.stan', line 12, column 89: Unknown annotation
45+
'bar' will be ignored by the compiler
46+
Warning in 'good_annotations.stan', line 12, column 89: Unknown annotation
47+
'baz' will be ignored by the compiler
48+
Warning in 'good_annotations.stan', line 12, column 89: Unknown annotation
49+
'flux' will be ignored by the compiler
50+
Warning in 'good_annotations.stan', line 12, column 89: Unknown annotation
51+
'really_extra_long_now' will be ignored by the compiler
3852
$ ../../../../../install/default/bin/stanc --auto-format good_const.stan
3953
transformed data {
4054
real y;

test/integration/good/warning/pretty.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,13 @@ Warning in 'self-assign.stan', line 22, column 2: Assignment of variable to
589589
itself.
590590
Warning in 'self-assign.stan', line 24, column 2: Assignment of variable to
591591
itself during declaration. This is almost certainly a bug.
592+
$ ../../../../../install/default/bin/stanc --auto-format unknown_annotation.stan
593+
parameters {
594+
@not_a_real_annotation real unused;
595+
}
596+
597+
Warning in 'unknown_annotation.stan', line 2, column 31: Unknown annotation
598+
'not_a_real_annotation' will be ignored by the compiler
592599
$ ../../../../../install/default/bin/stanc --auto-format unreachable_statement.stan
593600
functions {
594601
void foo(real x) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters {
2+
@not_a_real_annotation real unused;
3+
}

0 commit comments

Comments
 (0)