Skip to content

Commit dc2b7c5

Browse files
committed
Rust: Distinguish &mut T from &T in type inference
1 parent 5a5679b commit dc2b7c5

File tree

9 files changed

+460
-342
lines changed

9 files changed

+460
-342
lines changed

rust/ql/lib/codeql/rust/elements/internal/OperationImpl.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Impl {
3030
op = "!" and path = "core::ops::bit::Not" and method = "not" and borrows = 0
3131
or
3232
// Dereference
33+
// todo: handle `core::ops::deref::DerefMut`
3334
op = "*" and path = "core::ops::deref::Deref" and method = "deref" and borrows = 1
3435
)
3536
or

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,20 @@ class ArrayType extends BuiltinType {
162162
override string getDisplayName() { result = "[;]" }
163163
}
164164

165-
/** The builtin reference type `&T`. */
166-
class RefType extends BuiltinType {
167-
RefType() { this.getName() = "Ref" }
165+
/** A builtin reference type `&T` or `&mut T`. */
166+
abstract private class RefTypeImpl extends BuiltinType { }
167+
168+
final class RefType = RefTypeImpl;
169+
170+
/** The builtin shared reference type `&T`. */
171+
class RefSharedType extends RefTypeImpl {
172+
RefSharedType() { this.getName() = "Ref" }
168173

169174
override string getDisplayName() { result = "&" }
170175
}
171176

172-
/** The builtin reference type `&mut T`. */
173-
class RefMutType extends BuiltinType {
177+
/** The builtin mutable reference type `&mut T`. */
178+
class RefMutType extends RefTypeImpl {
174179
RefMutType() { this.getName() = "RefMut" }
175180

176181
override string getDisplayName() { result = "&mut" }

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,33 @@ TypeParamTypeParameter getArrayTypeParameter() {
224224
result = any(ArrayType t).getPositionalTypeParameter(0)
225225
}
226226

227-
/**
228-
* A reference type.
229-
*
230-
* Reference types like `& i64` are modeled as normal generic types
231-
* with a single type argument.
232-
*/
233-
class RefType extends StructType {
234-
RefType() { this.getStruct() instanceof Builtins::RefType }
227+
abstract class RefType extends StructType { }
228+
229+
pragma[nomagic]
230+
TypeParamTypeParameter getRefTypeParameter() {
231+
result = any(RefType t).getPositionalTypeParameter(0)
232+
}
233+
234+
class RefMutType extends RefType {
235+
RefMutType() { this.getStruct() instanceof Builtins::RefMutType }
236+
237+
override string toString() { result = "&mut" }
238+
}
239+
240+
pragma[nomagic]
241+
TypeParamTypeParameter getRefMutTypeParameter() {
242+
result = any(RefMutType t).getPositionalTypeParameter(0)
243+
}
244+
245+
class RefSharedType extends RefType {
246+
RefSharedType() { this.getStruct() instanceof Builtins::RefSharedType }
235247

236248
override string toString() { result = "&" }
237249
}
238250

239251
pragma[nomagic]
240-
TypeParamTypeParameter getRefTypeParameter() {
241-
result = any(RefType t).getPositionalTypeParameter(0)
252+
TypeParamTypeParameter getRefSharedTypeParameter() {
253+
result = any(RefSharedType t).getPositionalTypeParameter(0)
242254
}
243255

244256
/**

0 commit comments

Comments
 (0)