Skip to content

Commit 6e9fd49

Browse files
committed
Rust: Implement await expression using SatisfiesConstraint module
1 parent a367388 commit 6e9fd49

File tree

3 files changed

+18
-86
lines changed

3 files changed

+18
-86
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ pragma[nomagic]
11321132
private predicate crateDependencyEdge(SourceFileItemNode file, string name, CrateItemNode dep) {
11331133
exists(CrateItemNode c | dep = c.(Crate).getDependency(name) | file = c.getASourceFile())
11341134
or
1135-
// Give builtin files, such as `await.rs`, access to `std`
1135+
// Give builtin files access to `std`
11361136
file instanceof BuiltinSourceFile and
11371137
dep.getName() = name and
11381138
name = "std"
@@ -1501,7 +1501,7 @@ private predicate preludeEdge(SourceFile f, string name, ItemNode i) {
15011501
exists(Crate stdOrCore, ModuleLikeNode mod, ModuleItemNode prelude, ModuleItemNode rust |
15021502
f = any(Crate c0 | stdOrCore = c0.getDependency(_) or stdOrCore = c0).getASourceFile()
15031503
or
1504-
// Give builtin files, such as `await.rs`, access to the prelude
1504+
// Give builtin files access to the prelude
15051505
f instanceof BuiltinSourceFile
15061506
|
15071507
stdOrCore.getName() = ["std", "core"] and

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

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -997,93 +997,32 @@ private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
997997
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
998998
}
999999

1000-
/**
1001-
* A matching configuration for resolving types of `.await` expressions.
1002-
*/
1003-
private module AwaitExprMatchingInput implements MatchingInputSig {
1004-
private newtype TDeclarationPosition =
1005-
TSelfDeclarationPosition() or
1006-
TOutputPos()
1007-
1008-
class DeclarationPosition extends TDeclarationPosition {
1009-
predicate isSelf() { this = TSelfDeclarationPosition() }
1010-
1011-
predicate isOutput() { this = TOutputPos() }
1012-
1013-
string toString() {
1014-
this.isSelf() and
1015-
result = "self"
1016-
or
1017-
this.isOutput() and
1018-
result = "(output)"
1019-
}
1020-
}
1021-
1022-
private class BuiltinsAwaitFile extends File {
1023-
BuiltinsAwaitFile() {
1024-
this.getBaseName() = "await.rs" and
1025-
this.getParentContainer() instanceof Builtins::BuiltinsFolder
1026-
}
1027-
}
1028-
1029-
class Declaration extends Function {
1030-
Declaration() {
1031-
this.getFile() instanceof BuiltinsAwaitFile and
1032-
this.getName().getText() = "await_type_matching"
1033-
}
1034-
1035-
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
1036-
typeParamMatchPosition(this.getGenericParamList().getATypeParam(), result, ppos)
1037-
}
1038-
1039-
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
1040-
dpos.isSelf() and
1041-
result = this.getParam(0).getTypeRepr().(TypeMention).resolveTypeAt(path)
1042-
or
1043-
dpos.isOutput() and
1044-
result = this.getRetType().getTypeRepr().(TypeMention).resolveTypeAt(path)
1045-
}
1046-
}
1047-
1048-
class AccessPosition = DeclarationPosition;
1049-
1050-
class Access extends AwaitExpr {
1051-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
1052-
1053-
AstNode getNodeAt(AccessPosition apos) {
1054-
result = this.getExpr() and
1055-
apos.isSelf()
1056-
or
1057-
result = this and
1058-
apos.isOutput()
1059-
}
1060-
1061-
Type getInferredType(AccessPosition apos, TypePath path) {
1062-
result = inferType(this.getNodeAt(apos), path)
1063-
}
1064-
1065-
Declaration getTarget() { exists(this) and exists(result) }
1066-
}
1067-
1068-
predicate accessDeclarationPositionMatch(AccessPosition apos, DeclarationPosition dpos) {
1069-
apos = dpos
1070-
}
1071-
}
1072-
10731000
pragma[nomagic]
10741001
private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
10751002
// `typeEquality` handles the non-root case
10761003
exists(abe) and
10771004
result = getFutureTraitType()
10781005
}
10791006

1080-
private module AwaitExprMatching = Matching<AwaitExprMatchingInput>;
1007+
final class AwaitTarget extends Expr {
1008+
AwaitTarget() { this = any(AwaitExpr ae).getExpr() }
1009+
1010+
Type getTypeAt(TypePath path) { result = inferType(this, path) }
1011+
}
1012+
1013+
private module AwaitSatisfiesConstraintInput implements SatisfiesConstraintSig<AwaitTarget> {
1014+
predicate relevantConstraint(AwaitTarget term, Type constraint) {
1015+
exists(term) and
1016+
constraint.(TraitType).getTrait() instanceof FutureTrait
1017+
}
1018+
}
10811019

10821020
pragma[nomagic]
10831021
private Type inferAwaitExprType(AstNode n, TypePath path) {
1084-
exists(AwaitExprMatchingInput::Access a, AwaitExprMatchingInput::AccessPosition apos |
1085-
n = a.getNodeAt(apos) and
1086-
result = AwaitExprMatching::inferAccessType(a, apos, path)
1022+
exists(TypePath exprPath |
1023+
SatisfiesConstraint<AwaitTarget, AwaitSatisfiesConstraintInput>::satisfiesConstraintTypeMention(n.(AwaitExpr)
1024+
.getExpr(), _, exprPath, result) and
1025+
exprPath.isCons(getFutureOutputTypeParameter(), path)
10871026
)
10881027
or
10891028
// This case is needed for `async` functions and blocks, where we assign

rust/tools/builtins/await.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)