Skip to content

Swift: Add type inference tests#21777

Open
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:swift/type-inference-tests
Open

Swift: Add type inference tests#21777
hvitved wants to merge 1 commit intogithub:mainfrom
hvitved:swift/type-inference-tests

Conversation

@hvitved
Copy link
Copy Markdown
Contributor

@hvitved hvitved commented Apr 30, 2026

Adds a bunch of test cases for verifying inferred types, similar to what we have for Rust. Most of the tests have been written using Copilot CLI, but I have verified and tweaked them manually afterwards.

@github-actions github-actions Bot added the Swift label Apr 30, 2026
@hvitved hvitved force-pushed the swift/type-inference-tests branch from b98238d to 4042bbe Compare April 30, 2026 11:46
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Apr 30, 2026
@hvitved hvitved marked this pull request as ready for review April 30, 2026 12:24
@hvitved hvitved requested a review from a team as a code owner April 30, 2026 12:24
Copilot AI review requested due to automatic review settings April 30, 2026 12:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Swift type-inference library test suite using inline expectations (similar in spirit to existing Rust type inference tests) to validate inferred types and resolved targets.

Changes:

  • Introduces a large Swift test corpus (typeinference.swift) with inline // $ type=... and // $ target=... expectations.
  • Adds a new QL test driver (type-inference.ql) that interprets inline expectations for resolved targets and inferred types.
  • Adds an (intentionally) empty .expected file for the inline-expectations-based test.
Show a summary per file
File Description
swift/ql/test/library-tests/type-inference/typeinference.swift New Swift source corpus containing inline type/target expectations.
swift/ql/test/library-tests/type-inference/type-inference.ql New inline-expectations test query for resolution + type inference.
swift/ql/test/library-tests/type-inference/type-inference.expected Empty expected file consistent with inline expectations tests.

Copilot's findings

Comments suppressed due to low confidence (1)

swift/ql/test/library-tests/type-inference/typeinference.swift:1061

  • This section is labeled “subscript overloading”, but the code defines overloaded get methods rather than Swift subscript declarations. If the intent is to test subscript resolution/type inference, it would be better to implement real subscripts (e.g., subscript(_ index: Int) and subscript(_ key: String)) and update the expectations; otherwise, rename the section to avoid misleading coverage.
// --- Overload: subscript overloading ---

class MultiSubscript {
  var data : [Int] = [1, 2, 3]
  var dict : [String: Int] = ["a": 1]

  // MultiSubscript.init
  init() {}

  // MultiSubscript.get(_:)
  func get(_ index: Int) -> Int {
    return data[index]
  }

  // MultiSubscript.get(_:String)
  func get(_ key: String) -> Int {
    return dict[key] ?? 0
  }
}
  • Files reviewed: 2/3 changed files
  • Comments generated: 3

Comment on lines +669 to +692
// --- Overload by return type (contextual type) ---

class OverloadByReturn {
// OverloadByReturn.create()->Int
func create() -> Int {
return 0
}

// OverloadByReturn.create()->String
func create() -> String {
return ""
}

// OverloadByReturn.create()->Double
func create() -> Double {
return 0.0
}
}

func testOverloadByReturn() {
let o = OverloadByReturn() // $ target=init()
let r1 : Int = o.create() // $ type=r1:Int target=OverloadByReturn.create()->Int
let r2 : String = o.create() // $ type=r2:String target=OverloadByReturn.create()->String
let r3 : Double = o.create() // $ type=r3:Double target=OverloadByReturn.create()->Double
Comment on lines +243 to +257
// --- Subscripts ---

struct Matrix {
var data : [[Int]]

// Matrix.init
init(data: [[Int]]) {
self.data = data
}

// Matrix.rowCount
func rowCount() -> Int {
return data.count
}
}
@@ -0,0 +1,1372 @@
var topLevelDecl : Int = 0
0
Copy link
Copy Markdown
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Should there also be tests related to key paths?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Swift

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants