Skip to content

Commit 957b6a5

Browse files
authored
bugfix: don't suggest privileged @-names in error messages (#4916)
1 parent cfe8452 commit 957b6a5

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

src/mo_def/syntax.ml

+9
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@ open Source
305305
let anon_id sort at = "@anon-" ^ sort ^ "-" ^ string_of_pos at.left
306306
let is_anon_id id = Lib.String.chop_prefix "@anon-" id.it <> None
307307

308+
let is_privileged name =
309+
String.length name > 0 && name.[0] = '@'
310+
311+
let is_underscored name =
312+
String.length name > 0 && name.[0] = '_'
313+
314+
let is_scope name =
315+
String.length name > 0 && name.[0] = '$'
316+
308317
(* Types & Scopes *)
309318

310319
let arity t =

src/mo_frontend/suggest.ml

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
(* Suggestions *)
3+
open Mo_def
34
open Mo_types
45
open Mo_config
56
open Type
@@ -9,6 +10,11 @@ let oneof sep lastsep ss =
910
((if rest <> [] then (String.concat sep rest) ^ lastsep else "") ^ last)
1011

1112
let suggest_id desc id ids =
13+
let ids =
14+
List.filter (fun id ->
15+
not (Syntax.is_privileged id))
16+
ids
17+
in
1218
if !Flags.ai_errors then
1319
Printf.sprintf
1420
"\nThe %s %s is not available. Try something else?"

src/mo_frontend/typing.ml

+3-7
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ let display_obj fmt (s, fs) =
139139
let display_vals fmt vals =
140140
if !Flags.ai_errors then
141141
let tfs = T.Env.fold (fun x (t, _, _, _) acc ->
142-
if x = "Prim" || (String.length x >= 0 && x.[0] = '@')
142+
if x = "Prim" || Syntax.is_privileged x
143143
then acc
144144
else T.{lab = x; src = {depr = None; region = Source.no_region }; typ = t}::acc)
145145
vals []
@@ -163,7 +163,7 @@ let display_labs fmt labs =
163163
let display_typs fmt typs =
164164
if !Flags.ai_errors then
165165
let tfs = T.Env.fold (fun x c acc ->
166-
if (String.length x >= 0 && (x.[0] = '@' || x.[0] = '$')) ||
166+
if Syntax.(is_privileged x || is_scope x) ||
167167
T.(match Cons.kind c with
168168
| Def ([], Prim _)
169169
| Def ([], Any)
@@ -271,11 +271,7 @@ let emit_unused_warnings env =
271271
List.iter emit list
272272

273273
let ignore_warning_for_id id =
274-
if String.length id > 0 then
275-
let prefix = String.get id 0 in
276-
prefix = '_' || prefix = '@'
277-
else
278-
false
274+
Syntax.(is_underscored id || is_privileged id)
279275

280276
let detect_unused env inner_identifiers =
281277
if not env.pre && env.check_unused then
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
suggest-unprivileged.mo:5.1-5.7: type error [M0057], unbound variable cycles
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Return code 1

test/fail/suggest-unprivileged.mo

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Prim "mo:⛔";
2+
3+
// should not suggest @cycles because privileged
4+
5+
cycles;

0 commit comments

Comments
 (0)