mirror of
https://github.com/github/codeql.git
synced 2026-04-25 16:55:19 +02:00
Rust: Improve handling of where clauses in type inference and path resolution
This commit is contained in:
committed by
Tom Hvitved
parent
766083290c
commit
b302f3f98f
@@ -36,5 +36,36 @@ module Impl {
|
||||
not this.hasGenericParamList() and
|
||||
result = 0
|
||||
}
|
||||
|
||||
private int nrOfDirectTypeBounds() {
|
||||
result = this.getTypeBoundList().getNumberOfBounds()
|
||||
or
|
||||
not this.hasTypeBoundList() and
|
||||
result = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `index`th type bound of this trait, if any.
|
||||
*
|
||||
* This includes type bounds directly on the trait and bounds from any
|
||||
* `where` clauses for `Self`.
|
||||
*/
|
||||
TypeBound getTypeBound(int index) {
|
||||
result = this.getTypeBoundList().getBound(index)
|
||||
or
|
||||
exists(WherePred wp |
|
||||
wp = this.getWhereClause().getAPredicate() and
|
||||
wp.getTypeRepr().(PathTypeRepr).getPath().getText() = "Self" and
|
||||
result = wp.getTypeBoundList().getBound(index - this.nrOfDirectTypeBounds())
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type bound of this trait.
|
||||
*
|
||||
* This includes type bounds directly on the trait and bounds from any
|
||||
* `where` clauses for `Self`.
|
||||
*/
|
||||
TypeBound getATypeBound() { result = this.getTypeBound(_) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ private import codeql.rust.elements.internal.generated.TypeParam
|
||||
*/
|
||||
module Impl {
|
||||
private import rust
|
||||
private import codeql.rust.internal.PathResolution
|
||||
|
||||
// the following QLdoc is generated: if you need to edit it, do it in the schema file
|
||||
/**
|
||||
@@ -27,6 +28,36 @@ module Impl {
|
||||
/** Gets the position of this type parameter. */
|
||||
int getPosition() { this = any(GenericParamList l).getTypeParam(result) }
|
||||
|
||||
private int nrOfDirectTypeBounds() {
|
||||
result = this.getTypeBoundList().getNumberOfBounds()
|
||||
or
|
||||
not this.hasTypeBoundList() and
|
||||
result = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `index`th type bound of this type parameter, if any.
|
||||
*
|
||||
* This includes type bounds directly on this type parameter and bounds from
|
||||
* any `where` clauses for this type parameter.
|
||||
*/
|
||||
TypeBound getTypeBound(int index) {
|
||||
exists(TypeBoundList tbl, int offset | result = tbl.getBound(index - offset) |
|
||||
tbl = this.getTypeBoundList() and offset = 0
|
||||
or
|
||||
tbl = this.(TypeParamItemNode).getAWherePred().getTypeBoundList() and
|
||||
offset = this.nrOfDirectTypeBounds()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type bound of this type parameter.
|
||||
*
|
||||
* This includes type bounds directly on this type parameter and bounds from
|
||||
* any `where` clauses for this type parameter.
|
||||
*/
|
||||
TypeBound getATypeBound() { result = this.getTypeBound(_) }
|
||||
|
||||
override string toAbbreviatedString() { result = this.getName().getText() }
|
||||
|
||||
override string toStringImpl() { result = this.getName().getText() }
|
||||
|
||||
@@ -791,9 +791,7 @@ private class StructItemNode extends TypeItemNode instanceof Struct {
|
||||
|
||||
class TraitItemNode extends ImplOrTraitItemNode, TypeItemNode instanceof Trait {
|
||||
pragma[nomagic]
|
||||
Path getABoundPath() {
|
||||
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
|
||||
}
|
||||
Path getABoundPath() { result = super.getATypeBound().getTypeRepr().(PathTypeRepr).getPath() }
|
||||
|
||||
pragma[nomagic]
|
||||
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
|
||||
@@ -924,7 +922,8 @@ private class BlockExprItemNode extends ItemNode instanceof BlockExpr {
|
||||
}
|
||||
|
||||
class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
|
||||
private WherePred getAWherePred() {
|
||||
/** Gets a where predicate for this type parameter, if any */
|
||||
WherePred getAWherePred() {
|
||||
exists(ItemNode declaringItem |
|
||||
this = resolveTypeParamPathTypeRepr(result.getTypeRepr()) and
|
||||
result = declaringItem.getADescendant() and
|
||||
@@ -933,14 +932,12 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
|
||||
}
|
||||
|
||||
pragma[nomagic]
|
||||
Path getABoundPath() {
|
||||
exists(TypeBoundList tbl | result = tbl.getABound().getTypeRepr().(PathTypeRepr).getPath() |
|
||||
tbl = super.getTypeBoundList()
|
||||
or
|
||||
tbl = this.getAWherePred().getTypeBoundList()
|
||||
)
|
||||
Path getTypeBoundPath(int index) {
|
||||
result = super.getTypeBound(index).getTypeRepr().(PathTypeRepr).getPath()
|
||||
}
|
||||
|
||||
Path getABoundPath() { result = this.getTypeBoundPath(_) }
|
||||
|
||||
pragma[nomagic]
|
||||
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
|
||||
|
||||
@@ -956,12 +953,7 @@ class TypeParamItemNode extends TypeItemNode instanceof TypeParam {
|
||||
* ```
|
||||
*/
|
||||
cached
|
||||
predicate hasTraitBound() {
|
||||
Stages::PathResolutionStage::ref() and
|
||||
exists(this.getABoundPath())
|
||||
or
|
||||
exists(this.getAWherePred())
|
||||
}
|
||||
predicate hasTraitBound() { Stages::PathResolutionStage::ref() and exists(this.getABoundPath()) }
|
||||
|
||||
/**
|
||||
* Holds if this type parameter has no trait bound. Examples:
|
||||
|
||||
@@ -151,7 +151,7 @@ private module Input2 implements InputSig2 {
|
||||
TypeMention getABaseTypeMention(Type t) { none() }
|
||||
|
||||
TypeMention getATypeParameterConstraint(TypeParameter tp) {
|
||||
result = tp.(TypeParamTypeParameter).getTypeParam().getTypeBoundList().getABound().getTypeRepr()
|
||||
result = tp.(TypeParamTypeParameter).getTypeParam().getATypeBound().getTypeRepr()
|
||||
or
|
||||
result = tp.(SelfTypeParameter).getTrait()
|
||||
or
|
||||
@@ -184,12 +184,12 @@ private module Input2 implements InputSig2 {
|
||||
exists(Trait trait |
|
||||
abs = trait and
|
||||
condition = trait and
|
||||
constraint = trait.getTypeBoundList().getABound().getTypeRepr()
|
||||
constraint = trait.getATypeBound().getTypeRepr()
|
||||
)
|
||||
or
|
||||
// trait bounds on type parameters
|
||||
exists(TypeParam param |
|
||||
abs = param.getTypeBoundList().getABound() and
|
||||
abs = param.getATypeBound() and
|
||||
condition = param and
|
||||
constraint = abs.(TypeBound).getTypeRepr()
|
||||
)
|
||||
|
||||
@@ -321,9 +321,9 @@ mod m15 {
|
||||
TT: Trait1, // $ item=ITT item=I79
|
||||
{
|
||||
fn f(&self, tt: TT) { // $ item=ITT
|
||||
Self::g(self); // $ MISSING: item=I80
|
||||
Self::g(self); // $ item=I80
|
||||
TT::g(&tt); // $ item=I80
|
||||
self.g(); // $ MISSING: item=I80
|
||||
self.g(); // $ item=I80
|
||||
}
|
||||
} // ITrait3
|
||||
|
||||
|
||||
@@ -162,6 +162,7 @@ resolvePath
|
||||
| main.rs:321:13:321:18 | Trait1 | main.rs:299:5:303:5 | trait Trait1 |
|
||||
| main.rs:323:25:323:26 | TT | main.rs:317:9:317:10 | TT |
|
||||
| main.rs:324:13:324:16 | Self | main.rs:315:5:328:5 | trait Trait3 |
|
||||
| main.rs:324:13:324:19 | ...::g | main.rs:302:9:302:20 | fn g |
|
||||
| main.rs:325:13:325:14 | TT | main.rs:317:9:317:10 | TT |
|
||||
| main.rs:325:13:325:17 | ...::g | main.rs:302:9:302:20 | fn g |
|
||||
| main.rs:333:10:333:15 | Trait1 | main.rs:299:5:303:5 | trait Trait1 |
|
||||
|
||||
@@ -550,7 +550,7 @@ mod type_parameter_bounds {
|
||||
T: FirstTrait<S1>,
|
||||
{
|
||||
let s = x.method(); // $ target=FirstTrait::method
|
||||
println!("{:?}", s); // $ MISSING: type=s:S1
|
||||
println!("{:?}", s); // $ type=s:S1
|
||||
}
|
||||
|
||||
trait Pair<P1 = bool, P2 = i64> {
|
||||
@@ -830,8 +830,8 @@ mod associated_type_in_supertrait {
|
||||
{
|
||||
// Subtrait2::insert_two
|
||||
fn insert_two(&self, c1: Self::Content, c2: Self::Content) {
|
||||
self.insert(c1); // $ MISSING: target=Supertrait::insert
|
||||
self.insert(c2); // $ MISSING: target=Supertrait::insert
|
||||
self.insert(c1); // $ target=Supertrait::insert
|
||||
self.insert(c2); // $ target=Supertrait::insert
|
||||
}
|
||||
}
|
||||
|
||||
@@ -856,7 +856,7 @@ mod associated_type_in_supertrait {
|
||||
}
|
||||
|
||||
fn insert_three<T: Subtrait2>(item: &T, c1: T::Content, c2: T::Content, c3: T::Content) {
|
||||
item.insert(c1); // $ MISSING: target=Supertrait::insert
|
||||
item.insert(c1); // $ target=Supertrait::insert
|
||||
item.insert_two(c2, c3); // $ target=Subtrait2::insert_two
|
||||
}
|
||||
|
||||
|
||||
@@ -1418,11 +1418,14 @@ inferType
|
||||
| main.rs:545:18:545:26 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
|
||||
| main.rs:545:26:545:26 | s | | main.rs:508:5:509:14 | S1 |
|
||||
| main.rs:548:43:548:43 | x | | main.rs:548:40:548:40 | T |
|
||||
| main.rs:552:13:552:13 | s | | main.rs:508:5:509:14 | S1 |
|
||||
| main.rs:552:17:552:17 | x | | main.rs:548:40:548:40 | T |
|
||||
| main.rs:552:17:552:26 | x.method() | | main.rs:508:5:509:14 | S1 |
|
||||
| main.rs:553:18:553:23 | "{:?}\\n" | | file://:0:0:0:0 | & |
|
||||
| main.rs:553:18:553:23 | "{:?}\\n" | &T | {EXTERNAL LOCATION} | str |
|
||||
| main.rs:553:18:553:26 | FormatArgsExpr | | {EXTERNAL LOCATION} | Arguments |
|
||||
| main.rs:553:18:553:26 | MacroExpr | | {EXTERNAL LOCATION} | Arguments |
|
||||
| main.rs:553:26:553:26 | s | | main.rs:508:5:509:14 | S1 |
|
||||
| main.rs:557:16:557:19 | SelfParam | | main.rs:556:5:560:5 | Self [trait Pair] |
|
||||
| main.rs:559:16:559:19 | SelfParam | | main.rs:556:5:560:5 | Self [trait Pair] |
|
||||
| main.rs:562:58:562:58 | x | | main.rs:562:41:562:55 | T |
|
||||
@@ -1761,10 +1764,14 @@ inferType
|
||||
| main.rs:823:24:823:28 | SelfParam | &T | main.rs:821:5:824:5 | Self [trait Subtrait] |
|
||||
| main.rs:832:23:832:27 | SelfParam | | file://:0:0:0:0 | & |
|
||||
| main.rs:832:23:832:27 | SelfParam | &T | main.rs:826:5:836:5 | Self [trait Subtrait2] |
|
||||
| main.rs:832:30:832:31 | c1 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:832:49:832:50 | c2 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:833:13:833:16 | self | | file://:0:0:0:0 | & |
|
||||
| main.rs:833:13:833:16 | self | &T | main.rs:826:5:836:5 | Self [trait Subtrait2] |
|
||||
| main.rs:833:25:833:26 | c1 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:834:13:834:16 | self | | file://:0:0:0:0 | & |
|
||||
| main.rs:834:13:834:16 | self | &T | main.rs:826:5:836:5 | Self [trait Subtrait2] |
|
||||
| main.rs:834:25:834:26 | c2 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:842:19:842:23 | SelfParam | | file://:0:0:0:0 | & |
|
||||
| main.rs:842:19:842:23 | SelfParam | &T | main.rs:838:5:838:24 | MyType |
|
||||
| main.rs:842:19:842:23 | SelfParam | &T.T | main.rs:840:10:840:10 | T |
|
||||
@@ -1794,10 +1801,16 @@ inferType
|
||||
| main.rs:855:9:855:26 | item.get_content() | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:858:35:858:38 | item | | file://:0:0:0:0 | & |
|
||||
| main.rs:858:35:858:38 | item | &T | main.rs:858:21:858:32 | T |
|
||||
| main.rs:858:45:858:46 | c1 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:858:61:858:62 | c2 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:858:77:858:78 | c3 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:859:9:859:12 | item | | file://:0:0:0:0 | & |
|
||||
| main.rs:859:9:859:12 | item | &T | main.rs:858:21:858:32 | T |
|
||||
| main.rs:859:21:859:22 | c1 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:860:9:860:12 | item | | file://:0:0:0:0 | & |
|
||||
| main.rs:860:9:860:12 | item | &T | main.rs:858:21:858:32 | T |
|
||||
| main.rs:860:25:860:26 | c2 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:860:29:860:30 | c3 | | main.rs:816:9:816:21 | Content |
|
||||
| main.rs:864:13:864:17 | item1 | | main.rs:838:5:838:24 | MyType |
|
||||
| main.rs:864:13:864:17 | item1 | T | {EXTERNAL LOCATION} | i64 |
|
||||
| main.rs:864:21:864:33 | MyType(...) | | main.rs:838:5:838:24 | MyType |
|
||||
|
||||
Reference in New Issue
Block a user