Skip to content

Commit 924e615

Browse files
committed
dbg
1 parent b8f4690 commit 924e615

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ abstract class ItemNode extends Locatable {
183183
result = this.(ImplTraitTypeReprItemNode).resolveABound().getASuccessorRec(name).(AssocItemNode)
184184
or
185185
result = this.(TypeAliasItemNode).resolveAlias().getASuccessorRec(name) and
186+
// type parameters defined in the RHS are not available in the LHS
186187
not result instanceof TypeParam
187188
}
188189

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,6 @@ private Type inferImplicitSelfType(SelfParam self, TypePath path) {
354354
)
355355
}
356356

357-
/**
358-
* Gets any of the types mentioned in `path` that corresponds to the type
359-
* parameter `tp`.
360-
*/
361-
private TypeMention getExplicitTypeArgMention(Path path, TypeParam tp) {
362-
exists(int i |
363-
result = path.getSegment().getGenericArgList().getTypeArg(pragma[only_bind_into](i)) and
364-
tp = resolvePath(path).getTypeParam(pragma[only_bind_into](i))
365-
)
366-
or
367-
result = getExplicitTypeArgMention(path.getQualifier(), tp)
368-
}
369-
370357
/**
371358
* A matching configuration for resolving types of struct expressions
372359
* like `Foo { bar = baz }`.
@@ -453,9 +440,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
453440
class AccessPosition = DeclarationPosition;
454441

455442
class Access extends StructExpr {
456-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
457-
result = getExplicitTypeArgMention(this.getPath(), apos.asTypeParam()).resolveTypeAt(path)
458-
}
443+
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
459444

460445
AstNode getNodeAt(AccessPosition apos) {
461446
result = this.getFieldExpr(apos.asFieldPos()).getExpr()
@@ -466,6 +451,17 @@ private module StructExprMatchingInput implements MatchingInputSig {
466451

467452
Type getInferredType(AccessPosition apos, TypePath path) {
468453
result = inferType(this.getNodeAt(apos), path)
454+
or
455+
// The struct type is supplied explicitly as a type qualifier, e.g. TODO
456+
apos.isStructPos() and
457+
exists(TypeMention tm |
458+
// variant
459+
tm = this.getPath().getQualifier()
460+
or
461+
tm = this.getPath()
462+
|
463+
result = tm.resolveTypeAt(path)
464+
)
469465
}
470466

471467
Declaration getTarget() { result = resolvePath(this.getPath()) }
@@ -538,15 +534,24 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
538534

539535
abstract Type getReturnType(TypePath path);
540536

541-
final Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
537+
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
542538
result = this.getParameterType(dpos, path)
543539
or
544540
dpos.isReturn() and
545541
result = this.getReturnType(path)
546542
}
547543
}
548544

549-
private class TupleStructDecl extends Declaration, Struct {
545+
abstract private class TupleDeclaration extends Declaration {
546+
override Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
547+
result = super.getDeclaredType(dpos, path)
548+
or
549+
dpos.isSelf() and
550+
result = this.getReturnType(path)
551+
}
552+
}
553+
554+
private class TupleStructDecl extends TupleDeclaration, Struct {
550555
TupleStructDecl() { this.isTuple() }
551556

552557
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
@@ -569,7 +574,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
569574
}
570575
}
571576

572-
private class TupleVariantDecl extends Declaration, Variant {
577+
private class TupleVariantDecl extends TupleDeclaration, Variant {
573578
TupleVariantDecl() { this.isTuple() }
574579

575580
override TypeParameter getTypeParameter(TypeParameterPosition ppos) {
@@ -698,9 +703,14 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
698703
private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
699704

700705
final class Access extends Call {
706+
pragma[nomagic]
701707
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
702708
exists(TypeMention arg | result = arg.resolveTypeAt(path) |
703-
arg = getExplicitTypeArgMention(CallExprImpl::getFunctionPath(this), apos.asTypeParam())
709+
exists(Path p, int i |
710+
p = CallExprImpl::getFunctionPath(this) and
711+
arg = p.getSegment().getGenericArgList().getTypeArg(pragma[only_bind_into](i)) and
712+
apos.asTypeParam() = resolvePath(p).getTypeParam(pragma[only_bind_into](i))
713+
)
704714
or
705715
arg =
706716
this.(MethodCallExpr).getGenericArgList().getTypeArg(apos.asMethodTypeArgumentPosition())
@@ -1613,19 +1623,51 @@ private module Debug {
16131623
result = resolveMethodCallTarget(mce)
16141624
}
16151625

1626+
predicate debugInferImplicitSelfType(SelfParam self, TypePath path, Type t) {
1627+
self = getRelevantLocatable() and
1628+
t = inferImplicitSelfType(self, path)
1629+
}
1630+
1631+
predicate debugInferCallExprBaseType(AstNode n, TypePath path, Type t) {
1632+
n = getRelevantLocatable() and
1633+
t = inferCallExprBaseType(n, path)
1634+
}
1635+
16161636
predicate debugTypeMention(TypeMention tm, TypePath path, Type type) {
16171637
tm = getRelevantLocatable() and
16181638
tm.resolveTypeAt(path) = type
16191639
}
16201640

16211641
pragma[nomagic]
1622-
private int countTypes(AstNode n, TypePath path, Type t) {
1642+
private int countTypesAtPath(AstNode n, TypePath path, Type t) {
16231643
t = inferType(n, path) and
16241644
result = strictcount(Type t0 | t0 = inferType(n, path))
16251645
}
16261646

16271647
predicate maxTypes(AstNode n, TypePath path, Type t, int c) {
1628-
c = countTypes(n, path, t) and
1629-
c = max(countTypes(_, _, _))
1648+
c = countTypesAtPath(n, path, t) and
1649+
c = max(countTypesAtPath(_, _, _))
1650+
}
1651+
1652+
pragma[nomagic]
1653+
private predicate typePathLength(AstNode n, TypePath path, Type t, int len) {
1654+
t = inferType(n, path) and
1655+
len = path.length()
1656+
}
1657+
1658+
predicate maxTypePath(AstNode n, TypePath path, Type t, int len) {
1659+
typePathLength(n, path, t, len) and
1660+
len = max(int i | typePathLength(_, _, _, i))
1661+
}
1662+
1663+
pragma[nomagic]
1664+
private int countTypePaths(AstNode n, TypePath path, Type t) {
1665+
t = inferType(n, path) and
1666+
result = strictcount(TypePath path0, Type t0 | t0 = inferType(n, path0))
1667+
}
1668+
1669+
predicate maxTypePaths(AstNode n, TypePath path, Type t, int c) {
1670+
c = countTypePaths(n, path, t) and
1671+
c = max(countTypePaths(_, _, _))
16301672
}
16311673
}

rust/ql/lib/codeql/rust/internal/TypeMention.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class PathTypeMention extends TypeMention, Path {
8585
// If a type argument is not given in the path, then we use the default for
8686
// the type parameter if one exists for the type.
8787
not exists(this.getSegment().getGenericArgList().getTypeArg(i)) and
88-
result = this.resolveType().getTypeParameterDefault(i)
88+
result = this.resolveType().getTypeParameterDefault(i) and
89+
this = any(PathTypeRepr ptp).getPath().getQualifier*()
8990
or
9091
// `Self` paths inside `impl` blocks have implicit type arguments that are
9192
// the type parameters of the `impl` block. For example, in

0 commit comments

Comments
 (0)