Skip to content

Commit dde62d3

Browse files
authored
feat(compiler)!: Require extension when including relative file paths (#1842)
1 parent 5cf726e commit dde62d3

File tree

383 files changed

+2043
-1995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

383 files changed

+2043
-1995
lines changed

compiler/src/codegen/compcore.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,26 @@ let reloc_base = Ident.create_persistent("relocBase");
5757
let table_size = Ident.create_persistent("GRAIN$TABLE_SIZE");
5858

5959
/* Memory allocation */
60-
let malloc_mod = "GRAIN$MODULE$runtime/malloc";
60+
let malloc_mod = "GRAIN$MODULE$runtime/malloc.gr";
6161
let malloc_ident = Ident.create_persistent("malloc");
6262
let malloc_closure_ident = Ident.create_persistent("GRAIN$EXPORT$malloc");
6363

6464
/* Garbage collection */
65-
let gc_mod = "GRAIN$MODULE$runtime/gc";
65+
let gc_mod = "GRAIN$MODULE$runtime/gc.gr";
6666
let incref_ident = Ident.create_persistent("incRef");
6767
let incref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$incRef");
6868
let decref_ident = Ident.create_persistent("decRef");
6969
let decref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$decRef");
7070

7171
/* Exceptions */
72-
let exception_mod = "GRAIN$MODULE$runtime/exception";
72+
let exception_mod = "GRAIN$MODULE$runtime/exception.gr";
7373
let panic_with_exception_ident =
7474
Ident.create_persistent("panicWithException");
7575
let panic_with_exception_closure_ident =
7676
Ident.create_persistent("GRAIN$EXPORT$panicWithException");
7777

7878
/* Equality checking */
79-
let equal_mod = "GRAIN$MODULE$runtime/equal";
79+
let equal_mod = "GRAIN$MODULE$runtime/equal.gr";
8080
let equal_ident = Ident.create_persistent("equal");
8181
let equal_closure_ident = Ident.create_persistent("GRAIN$EXPORT$equal");
8282

compiler/src/compile.re

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ type error =
4141

4242
exception InlineFlagsError(Location.t, error);
4343

44-
let default_output_filename = name =>
45-
Filepath.String.remove_extension(name) ++ ".gr.wasm";
44+
let default_output_filename = name => name ++ ".wasm";
4645

4746
let default_mashtree_filename = name =>
4847
Filepath.String.remove_extension(name) ++ ".mashtree";

compiler/src/formatting/fmt.re

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3214,6 +3214,13 @@ let print_primitive_description = (fmt, {pprim_ident, pprim_name, pprim_loc}) =>
32143214

32153215
let print_include_declaration =
32163216
(fmt, {pinc_path, pinc_module, pinc_alias, pinc_loc}) => {
3217+
open Filepath.String;
3218+
let path =
3219+
if (!is_relpath(pinc_path.txt) && check_suffix(pinc_path.txt, ".gr")) {
3220+
chop_suffix(pinc_path.txt, ".gr");
3221+
} else {
3222+
pinc_path.txt;
3223+
};
32173224
string("from")
32183225
++ fmt.print_comment_range(
32193226
fmt,
@@ -3224,7 +3231,7 @@ let print_include_declaration =
32243231
enclosing_start_location(pinc_loc),
32253232
pinc_path.loc,
32263233
)
3227-
++ double_quotes(string(pinc_path.txt))
3234+
++ double_quotes(string(path))
32283235
++ fmt.print_comment_range(
32293236
fmt,
32303237
~allow_breaks=false,

compiler/src/linking/link.re

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ let is_grain_module = mod_name => {
3131
Str.string_match(Str.regexp_string("GRAIN$MODULE$"), mod_name, 0);
3232
};
3333

34-
let wasi_polyfill_module = () => {
35-
Filepath.String.remove_extension(Option.get(Config.wasi_polyfill_path()))
36-
++ ".gr.wasm";
37-
};
34+
let wasi_polyfill_module = () =>
35+
Option.get(Config.wasi_polyfill_path()) ++ ".wasm";
3836

3937
let is_wasi_module = mod_name => {
4038
mod_name == "wasi_snapshot_preview1";

compiler/src/middle_end/analyze_inline_wasm.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ let analyze = ({imports, body, analyses}) => {
5959
mod_has_inlineable_wasm := false;
6060
let process_import = ({imp_use_id, imp_desc}) => {
6161
switch (imp_desc) {
62-
| GrainValue("runtime/unsafe/memory", name) =>
62+
| GrainValue("runtime/unsafe/memory.gr", name) =>
6363
mod_has_inlineable_wasm := true;
6464
switch (get_primitive_memory(name)) {
6565
| Some(prim) => set_inlineable_wasm(imp_use_id, prim)

compiler/src/middle_end/analyze_manual_memory_management.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let analyze = ({imports, body, analyses}) => {
1919
mod_has_manual_memory_management := false;
2020
let process_import = ({imp_use_id, imp_desc}) => {
2121
switch (imp_desc) {
22-
| GrainValue("runtime/unsafe/memory", "incRef" | "decRef") =>
22+
| GrainValue("runtime/unsafe/memory.gr", "incRef" | "decRef") =>
2323
mod_has_manual_memory_management := true;
2424
set_manual_call(imp_use_id);
2525
| GrainValue(_)

compiler/src/parsing/ast_helper.re

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,15 @@ module MatchBranch = {
473473

474474
module IncludeDeclaration = {
475475
let mk = (~loc, path, module_, alias) => {
476-
{
477-
pinc_alias: alias,
478-
pinc_module: module_,
479-
pinc_path: normalize_string(~loc, path),
480-
pinc_loc: loc,
481-
};
476+
let path = normalize_string(~loc, path);
477+
let filename =
478+
if (!Grain_utils.Filepath.String.is_relpath(path.txt)) {
479+
path.txt ++ ".gr";
480+
} else {
481+
path.txt;
482+
};
483+
let path = {txt: filename, loc: path.loc};
484+
{pinc_alias: alias, pinc_module: module_, pinc_path: path, pinc_loc: loc};
482485
};
483486
};
484487

compiler/src/parsing/driver.re

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ let read_imports = (program: Parsetree.parsed_program) => {
154154
List.map(
155155
o => {
156156
switch (o) {
157-
| Grain_utils.Config.Pervasives_mod => Location.mknoloc("pervasives")
158-
| Grain_utils.Config.Gc_mod => Location.mknoloc("runtime/gc")
157+
| Grain_utils.Config.Pervasives_mod =>
158+
Location.mknoloc("pervasives.gr")
159+
| Grain_utils.Config.Gc_mod => Location.mknoloc("runtime/gc.gr")
159160
}
160161
},
161162
switch (program.comments) {

compiler/src/typed/env.re

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,14 @@ let check_consistency = ps =>
758758
~base_dir=Filepath.String.dirname(ps.ps_filename),
759759
name,
760760
);
761-
Consistbl.check(crc_units, resolved_file_name, crc, ps.ps_filename);
761+
Consistbl.check(
762+
crc_units,
763+
// This is a workaround; should address
764+
// TODO(#1843): Investigate CRC behavior
765+
Filepath.String.chop_suffix(resolved_file_name, ".gr"),
766+
crc,
767+
ps.ps_filename,
768+
);
762769
},
763770
ps.ps_crcs,
764771
)
@@ -2625,9 +2632,10 @@ let report_error = ppf =>
26252632
alt,
26262633
)
26272634
| Unbound_module(_, modname) => fprintf(ppf, "Unbound module %s", modname)
2628-
| No_module_file(m, None) => fprintf(ppf, "Missing file for module %s", m)
2635+
| No_module_file(m, None) =>
2636+
fprintf(ppf, "Missing file for module \"%s\"", m)
26292637
| No_module_file(m, Some(msg)) =>
2630-
fprintf(ppf, "Missing file for module %s: %s", m, msg)
2638+
fprintf(ppf, "Missing file for module \"%s\": %s", m, msg)
26312639
| Value_not_found_in_module(_, name, path) =>
26322640
fprintf(ppf, "Unbound value %s in module %s", name, path)
26332641
| Module_not_found_in_module(_, name, path, None) =>

compiler/src/typed/module_resolution.re

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ let read_file_cmi = f => {
4949
};
5050
};
5151

52-
let get_output_name = name => {
53-
Filepath.String.remove_extension(name) ++ ".gr.wasm";
54-
};
52+
let get_output_name = name => name ++ ".wasm";
5553

5654
let find_ext_in_dir = (dir, name) => {
5755
let fullname = Filepath.String.concat(dir, name);
5856
let rec process_ext =
5957
fun
6058
| [] => None
6159
| [ext, ..._] when file_exists(fullname ++ ext) =>
62-
Some((fullname ++ ext, dir, name, ext))
60+
Some((fullname ++ ext, dir, name))
6361
| [_, ...tl] => process_ext(tl);
6462
process_ext;
6563
};
6664

67-
let find_in_path_uncap = (~exts=[], base_dir, path, name) => {
65+
let find_in_path_uncap =
66+
(~check_src=false, ~check_wasm=false, base_dir, path, name) => {
67+
let exts = (check_src ? [""] : []) @ (check_wasm ? [".wasm"] : []);
6868
let rec try_dir =
6969
fun
7070
| [] => raise(Not_found)
@@ -75,12 +75,7 @@ let find_in_path_uncap = (~exts=[], base_dir, path, name) => {
7575
};
7676
};
7777
if (!Filepath.String.is_relative(name) && Fs_access.file_exists(name)) {
78-
(
79-
name,
80-
Filepath.String.dirname(name),
81-
Filepath.String.(remove_extension(basename(name))),
82-
Filepath.String.extension(name),
83-
);
78+
(name, Filepath.String.dirname(name), Filepath.String.basename(name));
8479
} else if (Filepath.String.is_relpath(name)) {
8580
try_dir([base_dir]);
8681
} else {
@@ -177,9 +172,14 @@ let resolve_unit = (~search_path=?, ~cache=true, ~base_dir=?, unit_name) => {
177172
) {
178173
| (true, Some(res)) => res
179174
| _ =>
180-
let exts = [".gr", ".gr.wasm"];
181-
let (_, dir, basename, _) =
182-
find_in_path_uncap(~exts, base_dir, path, unit_name);
175+
let (_, dir, basename) =
176+
find_in_path_uncap(
177+
~check_src=true,
178+
~check_wasm=true,
179+
base_dir,
180+
path,
181+
unit_name,
182+
);
183183
if (cache) {
184184
log_resolution(unit_name, dir, basename);
185185
} else {
@@ -202,30 +202,65 @@ let locate_module = (~disable_relpath=false, base_dir, path, unit_name) => {
202202
) {
203203
| Some(m) => m
204204
| None =>
205-
let grain_src_exts = [".gr"];
206205
let (dir, m) =
207-
switch (
208-
find_in_path_uncap(~exts=[".gr.wasm"], base_dir, path, unit_name)
209-
) {
210-
| (objpath, dir, basename, ext) =>
206+
switch (find_in_path_uncap(~check_wasm=true, base_dir, path, unit_name)) {
207+
| (objpath, dir, basename) =>
211208
ignore(log_resolution(unit_name, dir, basename));
212-
switch (find_ext_in_dir(dir, basename, grain_src_exts)) {
213-
| Some((srcpath, _, _, _)) => (
209+
let file = find_ext_in_dir(dir, basename, [""]);
210+
switch (file) {
211+
| Some((srcpath, _, _)) => (
214212
dir,
215213
GrainModule(srcpath, Some(objpath)),
216214
)
217215
| None => (dir, WasmModule(objpath))
218216
};
219217
| exception Not_found =>
220-
let (srcpath, dir, _, _) =
221-
find_in_path_uncap(~exts=grain_src_exts, base_dir, path, unit_name);
218+
let (srcpath, dir, _) =
219+
find_in_path_uncap(~check_src=true, base_dir, path, unit_name);
222220
(dir, GrainModule(srcpath, None));
223221
};
224222
PathTbl.add(current_located_module_cache(), (dir, unit_name), m);
225223
m;
226224
};
227225
};
228226

227+
let try_locate_module =
228+
(~disable_relpath=false, base_dir, active_search_path, name, loc) => {
229+
let locate = locate_module(~disable_relpath, base_dir, active_search_path);
230+
Filepath.String.(
231+
try(locate(name)) {
232+
| Not_found =>
233+
if (check_suffix(name, ".gr")) {
234+
let no_extension = chop_suffix(name, ".gr");
235+
switch (locate(no_extension)) {
236+
| exception Not_found => error(No_module_file(loc, name, None))
237+
| _ =>
238+
let name = !is_relpath(name) ? no_extension : name;
239+
error(
240+
No_module_file(
241+
loc,
242+
name,
243+
Some("did you mean \"" ++ no_extension ++ "\"?"),
244+
),
245+
);
246+
};
247+
} else {
248+
switch (locate(name ++ ".gr")) {
249+
| exception Not_found => error(No_module_file(loc, name, None))
250+
| _ =>
251+
error(
252+
No_module_file(
253+
loc,
254+
name,
255+
Some("did you mean \"" ++ name ++ ".gr\"?"),
256+
),
257+
)
258+
};
259+
}
260+
}
261+
);
262+
};
263+
229264
type dependency_node = {
230265
// dn_unit_name is a hashtable because we may have a situation
231266
// where A depends on B and C, and both B and C depend on D.
@@ -281,22 +316,12 @@ module Dependency_graph =
281316
List.map(
282317
name => {
283318
let located =
284-
try(
285-
locate_module(
286-
base_dir,
287-
active_search_path,
288-
name.Location.txt,
289-
)
290-
) {
291-
| Not_found =>
292-
error(
293-
No_module_file(
294-
name.Location.loc,
295-
name.Location.txt,
296-
None,
297-
),
298-
)
299-
};
319+
try_locate_module(
320+
base_dir,
321+
active_search_path,
322+
name.Location.txt,
323+
name.Location.loc,
324+
);
300325
let out_file_name = located_to_out_file_name(located);
301326
let existing_dependency = lookup(out_file_name);
302327
switch (existing_dependency) {
@@ -331,16 +356,12 @@ module Dependency_graph =
331356
List.map(
332357
((name, _)) => {
333358
let located =
334-
try(locate_module(base_dir, active_search_path, name)) {
335-
| Not_found =>
336-
error(
337-
No_module_file(
338-
Location.in_file(dn.dn_file_name),
339-
name,
340-
None,
341-
),
342-
)
343-
};
359+
try_locate_module(
360+
base_dir,
361+
active_search_path,
362+
name,
363+
Location.in_file(dn.dn_file_name),
364+
);
344365
let out_file_name = located_to_out_file_name(located);
345366
let existing_dependency = lookup(out_file_name);
346367
switch (existing_dependency) {
@@ -457,19 +478,14 @@ let locate_module_file = (~loc, ~disable_relpath=false, unit_name) => {
457478
let base_dir = Filepath.String.dirname(current_filename^());
458479
let path = Config.module_search_path();
459480
let located =
460-
try(locate_module(~disable_relpath, base_dir, path, unit_name)) {
461-
| Not_found => error(No_module_file(loc, unit_name, None))
462-
};
481+
try_locate_module(~disable_relpath, base_dir, path, unit_name, loc);
463482
located_to_out_file_name(located);
464483
};
465484

466485
let process_dependency = (~loc, ~base_file, unit_name) => {
467486
let base_dir = Filepath.String.dirname(base_file);
468487
let path = Config.module_search_path();
469-
let located =
470-
try(locate_module(~disable_relpath=false, base_dir, path, unit_name)) {
471-
| Not_found => error(No_module_file(loc, unit_name, None))
472-
};
488+
let located = try_locate_module(base_dir, path, unit_name, loc);
473489
let out_file = located_to_out_file_name(located);
474490
let current_dep_node = Dependency_graph.lookup_filename(base_file);
475491
let existing_dependency = Dependency_graph.lookup_filename(out_file);
@@ -553,9 +569,9 @@ let report_error = ppf =>
553569
);
554570
}
555571
| No_module_file(_, m, None) =>
556-
fprintf(ppf, "Missing file for module %s", m)
572+
fprintf(ppf, "Missing file for module \"%s\"", m)
557573
| No_module_file(_, m, Some(msg)) =>
558-
fprintf(ppf, "Missing file for module %s: %s", m, msg);
574+
fprintf(ppf, "Missing file for module \"%s\": %s", m, msg);
559575

560576
let () =
561577
Location.register_error_of_exn(

0 commit comments

Comments
 (0)