Skip to content

Commit 1847203

Browse files
spotandjakephated
andauthored
chore(compiler): Improve unsafe equality warning (#1892)
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent 18b553a commit 1847203

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

compiler/src/typed/typed_well_formedness.re

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ let rec exp_is_wasm_unsafe = ({exp_type}) => {
3737
};
3838
type_is_wasm_unsafe(exp_type);
3939
};
40+
let rec resolve_unsafe_type = ({exp_type}) => {
41+
let rec type_is_wasm_unsafe = t => {
42+
switch (t.desc) {
43+
| TTyConstr(path, _, _) =>
44+
switch (path) {
45+
| t when t == Builtin_types.path_wasmi32 => "WasmI32"
46+
| t when t == Builtin_types.path_wasmi64 => "WasmI64"
47+
| t when t == Builtin_types.path_wasmf32 => "WasmF32"
48+
| t when t == Builtin_types.path_wasmf64 => "WasmI64"
49+
| _ => failwith("Impossible: type cannot be a wasm unsafe value")
50+
}
51+
| TTyLink(t) => type_is_wasm_unsafe(t)
52+
| _ => failwith("Impossible: type cannot be a wasm unsafe value")
53+
};
54+
};
55+
type_is_wasm_unsafe(exp_type);
56+
};
4057

4158
let is_marked_unsafe = attrs => {
4259
// Disable_gc implies Unsafe
@@ -228,9 +245,16 @@ module WellFormednessArg: TypedtreeIter.IteratorArgument = {
228245
)
229246
when func == "==" || func == "!=" =>
230247
if (List.exists(((_, arg)) => exp_is_wasm_unsafe(arg), args)) {
248+
let typeName =
249+
switch (args) {
250+
| [(_, arg), _] => resolve_unsafe_type(arg)
251+
| _ => "(WasmI32 | WasmI64 | WasmF32 | WasmF64)"
252+
};
231253
let warning =
232254
Grain_utils.Warnings.FuncWasmUnsafe(
233255
Printf.sprintf("Pervasives.(%s)", func),
256+
Printf.sprintf("%s.(%s)", typeName, func),
257+
typeName,
234258
);
235259
if (Grain_utils.Warnings.is_active(warning)) {
236260
Grain_parsing.Location.prerr_warning(exp_loc, warning);

compiler/src/utils/warnings.re

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type t =
2323
| UnreachableCase
2424
| ShadowConstructor(string)
2525
| NoCmiFile(string, option(string))
26-
| FuncWasmUnsafe(string)
26+
| FuncWasmUnsafe(string, string, string)
2727
| FromNumberLiteralI32(string)
2828
| FromNumberLiteralI64(string)
2929
| FromNumberLiteralU32(string)
@@ -52,7 +52,7 @@ let number =
5252
| NoCmiFile(_) => 14
5353
| NonClosedRecordPattern(_) => 15
5454
| UnusedExtension => 16
55-
| FuncWasmUnsafe(_) => 17
55+
| FuncWasmUnsafe(_, _, _) => 17
5656
| FromNumberLiteralI32(_) => 18
5757
| FromNumberLiteralI64(_) => 19
5858
| FromNumberLiteralU32(_) => 20
@@ -119,10 +119,14 @@ let message =
119119
)
120120
| NonClosedRecordPattern(s) =>
121121
"the following fields are missing from the record pattern: " ++ s
122-
| FuncWasmUnsafe(func) =>
122+
| FuncWasmUnsafe(func, f, m) =>
123123
"it looks like you are using "
124124
++ func
125-
++ " on two unsafe Wasm values here.\nThis is generally unsafe and will cause errors. Use one of the equivalent functions in `WasmI32`, `WasmI64`, `WasmF32`, or `WasmF64` instead."
125+
++ " on two unsafe Wasm values here.\nThis is generally unsafe and will cause errors. Use "
126+
++ f
127+
++ " from the `"
128+
++ m
129+
++ "` module the instead."
126130
| FromNumberLiteralI32(n) =>
127131
Printf.sprintf(
128132
"it looks like you are calling Int32.fromNumber() with a constant number. Try using the literal syntax (e.g. `%sl`) instead.",
@@ -195,7 +199,7 @@ let defaults = [
195199
UnreachableCase,
196200
ShadowConstructor(""),
197201
NoCmiFile("", None),
198-
FuncWasmUnsafe(""),
202+
FuncWasmUnsafe("", "", ""),
199203
FromNumberLiteralI32(""),
200204
FromNumberLiteralI64(""),
201205
FromNumberLiteralU32(""),

compiler/src/utils/warnings.rei

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type t =
3737
| UnreachableCase
3838
| ShadowConstructor(string)
3939
| NoCmiFile(string, option(string))
40-
| FuncWasmUnsafe(string)
40+
| FuncWasmUnsafe(string, string, string)
4141
| FromNumberLiteralI32(string)
4242
| FromNumberLiteralI64(string)
4343
| FromNumberLiteralU32(string)

0 commit comments

Comments
 (0)