Skip to content

Commit f4f8150

Browse files
committed
postgres: fix stage 2 self-hosting regression via minimal src surface
step 2 added a usesPostgres field to LLVMGenerator plus a cs_pg_ prefix check in generateExternDeclaration plus a trailing-return workaround in parser-native/transformer.ts. any of those shifted enough state that the native stage 1 compiler began miscompiling generateGlobalVariableDeclarations — storing a pointer into a double slot for isClassInstance. --quick skipped stage 2 so it passed local verify but failed CI. fix: do the -lpq/pg-bridge.o detection entirely in compiler.ts and native-compiler-lib.ts by walking generator.declaredExternFunctions for anything starting with cs_pg_. no changes to llvm-generator.ts, generator-context.ts, or parser-native/transformer.ts. feature is functionally identical end-to-end (verified against local postgres). npm run verify (full, including stage 2) is green on this commit.
1 parent 73a4226 commit f4f8150

5 files changed

Lines changed: 21 additions & 26 deletions

File tree

src/codegen/infrastructure/generator-context.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,6 @@ export interface IGeneratorContext {
800800
getUsesTimers(): boolean;
801801
setUsesTreeSitter(value: boolean): void;
802802
setUsesSqlite(value: boolean): void;
803-
setUsesPostgres(value: boolean): void;
804-
getUsesPostgres(): boolean;
805803
setUsesCurl(value: boolean): void;
806804
setUsesOs(value: boolean): void;
807805
setUsesUvHrtime(value: boolean): void;
@@ -1036,7 +1034,6 @@ export class MockGeneratorContext implements IGeneratorContext {
10361034
public usesPromises: number = 0;
10371035
public usesTimers: number = 0;
10381036
public usesSqlite: number = 0;
1039-
public usesPostgres: number = 0;
10401037
public usesCurl: number = 0;
10411038
public usesUvHrtime: number = 0;
10421039
public usesCrypto: number = 0;
@@ -1241,12 +1238,6 @@ export class MockGeneratorContext implements IGeneratorContext {
12411238
setUsesSqlite(value: boolean): void {
12421239
this.usesSqlite = value ? 1 : 0;
12431240
}
1244-
setUsesPostgres(value: boolean): void {
1245-
this.usesPostgres = value ? 1 : 0;
1246-
}
1247-
getUsesPostgres(): boolean {
1248-
return this.usesPostgres !== 0;
1249-
}
12501241
setUsesCurl(value: boolean): void {
12511242
this.usesCurl = value ? 1 : 0;
12521243
}

src/codegen/llvm-generator.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ export class LLVMGenerator extends BaseGenerator implements IGeneratorContext {
265265
public usesStringBuilder: number = 0;
266266
public usesLLVM: number = 0;
267267
public usesLLD: number = 0;
268-
public usesPostgres: number = 0;
269268
public usesCompression: number = 0;
270269
public usesYaml: number = 0;
271270
private stringBuilderSlen: Map<string, string> = new Map();
@@ -993,12 +992,6 @@ export class LLVMGenerator extends BaseGenerator implements IGeneratorContext {
993992
public getUsesLLD(): boolean {
994993
return this.usesLLD !== 0;
995994
}
996-
public setUsesPostgres(value: boolean): void {
997-
this.usesPostgres = value ? 1 : 0;
998-
}
999-
public getUsesPostgres(): boolean {
1000-
return this.usesPostgres !== 0;
1001-
}
1002995
public setUsesCompression(value: boolean): void {
1003996
this.usesCompression = value ? 1 : 0;
1004997
}
@@ -1415,7 +1408,6 @@ export class LLVMGenerator extends BaseGenerator implements IGeneratorContext {
14151408
this.usesAsyncFs = 0;
14161409
this.usesGC = 0;
14171410
this.usesMathRandom = 0;
1418-
this.usesPostgres = 0;
14191411

14201412
this.ast = ast;
14211413

@@ -3387,7 +3379,6 @@ export class LLVMGenerator extends BaseGenerator implements IGeneratorContext {
33873379
this.declaredExternFunctions.push(func.name);
33883380
if (func.name.startsWith("cs_llvm_")) this.usesLLVM = 1;
33893381
if (func.name.startsWith("cs_lld_")) this.usesLLD = 1;
3390-
if (func.name.startsWith("cs_pg_")) this.usesPostgres = 1;
33913382
return `declare ${retType} @${func.name}(${paramLlvmTypes.join(", ")})\n`;
33923383
}
33933384

src/compiler.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ export function compile(
406406
if (generator.usesSqlite) {
407407
linkLibs += " -lsqlite3";
408408
}
409-
if (generator.usesPostgres) {
409+
let usesPostgres = false;
410+
for (let i = 0; i < generator.declaredExternFunctions.length; i++) {
411+
if (generator.declaredExternFunctions[i].startsWith("cs_pg_")) {
412+
usesPostgres = true;
413+
break;
414+
}
415+
}
416+
if (usesPostgres) {
410417
linkLibs += " -lpq";
411418
}
412419
if (generator.usesHttpServer) {
@@ -433,7 +440,7 @@ export function compile(
433440
if (generator.usesSqlite) {
434441
linkLibs = `-L${brewPrefix}/sqlite/lib ` + linkLibs;
435442
}
436-
if (generator.usesPostgres) {
443+
if (usesPostgres) {
437444
linkLibs = `-L${brewPrefix}/libpq/lib ` + linkLibs;
438445
}
439446
if (generator.usesHttpServer || generator.usesCompression) {
@@ -464,7 +471,7 @@ export function compile(
464471
const arenaBridgeObj = `${bridgePath}/arena-bridge.o`;
465472
const cpSpawnObj = generator.getUsesSpawn() ? `${bridgePath}/child-process-spawn.o` : "";
466473
const curlBridgeObj = generator.usesCurl ? `${bridgePath}/curl-bridge.o` : "";
467-
const pgBridgeObj = generator.usesPostgres ? `${bridgePath}/pg-bridge.o` : "";
474+
const pgBridgeObj = usesPostgres ? `${bridgePath}/pg-bridge.o` : "";
468475
const compressBridgeObj = generator.usesCompression ? `${bridgePath}/compress-bridge.o` : "";
469476
const yamlBridgeObj = generator.usesYaml ? `${bridgePath}/yaml-bridge.o` : "";
470477
let extraObjs = "";

src/native-compiler-lib.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,14 @@ export function compileNative(inputFile: string, outputFile: string): void {
664664
if (generator.getUsesSqlite()) {
665665
linkLibs = "-lsqlite3 " + linkLibs;
666666
}
667-
if (generator.getUsesPostgres()) {
667+
let usesPostgres: boolean = false;
668+
for (let i = 0; i < generator.declaredExternFunctions.length; i++) {
669+
if (generator.declaredExternFunctions[i].startsWith("cs_pg_")) {
670+
usesPostgres = true;
671+
break;
672+
}
673+
}
674+
if (usesPostgres) {
668675
linkLibs = "-lpq " + linkLibs;
669676
}
670677
if (generator.getUsesHttpServer()) {
@@ -698,7 +705,7 @@ export function compileNative(inputFile: string, outputFile: string): void {
698705
const arenaBridgeObj = effectiveBridgePath + "/arena-bridge.o";
699706
const cpSpawnObj = generator.getUsesSpawn() ? effectiveBridgePath + "/child-process-spawn.o" : "";
700707
const curlBridgeObj = generator.getUsesCurl() ? effectiveBridgePath + "/curl-bridge.o" : "";
701-
const pgBridgeObj = generator.getUsesPostgres() ? effectiveBridgePath + "/pg-bridge.o" : "";
708+
const pgBridgeObj = usesPostgres ? effectiveBridgePath + "/pg-bridge.o" : "";
702709
const compressBridgeObj = generator.getUsesCompression()
703710
? effectiveBridgePath + "/compress-bridge.o"
704711
: "";
@@ -825,7 +832,7 @@ export function compileNative(inputFile: string, outputFile: string): void {
825832
if (fs.existsSync("/usr/local/opt/sqlite/lib"))
826833
linkLibs = "-L/usr/local/opt/sqlite/lib " + linkLibs;
827834
}
828-
if (generator.getUsesPostgres()) {
835+
if (usesPostgres) {
829836
if (fs.existsSync("/opt/homebrew/opt/libpq/lib"))
830837
linkLibs = "-L/opt/homebrew/opt/libpq/lib " + linkLibs;
831838
if (fs.existsSync("/usr/local/opt/libpq/lib"))
@@ -906,7 +913,7 @@ export function compileNative(inputFile: string, outputFile: string): void {
906913
if (fs.existsSync("/usr/local/opt/sqlite/lib"))
907914
linkLibs = "-L/usr/local/opt/sqlite/lib " + linkLibs;
908915
}
909-
if (generator.getUsesPostgres()) {
916+
if (usesPostgres) {
910917
if (fs.existsSync("/opt/homebrew/opt/libpq/lib"))
911918
linkLibs = "-L/opt/homebrew/opt/libpq/lib " + linkLibs;
912919
if (fs.existsSync("/usr/local/opt/libpq/lib"))

src/parser-native/transformer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ function transformExpression(node: TreeSitterNode): Expression {
614614
default:
615615
return { type: "variable", name: "undefined" };
616616
}
617-
return { type: "variable", name: "undefined" };
618617
}
619618

620619
// ============================================

0 commit comments

Comments
 (0)