implements PR023
This commit is contained in:
+140
@@ -8,6 +8,10 @@ import p.studio.compiler.models.BuildStack;
|
||||
import p.studio.compiler.models.ProjectDescriptor;
|
||||
import p.studio.compiler.models.SourceHandle;
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.stdlib.InterfaceModuleLoader;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironment;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibModuleSource;
|
||||
import p.studio.compiler.pbs.linking.PbsLinkErrors;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.ProjectId;
|
||||
@@ -21,7 +25,10 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -216,6 +223,111 @@ class PBSFrontendPhaseServiceTest {
|
||||
assertEquals(0, irBackend.getFunctions().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveReservedImportsThroughStdlibResolverForSelectedMajor() throws IOException {
|
||||
final var projectRoot = tempDir.resolve("project-stdlib");
|
||||
final var sourceRoot = projectRoot.resolve("src");
|
||||
final var modulePath = sourceRoot.resolve("app");
|
||||
Files.createDirectories(modulePath);
|
||||
|
||||
final var sourceFile = modulePath.resolve("source.pbs");
|
||||
final var modBarrel = modulePath.resolve("mod.barrel");
|
||||
Files.writeString(sourceFile, """
|
||||
import { draw } from @sdk:gfx;
|
||||
fn use() -> int { return 1; }
|
||||
""");
|
||||
Files.writeString(modBarrel, "pub fn use() -> int;");
|
||||
|
||||
final var projectTable = new ProjectTable();
|
||||
final var fileTable = new FileTable(1);
|
||||
final var projectId = projectTable.register(ProjectDescriptor.builder()
|
||||
.rootPath(projectRoot)
|
||||
.name("app")
|
||||
.version("1.0.0")
|
||||
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
|
||||
.build());
|
||||
|
||||
registerFile(projectId, projectRoot, sourceFile, fileTable);
|
||||
registerFile(projectId, projectRoot, modBarrel, fileTable);
|
||||
|
||||
final var stdlibModule = new StdlibModuleSource(
|
||||
"sdk",
|
||||
ReadOnlyList.wrap(List.of("gfx")),
|
||||
ReadOnlyList.wrap(List.of(new StdlibModuleSource.SourceFile("gfx.pbs", "fn draw() -> int { return 1; }"))),
|
||||
"pub fn draw() -> int;");
|
||||
final var frontendService = new PBSFrontendPhaseService(
|
||||
resolverForMajor(7, stdlibModule),
|
||||
new InterfaceModuleLoader(2_000_000));
|
||||
|
||||
final var ctx = new FrontendPhaseContext(
|
||||
projectTable,
|
||||
fileTable,
|
||||
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
|
||||
7);
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
final var irBackend = frontendService.compile(
|
||||
ctx,
|
||||
diagnostics,
|
||||
LogAggregator.empty(),
|
||||
BuildingIssueSink.empty());
|
||||
|
||||
assertTrue(diagnostics.stream().noneMatch(d ->
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_MODULE_NOT_FOUND.name())));
|
||||
assertTrue(diagnostics.stream().noneMatch(d ->
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_SYMBOL_UNRESOLVED.name())));
|
||||
assertEquals(1, irBackend.getFunctions().size());
|
||||
assertEquals("use", irBackend.getFunctions().getFirst().name());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReportReservedImportModuleNotFoundWhenStdlibResolverMissesModule() throws IOException {
|
||||
final var projectRoot = tempDir.resolve("project-stdlib-missing");
|
||||
final var sourceRoot = projectRoot.resolve("src");
|
||||
final var modulePath = sourceRoot.resolve("app");
|
||||
Files.createDirectories(modulePath);
|
||||
|
||||
final var sourceFile = modulePath.resolve("source.pbs");
|
||||
final var modBarrel = modulePath.resolve("mod.barrel");
|
||||
Files.writeString(sourceFile, """
|
||||
import { draw } from @sdk:gfx;
|
||||
fn use() -> int { return 1; }
|
||||
""");
|
||||
Files.writeString(modBarrel, "pub fn use() -> int;");
|
||||
|
||||
final var projectTable = new ProjectTable();
|
||||
final var fileTable = new FileTable(1);
|
||||
final var projectId = projectTable.register(ProjectDescriptor.builder()
|
||||
.rootPath(projectRoot)
|
||||
.name("app")
|
||||
.version("1.0.0")
|
||||
.sourceRoots(ReadOnlyList.wrap(List.of(sourceRoot)))
|
||||
.build());
|
||||
|
||||
registerFile(projectId, projectRoot, sourceFile, fileTable);
|
||||
registerFile(projectId, projectRoot, modBarrel, fileTable);
|
||||
|
||||
final var frontendService = new PBSFrontendPhaseService(
|
||||
resolverForMajor(7),
|
||||
new InterfaceModuleLoader(2_100_000));
|
||||
final var ctx = new FrontendPhaseContext(
|
||||
projectTable,
|
||||
fileTable,
|
||||
new BuildStack(ReadOnlyList.wrap(List.of(projectId))),
|
||||
7);
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
final var irBackend = frontendService.compile(
|
||||
ctx,
|
||||
diagnostics,
|
||||
LogAggregator.empty(),
|
||||
BuildingIssueSink.empty());
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsLinkErrors.E_LINK_IMPORT_MODULE_NOT_FOUND.name())));
|
||||
assertEquals(0, irBackend.getFunctions().size());
|
||||
}
|
||||
|
||||
private void registerFile(
|
||||
final ProjectId projectId,
|
||||
final Path projectRoot,
|
||||
@@ -231,4 +343,32 @@ class PBSFrontendPhaseServiceTest {
|
||||
SourceProviderFactory.filesystem()));
|
||||
}
|
||||
|
||||
private StdlibEnvironmentResolver resolverForMajor(
|
||||
final int acceptedStdlib,
|
||||
final StdlibModuleSource... modules) {
|
||||
final var byModuleKey = new HashMap<String, StdlibModuleSource>();
|
||||
for (final var module : modules) {
|
||||
byModuleKey.put(moduleKey(module.project(), module.pathSegments()), module);
|
||||
}
|
||||
return stdlib -> {
|
||||
if (stdlib != acceptedStdlib) {
|
||||
return (project, pathSegments) -> Optional.empty();
|
||||
}
|
||||
return new StdlibEnvironment() {
|
||||
@Override
|
||||
public Optional<StdlibModuleSource> resolveModule(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
return Optional.ofNullable(byModuleKey.get(moduleKey(project, pathSegments)));
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
private String moduleKey(
|
||||
final String project,
|
||||
final ReadOnlyList<String> pathSegments) {
|
||||
return project + ":" + String.join("/", pathSegments.asList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user