implements PLN-0118

This commit is contained in:
2026-07-15 12:13:40 +01:00
parent bde999f1fe
commit e8079b67fb
8 changed files with 185 additions and 14 deletions
@@ -11,9 +11,75 @@ import p.studio.compiler.source.tables.IntrinsicReference;
import p.studio.compiler.source.tables.ModuleReference;
import p.studio.utilities.structures.ReadOnlyList;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class IRBackendExecutableContractTest {
private static final String FORBIDDEN_PBS_PACKAGE = "p.studio.compiler.pbs";
private static final List<Class<?>> PUBLIC_CONTRACT_TYPES = List.of(
IRBackend.class,
IRBackend.IRBackendAggregator.class,
IRBackendReader.class,
IRBackendFile.class,
IRBackendExecutableFunction.class,
IRBackendExecutableFunction.Instruction.class,
IRBackendExecutableFunction.InstructionKind.class,
IRBackendExecutableFunction.HostCallMetadata.class,
IRBackendExecutableFunction.IntrinsicCallMetadata.class,
IRFunction.class,
IRSyntheticFunction.class,
IRSyntheticCallableKind.class,
IRSyntheticOrigin.class,
IRGlobal.class,
IRGlobalOrigin.class,
IRGlobalVisibility.class,
IRHiddenGlobalKind.class,
IRReservedMetadata.class,
IRReservedMetadata.HostMethodBinding.class,
IRReservedMetadata.BuiltinTypeSurface.class,
IRReservedMetadata.BuiltinFieldSurface.class,
IRReservedMetadata.IntrinsicSurface.class,
IRReservedMetadata.BuiltinConstSurface.class);
@Test
void publicIRBackendContractMustNotExposePbsTypes() {
final var violations = new ArrayList<String>();
for (final var contractType : PUBLIC_CONTRACT_TYPES) {
inspectType(contractType.getName(), contractType, violations);
for (final var field : contractType.getFields()) {
inspectType(contractType.getName() + "#" + field.getName(), field.getGenericType(), violations);
}
for (final var constructor : contractType.getConstructors()) {
for (final var parameterType : constructor.getGenericParameterTypes()) {
inspectType(contractType.getName() + "#<init>", parameterType, violations);
}
}
for (final var method : contractType.getMethods()) {
if (method.getDeclaringClass().equals(Object.class)
|| !Modifier.isPublic(method.getModifiers())) {
continue;
}
inspectType(contractType.getName() + "#" + method.getName(), method.getGenericReturnType(), violations);
for (final var parameterType : method.getGenericParameterTypes()) {
inspectType(contractType.getName() + "#" + method.getName(), parameterType, violations);
}
}
if (contractType.isRecord()) {
for (final var component : contractType.getRecordComponents()) {
inspectType(contractType.getName() + "#" + component.getName(), component.getGenericType(), violations);
}
}
}
assertTrue(
violations.isEmpty(),
"public IRBackend contract must not expose PBS frontend types: " + violations);
}
@Test
void callInstructionMustRequireCategorySpecificMetadata() {
@@ -366,4 +432,17 @@ class IRBackendExecutableContractTest {
assertEquals(0, backend.getCallableSignatures().get(0).moduleId().getIndex());
assertEquals(1, backend.getCallableSignatures().get(1).moduleId().getIndex());
}
private static void inspectType(
final String owner,
final Type type,
final ArrayList<String> violations) {
if (type == null) {
return;
}
final var typeName = type.getTypeName();
if (typeName.contains(FORBIDDEN_PBS_PACKAGE)) {
violations.add(owner + " -> " + typeName);
}
}
}