-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: main
Are you sure you want to change the base?
Rust: Support non-universal impl
blocks
#19372
Conversation
c66a71a
to
a9afbb3
Compare
76baa34
to
02115f6
Compare
…rait implementation
439b202
to
68860b1
Compare
68860b1
to
a545361
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. */ |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
* 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. | ||
*/ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
* 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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
shared/typeinference/codeql/typeinference/internal/TypeInference.qll
Outdated
Show resolved
Hide resolved
d55edd7
to
0cf60c4
Compare
* `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
I think the PR is now ready for another look :) DCA shows a big slowdown on |
This fixes a test failure where duplicated functions from extraction caused a bunch of spurious results to pop up
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? |
I ran |
It only now occurred to me that it was not my proposed changes that resulted in the slowdown on |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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> { } |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
).
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 | ||
) |
There was a problem hiding this comment.
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()))
}
Adds type inference support for non-universal
impl
blocks. By "non-universal" we meanimpl
blocks that target generic types but which are not valid for all instantiations of the generic type.For instance
where the
flatten
method is only valid for someOption
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. AFoo<i64>
might have a different set of methods/traits than aFoo<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.getMethod
andgetABaseTypeMention
member predicate onType
is removed, as aType
is now not enough to determine these things.conditionSatisfiesConstraint
takes aTypeMention
as its second parameter as aType
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 newAccessConstraint
sub-module. There's also a predicate for each in the module signature:getABaseTypeMention
andconditionSatisfiesConstraint
.This has both pros and cons:
getABaseTypeMention
asnone()
and avoid any computation related to subtyping.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 becovariant
orinvariant
, 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 withcountConstraintImplementations
would mean that subtyping for languages like C# wouldn't regress in performance (but we'd have no way to measure that).