implements PR015
This commit is contained in:
+41
@@ -6,6 +6,7 @@ import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsSemanticsControlFlowTest {
|
||||
@@ -82,4 +83,44 @@ class PbsSemanticsControlFlowTest {
|
||||
.count();
|
||||
assertEquals(1, duplicateWildcardCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAllowThisInsideStructServiceAndCtorBodies() {
|
||||
final var source = """
|
||||
declare struct Box(v: int) {
|
||||
fn read() -> int { return this.v; }
|
||||
ctor make(seed: int) { this.v = seed; }
|
||||
}
|
||||
|
||||
declare service Game {
|
||||
fn ping() -> int { this; return 0; }
|
||||
}
|
||||
|
||||
fn ok() -> 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_INVALID_THIS_CONTEXT.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectThisOutsideReceiverContexts() {
|
||||
final var source = """
|
||||
fn bad() -> int {
|
||||
this;
|
||||
return 0;
|
||||
}
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
final var invalidThisCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_THIS_CONTEXT.name()))
|
||||
.count();
|
||||
assertEquals(1, invalidThisCount);
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -5,6 +5,7 @@ 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.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -144,4 +145,40 @@ class PbsSemanticsDeclarationsTest {
|
||||
assertTrue(diagnostics.stream().anyMatch(d ->
|
||||
d.getCode().equals(PbsSemanticsErrors.E_SEM_DUPLICATE_DECLARATION.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAllowSelfInStructServiceMethodsAndCtors() {
|
||||
final var source = """
|
||||
declare struct Box(v: int) {
|
||||
fn merge(other: Self) -> Self { return this; }
|
||||
ctor make(copy: Self) { this.v = copy.v; }
|
||||
}
|
||||
|
||||
declare service Game {
|
||||
fn mirror(current: Self) -> 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_INVALID_SELF_CONTEXT.name())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectSelfOutsideAllowedDeclarationContexts() {
|
||||
final var source = """
|
||||
fn bad(v: Self) -> int { return 0; }
|
||||
declare const GLOBAL: Self = 1;
|
||||
""";
|
||||
final var diagnostics = DiagnosticSink.empty();
|
||||
|
||||
new PbsFrontendCompiler().compileFile(new FileId(0), source, diagnostics);
|
||||
|
||||
final var invalidSelfCount = diagnostics.stream()
|
||||
.filter(d -> d.getCode().equals(PbsSemanticsErrors.E_SEM_INVALID_SELF_CONTEXT.name()))
|
||||
.count();
|
||||
assertEquals(2, invalidSelfCount);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user