Skip to content

Rust: Implement type inference for trait objects/dyn types #20084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Jul 17, 2025

This PR implements support for trait objects on the form dyn Trait.

The implementation works as follows:

  1. If dyn Trait appears somewhere as a declared type then we create a DynTraitType that consists of the trait Trait.
  2. The dyn Trait has a type parameter for every type parameter of Trait. There is a 1-to-1 correspondence but they're not equal.
  3. The dyn Trait type is made to implement Trait as if there was an impl<A, B, ...> Trait<A, B, ...> for dyn Trait<A, B, ..> block in the source.

Currently the PR doesn't account for the auto traits that one can specify with a +, for instance dyn Clone + Send + Sync. I don't know if we have a need to consider those, but if we do it should be possible by building on top of the work in this PR.

DCA shows a 0.33% point increase in resolved call targets and no change in analysis time.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jul 17, 2025
@paldepind paldepind force-pushed the rust/type-inference-trait-object branch from 59e3ac7 to 8de4313 Compare July 17, 2025 14:14
@paldepind paldepind force-pushed the rust/type-inference-trait-object branch 2 times, most recently from 55c3674 to 219e26e Compare July 18, 2025 07:30
@paldepind paldepind marked this pull request as ready for review July 18, 2025 08:18
@paldepind paldepind requested a review from a team as a code owner July 18, 2025 08:18
@paldepind
Copy link
Contributor Author

paldepind commented Jul 18, 2025

Note: The first DCA run showed an increase in type inference inconsistencies. I've fixed those and started a second DCA run.

EDIT: The second DCA shows that the type inference inconsistencies are now fixed. The number of type inference inconsistencies now only decreases.

@paldepind paldepind force-pushed the rust/type-inference-trait-object branch from e301fd5 to f5605c9 Compare July 23, 2025 12:10
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

QL LGTM, though I didn't quite follow every detail in TypeMention.qll. Tests look great. I'm looking at the DCA run as well but it looks like you've already fixed an issue there.

override TupleField getTupleField(int i) { none() }

override DynTraitTypeParameter getTypeParameter(int i) {
result = TDynTraitTypeParameter(trait.getGenericParamList().getTypeParam(i))
Copy link
Contributor

Choose a reason for hiding this comment

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

How come the definition of TDynTraitTypeParameter (on line 34) uses .getAGenericParam(), but here we use .getTypeParam(i)? I was expecting them to match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I've changed the getAGenericParam to getATypeParam 👍. I didn't think much of the difference, but I guess the former can contain stuff that are not type parameters (lifetimes?) so using getATypeParam is probably more correct.

@@ -97,6 +97,9 @@ private module Input1 implements InputSig1<Location> {
id = 2
or
kind = 1 and
id = idOfTypeParameterAstNode(tp0.(DynTraitTypeParameter).getTypeParam())
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this separate from the cases for kind = 2? Can a DynTraitTypeParameter overlap with one or more of the cases in the bit below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, TypeParamTypeParameter below uses the same TypeParam AST nodes so we need a new kind.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy