implements PR007
This commit is contained in:
+18
-1
@@ -1,6 +1,7 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.pbs.semantics.PbsSemanticsErrors;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
@@ -45,6 +46,22 @@ class PbsFrontendCompilerTest {
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
compiler.compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.hasErrors());
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAllowOverloadedFunctionNamesWhenShapeDiffers() {
|
||||
final var source = """
|
||||
fn a(x: int) -> int { return x; }
|
||||
fn a(x: float) -> int { return 0; }
|
||||
""";
|
||||
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
final var compiler = new PbsFrontendCompiler();
|
||||
compiler.compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().noneMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
|
||||
}
|
||||
}
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsSemanticsDeclarationsTest {
|
||||
|
||||
@Test
|
||||
void shouldAllowFunctionOverloadsWithDifferentShapes() {
|
||||
final var source = """
|
||||
fn run(x: int) -> int { return x; }
|
||||
fn run(x: float) -> int { return 0; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertFalse(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateCallableShapeIgnoringLabels() {
|
||||
final var source = """
|
||||
fn run(a: int) -> (x: int, y: int) { return 0; }
|
||||
fn run(b: int) -> (left: int, right: int) { return 1; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateParametersAndReturnLabels() {
|
||||
final var source = """
|
||||
fn dup(a: int, a: int) -> (x: int, x: int) { return 0; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_PARAMETER_NAME.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_RETURN_LABEL.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateEnumAndErrorCases() {
|
||||
final var source = """
|
||||
declare error Err { Fail; Fail; }
|
||||
declare enum Mode(Idle = 0, Idle = 1, Run = 1);
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_ERROR_CASE_LABEL.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_ENUM_CASE_LABEL.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_ENUM_CASE_ID.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidReturnSurfaces() {
|
||||
final var source = """
|
||||
declare error Err { Fail; }
|
||||
fn badResult() -> result<Err> optional int { return ok(1); }
|
||||
fn badOptional(v: optional void) -> int { return 0; }
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_MIXED_OPTIONAL_RESULT_RETURN.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_OPTIONAL_VOID_TYPE_SURFACE.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectConstWithoutInitializer() {
|
||||
final var source = "declare const LIMIT: int;";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_MISSING_CONST_INITIALIZER.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectInvalidCtorServiceAndCallbackShapes() {
|
||||
final var source = """
|
||||
declare struct S(a: int) {
|
||||
ctor make(x: int) { return; }
|
||||
ctor make(y: int) { }
|
||||
}
|
||||
|
||||
declare service Game {
|
||||
fn run(a: int) -> int { return a; }
|
||||
fn run(b: int) -> int { return b; }
|
||||
}
|
||||
|
||||
declare callback Tick(a: int, a: int) -> int;
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_RETURN_INSIDE_CTOR.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_CALLABLE_SHAPE.name())));
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_PARAMETER_NAME.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateDeclarationNamesByNamespace() {
|
||||
final var source = """
|
||||
declare struct State();
|
||||
declare enum State(Idle);
|
||||
|
||||
declare const Tick: int = 1;
|
||||
declare service Tick {
|
||||
fn run() -> void { return; }
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_DECLARATION.name())));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user