Skip to content

Commit 1e5e23a

Browse files
authored
flambda-backend: Add hint for common misplaced [@unboxed] attribute (#1164)
1 parent 10f870a commit 1e5e23a

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

testsuite/tests/typing-unboxed-types/test.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ Error: Signature mismatch:
128128
the first declaration uses unboxed representation.
129129
|}];;
130130

131+
module M' : sig
132+
type t = A of string [@ocaml.unboxed]
133+
end = struct
134+
type t = A of string [@@ocaml.unboxed]
135+
end;;
136+
[%%expect{|
137+
Lines 3-5, characters 6-3:
138+
3 | ......struct
139+
4 | type t = A of string [@@ocaml.unboxed]
140+
5 | end..
141+
Error: Signature mismatch:
142+
Modules do not match:
143+
sig type t = A of string [@@unboxed] end
144+
is not included in
145+
sig type t = A of string end
146+
Type declarations do not match:
147+
type t = A of string [@@unboxed]
148+
is not included in
149+
type t = A of string
150+
Their internal representations differ:
151+
the first declaration uses unboxed representation.
152+
Hint: the second declaration has [@unboxed]. Did you mean [@@unboxed]?
153+
|}];;
154+
131155
module N : sig
132156
type t = A of string [@@ocaml.unboxed]
133157
end = struct

typing/includecore.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ type type_mismatch =
226226
| Variance
227227
| Record_mismatch of record_mismatch
228228
| Variant_mismatch of variant_change list
229-
| Unboxed_representation of position
229+
| Unboxed_representation of position * attributes
230230
| Immediate of Type_immediacy.Violation.t
231231

232232
let report_locality_mismatch first second ppf err =
@@ -469,10 +469,13 @@ let report_type_mismatch first second decl env ppf err =
469469
report_record_mismatch first second decl env ppf err
470470
| Variant_mismatch err ->
471471
report_patch pp_variant_diff first second decl env ppf err
472-
| Unboxed_representation ord ->
472+
| Unboxed_representation (ord, attrs) ->
473473
pr "Their internal representations differ:@ %s %s %s."
474474
(choose ord first second) decl
475-
"uses unboxed representation"
475+
"uses unboxed representation";
476+
if Builtin_attributes.has_unboxed attrs then
477+
pr "@ Hint: %s %s has [%@unboxed]. Did you mean [%@%@unboxed]?"
478+
(choose ord second first) decl
476479
| Immediate violation ->
477480
let first = StringLabels.capitalize_ascii first in
478481
match violation with
@@ -618,8 +621,8 @@ module Record_diffing = struct
618621
else
619622
match rep1, rep2 with
620623
| Record_unboxed _, Record_unboxed _ -> None
621-
| Record_unboxed _, _ -> Some (Unboxed_representation First)
622-
| _, Record_unboxed _ -> Some (Unboxed_representation Second)
624+
| Record_unboxed _, _ -> Some (Unboxed_representation (First, []))
625+
| _, Record_unboxed _ -> Some (Unboxed_representation (Second, []))
623626

624627
| Record_float, Record_float -> None
625628
| Record_float, _ ->
@@ -766,16 +769,21 @@ module Variant_diffing = struct
766769
cstrs1 cstrs2 rep1 rep2
767770
=
768771
let err = compare ~loc env params1 params2 cstrs1 cstrs2 in
772+
let attrs_of_only cstrs =
773+
match cstrs with
774+
| [cstr] -> cstr.Types.cd_attributes
775+
| _ -> []
776+
in
769777
match err, rep1, rep2 with
770778
| None, Variant_regular, Variant_regular
771779
| None, Variant_unboxed, Variant_unboxed ->
772780
None
773781
| Some err, _, _ ->
774782
Some (Variant_mismatch err)
775783
| None, Variant_unboxed, Variant_regular ->
776-
Some (Unboxed_representation First)
784+
Some (Unboxed_representation (First, attrs_of_only cstrs2))
777785
| None, Variant_regular, Variant_unboxed ->
778-
Some (Unboxed_representation Second)
786+
Some (Unboxed_representation (Second, attrs_of_only cstrs1))
779787
end
780788

781789
(* Inclusion between "private" annotations *)

typing/includecore.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type type_mismatch =
104104
| Variance
105105
| Record_mismatch of record_mismatch
106106
| Variant_mismatch of variant_change list
107-
| Unboxed_representation of position
107+
| Unboxed_representation of position * attributes
108108
| Immediate of Type_immediacy.Violation.t
109109

110110
val value_descriptions:

0 commit comments

Comments
 (0)