Skip to content

Commit b2fc4f8

Browse files
committed
Rust: Adjustments to type inference
1 parent d9c1589 commit b2fc4f8

File tree

9 files changed

+172
-161
lines changed

9 files changed

+172
-161
lines changed

Diff for: 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) }

Diff for: 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
}

Diff for: 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() }

Diff for: 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

Diff for: rust/ql/lib/codeql/rust/internal/TypeInference.qll

+19-19
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,24 @@ private TypeMention getExplicitTypeArgMention(Path path, TypeParam tp) {
248248
}
249249

250250
/**
251-
* A matching configuration for resolving types of record expressions
251+
* A matching configuration for resolving types of struct expressions
252252
* like `Foo { bar = baz }`.
253253
*/
254254
private module StructExprMatchingInput implements MatchingInputSig {
255255
private newtype TPos =
256256
TFieldPos(string name) { exists(any(Declaration decl).getField(name)) } or
257-
TRecordPos()
257+
TStructPos()
258258

259259
class DeclarationPosition extends TPos {
260260
string asFieldPos() { this = TFieldPos(result) }
261261

262-
predicate isRecordPos() { this = TRecordPos() }
262+
predicate isStructPos() { this = TStructPos() }
263263

264264
string toString() {
265265
result = this.asFieldPos()
266266
or
267-
this.isRecordPos() and
268-
result = "(record)"
267+
this.isStructPos() and
268+
result = "(struct)"
269269
}
270270
}
271271

@@ -286,15 +286,15 @@ private module StructExprMatchingInput implements MatchingInputSig {
286286
result = tp.resolveTypeAt(path)
287287
)
288288
or
289-
// type parameter of the record itself
290-
dpos.isRecordPos() and
289+
// type parameter of the struct itself
290+
dpos.isStructPos() and
291291
result = this.getTypeParameter(_) and
292292
path = TypePath::singleton(result)
293293
}
294294
}
295295

296-
private class RecordStructDecl extends Declaration, Struct {
297-
RecordStructDecl() { this.isRecord() }
296+
private class StructDecl extends Declaration, Struct {
297+
StructDecl() { this.isStruct() }
298298

299299
override TypeParam getATypeParam() { result = this.getGenericParamList().getATypeParam() }
300300

@@ -304,14 +304,14 @@ private module StructExprMatchingInput implements MatchingInputSig {
304304
result = super.getDeclaredType(dpos, path)
305305
or
306306
// type of the struct itself
307-
dpos.isRecordPos() and
307+
dpos.isStructPos() and
308308
path.isEmpty() and
309309
result = TStruct(this)
310310
}
311311
}
312312

313-
private class RecordVariantDecl extends Declaration, Variant {
314-
RecordVariantDecl() { this.isRecord() }
313+
private class StructVariantDecl extends Declaration, Variant {
314+
StructVariantDecl() { this.isStruct() }
315315

316316
Enum getEnum() { result.getVariantList().getAVariant() = this }
317317

@@ -325,7 +325,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
325325
result = super.getDeclaredType(dpos, path)
326326
or
327327
// type of the enum itself
328-
dpos.isRecordPos() and
328+
dpos.isStructPos() and
329329
path.isEmpty() and
330330
result = TEnum(this.getEnum())
331331
}
@@ -342,7 +342,7 @@ private module StructExprMatchingInput implements MatchingInputSig {
342342
result = this.getFieldExpr(apos.asFieldPos()).getExpr()
343343
or
344344
result = this and
345-
apos.isRecordPos()
345+
apos.isStructPos()
346346
}
347347

348348
Type getInferredType(AccessPosition apos, TypePath path) {
@@ -360,8 +360,8 @@ private module StructExprMatchingInput implements MatchingInputSig {
360360
private module StructExprMatching = Matching<StructExprMatchingInput>;
361361

362362
/**
363-
* Gets the type of `n` at `path`, where `n` is either a record expression or
364-
* a field expression of a record expression.
363+
* Gets the type of `n` at `path`, where `n` is either a struct expression or
364+
* a field expression of a struct expression.
365365
*/
366366
pragma[nomagic]
367367
private Type inferStructExprType(AstNode n, TypePath path) {
@@ -777,7 +777,7 @@ private module FieldExprMatchingInput implements MatchingInputSig {
777777

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

@@ -921,10 +921,10 @@ private module Cached {
921921
}
922922

923923
/**
924-
* Gets the record field that the field expression `fe` resolves to, if any.
924+
* Gets the struct field that the field expression `fe` resolves to, if any.
925925
*/
926926
cached
927-
StructField resolveRecordFieldExpr(FieldExpr fe) {
927+
StructField resolveStructFieldExpr(FieldExpr fe) {
928928
exists(string name | result = getFieldExprLookupType(fe, name).getStructField(name))
929929
}
930930

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ mod type_parameter_bounds {
255255

256256
mod function_trait_bounds {
257257
#[derive(Debug)]
258-
struct MyThing<A> {
259-
a: A,
258+
struct MyThing<T> {
259+
a: T,
260260
}
261261

262262
#[derive(Debug)]
@@ -387,12 +387,12 @@ mod method_supertraits {
387387
#[derive(Debug)]
388388
struct S2;
389389

390-
trait MyTrait1<A> {
391-
fn m1(self) -> A;
390+
trait MyTrait1<Tr1> {
391+
fn m1(self) -> Tr1;
392392
}
393393

394-
trait MyTrait2<A>: MyTrait1<A> {
395-
fn m2(self) -> A
394+
trait MyTrait2<Tr2>: MyTrait1<Tr2> {
395+
fn m2(self) -> Tr2
396396
where
397397
Self: Sized,
398398
{
@@ -404,8 +404,8 @@ mod method_supertraits {
404404
}
405405
}
406406

407-
trait MyTrait3<A>: MyTrait2<MyThing<A>> {
408-
fn m3(self) -> A
407+
trait MyTrait3<Tr3>: MyTrait2<MyThing<Tr3>> {
408+
fn m3(self) -> Tr3
409409
where
410410
Self: Sized,
411411
{

0 commit comments

Comments
 (0)