Skip to content

Commit

Permalink
handling import of variants
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Nov 23, 2024
1 parent 6bf5211 commit 95fe9c1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/analysis/analyse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,18 @@ describe("modules", () => {
expect(a.errors).toEqual([]);
});

test.todo("implicitly imports variants of the modules in the prelude", () => {
test("implicitly imports variants of the modules in the prelude", () => {
const [A] = performAnalysis(
`
pub(..) type MyType { A }
pub(..) type MyType { Variant }
`,
{ namespace: "A" },
);

const [a] = performAnalysis(
`
pub let x = A
// import A.{MyType(..)}
pub let x = Variant
`,
{
dependencies: { A },
Expand All @@ -301,7 +302,7 @@ describe("modules", () => {
});
});

test.todo("handles nested type references from other modules", () => {
test("handles nested type references from other modules", () => {
const [A] = performAnalysis(
`
pub(..) type T { T }
Expand Down Expand Up @@ -391,7 +392,7 @@ describe("modules", () => {
});

Check warning on line 392 in src/analysis/analyse.test.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/analyse.test.ts#L389-L392

Added lines #L389 - L392 were not covered by tests
});

test.todo("allow importing types (unqualified)", () => {
test("allow importing types (unqualified)", () => {
const [Mod] = performAnalysis(`pub type Example { }`, { namespace: "Mod" });

const [a] = performAnalysis(
Expand Down
14 changes: 12 additions & 2 deletions src/analysis/analyse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,17 @@ export class Analysis {
return;

case "constructor": {
const declarationType = this.typesHydration.getPolyType(
const analysis =
resolution.namespace === this.ns
? this
: this.options.getDependency?.(resolution.namespace);

if (analysis === undefined) {
// probably unreachable
throw new Error("TODO handle");
}

Check warning on line 160 in src/analysis/analyse.ts

View check run for this annotation

Codecov / codecov/patch

src/analysis/analyse.ts#L159-L160

Added lines #L159 - L160 were not covered by tests

const declarationType = analysis.typesHydration.getPolyType(
resolution.declaration,
);

Expand All @@ -159,7 +169,7 @@ export class Analysis {
: {
tag: "Fn",
args: resolution.variant.args.map((arg) =>
this.typesHydration.getPolyType(arg),
analysis.typesHydration.getPolyType(arg),
),
return: declarationType,
};
Expand Down
14 changes: 14 additions & 0 deletions src/analysis/resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ export class ResolutionAnalysis {
declarationLookup,
]);

if (
declarationLookup.type === "adt" &&
declarationLookup.pub === ".."
) {
for (const exposedVariant of declarationLookup.variants) {
this.importedValues.set(exposedVariant.name, {
type: "constructor",
declaration: declarationLookup,
variant: exposedVariant,
namespace: analysis.ns,
});
}
}

break;
}

Expand Down

0 comments on commit 95fe9c1

Please sign in to comment.