Skip to content

Unified: Improve type inference for constructor calls - #22796

Merged
hvitved merged 7 commits into
github:mainfrom
hvitved:unified/type-inference-constructors
Oct 21, 2026
Merged

hvitved merged 7 commits into
github:mainfrom
hvitved:unified/type-inference-constructors

Conversation

@hvitved

@hvitved hvitved commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

This PR starts treating constructors like ordinary instance methods without return types, in order to be able to handle inheritance in the same way as for ordinary methods.

class A<Ta> {
  init(x : Ta) { ... }
}

class B<Tb> : A<Tb?> { }

A.init(0);        // (1) has type `A<Int>`
B.init(.some(0)); // (2) has type `B<Int>`
B<Int>(.none);    // (3) has type `B<Int>`

Because the constructor calls are treated as normal instance method calls, we use the type qualifier to represent the receiver, and then allow for contextual type information to flow into the receiver:

  • (1) The receiver has type A, Ta is inferred to be instantiated to Int, which is propagated directly back into the receiver.
  • (2) The receiver has type B, Ta is inferred to be instantiated to Int? which in turn means Tb is inferred to be instantiated to Int, which is then propagated back into the receiver.
  • (3) The receiver has type B<Int>, and no further type inference is needed.

DCA shows a small increase in resolution rates.

Commit-by-commit review is encouraged (third commit is best reviewed ignoring whitespace changes).

@hvitved
hvitved force-pushed the unified/type-inference-constructors branch 2 times, most recently from e5f4c43 to 909aa94 Compare October 10, 2026 07:22
@hvitved
hvitved force-pushed the unified/type-inference-constructors branch from 8102a8e to 80c4bfb Compare October 19, 2026 07:54
@hvitved
hvitved force-pushed the unified/type-inference-constructors branch 3 times, most recently from 7a3d1be to 1c20541 Compare October 19, 2026 19:51
@hvitved
hvitved requested a max review from Copilot October 19, 2026 19:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Constructor inference loses contextual argument types and can produce incorrect inherited type mappings and superclass results.

4 open issues
What changed in this PR

Improves constructor-call type inference in Unified, particularly for inherited Swift initializers.

Changes:

  • Models constructors using receiver types.
  • Extends shared contextual inference support.
  • Adds inherited-constructor tests and updates expectations.
File Description
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Updates generated inference expectations.
unified/​ql/​test/​library-tests/​type-inference/​lub.swift Updates an inline expected initializer target.
unified/​ql/​test/​library-tests/​type-inference/​constructors.swift Adds inherited and generic constructor cases.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeMention.qll Excludes enum cases from resolved types.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Adds receiver-based constructor inference.
unified/​ql/​lib/​codeql/​unified/​internal/​ExprPositions.qll Excludes enum cases from type contexts.
shared/​typeinference/​codeql/​typeinference/​internal/​TypeInference.qll Extracts reusable contextual typing logic and adds an inference hook.

Review effort: Max

Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll Outdated
Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll Outdated
Comment thread unified/ql/test/library-tests/type-inference/constructors.swift Outdated
@hvitved
hvitved force-pushed the unified/type-inference-constructors branch from 1c20541 to b3e63f8 Compare October 20, 2026 09:03
@hvitved
hvitved requested a max review from Copilot October 20, 2026 09:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll Outdated
Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll Outdated
Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeMention.qll Outdated
Comment thread unified/ql/test/library-tests/type-inference/constructors.swift Outdated
@hvitved
hvitved force-pushed the unified/type-inference-constructors branch from b3e63f8 to a5037ad Compare October 20, 2026 11:51
@hvitved
hvitved requested a max review from Copilot October 20, 2026 11:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment thread unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll Outdated
Comment thread unified/ql/test/library-tests/type-inference/constructors.swift Outdated
@hvitved
hvitved force-pushed the unified/type-inference-constructors branch from a5037ad to 4898fef Compare October 20, 2026 12:34
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Oct 20, 2026
@hvitved
hvitved marked this pull request as ready for review October 20, 2026 18:18
@hvitved
hvitved requested review from a team as code owners October 20, 2026 18:18
@hvitved
hvitved requested review from asgerf and paldepind October 21, 2026 06:28

@asgerf asgerf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The changes in unified look reasonable to me. I'll defer to @paldepind to approve after reviewing the type inference library changes.

Comment thread unified/ql/test/library-tests/type-inference/constructors.swift Outdated
Comment thread shared/typeinference/codeql/typeinference/internal/TypeInference.qll Outdated
Comment thread shared/typeinference/codeql/typeinference/internal/TypeInference.qll Outdated
@hvitved
hvitved requested a review from paldepind October 21, 2026 12:05

@paldepind paldepind left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 🎉

@hvitved
hvitved merged commit 5be6d86 into github:main Oct 21, 2026
111 of 112 checks passed
@hvitved
hvitved deleted the unified/type-inference-constructors branch October 21, 2026 13:00
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 Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants