Skip to content

Commit b92b5e3

Browse files
cknittclaude
andcommitted
Keep @deprecated on let bindings in inferred signatures
A module without an interface file gets its signature from its implementation. Values bound by `let` entered that signature without their attributes, so `@deprecated` on a `let` had no effect on other modules, while `@deprecated` externals, types and modules warned. The signature item of each let-bound value now carries the binding's `@deprecated` attributes. Uses inside the defining structure still do not warn; uses from other modules, and from outside a submodule, do. Example (rescript): // Legacy.res @deprecated("Use newF instead") let oldF = x => x + 1 // User.res let a = Legacy.oldF(1) Diagnostic (text): Warning number 3 User.res:1:9-19 deprecated: Legacy.oldF Use newF instead Review: Only the `deprecated` attributes are copied, so docstrings and other binding attributes stay out of the `.cmi` and gentype, analysis and reanalyze see the same value descriptions as before. Review: An implementation with `@deprecated let x` checked against an interface whose `let x` lacks it now gets warning 3 ("deprecated: x"), as `@deprecated` externals, types and modules already do. Review: The issue's duplicate warnings come from `translate_unified_ops` typing an operand of `+` and the other unified operators twice; any warning inside that operand repeats. This change leaves that alone. Area: typechecker tests Output: unchanged Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged in the analysis fixtures; values the analysis reads from a module type (`Sig_value`, e.g. through `include`) can now show as deprecated Tests: +tests/build_tests/super_errors_multi/fixtures/Cross_deprecated_res_items +tests/build_tests/super_errors_multi/expected/Cross_deprecated_res_items.expected Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent b7c67d2 commit b92b5e3

7 files changed

Lines changed: 96 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Warn when another module uses a `@deprecated` `let` binding from a module without an interface file.

‎compiler/ml/builtin_attributes.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ let rec deprecated_of_attrs = function
6262
| ({txt = "deprecated"; _}, p) :: _ -> Some (string_of_opt_payload p)
6363
| _ :: tl -> deprecated_of_attrs tl
6464

65+
let deprecated_attributes attrs =
66+
List.filter (fun ({txt}, _) -> txt = "deprecated") attrs
67+
6568
let rec deprecated_of_attrs_with_migrate = function
6669
| [] -> None
6770
| ({txt = "deprecated"; _}, payload) :: _ -> (

‎compiler/ml/builtin_attributes.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ val check_deprecated_inclusion :
4040
string ->
4141
unit
4242
val deprecated_of_attrs : Parsetree.attributes -> string option
43+
44+
val deprecated_attributes : Parsetree.attributes -> Parsetree.attributes
45+
(** The [deprecated] attributes in a list *)
46+
4347
val deprecated_of_sig : Parsetree.signature -> string option
4448
val deprecated_of_str : Parsetree.structure -> string option
4549

‎compiler/ml/typemod.ml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,11 +1398,33 @@ and type_structure ?(toplevel = false) funct_body anchor env sstr =
13981398
let () =
13991399
if rec_flag = Recursive then Rec_check.check_recursive_bindings defs
14001400
in
1401+
(* The environment keeps let-bound values without attributes, so uses
1402+
in this module do not warn. The signature carries their [deprecated]
1403+
attributes, so uses from other modules do. *)
1404+
let deprecated =
1405+
List.concat_map
1406+
(fun (vb : value_binding) ->
1407+
match Builtin_attributes.deprecated_attributes vb.vb_attributes with
1408+
| [] -> []
1409+
| attrs ->
1410+
List.map (fun id -> (id, attrs)) (pat_bound_idents vb.vb_pat))
1411+
defs
1412+
in
14011413
(* Note: Env.find_value does not trigger the value_used event. Values
14021414
will be marked as being used during the signature inclusion test. *)
14031415
( Tstr_value (rec_flag, defs),
14041416
List.map
1405-
(fun id -> Sig_value (id, Env.find_value (Pident id) newenv))
1417+
(fun id ->
1418+
let vd = Env.find_value (Pident id) newenv in
1419+
let vd =
1420+
match
1421+
List.find_opt (fun (id', _) -> Ident.same id id') deprecated
1422+
with
1423+
| None -> vd
1424+
| Some (_, attrs) ->
1425+
{vd with val_attributes = attrs @ vd.val_attributes}
1426+
in
1427+
Sig_value (id, vd))
14061428
(let_bound_idents defs),
14071429
newenv )
14081430
| Pstr_primitive sdesc ->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
===== User.res =====
2+
3+
Warning number 3
4+
/.../fixtures/Cross_deprecated_res_items/User.res:1:9-19
5+
6+
1 │ let a = Legacy.oldF(1)
7+
2 │ let b = Legacy.compute(1)
8+
3 │ let c = Legacy.OldM.v
9+
10+
deprecated: Legacy.oldF
11+
Use newF instead
12+
13+
14+
Warning number 3
15+
/.../fixtures/Cross_deprecated_res_items/User.res:2:9-22
16+
17+
1 │ let a = Legacy.oldF(1)
18+
2 │ let b = Legacy.compute(1)
19+
3 │ let c = Legacy.OldM.v
20+
4 │ let d: Legacy.oldT = 1
21+
22+
deprecated: Legacy.compute
23+
Use computeNew instead
24+
25+
26+
Warning number 3
27+
/.../fixtures/Cross_deprecated_res_items/User.res:3:9-21
28+
29+
1 │ let a = Legacy.oldF(1)
30+
2 │ let b = Legacy.compute(1)
31+
3 │ let c = Legacy.OldM.v
32+
4 │ let d: Legacy.oldT = 1
33+
5 │
34+
35+
deprecated: module Legacy.OldM
36+
Use NewM instead
37+
38+
39+
Warning number 3
40+
/.../fixtures/Cross_deprecated_res_items/User.res:4:8-18
41+
42+
2 │ let b = Legacy.compute(1)
43+
3 │ let c = Legacy.OldM.v
44+
4 │ let d: Legacy.oldT = 1
45+
5 │
46+
47+
deprecated: Legacy.oldT
48+
Use newT instead
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@deprecated("Use newF instead")
2+
let oldF = x => x + 1
3+
4+
@deprecated("Use computeNew instead") @val
5+
external compute: int => int = "compute"
6+
7+
@deprecated("Use NewM instead")
8+
module OldM = {
9+
let v = 1
10+
}
11+
12+
@deprecated("Use newT instead")
13+
type oldT = int
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let a = Legacy.oldF(1)
2+
let b = Legacy.compute(1)
3+
let c = Legacy.OldM.v
4+
let d: Legacy.oldT = 1

0 commit comments

Comments
 (0)