Skip to content

Unified: Type inference for nested types - #22866

Open
hvitved wants to merge 1 commit into
github:mainfrom
hvitved:unified/type-inference-nested-types
Open

hvitved wants to merge 1 commit into
github:mainfrom
hvitved:unified/type-inference-nested-types

Conversation

@hvitved

@hvitved hvitved commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds support for nested types in type inference. Since nested types can interact with both generics and sub typing, we need a way to represent outer types in addition to the nested type itself:

class Outer<TOuter> {
    class Inner<TInner> {
    }
}

class SubOuter<TSubOuter> : Outer<TSubOuter?>

For example, we need to distinguish Outer<Int>.Inner<Int> from Outer<Bool>.Inner<Int>, as well as from SubOuter<Int>.Inner<Int>.

We do this by adding a pseudo type parameter to each nested type, which will hold type information about the outer type. For example, Outer<Int>.Inner<String> is represented as

TypePath Type
"" Inner
"TInner" String
"Inner_Parent" Outer
"Inner_Parent.TOuter" Int

where Inner_Parent is the special type parameter belonging to the nested class Inner.

Things can get further complicated when type aliases are used, which prompted a change to the existing TypeMention logic: Instead of resolving aliases directly in the getTypeAt relation, we split resolution logic into different phases, where the initial getUnexpandedTypeAt does not expand aliases, but instead treat them as ordinary types. Ultimately, in getTypeAt, aliases are fully expanded, and type inference still does not need to worry about them.

DCA is fine.

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from 2c69161 to b14b51a Compare October 21, 2026 13:57
@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch 6 times, most recently from 03284cb to 0455baf Compare October 22, 2026 09:30
@hvitved
hvitved requested a max review from Copilot October 22, 2026 09:30

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

Implicit argument handling and parent-scope checks still produce incorrect or incomplete nested-type inference.

2 open issues
What changed in this PR

Extends Unified’s type inference to represent nested types and preserve their enclosing generic arguments, with Swift regression coverage.

Changes:

  • Adds parent-type parameters and qualified type names.
  • Updates type mentions and enum construction to propagate parent-type paths.
  • Adds nested-type tests and updates inference expectations.
File Description
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Updates expected inference results.
unified/​ql/​test/​library-tests/​type-inference/​nested_types.swift Adds nested-generic, self-type, and extension coverage.
unified/​ql/​test/​library-tests/​type-inference/​generics.swift Removes a resolved MISSING annotation.
unified/​ql/​test/​library-tests/​type-inference/​classes.swift Updates the inline expected nested-type name.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeMention.qll Adds nested-type qualification and implicit argument handling.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInferencePluginSwift.qll Removes Swift-specific enum plugin hooks.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInferencePlugin.qll Removes enum-specific hooks and wrappers.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Integrates parent paths into constructor inference.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​Type.qll Introduces parent-type parameters and qualified names.
unified/​ql/​lib/​codeql/​unified/​internal/​AstExtra.qll Adds shared enum relationships and enum-field modeling.

Review effort: Max

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from 0455baf to e55b569 Compare October 22, 2026 13:31
@hvitved
hvitved requested a max review from Copilot October 22, 2026 13:32

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.

🔵 Needs a closer look

Unresolved generic-inference regressions and changes to recursive type paths require fixes and human validation.

3 open issues
2 resolved since last review

Review effort: Max

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.

🔵 Needs a closer look

Recursive generic substitution and constructor propagation have unresolved correctness concerns and need expert validation.

2 open issues
3 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch 4 times, most recently from 606fac6 to fee52f9 Compare October 23, 2026 09:51
@hvitved
hvitved requested a max review from Copilot October 23, 2026 09: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.

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from fee52f9 to a014cb6 Compare October 23, 2026 12:24
@hvitved
hvitved requested a max review from Copilot October 23, 2026 12:25

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.

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.

🔵 Needs a closer look

The coupled recursive inference changes retain a correctness defect and require human validation.

1 open issue
1 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch 3 times, most recently from 9a2da2f to f27b5df Compare October 29, 2026 19:32
@github-actions github-actions Bot added the Rust Pull requests that update Rust code label Oct 29, 2026
@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch 2 times, most recently from 5ffdc62 to 16e479a Compare October 30, 2026 12:23
@github-actions github-actions Bot removed the Rust Pull requests that update Rust code label Oct 30, 2026
@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from 16e479a to d259816 Compare October 30, 2026 12:34
@hvitved
hvitved requested a max review from Copilot October 30, 2026 12: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.

🟡 Changes recommended

Nested alias handling still loses qualifier information in forwarded references, generic arguments, and tuple elements, causing incorrect outer-type inference for qualified alias uses.

2 open issues
1 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch 2 times, most recently from af6d3b3 to cd773cb Compare October 30, 2026 14:17
@hvitved
hvitved requested a max review from Copilot October 30, 2026 14:31

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

The enum-field inference rule misses generic arguments beneath parent paths, preventing nested enum initializers from acquiring outer type arguments from their annotations.

1 open issue
2 resolved since last review

Review effort: Max

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

The QLDoc references a nonexistent parameter, and test annotations contain internal issue URLs that external contributors cannot access.

2 open issues
1 resolved since last review

Review effort: Max

@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from 12f09e4 to 6475b06 Compare October 31, 2026 09:58
@hvitved
hvitved force-pushed the unified/type-inference-nested-types branch from 6475b06 to 6ca07df Compare November 2, 2026 08:48
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Nov 2, 2026
@hvitved
hvitved marked this pull request as ready for review November 2, 2026 11:08
receiver = i.getArgument(0)
|
enumPrefixPath =
TypePath::singleton(TParentTypeParameterType(cd.(EnumCaseConstructor).getEnumCaseClass()))

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.

Why do enum constructors need special handling here (and in needsContextualTyping)? If the enum case class is considered a subtype of the enum class, doesn't it work out via the normal code path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In the end I decided to not give enum constructors the special types, but instead the type of the whole enum as before. So this logic is about translating the special constructor types to the enum types.

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

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.

4 participants