Skip to content

Commit 25ac971

Browse files
authored
Merge pull request #1568 from stan-dev/feat/pedantic-warnings-filename
Include locations in all pedantic warnings
2 parents 8317595 + 5974386 commit 25ac971

File tree

11 files changed

+694
-620
lines changed

11 files changed

+694
-620
lines changed

src/analysis_and_optimization/Dataflow_utils.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ let merge_set_maps m1 m2 =
2424
| `Both (e1, e2) -> Some (Set.union e1 e2) in
2525
Map.Poly.merge ~f:merge_map_elems m1 m2
2626

27-
(**
28-
Generate a Map by applying a function to each element of a key set.
29-
*)
30-
let generate_map s ~f =
31-
Set.fold s ~init:Map.Poly.empty ~f:(fun m e ->
32-
Map.Poly.add_exn m ~key:e ~data:(f e))
33-
3427
(**
3528
Like a forward traversal, but branches accumulate two different states that are
3629
recombined with join.

src/analysis_and_optimization/Dataflow_utils.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ val merge_set_maps :
6969
Merge two maps whose values are sets, and union the sets when there's a collision.
7070
*)
7171

72-
val generate_map : 'a Set.Poly.t -> f:('a -> 'b) -> ('a, 'b) Map.Poly.t
73-
(**
74-
Generate a Map by applying a function to each element of a key set.
75-
*)
76-
7772
val build_statement_map :
7873
('s -> ('e, 's) Stmt.Pattern.t)
7974
-> ('s -> 'm)

src/analysis_and_optimization/Factor_graph.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,19 @@ let fg_var_priors (var : vexpr) (data : vexpr Set.Poly.t) (fg : factor_graph) :
161161
| None -> None
162162

163163
let list_priors ?factor_graph:(fg_opt = None) (mir : Program.Typed.t) :
164-
(vexpr, (factor * label) Set.Poly.t option) Map.Poly.t =
164+
(vexpr, (factor * label) Set.Poly.t option * Location_span.t) Map.Poly.t =
165165
let fg = Option.value ~default:(prog_factor_graph mir) fg_opt in
166-
let params = Set.Poly.map ~f:(fun v -> VVar v) (parameter_names_set mir) in
166+
let params =
167+
Set.Poly.map ~f:(fun (v, _, loc) -> (VVar v, loc)) (parameter_set mir) in
167168
let data = Set.Poly.map ~f:(fun v -> VVar v) (data_set mir) in
168169
let likely_sizes =
169170
Set.diff data
170171
(Set.Poly.map ~f:(fun v -> VVar v) (data_set ~exclude_ints:true mir))
171172
in
172173
let fg' = Set.fold ~init:fg ~f:fg_remove_var likely_sizes in
173174
(* for each param, apply fg_var_priors and collect results in a map*)
174-
generate_map params ~f:(fun p -> fg_var_priors p data fg')
175+
Set.fold params ~init:Map.Poly.empty ~f:(fun m (p, loc) ->
176+
Map.Poly.add_exn m ~key:p ~data:(fg_var_priors p data fg', loc))
175177

176178
let string_of_factor (factor : factor) : string =
177179
match factor with

src/analysis_and_optimization/Mir_utils.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,17 @@ let data_set ?(exclude_transformed = false) ?(exclude_ints = false)
9191

9292
let parameter_set ?(include_transformed = false) (mir : Program.Typed.t) =
9393
Set.Poly.of_list
94-
(List.map
95-
~f:(fun (pname, _, {out_trans; _}) -> (pname, out_trans))
96-
(List.filter
97-
~f:(fun (_, _, {out_block; _}) ->
98-
out_block = Parameters
99-
|| (include_transformed && out_block = TransformedParameters))
100-
mir.output_vars))
94+
(List.filter_map
95+
~f:(fun (pname, l, {out_trans; out_block; _}) ->
96+
if
97+
out_block = Parameters
98+
|| (include_transformed && out_block = TransformedParameters)
99+
then Some (pname, out_trans, l)
100+
else None)
101+
mir.output_vars)
101102

102103
let parameter_names_set ?(include_transformed = false) (mir : Program.Typed.t) =
103-
Set.Poly.map ~f:fst (parameter_set ~include_transformed mir)
104+
Set.Poly.map ~f:fst3 (parameter_set ~include_transformed mir)
104105

105106
let rec var_declarations Stmt.{pattern; _} : string Set.Poly.t =
106107
match pattern with

src/analysis_and_optimization/Mir_utils.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ val data_set :
2222
val parameter_set :
2323
?include_transformed:bool
2424
-> Program.Typed.t
25-
-> (string * Expr.Typed.t Transformation.t) Set.Poly.t
25+
-> (string * Expr.Typed.t Transformation.t * Location_span.t) Set.Poly.t
2626

2727
val parameter_names_set :
2828
?include_transformed:bool -> Program.Typed.t -> string Set.Poly.t

src/analysis_and_optimization/Pedantic_analysis.ml

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ type warning_span = Location_span.t * string [@@deriving compare]
1717
********************)
1818

1919
let list_unused_params (factor_graph : factor_graph) (mir : Program.Typed.t) :
20-
string Set.Poly.t =
20+
(string * Location_span.t) Set.Poly.t =
2121
(* Build a factor graph of the program, check for missing parameters *)
22-
let params = parameter_names_set ~include_transformed:false mir in
22+
let param_info = parameter_set ~include_transformed:false mir in
23+
let params = Set.Poly.map ~f:fst3 param_info in
2324
let used_params =
2425
Set.Poly.map
2526
~f:(fun (VVar v) -> v)
2627
(Set.Poly.of_list (Map.Poly.keys factor_graph.var_map)) in
27-
Set.diff params used_params
28+
let unused = Set.diff params used_params in
29+
Set.Poly.filter_map
30+
~f:(fun (pname, _, loc) ->
31+
if Set.mem unused pname then Some (pname, loc) else None)
32+
param_info
2833

2934
let list_hard_constrained (mir : Program.Typed.t) :
30-
(string * [`HardConstraint | `NonsenseConstraint]) Set.Poly.t =
35+
(string * [`HardConstraint | `NonsenseConstraint] * Location_span.t)
36+
Set.Poly.t =
3137
(* Iterate through all parameters' transformations for hard constraints *)
3238
let constrained (e : bound_values) =
3339
match e with
@@ -37,9 +43,9 @@ let list_hard_constrained (mir : Program.Typed.t) :
3743
| {lower= `Lit _; upper= `Lit _} -> Some `HardConstraint
3844
| _ -> None in
3945
Set.Poly.filter_map
40-
~f:(fun (name, trans) ->
46+
~f:(fun (name, trans, loc) ->
4147
Option.map
42-
~f:(fun c -> (name, c))
48+
~f:(fun c -> (name, c, loc))
4349
(constrained (trans_bounds_values trans)))
4450
(parameter_set mir)
4551

@@ -261,30 +267,31 @@ let list_param_dependant_fundefs_cf (mir : Program.Typed.t) :
261267
(fun_def.fdname, cf_loc, deps, arg_name, arg_loc)))
262268

263269
let list_non_one_priors (fg : factor_graph) (mir : Program.Typed.t) :
264-
(string * int) Set.Poly.t =
270+
(string * int * Location_span.t) Set.Poly.t =
265271
(* Use the factor graph definition of priors, which treats a neighboring
266272
factor as a prior for parameter P if it has no connection to the data
267273
except through P *)
268274
let priors = list_priors ~factor_graph:(Some fg) mir in
269275
let prior_set =
270276
Map.Poly.fold priors ~init:Set.Poly.empty
271-
~f:(fun ~key:(VVar v) ~data:factors_opt s ->
277+
~f:(fun ~key:(VVar v) ~data:(factors_opt, loc) s ->
272278
Option.value_map factors_opt ~default:s ~f:(fun factors ->
273-
Set.add s (v, Set.length factors))) in
279+
Set.add s (v, Set.length factors, loc))) in
274280
(* Return only multi-prior parameters *)
275-
Set.filter prior_set ~f:(fun (_, n) -> n <> 1)
281+
Set.filter prior_set ~f:(fun (_, n, _) -> n <> 1)
276282

277283
(* Collect useful information about an expression that's available at
278284
compile-time into a convenient form. *)
279285
let compiletime_value_of_expr
280-
(params : (string * Expr.Typed.t Transformation.t) Set.Poly.t)
286+
(params :
287+
(string * Expr.Typed.t Transformation.t * Location_span.t) Set.Poly.t)
281288
(data : string Set.Poly.t) (expr : Expr.Typed.t) :
282289
compiletime_val * Expr.Typed.Meta.t =
283290
let v =
284291
match expr with
285292
| {pattern= Var pname; _} -> (
286-
match Set.find params ~f:(fun (name, _) -> name = pname) with
287-
| Some (name, trans) -> Param (name, trans)
293+
match Set.find params ~f:(fun (name, _, _) -> name = pname) with
294+
| Some (name, trans, _) -> Param (name, trans)
288295
| None -> (
289296
match Set.find data ~f:(fun name -> name = pname) with
290297
| Some name -> Data name
@@ -372,11 +379,10 @@ let hard_constrained_message (pname : string) : string =
372379
let hard_constrained_warnings (mir : Program.Typed.t) =
373380
let pnames = list_hard_constrained mir in
374381
Set.Poly.map
375-
~f:(fun (pname, c) ->
382+
~f:(fun (pname, c, loc) ->
376383
match c with
377-
| `HardConstraint -> (Location_span.empty, hard_constrained_message pname)
378-
| `NonsenseConstraint ->
379-
(Location_span.empty, nonsense_constrained_message pname))
384+
| `HardConstraint -> (loc, hard_constrained_message pname)
385+
| `NonsenseConstraint -> (loc, nonsense_constrained_message pname))
380386
pnames
381387

382388
let maybe_jacobian_adjustment_warnings (mir : Program.Typed.t) =
@@ -440,7 +446,7 @@ let unused_params_message (pname : string) : string =
440446
let unused_params_warnings (factor_graph : factor_graph) (mir : Program.Typed.t)
441447
=
442448
Set.Poly.map
443-
~f:(fun pname -> (Location_span.empty, unused_params_message pname))
449+
~f:(fun (pname, loc) -> (loc, unused_params_message pname))
444450
(list_unused_params factor_graph mir)
445451

446452
let non_one_priors_message (pname : string) (n : int) : string =
@@ -455,7 +461,7 @@ let non_one_priors_message (pname : string) (n : int) : string =
455461
let non_one_priors_warnings (factor_graph : factor_graph)
456462
(mir : Program.Typed.t) =
457463
Set.Poly.map
458-
~f:(fun (pname, n) -> (Location_span.empty, non_one_priors_message pname n))
464+
~f:(fun (pname, n, loc) -> (loc, non_one_priors_message pname n))
459465
(list_non_one_priors factor_graph mir)
460466

461467
let uninitialized_message (vname : string) : string =
@@ -500,4 +506,4 @@ let warn_pedantic (mir_unopt : Program.Typed.t) =
500506
; param_dependant_cf_warnings mir; param_dependant_fundef_cf_warnings mir
501507
; non_one_priors_warnings factor_graph mir
502508
; distribution_warnings distributions_info ]
503-
|> to_list
509+
|> to_list |> List.rev

src/middle/Location.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let pp_context_for ppf (({line_num; _} as loc), lines) =
3636
pp_line_and_number ppf (line_num + 2);
3737
bars ppf ()
3838

39-
let empty = {filename= ""; line_num= 0; col_num= 0; included_from= None}
39+
let empty = {filename= ""; line_num= -1; col_num= -1; included_from= None}
4040

4141
(**
4242
Format the location for error messaging.
@@ -48,8 +48,7 @@ let rec pp ?(print_file = true) ?(print_line = true) printed_filename ppf loc =
4848
let incl, filename =
4949
match loc.included_from with
5050
| Some loc2 ->
51-
( (fun ppf ->
52-
Fmt.pf ppf ", included from\n%a" (pp printed_filename) loc2)
51+
( Format.dprintf ", included from\n%a" (pp printed_filename) loc2
5352
, loc.filename )
5453
| None -> (ignore, Option.value ~default:loc.filename printed_filename)
5554
in

0 commit comments

Comments
 (0)