Skip to content

Commit b603d97

Browse files
committed
Fixed check not having access to defined column
1 parent b4ba655 commit b603d97

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

Sources/Compiler/Sema/Builtins.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ enum Builtins {
140140
"unixepoch": Function(returning: .integer),
141141
"julianday": Function(returning: .real),
142142
"strftime": Function(
143-
.text,
143+
.text, .any,
144144
returning: .text,
145145
variadic: true
146146
) { _, args, location, diagnostics in

Sources/Compiler/Sema/StmtTypeChecker.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,14 @@ extension StmtTypeChecker {
12991299
tableColumns: borrowing Columns,
13001300
tableName: Substring
13011301
) -> Type {
1302+
let nominal: Type = .nominal(column.type.name.value)
1303+
1304+
var type: Type = if let alias = column.type.alias {
1305+
.alias(nominal, .explicit(alias.identifier.value))
1306+
} else {
1307+
nominal
1308+
}
1309+
13021310
var isNotNullable = false
13031311
for constraint in column.constraints {
13041312
switch constraint.kind {
@@ -1309,6 +1317,12 @@ extension StmtTypeChecker {
13091317
case let .check(expr):
13101318
inNewEnvironment { typeChecker in
13111319
typeChecker.env.import(columns: tableColumns)
1320+
// Technically this could be incorrect if the check
1321+
// comes before the not null. But it'll do.
1322+
typeChecker.env.import(
1323+
column: column.name.value,
1324+
with: isNotNullable ? type : .optional(type)
1325+
)
13121326
_ = typeChecker.typeCheck(expr)
13131327
}
13141328
case let .default(expr):
@@ -1353,19 +1367,7 @@ extension StmtTypeChecker {
13531367
))
13541368
}
13551369

1356-
let nominal: Type = .nominal(column.type.name.value)
1357-
1358-
let type: Type = if let alias = column.type.alias {
1359-
.alias(nominal, .explicit(alias.identifier.value))
1360-
} else {
1361-
nominal
1362-
}
1363-
1364-
if isNotNullable {
1365-
return type
1366-
} else {
1367-
return .optional(type)
1368-
}
1370+
return isNotNullable ? type : .optional(type)
13691371
}
13701372

13711373
private mutating func typeCheck(

Tests/CompilerTests/Compiler/CompileTableSchema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
-- CHECK: KIND normal
1313
CREATE TABLE foo (
1414
bar INTEGER PRIMARY KEY,
15-
baz TEXT
15+
baz TEXT CHECK (baz = 'baz')
1616
);
1717

1818
ALTER TABLE foo ADD COLUMN qux TEXT;

0 commit comments

Comments
 (0)