Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): Type aliasing in use statements #1887

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions compiler/src/typed/env.re
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ module IdTbl = {
root: Path.t,
/** Components from the opened module. */
components: Tbl.t(string, ('a, int)),
/** Aliased names in the module. */
aliases: Tbl.t(string, string),
/** A callback to be applied when a component is used from this
"open". This is used to detect unused "opens". The
arguments are used to detect shadowing. */
Expand All @@ -344,14 +346,17 @@ module IdTbl = {
current: Ident.add(id, x, tbl.current),
};

let add_open = (slot, wrap, root, components, next) => {
let add_open = (slot, wrap, root, components, aliases, next) => {
let using =
switch (slot) {
| None => None
| Some(f) => Some((s, x) => f(s, wrap(x)))
};

{current: Ident.empty, opened: Some({using, root, components, next})};
{
current: Ident.empty,
opened: Some({using, root, components, aliases, next}),
};
};

let rec find_same = (id, tbl) =>
Expand All @@ -370,10 +375,14 @@ module IdTbl = {
}) {
| Not_found as exn =>
switch (tbl.opened) {
| Some({using, root, next, components}) =>
| Some({using, root, next, components, aliases}) =>
try({
let (descr, pos) = Tbl.find(name, components);
let res = (PExternal(root, name), descr);
let aliased_name =
try(Tbl.find(name, aliases)) {
| Not_found => name
};
let res = (PExternal(root, aliased_name), descr);
if (mark) {
switch (using) {
| None => ()
Expand All @@ -400,16 +409,16 @@ module IdTbl = {
}) {
| Not_found =>
switch (tbl.opened) {
| Some({root, using, next, components}) =>
| Some({root, using, next, components, aliases}) =>
try({
let (desc, pos) = Tbl.find(name, components);
let new_desc = f(desc);
let components = Tbl.add(name, (new_desc, pos), components);
{...tbl, opened: Some({root, using, next, components})};
{...tbl, opened: Some({root, using, next, components, aliases})};
}) {
| Not_found =>
let next = update(name, f, next);
{...tbl, opened: Some({root, using, next, components})};
{...tbl, opened: Some({root, using, next, components, aliases})};
}
| None => tbl
}
Expand Down Expand Up @@ -2012,10 +2021,11 @@ let rec add_signature = (sg, env) =>

/* Open a signature path */

let add_components = (slot, root, env0, comps) => {
let add_components = (slot, root, env0, ~type_aliases=Tbl.empty, comps) => {
let add_l = (w, comps, env0) => TycompTbl.add_open(slot, w, comps, env0);

let add = (w, comps, env0) => IdTbl.add_open(slot, w, root, comps, env0);
let add = (w, comps, ~aliases=Tbl.empty, env0) =>
IdTbl.add_open(slot, w, root, comps, aliases, env0);

let constructors =
add_l(x => `Constructor(x), comps.comp_constrs, env0.constructors);
Expand All @@ -2024,7 +2034,8 @@ let add_components = (slot, root, env0, comps) => {

let values = add(x => `Value(x), comps.comp_values, env0.values);

let types = add(x => `Type(x), comps.comp_types, env0.types);
let types =
add(x => `Type(x), comps.comp_types, ~aliases=type_aliases, env0.types);

let modtypes =
add(x => `Module_type(x), comps.comp_modtypes, env0.modtypes);
Expand Down Expand Up @@ -2125,6 +2136,8 @@ let use_partial_signature = (root, items, env0) => {
comp_modtypes: Tbl.empty,
};

let type_aliases = ref(Tbl.empty);

let items =
List.map(
item => {
Expand Down Expand Up @@ -2184,6 +2197,7 @@ let use_partial_signature = (root, items, env0) => {
| ((decl, (constructors, labels)), _) as descr =>
new_comps.comp_types =
Tbl.add(new_name, descr, new_comps.comp_types);
type_aliases := Tbl.add(new_name, old_name, type_aliases^);
List.iter(
({cstr_name}) => {
new_comps.comp_constrs =
Expand Down Expand Up @@ -2213,7 +2227,10 @@ let use_partial_signature = (root, items, env0) => {
items,
);

(add_components(None, root, env0, new_comps), items);
(
add_components(None, root, env0, new_comps, ~type_aliases=type_aliases^),
items,
);
};

let use_full_signature = (root, env0) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/typed/typemod.re
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ let rec type_module = (~toplevel=false, anchor, env, statements) => {
[(type_path, PIdent(id)), ...type_export_aliases^];
id;
| Some(_) => failwith("Impossible: invalid alias")
| None => Ident.create(Path.last(type_path))
| None => Ident.create(name.txt)
};
([TSigType(id, type_, TRecNot), ...sigs], stmts);
| PProvideModule({name: {txt: IdentName(name)}, alias, loc}) =>
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/suites/includes.re
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ describe("includes", ({test, testSkip}) => {
"include \"reprovideContents\"; from ReprovideContents use { type Type, module Mod }; print(A); print(Mod.val)",
"A\n123\n",
);
assertRun(
"reprovide_type2",
"include \"reprovideContents\"; from ReprovideContents use { type OtherT as TT, val }; print(val); print({ x: 2 })",
"{\n x: 1\n}\n{\n x: 2\n}\n",
);
assertRun(
"reprovide_type3",
"include \"reprovideContents\"; from ReprovideContents use { type OtherT as Other }; print({ x: 1 }: Other)",
"{\n x: 1\n}\n",
);
});
6 changes: 6 additions & 0 deletions compiler/test/test-libs/provideContents.gr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ provide enum Type {
C,
}

record Type2 {
x: Number
}

provide { type Type2 as Other }

provide module Mod {
provide let val = 123
}
6 changes: 4 additions & 2 deletions compiler/test/test-libs/reprovideContents.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module ReprovideContents

include "./provideContents"

from ProvideContents use { type Type, module Mod }
from ProvideContents use { type Type, type Other as OtherT, module Mod }

provide { type Type, module Mod }
let val = { x: 1 }

provide { type Type, type OtherT, module Mod, val }
Loading