Skip to content

Commit 27ddc81

Browse files
committed
Rust: Cleanup of raw pointer types based in PR feedback
1 parent 299fed5 commit 27ddc81

File tree

4 files changed

+66
-60
lines changed

4 files changed

+66
-60
lines changed

rust/ql/lib/codeql/rust/frameworks/stdlib/Builtins.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,19 @@ class RefMutType extends BuiltinType {
177177
}
178178

179179
/** A builtin raw pointer type `*const T` or `*mut T`. */
180-
abstract class PtrType extends BuiltinType { }
180+
abstract private class PtrTypeImpl extends BuiltinType { }
181+
182+
final class PtrType = PtrTypeImpl;
181183

182184
/** The builtin raw pointer type `*const T`. */
183-
class PtrConstType extends PtrType {
185+
class PtrConstType extends PtrTypeImpl {
184186
PtrConstType() { this.getName() = "PtrConst" }
185187

186188
override string getDisplayName() { result = "*const" }
187189
}
188190

189191
/** The builtin raw pointer type `*mut T`. */
190-
class PtrMutType extends PtrType {
192+
class PtrMutType extends PtrTypeImpl {
191193
PtrMutType() { this.getName() = "PtrMut" }
192194

193195
override string getDisplayName() { result = "*mut" }

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ class NeverType extends Type, TNeverType {
339339
override Location getLocation() { result instanceof EmptyLocation }
340340
}
341341

342-
abstract class PtrType extends StructType {
343-
override Location getLocation() { result instanceof EmptyLocation }
344-
}
342+
abstract class PtrType extends StructType { }
345343

346344
pragma[nomagic]
347345
TypeParamTypeParameter getPtrTypeParameter() {

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,12 @@ private predicate typeEquality(AstNode n1, TypePath prefix1, AstNode n2, TypePat
609609
strictcount(Expr e | bodyReturns(n1, e)) = 1
610610
)
611611
or
612-
exists(RefExpr re |
613-
n2 = re and
614-
n1 = re.getExpr() and
615-
prefix1.isEmpty() and
616-
prefix2 = TypePath::singleton(inferRefExprType(re).getPositionalTypeParameter(0))
617-
)
612+
n2 =
613+
any(RefExpr re |
614+
n1 = re.getExpr() and
615+
prefix1.isEmpty() and
616+
prefix2 = TypePath::singleton(inferRefExprType(re).getPositionalTypeParameter(0))
617+
)
618618
or
619619
n1 = n2.(RefPat).getPat() and
620620
prefix1.isEmpty() and
@@ -3162,6 +3162,12 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) {
31623162
)
31633163
}
31643164

3165+
pragma[nomagic]
3166+
private Type getInferredDerefType(DerefExpr de, TypePath path) { result = inferType(de, path) }
3167+
3168+
pragma[nomagic]
3169+
private PtrType getInferredDerefExprPtrType(DerefExpr de) { result = inferType(de.getExpr()) }
3170+
31653171
/**
31663172
* Gets the inferred type of `n` at `path` when `n` occurs in a dereference
31673173
* expression `*n` and when `n` is known to have a raw pointer type.
@@ -3171,8 +3177,8 @@ private Type inferIndexExprType(IndexExpr ie, TypePath path) {
31713177
private Type inferDereferencedExprPtrType(AstNode n, TypePath path) {
31723178
exists(DerefExpr de, PtrType type, TypePath suffix |
31733179
de.getExpr() = n and
3174-
type = inferType(de.getExpr()) and
3175-
result = inferType(de, suffix) and
3180+
type = getInferredDerefExprPtrType(de) and
3181+
result = getInferredDerefType(de, suffix) and
31763182
path = TypePath::cons(type.getPositionalTypeParameter(0), suffix)
31773183
)
31783184
}

0 commit comments

Comments
 (0)