Skip to content

Commit 266d5c6

Browse files
committed
Rust: Implement await expression usingSatisfiesConstraint module
1 parent 16da9e8 commit 266d5c6

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
@@ -1128,7 +1128,7 @@ pragma[nomagic]
11281128
private predicate crateDependencyEdge(SourceFileItemNode file, string name, CrateItemNode dep) {
11291129
exists(CrateItemNode c | dep = c.(Crate).getDependency(name) | file = c.getASourceFile())
11301130
or
1131-
// Give builtin files, such as `await.rs`, access to `std`
1131+
// Give builtin files access to `std`
11321132
file instanceof BuiltinSourceFile and
11331133
dep.getName() = name and
11341134
name = "std"
@@ -1497,7 +1497,7 @@ private predicate preludeEdge(SourceFile f, string name, ItemNode i) {
14971497
exists(Crate stdOrCore, ModuleLikeNode mod, ModuleItemNode prelude, ModuleItemNode rust |
14981498
f = any(Crate c0 | stdOrCore = c0.getDependency(_) or stdOrCore = c0).getASourceFile()
14991499
or
1500-
// Give builtin files, such as `await.rs`, access to the prelude
1500+
// Give builtin files access to the prelude
15011501
f instanceof BuiltinSourceFile
15021502
|
15031503
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
@@ -981,93 +981,32 @@ private AssociatedTypeTypeParameter getFutureOutputTypeParameter() {
981981
result.getTypeAlias() = any(FutureTrait ft).getOutputType()
982982
}
983983

984-
/**
985-
* A matching configuration for resolving types of `.await` expressions.
986-
*/
987-
private module AwaitExprMatchingInput implements MatchingInputSig {
988-
private newtype TDeclarationPosition =
989-
TSelfDeclarationPosition() or
990-
TOutputPos()
991-
992-
class DeclarationPosition extends TDeclarationPosition {
993-
predicate isSelf() { this = TSelfDeclarationPosition() }
994-
995-
predicate isOutput() { this = TOutputPos() }
996-
997-
string toString() {
998-
this.isSelf() and
999-
result = "self"
1000-
or
1001-
this.isOutput() and
1002-
result = "(output)"
1003-
}
1004-
}
1005-
1006-
private class BuiltinsAwaitFile extends File {
1007-
BuiltinsAwaitFile() {
1008-
this.getBaseName() = "await.rs" and
1009-
this.getParentContainer() instanceof Builtins::BuiltinsFolder
1010-
}
1011-
}
1012-
1013-
class Declaration extends Function {
1014-
Declaration() {
1015-
this.getFile() instanceof BuiltinsAwaitFile and
1016-
this.getName().getText() = "await_type_matching"
1017-
}
1018-
1019-
TypeParameter getTypeParameter(TypeParameterPosition ppos) {
1020-
typeParamMatchPosition(this.getGenericParamList().getATypeParam(), result, ppos)
1021-
}
1022-
1023-
Type getDeclaredType(DeclarationPosition dpos, TypePath path) {
1024-
dpos.isSelf() and
1025-
result = this.getParam(0).getTypeRepr().(TypeMention).resolveTypeAt(path)
1026-
or
1027-
dpos.isOutput() and
1028-
result = this.getRetType().getTypeRepr().(TypeMention).resolveTypeAt(path)
1029-
}
1030-
}
1031-
1032-
class AccessPosition = DeclarationPosition;
1033-
1034-
class Access extends AwaitExpr {
1035-
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
1036-
1037-
AstNode getNodeAt(AccessPosition apos) {
1038-
result = this.getExpr() and
1039-
apos.isSelf()
1040-
or
1041-
result = this and
1042-
apos.isOutput()
1043-
}
1044-
1045-
Type getInferredType(AccessPosition apos, TypePath path) {
1046-
result = inferType(this.getNodeAt(apos), path)
1047-
}
1048-
1049-
Declaration getTarget() { exists(this) and exists(result) }
1050-
}
1051-
1052-
predicate accessDeclarationPositionMatch(AccessPosition apos, DeclarationPosition dpos) {
1053-
apos = dpos
1054-
}
1055-
}
1056-
1057984
pragma[nomagic]
1058985
private TraitType inferAsyncBlockExprRootType(AsyncBlockExpr abe) {
1059986
// `typeEquality` handles the non-root case
1060987
exists(abe) and
1061988
result = getFutureTraitType()
1062989
}
1063990

1064-
private module AwaitExprMatching = Matching<AwaitExprMatchingInput>;
991+
final class AwaitTarget extends Expr {
992+
AwaitTarget() { this = any(AwaitExpr ae).getExpr() }
993+
994+
Type getTypeAt(TypePath path) { result = inferType(this, path) }
995+
}
996+
997+
private module AwaitSatisfiesConstraintInput implements SatisfiesConstraintSig<AwaitTarget> {
998+
predicate relevantConstraint(AwaitTarget term, Type constraint) {
999+
exists(term) and
1000+
constraint.(TraitType).getTrait() instanceof FutureTrait
1001+
}
1002+
}
10651003

10661004
pragma[nomagic]
10671005
private Type inferAwaitExprType(AstNode n, TypePath path) {
1068-
exists(AwaitExprMatchingInput::Access a, AwaitExprMatchingInput::AccessPosition apos |
1069-
n = a.getNodeAt(apos) and
1070-
result = AwaitExprMatching::inferAccessType(a, apos, path)
1006+
exists(TypePath exprPath |
1007+
SatisfiesConstraint<AwaitTarget, AwaitSatisfiesConstraintInput>::satisfiesConstraintTypeMention(n.(AwaitExpr)
1008+
.getExpr(), _, exprPath, result) and
1009+
exprPath.isCons(getFutureOutputTypeParameter(), path)
10711010
)
10721011
or
10731012
// 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)