Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ class EtsMethodBuilder(
type = type.toEtsType(),
isSuperCall = isSuperCall,
wellKnownSymbol = wellKnownSymbol.toEtsWellKnownSymbol(),
forwardRestArguments = forwardRestArguments,
)

is StaticCallExprDto -> EtsStaticCallExpr(
Expand Down
1 change: 1 addition & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Values.kt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ data class InstanceCallExprDto(
override val args: List<ValueDto>,
val isSuperCall: Boolean = false,
val wellKnownSymbol: String? = null,
val forwardRestArguments: Boolean = false,
) : CallExprDto

@Serializable
Expand Down
1 change: 1 addition & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/model/Expr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ data class EtsInstanceCallExpr(
override val type: EtsType,
val wellKnownSymbol: EtsWellKnownSymbol? = null,
val isSuperCall: Boolean = false,
val forwardRestArguments: Boolean = false,
) : EtsCallExpr, CommonInstanceCallExpr {
override fun toString(): String {
return "call ${instance}.${callee.name}(${args.joinToString()})"
Expand Down
2 changes: 2 additions & 0 deletions jacodb-ets/ts-frontend/src/dto/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ export interface InstanceCallExprDto {
args: ValueDto[];
/** Explicit `super.m()` dispatch keeps the declaring superclass independent of the receiver's runtime class. */
isSuperCall?: boolean;
/** A synthetic derived constructor forwards the packed rest parameter received by its frame. */
forwardRestArguments?: boolean;
/** Intrinsic key identity, not a property whose string name happens to mention Symbol. */
wellKnownSymbol?: "iterator";
}
Expand Down
25 changes: 3 additions & 22 deletions jacodb-ets/ts-frontend/src/lowering/classBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class ClassBuilder {
methods.push(
ctorDecl !== undefined
? this.buildConstructor(signature, ctorDecl, superClass?.signature)
: this.synthesizeDefaultConstructor(signature, decl, superClass),
: this.synthesizeDefaultConstructor(signature, superClass),
);
} else {
this.ctx.diagnostics.warn(decl, `class bodies retained without definition semantics: ${definitionFailure}`);
Expand Down Expand Up @@ -505,34 +505,14 @@ export class ClassBuilder {

private synthesizeDefaultConstructor(
declaringClass: ClassSignatureDto,
decl: ts.ClassLikeDeclaration,
superClass?: { signature: ClassSignatureDto; constructorDecl?: ts.ConstructorDeclaration },
): MethodDto {
const classType: ClassTypeDto = { _: "ClassType", signature: declaringClass };
const m = new MethodContext(this.ctx, declaringClass, CONSTRUCTOR_NAME);
const inherited = superClass?.constructorDecl !== undefined
? buildParameters(this.ctx, superClass.constructorDecl)
: { parameters: [], prologueParams: [] };
const hasRestParameter = inherited.parameters.some((parameter) => parameter.isRest === true);
if (hasRestParameter) {
// ParameterRef for a rest parameter is already a packed array. Passing
// it as one call argument would pack it again in the base constructor.
this.ctx.diagnostics.warn(decl, "unsupported default constructor forwarding of rest arguments");
m.withOrigin(decl, () => m.cfg.emit(unsupportedStmt(decl)));
return {
signature: {
declaringClass,
name: CONSTRUCTOR_NAME,
parameters: inherited.parameters,
returnType: classType,
},
modifiers: 0,
decorators: [],
isStrictMode: true,
body: m.build(),
};
}

const hasRestParameter = inherited.parameters[inherited.parameters.length - 1]?.isRest === true;
m.emitPrologue(inherited.prologueParams);
if (superClass !== undefined) {
const thisLocal = m.getOrCreateLocal("this", classType);
Expand All @@ -552,6 +532,7 @@ export class ClassBuilder {
? m.localForIdentifier(param.identifier, param.type)
: m.getOrCreateLocal(param.name, param.type),
),
...(hasRestParameter ? { forwardRestArguments: true } : {}),
},
});
}
Expand Down
16 changes: 9 additions & 7 deletions jacodb-ets/ts-frontend/test/classes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,26 +798,28 @@ describe("class lowering", () => {
&& stmt.right._ === "PtrCallExpr" && stmt.right.method.name === "mark")).toBe(true);
});

it("marks default rest forwarding unsupported instead of passing a packed array as one argument", () => {
it("forwards an inherited rest array through default constructors", () => {
const { file, diagnostics } = lower(`
class Grand { constructor(...values: number[]) {} }
class Base extends Grand {}
class Derived extends Base {}
export function create(): Derived { return new Derived(1, 2); }
`);

for (const className of ["Base", "Derived"]) {
for (const [className, parent] of [["Base", "Grand"], ["Derived", "Base"]]) {
const ctor = methodOf(classByName(file, className), "constructor");
const stmts = ctor.body!.cfg.blocks.flatMap((block) => block.stmts);
const forwardedCall = stmts.map(constructorCallOf).find((call) => call !== undefined);

expect(ctor.signature.parameters).toMatchObject([{ name: "values", isRest: true }]);
expect(stmts).toContainEqual(expect.objectContaining({ _: "UnsupportedStmt" }));
expect(stmts.some((stmt) => constructorCallOf(stmt) !== undefined)).toBe(false);
expect(forwardedCall).toMatchObject({
method: { declaringClass: { name: parent }, parameters: [{ name: "values", isRest: true }] },
args: [{ name: "values" }],
forwardRestArguments: true,
});
}

expect(diagnostics.messages.filter((message) =>
message.includes("unsupported default constructor forwarding of rest arguments"),
)).toHaveLength(2);
expect(diagnostics.messages).toEqual([]);
});

it("uses an explicit intermediate constructor as the forwarding boundary", () => {
Expand Down
Loading