Skip to content

Rust: Support non-universal impl blocks #19372

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 10 commits into
base: main
Choose a base branch
from

Conversation

paldepind
Copy link
Contributor

@paldepind paldepind commented Apr 24, 2025

Adds type inference support for non-universal impl blocks. By "non-universal" we mean impl blocks that target generic types but which are not valid for all instantiations of the generic type.

For instance

impl<T> Option<Option<T>> {
  fn flatten(self) -> Option<T> { ... }
}

where the flatten method is only valid for some Option instantiations.

Non-universal impl block affect both method resolution and trait implementations as the method/trait implementation can be valid only for some instantiations of a type. A Foo<i64> might have a different set of methods/traits than a Foo<String>. Finding the right method/trait implementation is the crux of the matter. The tests have examples of this.

I've tried to document the new additions in this PR with QLdoc, but here are some additional high-level comments on the changes:

  • As mentioned above, with non-universal impl blocks it is no longer enough to know the root of a type to determine which traits it implements and which methods it supports. This affects a bunch of things.

    • The getMethod and getABaseTypeMention member predicate on Type is removed, as a Type is now not enough to determine these things.
    • The new conditionSatisfiesConstraint takes a TypeMention as its second parameter as a Type is not enough.
  • In this PR I've split subtype handling (inferring type parameters through supertypes) from constraint handling (inferring type parameters from type parameter interface constraints (in C#)/trait bounds (in Rust). The former is now the sole job of the AccessBaseType sub-module and the later is done in the new AccessConstraint sub-module. There's also a predicate for each in the module signature: getABaseTypeMention and conditionSatisfiesConstraint.

    This has both pros and cons:

    • PRO: Languages (like Rust) that don't have subtyping can leave getABaseTypeMention as none() and avoid any computation related to subtyping.
    • PRO: Constraints are now more complicated and I don't think any languages have subtyping that is similarly complicated. So subtyping is supported in a simpler and potentially more efficient way.
    • CON: It leads to more code within the shared library compared to handling these two things uniformly as before.
    • CON: In languages where these things are identical, i.e., C# where the question "does T implement the interface I" is equal to asking "is T a subtype of I" doing it as one thing could be more performant.

    All in all I'm not sure which approach is best, but when I made this change performance improved quite a bit (but that was before making some other optimizations) so that's why I ended up with this. Another approach (in follow up work) could be to 1/ merge the two things back again, 2/ add a getVariance predicate for access positions than can be covariant or invariant, 3/ only do subtyping for covariant positions, 4/ make all positions invariant for Rust. That should give equal performance for Rust, cut down on the duplicated code and hopefully the optimization with countConstraintImplementations would mean that subtyping for languages like C# wouldn't regress in performance (but we'd have no way to measure that).

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Apr 24, 2025
@paldepind paldepind force-pushed the rust-ti-implementing-type-method branch from c66a71a to a9afbb3 Compare April 28, 2025 13:15
@paldepind paldepind force-pushed the rust-ti-implementing-type-method branch 2 times, most recently from 76baa34 to 02115f6 Compare April 29, 2025 09:18
@paldepind paldepind marked this pull request as ready for review April 29, 2025 12:54
@paldepind paldepind requested a review from a team as a code owner April 29, 2025 12:54
@paldepind paldepind requested a review from hvitved April 29, 2025 12:54
@paldepind paldepind added the no-change-note-required This PR does not need a change note label Apr 29, 2025
@paldepind paldepind force-pushed the rust-ti-implementing-type-method branch from 439b202 to 68860b1 Compare May 1, 2025 10:00
@paldepind paldepind force-pushed the rust-ti-implementing-type-method branch from 68860b1 to a545361 Compare May 1, 2025 10:36
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.

Results on Rust LGTM - fewer consistency check failures, DCA looks good, except there appears to be an analysis time slowdown. It might be worth checking what's causing that, whether it affects other languages, and whether it's easy to fix.

result = this.getStaticTargetFrom(true)
or
not exists(this.getStaticTargetFrom(true)) and
result = this.getStaticTargetFrom(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we prioritize results with fromSource above everything else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's because the extractor currently duplicates function in source: once as a function from the source code and once as a function from library code. Hence we favor those in the source as the non-source one is probably a duplicate of the same thing.

I've added a comment now to make this clear.

override Location getLocation() { result = trait.getLocation() }
}

/** A type abstraction. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd appreciate a bit more QLDoc here. What is a "type abstraction" in this context? (assuming it's not a Rust term)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A tried to explain this over in the shared module. Would it make sense to repeat the Rust specific part of that here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, that looks good, but a little difficult to find for someone who is here. I think a reference to it (as in, "see TypeAbstraction in ...") would be sufficient.

result = tp.(SelfTypeParameter).getTrait()
}

predicate conditionSatisfiesConstraint(
Copy link
Contributor

Choose a reason for hiding this comment

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

This could also do with QLDoc, in particular explaining the condition and constraint parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added QLDoc for this and TypeAbstraction as mentioned above.

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

First batch of review comments, mostly concerning performance.

Comment on lines 330 to 344
* Note that the type parameters in `abs` significantly change the meaning
* of type parameters that occur in `condition`. For instance, in the Rust
* example
* ```rust
* fn foo<T: Trait>() { }
* ```
* we have that the type parameter `T` satisfies the constraint `Trait`. But,
* only that specific `T` satisfy the constraint. Hence we would not have
* `T` in `abs`. On the other hand, in the Rust example
* ```rust
* impl<T> Trait for T { }
* ```
* the constraint `Trait` is in fact satisfied for all types, and we would
* have `T` in `abs` to make it free in the condition.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

This bit is hard to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried to rework this text. Hopefully it's a bit clearer now. I wanted to highlight this example, as it's the reason why I had to add abs in the first place.

Comment on lines 362 to 364
* Holds if `abs` is a type abstraction under which `tm` occurs and if
* `app` is potentially the result of applying the abstraction to type
* some type argument.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate what under which `tm` occurs means?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just mean that the type parameters introduced in abs are in scope in tm, that is, tm is "under" the type abstraction. I've added an example and a bit more text to make this clearer.

@paldepind paldepind force-pushed the rust-ti-implementing-type-method branch 2 times, most recently from d55edd7 to 0cf60c4 Compare May 12, 2025 09:12
* `potentialInstantiationOf`. Defaults to simply projecting the third
* argument of `potentialInstantiationOf`.
*/
default predicate relevantTypeMention(TypeMention tm) { potentialInstantiationOf(_, _, tm) }

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for tm, but the QLDoc mentions constraint
@paldepind
Copy link
Contributor Author

I think the PR is now ready for another look :)

DCA shows a big slowdown on rust but I can't reproduce anything locally. For me both getStaticTarget, resolveMethodCallExpr, and inferType quick eval at basically the same time as on main. I can try an investigate some more, but perhaps someone know of a good way to see what might be up?

This fixes a test failure where duplicated functions from extraction caused a bunch of spurious results to pop up
@hvitved
Copy link
Contributor

hvitved commented May 14, 2025

DCA shows a big slowdown on rust but I can't reproduce anything locally.

I can reproduce; will work on a fix.

@paldepind
Copy link
Contributor Author

I can reproduce; will work on a fix.

Great that you can reproduce and thanks for looking into it. What did you do to reproduce?

@hvitved
Copy link
Contributor

hvitved commented May 14, 2025

What did you do to reproduce?

I ran DataFlowConsistencyCounts.ql, which was reported in the stage stats report by DCA.

@hvitved
Copy link
Contributor

hvitved commented May 14, 2025

It only now occurred to me that it was not my proposed changes that resulted in the slowdown on rust; it actually improved things significantly, as the analysis timed out before those changes 😌

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

Very impressive work. It would be nice if we can, as you hint at in the description, simplify the code via generalization, but let's postpone that as follow-up work.

@@ -1,13 +0,0 @@
multipleMethodCallTargets
Copy link
Contributor

Choose a reason for hiding this comment

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

This file can now be deleted.

@@ -212,6 +237,17 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
TypePath cons(TypeParameter tp, TypePath suffix) { result = singleton(tp).append(suffix) }
}

/** A class that represents a type tree. */
private signature class TypeTreeSig {
Copy link
Contributor

Choose a reason for hiding this comment

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

Used in the exposed IsInstantiationOf module, so should be public.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we could choose a better name; perhaps Resolvable or similar? The three instantiations are TypeMention, RelevantAccess, and ReceiverExpr, neither of which are types themselves, but which can resolve to types.

* Holds if
* - `abs` is a type abstraction that introduces type variables that are
* free in `condition` and `constraint`,
* - and for every instantiation of the type parameters the resulting
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps make it explicit: of the type parameters from `abs`

*
* Example in Rust:
* ```rust
* impl<A> Trait<i64, B> for Type<String, A> { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the B be A?

@@ -265,8 +362,272 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
result = tm.resolveTypeAt(TypePath::nil())
}

signature module IsInstantiationOfInputSig<TypeTreeSig App> {
Copy link
Contributor

Choose a reason for hiding this comment

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

QL doc, simply say that it provides the input to IsInstantiationOf.

path = prefix.append(suffix)
)
)
}
}

private module AccessConstraint {
private newtype TTRelevantAccess =
TRelevantAccess(Access a, AccessPosition apos, TypePath path, Type constraint) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels wrong that constraint is part of an access; I tried removing it locally, and that seemed to work (it requires exposing Type constraint in the predicate hasConstraintMention.

* // ^^^^^^ a type abstraction
* ```
*/
class TypeAbstraction = T::TypeAbstraction;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this QL doc in to the class being aliased.

// The `Self` type parameter is an implementation of the trait, so it has
// all the trait's methods.
result = trait.(ItemNode).getASuccessor(name)
final class ImplTypeAbstraction extends TypeAbstraction, Impl {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to collect ImplTypeAbstraction and TraitTypeAbstraction as simply GenericParamList? That way it would also better match the ^^^^ markers in the comments.

Comment on lines +971 to +983
pragma[nomagic]
private Type receiverRootType(Expr e) {
any(MethodCallExpr mce).getReceiver() = e and
result = inferType(e)
}

pragma[nomagic]
private Type inferReceiverType(Expr e, TypePath path) {
exists(Type root | root = receiverRootType(e) |
// for reference types, lookup members in the type being referenced
if root = TRefType()
then result = inferType(e, TypePath::cons(TRefTypeParameter(), path))
else result = inferType(e, path)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these two can be replaced by

  pragma[nomagic]
  private Type inferReceiverType(Expr e, TypePath path) {
    exists(TypePath path0 |
      any(MethodCallExpr mce).getReceiver() = e and
      result = inferType(e, path0)
    |
      path0 = TypePath::cons(TRefTypeParameter(), path)
      or
      not path0.isCons(TRefTypeParameter(), _) and
      path = path0
    )
  }

which is better, because it does not involve non-linear recursion.

Copy link
Contributor

Choose a reason for hiding this comment

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

And the predicate logic may just as well be moved directly into resolveTypeAt below (still with nomagic).

Comment on lines +1066 to +1074
pragma[inline]
private Type inferRootTypeDeref(AstNode n) {
exists(Type t |
t = inferType(n) and
// for reference types, lookup members in the type being referenced
if t = TRefType()
then result = inferType(n, TypePath::singleton(TRefTypeParameter()))
else result = t
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, better to avoid non-linear recursion when possible:

  pragma[inline]
  private Type inferRootTypeDeref(AstNode n) {
    result = inferType(n) and
    result != TRefType()
    or
    // for reference types, lookup members in the type being referenced
    result = inferType(n, TypePath::singleton(TRefTypeParameter()))
  }

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 Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants