Skip to content

Commit

Permalink
chore(compiler): Improve unsafe equality warning (#1892)
Browse files Browse the repository at this point in the history
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
spotandjake and phated committed Aug 22, 2023
1 parent 18b553a commit 1847203
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
24 changes: 24 additions & 0 deletions compiler/src/typed/typed_well_formedness.re
Expand Up @@ -37,6 +37,23 @@ let rec exp_is_wasm_unsafe = ({exp_type}) => {
};
type_is_wasm_unsafe(exp_type);
};
let rec resolve_unsafe_type = ({exp_type}) => {
let rec type_is_wasm_unsafe = t => {
switch (t.desc) {
| TTyConstr(path, _, _) =>
switch (path) {
| t when t == Builtin_types.path_wasmi32 => "WasmI32"
| t when t == Builtin_types.path_wasmi64 => "WasmI64"
| t when t == Builtin_types.path_wasmf32 => "WasmF32"
| t when t == Builtin_types.path_wasmf64 => "WasmI64"
| _ => failwith("Impossible: type cannot be a wasm unsafe value")
}
| TTyLink(t) => type_is_wasm_unsafe(t)
| _ => failwith("Impossible: type cannot be a wasm unsafe value")
};
};
type_is_wasm_unsafe(exp_type);
};

let is_marked_unsafe = attrs => {
// Disable_gc implies Unsafe
Expand Down Expand Up @@ -228,9 +245,16 @@ module WellFormednessArg: TypedtreeIter.IteratorArgument = {
)
when func == "==" || func == "!=" =>
if (List.exists(((_, arg)) => exp_is_wasm_unsafe(arg), args)) {
let typeName =
switch (args) {
| [(_, arg), _] => resolve_unsafe_type(arg)
| _ => "(WasmI32 | WasmI64 | WasmF32 | WasmF64)"
};
let warning =
Grain_utils.Warnings.FuncWasmUnsafe(
Printf.sprintf("Pervasives.(%s)", func),
Printf.sprintf("%s.(%s)", typeName, func),
typeName,
);
if (Grain_utils.Warnings.is_active(warning)) {
Grain_parsing.Location.prerr_warning(exp_loc, warning);
Expand Down
14 changes: 9 additions & 5 deletions compiler/src/utils/warnings.re
Expand Up @@ -23,7 +23,7 @@ type t =
| UnreachableCase
| ShadowConstructor(string)
| NoCmiFile(string, option(string))
| FuncWasmUnsafe(string)
| FuncWasmUnsafe(string, string, string)
| FromNumberLiteralI32(string)
| FromNumberLiteralI64(string)
| FromNumberLiteralU32(string)
Expand Down Expand Up @@ -52,7 +52,7 @@ let number =
| NoCmiFile(_) => 14
| NonClosedRecordPattern(_) => 15
| UnusedExtension => 16
| FuncWasmUnsafe(_) => 17
| FuncWasmUnsafe(_, _, _) => 17
| FromNumberLiteralI32(_) => 18
| FromNumberLiteralI64(_) => 19
| FromNumberLiteralU32(_) => 20
Expand Down Expand Up @@ -119,10 +119,14 @@ let message =
)
| NonClosedRecordPattern(s) =>
"the following fields are missing from the record pattern: " ++ s
| FuncWasmUnsafe(func) =>
| FuncWasmUnsafe(func, f, m) =>
"it looks like you are using "
++ func
++ " 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."
++ " on two unsafe Wasm values here.\nThis is generally unsafe and will cause errors. Use "
++ f
++ " from the `"
++ m
++ "` module the instead."
| FromNumberLiteralI32(n) =>
Printf.sprintf(
"it looks like you are calling Int32.fromNumber() with a constant number. Try using the literal syntax (e.g. `%sl`) instead.",
Expand Down Expand Up @@ -195,7 +199,7 @@ let defaults = [
UnreachableCase,
ShadowConstructor(""),
NoCmiFile("", None),
FuncWasmUnsafe(""),
FuncWasmUnsafe("", "", ""),
FromNumberLiteralI32(""),
FromNumberLiteralI64(""),
FromNumberLiteralU32(""),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/utils/warnings.rei
Expand Up @@ -37,7 +37,7 @@ type t =
| UnreachableCase
| ShadowConstructor(string)
| NoCmiFile(string, option(string))
| FuncWasmUnsafe(string)
| FuncWasmUnsafe(string, string, string)
| FromNumberLiteralI32(string)
| FromNumberLiteralI64(string)
| FromNumberLiteralU32(string)
Expand Down

0 comments on commit 1847203

Please sign in to comment.