Skip to content

Commit 0cefb35

Browse files
committed
Rust: Adjustments to type inference
1 parent b096696 commit 0cefb35

File tree

9 files changed

+172
-161
lines changed

9 files changed

+172
-161
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Impl {
2323
*/
2424
class FieldExpr extends Generated::FieldExpr {
2525
/** Gets the record field that this access references, if any. */
26-
StructField getStructField() { result = TypeInference::resolveRecordFieldExpr(this) }
26+
StructField getStructField() { result = TypeInference::resolveStructFieldExpr(this) }
2727

2828
/** Gets the tuple field that this access references, if any. */
2929
TupleField getTupleField() { result = TypeInference::resolveTupleFieldExpr(this) }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ module Impl {
4343
* Empty structs are considered to use record fields.
4444
*/
4545
pragma[nomagic]
46-
predicate isRecord() { not this.isTuple() }
46+
predicate isStruct() { not this.isTuple() }
4747
}
4848
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ module Impl {
3838
predicate isTuple() { this.getFieldList() instanceof TupleFieldList }
3939

4040
/**
41-
* Holds if this variant uses record fields.
41+
* Holds if this variant uses struct fields.
4242
*
43-
* Empty variants are considered to use record fields.
43+
* Empty variants are considered to use struct fields.
4444
*/
4545
pragma[nomagic]
46-
predicate isRecord() { not this.isTuple() }
46+
predicate isStruct() { not this.isTuple() }
4747

4848
/** Gets the enum that this variant belongs to. */
4949
Enum getEnum() { this = result.getVariantList().getAVariant() }

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class Type extends TType {
2929
pragma[nomagic]
3030
abstract Function getMethod(string name);
3131

32-
/** Gets the record field `name` belonging to this type, if any. */
32+
/** Gets the struct field `name` belonging to this type, if any. */
3333
pragma[nomagic]
3434
abstract StructField getStructField(string name);
3535

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

+19-19
Original file line numberDiff line numberDiff line change
@@ -244,24 +244,24 @@ private TypeMention getExplicitTypeArgMention(Path path, TypeParam tp) {
244244
}
245245

246246
/**
247-
* A matching configuration for resolving types of record expressions
247+
* A matching configuration for resolving types of struct expressions
248248
* like `Foo { bar = baz }`.
249249
*/
250250
private module StructExprMatchingInput implements MatchingInputSig {
251251
private newtype TPos =
252252
TFieldPos(string name) { exists(any(Declaration decl).getField(name)) } or
253-
TRecordPos()
253+
TStructPos()
254254

255255
class DeclarationPosition extends TPos {
256256
string asFieldPos() { this = TFieldPos(result) }
257257

258-
predicate isRecordPos() { this = TRecordPos() }
258+
predicate isStructPos() { this = TStructPos() }
259259

260260
string toString() {
261261
result = this.asFieldPos()
262262
or
263-
this.isRecordPos() and
264-
result = "(record)"
263+
this.isStructPos() and
264+
result = "(struct)"
265265
}
266266
}
267267

@@ -282,15 +282,15 @@ private module StructExprMatchingInput implements MatchingInputSig {
282282
result = tp.resolveTypeAt(path)
283283
)
284284
or
285-
// type parameter of the record itself
286-
dpos.isRecordPos() and
285+
// type parameter of the struct itself
286+
dpos.isStructPos() and
287287
result = this.getTypeParameter(_) and
288288
path = TypePath::singleton(result)
289289
}
290290
}
291291

292-
private class RecordStructDecl extends Declaration, Struct {
293-
RecordStructDecl() { this.isRecord() }
292+
private class StructDecl extends Declaration, Struct {
293+
StructDecl() { this.isStruct() }
294294

295295
override TypeParam getATypeParam() { result = this.getGenericParamList().getATypeParam() }
296296

@@ -300,14 +300,14 @@ private module StructExprMatchingInput implements MatchingInputSig {
300300
result = super.getDeclaredType(dpos, path)
301301
or
302302
// type of the struct itself
303-
dpos.isRecordPos() and
303+
dpos.isStructPos() and
304304
path.isEmpty() and
305305
result = TStruct(this)
306306
}
307307
}
308308

309-
private class RecordVariantDecl extends Declaration, Variant {
310-
RecordVariantDecl() { this.isRecord() }
309+
private class StructVariantDecl extends Declaration, Variant {
310+
StructVariantDecl() { this.isStruct() }
311311

312312
Enum getEnum() { result.getVariantList().getAVariant() = this }
313313

@@ -321,7 +321,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
321321
result = super.getDeclaredType(dpos, path)
322322
or
323323
// type of the enum itself
324-
dpos.isRecordPos() and
324+
dpos.isStructPos() and
325325
path.isEmpty() and
326326
result = TEnum(this.getEnum())
327327
}
@@ -338,7 +338,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
338338
result = this.getFieldExpr(apos.asFieldPos()).getExpr()
339339
or
340340
result = this and
341-
apos.isRecordPos()
341+
apos.isStructPos()
342342
}
343343

344344
Type getInferredType(AccessPosition apos, TypePath path) {
@@ -356,8 +356,8 @@ private module StructExprMatchingInput implements MatchingInputSig {
356356
private module StructExprMatching = Matching<StructExprMatchingInput>;
357357

358358
/**
359-
* Gets the type of `n` at `path`, where `n` is either a record expression or
360-
* a field expression of a record expression.
359+
* Gets the type of `n` at `path`, where `n` is either a struct expression or
360+
* a field expression of a struct expression.
361361
*/
362362
pragma[nomagic]
363363
private Type inferStructExprType(AstNode n, TypePath path) {
@@ -773,7 +773,7 @@ private module FieldExprMatchingInput implements MatchingInputSig {
773773

774774
Declaration getTarget() {
775775
// mutual recursion; resolving fields requires resolving types and vice versa
776-
result = [resolveRecordFieldExpr(this).(AstNode), resolveTupleFieldExpr(this)]
776+
result = [resolveStructFieldExpr(this).(AstNode), resolveTupleFieldExpr(this)]
777777
}
778778
}
779779

@@ -917,10 +917,10 @@ private module Cached {
917917
}
918918

919919
/**
920-
* Gets the record field that the field expression `fe` resolves to, if any.
920+
* Gets the struct field that the field expression `fe` resolves to, if any.
921921
*/
922922
cached
923-
StructField resolveRecordFieldExpr(FieldExpr fe) {
923+
StructField resolveStructFieldExpr(FieldExpr fe) {
924924
exists(string name | result = getFieldExprLookupType(fe, name).getStructField(name))
925925
}
926926

rust/ql/test/library-tests/type-inference/main.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ mod method_non_parametric_trait_impl {
193193

194194
mod function_trait_bounds {
195195
#[derive(Debug)]
196-
struct MyThing<A> {
197-
a: A,
196+
struct MyThing<T> {
197+
a: T,
198198
}
199199

200200
#[derive(Debug)]
@@ -325,12 +325,12 @@ mod method_supertraits {
325325
#[derive(Debug)]
326326
struct S2;
327327

328-
trait MyTrait1<A> {
329-
fn m1(self) -> A;
328+
trait MyTrait1<Tr1> {
329+
fn m1(self) -> Tr1;
330330
}
331331

332-
trait MyTrait2<A>: MyTrait1<A> {
333-
fn m2(self) -> A
332+
trait MyTrait2<Tr2>: MyTrait1<Tr2> {
333+
fn m2(self) -> Tr2
334334
where
335335
Self: Sized,
336336
{
@@ -342,8 +342,8 @@ mod method_supertraits {
342342
}
343343
}
344344

345-
trait MyTrait3<A>: MyTrait2<MyThing<A>> {
346-
fn m3(self) -> A
345+
trait MyTrait3<Tr3>: MyTrait2<MyThing<Tr3>> {
346+
fn m3(self) -> Tr3
347347
where
348348
Self: Sized,
349349
{

0 commit comments

Comments
 (0)