I'm attempting to write a simple script - i.e. one file, largely top-level code - that uses PostgresNIO's async interface. I'm running into trouble with what appears to be Swift inferring the wrong (non-async) overloads in calls to various PostgresNIO functions:
import Foundation
import Logging
import PostgresNIO
let logger = Logger(label: "example")
let dbConfig = PostgresConnection.Configuration(
unixSocketPath: "/run/postgresql/.s.PGSQL.5432",
username: NSUserName(),
password: nil,
database: "example"
)
let dbConnection = try await PostgresConnection.connect(
configuration: dbConfig,
id: 1,
logger: logger
)
try await dbConnection.query(
"""
CREATE OR REPLACE TRIGGER ...
""",
logger: logger
)
for try await _ in try await dbConnection.listen("example") {
let rows = try await dbConnection.query(
"SELECT ...",
logger: logger
)
}
The compiler reports:
.../Sources/main.swift:20:24: warning: result of call to 'query(_:logger:file:line:)' is unused
try await dbConnection.query(
^ ~
.../Sources/main.swift:20:5: warning: no 'async' operations occur within 'await' expression
try await dbConnection.query(
^
.../Sources/main.swift:20:1: warning: no calls to throwing functions occur within 'try' expression
try await dbConnection.query(
^
.../Sources/main.swift:30:20: warning: no 'async' operations occur within 'await' expression
let rows = try await dbConnection.query(
^
.../Sources/main.swift:30:16: warning: no calls to throwing functions occur within 'try' expression
let rows = try await dbConnection.query(
^
.../Sources/main.swift:30:9: warning: initialization of immutable value 'rows' was never used; consider replacing with assignment to '_' or removing it
let rows = try await dbConnection.query(
~~~~^~~~
_
My understanding (as a beginner) was that, per SE-0343, these top-level await
s should cause the top-level code to be interpreted as an async context, so why is Swift inferring the synchronous overloads of these functions?
Or am I misreading or drawing the wrong conclusions from these errors, and something else is going wrong?