Skip to content

Commit 1857683

Browse files
committed
Rust: Minor tweaks and improvements
1 parent 2cb0e81 commit 1857683

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pragma[nomagic]
110110
private ItemNode getAChildSuccessor(ItemNode item, string name, SuccessorKind kind) {
111111
item = result.getImmediateParent() and
112112
name = result.getName() and
113-
// Associated types in `impl` and `trait` blocks are handled elsewhere
113+
// Associated items in `impl` and `trait` blocks are handled elsewhere
114114
not (item instanceof ImplOrTraitItemNode and result instanceof AssocItem) and
115115
// type parameters are only available inside the declaring item
116116
if result instanceof TypeParam
@@ -324,13 +324,14 @@ abstract class ItemNode extends Locatable {
324324
)
325325
)
326326
or
327-
exists(TraitItemNodeImpl trait | this = trait |
328-
result = trait.getAssocItem(name)
329-
or
330-
// a trait has access to the associated items of its supertraits
331-
not trait.hasAssocItem(name) and
332-
result = trait.resolveABoundCand().getASuccessor(name).(AssocItemNode)
333-
) and
327+
this =
328+
any(TraitItemNodeImpl trait |
329+
result = trait.getAssocItem(name)
330+
or
331+
// a trait has access to the associated items of its supertraits
332+
not trait.hasAssocItem(name) and
333+
result = trait.resolveABoundCand().getASuccessor(name).(AssocItemNode)
334+
) and
334335
kind.isExternal() and
335336
useOpt.isNone()
336337
or
@@ -1788,6 +1789,8 @@ private ItemNode resolvePathCand0(PathExt path, Namespace ns) {
17881789
exists(ItemNode res |
17891790
res = unqualifiedPathLookup(path, ns, _) and
17901791
if
1792+
// `Self` paths that are not used as qualifiers (for instance `Self` in
1793+
// `fn(..) -> Self`) should resolve to the type being implemented.
17911794
not any(PathExt parent).getQualifier() = path and
17921795
isUnqualifiedSelfPath(path) and
17931796
res instanceof ImplItemNode
@@ -2191,7 +2194,7 @@ private predicate externCrateEdge(
21912194

21922195
/**
21932196
* Holds if `typeItem` is the implementing type of `impl` and the implementation
2194-
* makes `assoc` available as `name` at `kind`.
2197+
* makes `assoc` available as `name`.
21952198
*/
21962199
private predicate typeImplEdge(
21972200
TypeItemNode typeItem, ImplItemNodeImpl impl, string name, AssocItemNode assoc
@@ -2201,7 +2204,7 @@ private predicate typeImplEdge(
22012204
// Functions in `impl` blocks are made available on the implementing type
22022205
// (e.g., `S::fun` is valid) but associated types are not (e.g., `S::Output`
22032206
// is invalid).
2204-
(assoc instanceof FunctionItemNode or assoc instanceof ConstItemNode)
2207+
not assoc instanceof TypeAlias
22052208
}
22062209

22072210
pragma[nomagic]

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ private import Type
77
private import TypeAbstraction
88
private import TypeInference
99

10+
bindingset[trait, name]
11+
pragma[inline_late]
12+
private TypeAlias getTraitAssocType(TraitItemNode trait, string name) {
13+
result = trait.getAssocItem(name)
14+
}
15+
1016
private signature Type getAdditionalPathTypeAtSig(Path p, TypePath typePath);
1117

1218
/**
@@ -286,11 +292,6 @@ private module MkTypeMention<getAdditionalPathTypeAtSig/2 getAdditionalPathTypeA
286292
)
287293
}
288294

289-
bindingset[name]
290-
private TypeAlias getResolvedAlias(string name) {
291-
result = resolved.(TraitItemNode).getAssocItem(name)
292-
}
293-
294295
bindingset[name]
295296
private TypeAlias getResolvedTraitAssocType(string name) {
296297
result = resolved.(TraitItemNode).getASuccessor(name)
@@ -322,7 +323,7 @@ private module MkTypeMention<getAdditionalPathTypeAtSig/2 getAdditionalPathTypeA
322323
this = impl.getTraitPath() and
323324
alias = impl.getASuccessor(name) and
324325
result = alias.getTypeRepr() and
325-
tp = TAssociatedTypeTypeParameter(resolved, this.getResolvedAlias(name))
326+
tp = TAssociatedTypeTypeParameter(resolved, getTraitAssocType(resolved, name))
326327
)
327328
}
328329

@@ -651,18 +652,20 @@ class PreTypeMention = PreTypeMention::TypeMention;
651652
* Holds if `path` accesses an associated type `alias` from `trait` on a
652653
* concrete type given by `tm`.
653654
*/
654-
predicate pathConcreteTypeAssocType(Path path, PreTypeMention tm, Trait trait, TypeAlias alias) {
655+
private predicate pathConcreteTypeAssocType(
656+
Path path, PreTypeMention tm, Trait trait, TypeAlias alias
657+
) {
655658
exists(Path qualifier |
656659
qualifier = path.getQualifier() and
657660
not resolvePath(tm.(PathTypeRepr).getPath()) instanceof TypeParam
658661
|
659662
// path of the form `<Type as Trait>::AssocType`
660663
// ^^^ tm ^^^^^^^^^ name
661664
exists(string name |
662-
name = path.getSegment().getIdentifier().getText() and
663-
tm = qualifier.getSegment().getTypeRepr() and
665+
name = path.getText() and
664666
trait = resolvePath(qualifier.getSegment().getTraitTypeRepr().getPath()) and
665-
trait.(TraitItemNode).getAssocItem(name) = alias
667+
getTraitAssocType(trait, name) = alias and
668+
tm = qualifier.getSegment().getTypeRepr()
666669
)
667670
or
668671
// path of the form `Self::AssocType` within an `impl` block
@@ -676,21 +679,22 @@ predicate pathConcreteTypeAssocType(Path path, PreTypeMention tm, Trait trait, T
676679
)
677680
}
678681

679-
private module PathSatisfiesConstraint implements SatisfiesConstraintInputSig<PreTypeMention> {
682+
private module PathSatisfiesConstraintInput implements SatisfiesConstraintInputSig<PreTypeMention> {
680683
predicate relevantConstraint(PreTypeMention tm, Type constraint) {
681684
pathConcreteTypeAssocType(_, tm, constraint.(TraitType).getTrait(), _)
682685
}
683686
}
684687

688+
module PathSatisfiesConstraint = SatisfiesConstraint<PreTypeMention, PathSatisfiesConstraintInput>;
689+
685690
/**
686691
* Gets the type of `path` at `typePath` when `path` accesses an associated type
687692
* on a concrete type.
688693
*/
689694
private Type getPathConcreteAssocTypeAt(Path path, TypePath typePath) {
690695
exists(PreTypeMention tm, TraitItemNode t, TypeAlias alias, TypePath path0 |
691696
pathConcreteTypeAssocType(path, tm, t, alias) and
692-
SatisfiesConstraint<PreTypeMention, PathSatisfiesConstraint>::satisfiesConstraintType(tm,
693-
TTrait(t), path0, result) and
697+
PathSatisfiesConstraint::satisfiesConstraintType(tm, TTrait(t), path0, result) and
694698
path0.isCons(TAssociatedTypeTypeParameter(t, alias), typePath)
695699
)
696700
}

0 commit comments

Comments
 (0)