Skip to content

Support variance in type inference - #23067

Draft
hvitved wants to merge 3 commits into
mainfrom
hvitved-share-type-matching-logic
Draft

hvitved wants to merge 3 commits into
mainfrom
hvitved-share-type-matching-logic

Conversation

@hvitved

@hvitved hvitved commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Base-type matching and type-parameter-constraint matching implement the same inference steps through separate representations and predicates. This refactor shares those steps without adding tagged wrapper dispatch.

Introduce MatchingPosition for argument positions and constrained type arguments. Each position supplies its declared and inferred types, target, and candidate-selection rules. Both cases then use the same relevant-access representation, constraint matching, and baseTypeMatch path projection. Existing base-type candidate filtering and type-parameter-only disambiguation are preserved.

Validation: codeql test run --additional-packs=. unified/ql/test/library-tests/type-inference passes with unchanged expected results.

hvitved and others added 2 commits November 18, 2026 12:56
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved hvitved changed the title Share type inference constraint matching through matching positions Support variance in type inference Nov 18, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hvitved
hvitved force-pushed the hvitved-share-type-matching-logic branch from 861cd99 to 7515c26 Compare November 18, 2026 13:27
TMkCovariant() or
TMkContravariant()

/** Describes how a type parameter relates to its enclosing type. */
TMkContravariantRelation() or
TMkInvariantRelation()

/** Describes the relation used while matching two type trees. */
private import Input2

private predicate varianceEnabled() {
exists(TypeParameter tp, Variance variance | variance = getTypeParameterVariance(tp))
}

private predicate canDescend(Tree tree) {
not exists(getTypePathLimit()) or tree.getDepth() < getTypePathLimit()
}

private MatchingRelation childRelation(MatchingRelation relation, TypeParameter tp) {
not exists(Variance parameterVariance | parameterVariance = getTypeParameterVariance(tp)) and

private predicate projection(Tree tree, Type root) {
exists(
Term term, Constraint constraint, Tree actual, Tree expected, MatchingRelation relation,

private predicate projection(Tree tree, Type root) {
exists(
Term term, Constraint constraint, Tree actual, Tree expected, MatchingRelation relation,

pragma[nomagic]
private predicate relevantOrigin(Tree tree, TypePath path) {
exists(Term term, Constraint constraint, Tree expected, MatchingRelation relation |
/** Gets a contextual type for an unknown subtree of the original term. */
pragma[nomagic]
Type contextualType(Term term, Constraint constraint, TypePath path) {
exists(Tree actual, Tree expected, MatchingRelation relation, TypePath suffix |

query predicate bounds(string name, string parameter, string relationName, string path, string type) {
exists(
Actual actual, TypeMention constraint, TypeParameter tp, TypePath p, Type t,

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants