Skip to content

Commit af91152

Browse files
committed
Address review comments
1 parent 78280af commit af91152

File tree

17 files changed

+185
-142
lines changed

17 files changed

+185
-142
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @name Path resolution inconsistencies
3+
* @description Lists the path resolution inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/path-resolution-consistency
6+
*/
7+
8+
import codeql.rust.internal.PathResolutionConsistency
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @name Type inference inconsistencies
3+
* @description Lists the type inference inconsistencies in the database. This query is intended for internal use.
4+
* @kind table
5+
* @id rust/diagnostics/type-inference-consistency
6+
*/
7+
8+
import codeql.rust.internal.TypeInferenceConsistency

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ module Impl {
2727
*/
2828
class CallExprBase extends Generated::CallExprBase {
2929
/** Gets the static target of this call, if any. */
30-
Callable getStaticTarget() { none() }
30+
Callable getStaticTarget() { none() } // overridden by subclasses
3131
}
3232
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Impl {
2626
override string toAbbreviatedString() { result = "<...>" }
2727

2828
/** Gets the `i`th type argument of this list. */
29-
TypeRepr getTypeArgument(int i) {
29+
TypeRepr getTypeArg(int i) {
3030
result =
3131
rank[i + 1](TypeRepr res, int j |
3232
res = this.getGenericArg(j).(TypeArg).getTypeRepr()
@@ -36,6 +36,6 @@ module Impl {
3636
}
3737

3838
/** Gets a type argument of this list. */
39-
TypeRepr getATypeArgument() { result = this.getTypeArgument(_) }
39+
TypeRepr getATypeArg() { result = this.getTypeArg(_) }
4040
}
4141
}

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

+31-27
Original file line numberDiff line numberDiff line change
@@ -317,49 +317,53 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
317317
}
318318

319319
/**
320-
* Holds if this `impl` block is constrained. Examples:
320+
* Holds if this `impl` block is not fully parametric. That is, the implementing
321+
* type is generic and the implementation is not parametrically polymorphic in all
322+
* the implementing type's arguments.
323+
*
324+
* Examples:
321325
*
322326
* ```rust
323-
* impl Foo { ... } // unconstrained
327+
* impl Foo { ... } // fully parametric
324328
*
325-
* impl<T> Foo<T> { ... } // unconstrained
329+
* impl<T> Foo<T> { ... } // fully parametric
326330
*
327-
* impl Foo<i64> { ... } // constrained
331+
* impl Foo<i64> { ... } // not fully parametric
328332
*
329-
* impl<T> Foo<Foo<T>> { ... } // constrained
333+
* impl<T> Foo<Foo<T>> { ... } // not fully parametric
330334
*
331-
* impl<T: Trait> Foo<T> { ... } // constrained
335+
* impl<T: Trait> Foo<T> { ... } // not fully parametric
332336
*
333-
* impl<T> Foo<T> where T: Trait { ... } // constrained
337+
* impl<T> Foo<T> where T: Trait { ... } // not fully parametric
334338
* ```
335339
*/
336340
pragma[nomagic]
337-
predicate isConstrained() {
341+
predicate isNotFullyParametric() {
338342
exists(TypeRepr arg | arg = this.getASelfTyArg() |
339343
not exists(this.getASelfTyTypeParamArg(arg))
340344
or
341-
this.getASelfTyTypeParamArg(arg).isConstrained()
345+
this.getASelfTyTypeParamArg(arg).hasTraitBound()
342346
)
343347
}
344348

345349
/**
346-
* Holds if this `impl` block is unconstrained. Examples:
350+
* Holds if this `impl` block is fully parametric. Examples:
347351
*
348352
* ```rust
349-
* impl Foo { ... } // unconstrained
353+
* impl Foo { ... } // fully parametric
350354
*
351-
* impl<T> Foo<T> { ... } // unconstrained
355+
* impl<T> Foo<T> { ... } // fully parametric
352356
*
353-
* impl Foo<i64> { ... } // constrained
357+
* impl Foo<i64> { ... } // not fully parametric
354358
*
355-
* impl<T> Foo<Foo<T>> { ... } // constrained
359+
* impl<T> Foo<Foo<T>> { ... } // not fully parametric
356360
*
357-
* impl<T: Trait> Foo<T> { ... } // constrained
361+
* impl<T: Trait> Foo<T> { ... } // not fully parametric
358362
*
359-
* impl<T> Foo<T> where T: Trait { ... } // constrained
363+
* impl<T> Foo<T> where T: Trait { ... } // not fully parametric
360364
* ```
361365
*/
362-
predicate isUnconstrained() { not this.isConstrained() }
366+
predicate isFullyParametric() { not this.isNotFullyParametric() }
363367

364368
override AssocItemNode getAnAssocItem() { result = super.getAssocItemList().getAnAssocItem() }
365369

@@ -481,18 +485,18 @@ private class TypeParamItemNode extends ItemNode instanceof TypeParam {
481485
ItemNode resolveABound() { result = resolvePath(this.getABoundPath()) }
482486

483487
/**
484-
* Holds if this type parameter is constrained. Examples:
488+
* Holds if this type parameter has a trait bound. Examples:
485489
*
486490
* ```rust
487-
* impl<T> Foo<T> { ... } // unconstrained
491+
* impl<T> Foo<T> { ... } // has no trait bound
488492
*
489-
* impl<T: Trait> Foo<T> { ... } // constrained
493+
* impl<T: Trait> Foo<T> { ... } // has trait bound
490494
*
491-
* impl<T> Foo<T> where T: Trait { ... } // constrained
495+
* impl<T> Foo<T> where T: Trait { ... } // has trait bound
492496
* ```
493497
*/
494498
pragma[nomagic]
495-
predicate isConstrained() {
499+
predicate hasTraitBound() {
496500
exists(this.getABoundPath())
497501
or
498502
exists(ItemNode declaringItem, WherePred wp |
@@ -503,18 +507,18 @@ private class TypeParamItemNode extends ItemNode instanceof TypeParam {
503507
}
504508

505509
/**
506-
* Holds if this type parameter is unconstrained. Examples:
510+
* Holds if this type parameter has no trait bound. Examples:
507511
*
508512
* ```rust
509-
* impl<T> Foo<T> { ... } // unconstrained
513+
* impl<T> Foo<T> { ... } // has no trait bound
510514
*
511-
* impl<T: Trait> Foo<T> { ... } // constrained
515+
* impl<T: Trait> Foo<T> { ... } // has trait bound
512516
*
513-
* impl<T> Foo<T> where T: Trait { ... } // constrained
517+
* impl<T> Foo<T> where T: Trait { ... } // has trait bound
514518
* ```
515519
*/
516520
pragma[nomagic]
517-
predicate isUnconstrained() { not this.isConstrained() }
521+
predicate hasNoTraitBound() { not this.hasTraitBound() }
518522

519523
override string getName() { result = TypeParam.super.getName().getText() }
520524

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ newtype TType =
1818
TRefTypeParameter() or
1919
TSelfTypeParameter()
2020

21-
/** A type without type arguments. */
21+
/**
22+
* A type without type arguments.
23+
*
24+
* Note that this type includes things that, strictly speaking, are not Rust
25+
* types, such as traits and implementation blocks.
26+
*/
2227
abstract class Type extends TType {
2328
/** Gets the method `name` belonging to this type, if any. */
2429
pragma[nomagic]
@@ -72,13 +77,13 @@ abstract private class StructOrEnumType extends Type {
7277
exists(ImplOrTraitItemNode impl | result = impl.getAnAssocItem() |
7378
impl instanceof Trait
7479
or
75-
impl.(ImplItemNode).isUnconstrained()
80+
impl.(ImplItemNode).isFullyParametric()
7681
)
7782
}
7883

7984
final override ImplMention getABaseTypeMention() {
8085
this.asItemNode() = result.resolveSelfTy() and
81-
result.isUnconstrained()
86+
result.isFullyParametric()
8287
}
8388
}
8489

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

+25-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private module Input1 implements InputSig1<Location> {
2222
// method type parameters are matched by position instead of by type
2323
// parameter entity, to avoid extra recursion through method call resolution
2424
TMethodTypeArgumentPosition(int pos) {
25-
exists(any(MethodCallExpr mce).getGenericArgList().getTypeArgument(pos))
25+
exists(any(MethodCallExpr mce).getGenericArgList().getTypeArg(pos))
2626
} or
2727
TTypeParamTypeArgumentPosition(TypeParam tp)
2828

@@ -124,11 +124,13 @@ private Type inferAnnotatedType(AstNode n, TypePath path) {
124124
}
125125

126126
/**
127-
* Holds if the type of `n1` at `path1` is the same as the type of `n2` at `path2`.
127+
* Holds if the type of `n1` at `path1` is the same as the type of `n2` at
128+
* `path2` and type information should propagate in both directions through the
129+
* type equality.
128130
*/
129131
bindingset[path1]
130132
bindingset[path2]
131-
private predicate typeSymmetry(AstNode n1, TypePath path1, AstNode n2, TypePath path2) {
133+
private predicate typeEquality(AstNode n1, TypePath path1, AstNode n2, TypePath path2) {
132134
exists(Variable v |
133135
path1 = path2 and
134136
n1 = v.getAnAccess()
@@ -159,11 +161,11 @@ private predicate typeSymmetry(AstNode n1, TypePath path1, AstNode n2, TypePath
159161
}
160162

161163
pragma[nomagic]
162-
private Type inferTypeSymmetry(AstNode n, TypePath path) {
164+
private Type inferTypeEquality(AstNode n, TypePath path) {
163165
exists(AstNode n2, TypePath path2 | result = inferType(n2, path2) |
164-
typeSymmetry(n, path, n2, path2)
166+
typeEquality(n, path, n2, path2)
165167
or
166-
typeSymmetry(n2, path2, n, path)
168+
typeEquality(n2, path2, n, path)
167169
)
168170
}
169171

@@ -222,7 +224,7 @@ private Type inferImplicitSelfType(SelfParam self, TypePath path) {
222224

223225
private TypeMention getExplicitTypeArgMention(Path path, TypeParam tp) {
224226
exists(int i |
225-
result = path.getPart().getGenericArgList().getTypeArgument(pragma[only_bind_into](i)) and
227+
result = path.getPart().getGenericArgList().getTypeArg(pragma[only_bind_into](i)) and
226228
tp = resolvePath(path).getTypeParam(pragma[only_bind_into](i))
227229
)
228230
or
@@ -316,7 +318,7 @@ private module RecordExprMatchingInput implements MatchingInputSig {
316318
class AccessPosition = DeclarationPosition;
317319

318320
class Access extends RecordExpr {
319-
Type getExplicitTypeArgument(TypeArgumentPosition apos, TypePath path) {
321+
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
320322
result = getExplicitTypeArgMention(this.getPath(), apos.asTypeParam()).resolveTypeAt(path)
321323
}
322324

@@ -535,10 +537,10 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
535537

536538
class Access extends CallExprBase {
537539
private TypeReprMention getMethodTypeArg(int i) {
538-
result = this.(MethodCallExpr).getGenericArgList().getTypeArgument(i)
540+
result = this.(MethodCallExpr).getGenericArgList().getTypeArg(i)
539541
}
540542

541-
Type getExplicitTypeArgument(TypeArgumentPosition apos, TypePath path) {
543+
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
542544
exists(TypeMention arg | result = arg.resolveTypeAt(path) |
543545
arg = getExplicitTypeArgMention(CallExprImpl::getFunctionPath(this), apos.asTypeParam())
544546
or
@@ -616,7 +618,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
616618
pathAdj = TypePath::singleton(TRefTypeParameter()) and
617619
tAdj = t
618620
else
619-
if path.startsWith(TRefTypeParameter(), _)
621+
if path.isCons(TRefTypeParameter(), _)
620622
then
621623
pathAdj = path and
622624
tAdj = t
@@ -628,10 +630,10 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
628630
)
629631
else (
630632
// adjust for implicit deref
631-
path.startsWith(TRefTypeParameter(), pathAdj) and
633+
path.isCons(TRefTypeParameter(), pathAdj) and
632634
tAdj = t
633635
or
634-
not path.startsWith(TRefTypeParameter(), _) and
636+
not path.isCons(TRefTypeParameter(), _) and
635637
not (t = TRefType() and path.isEmpty()) and
636638
pathAdj = path and
637639
tAdj = t
@@ -674,19 +676,19 @@ private Type inferCallExprBaseType(AstNode n, TypePath path) {
674676
if receiverType = TRefType()
675677
then
676678
path = path0 and
677-
path0.startsWith(TRefTypeParameter(), _)
679+
path0.isCons(TRefTypeParameter(), _)
678680
or
679681
// adjust for implicit deref
680-
not path0.startsWith(TRefTypeParameter(), _) and
682+
not path0.isCons(TRefTypeParameter(), _) and
681683
not (path0.isEmpty() and result = TRefType()) and
682684
path = TypePath::cons(TRefTypeParameter(), path0)
683685
else (
684-
not path0.startsWith(TRefTypeParameter(), _) and
686+
not path0.isCons(TRefTypeParameter(), _) and
685687
not (path0.isEmpty() and result = TRefType()) and
686688
path = path0
687689
or
688690
// adjust for implicit borrow
689-
path0.startsWith(TRefTypeParameter(), path)
691+
path0.isCons(TRefTypeParameter(), path)
690692
)
691693
)
692694
else path = path0
@@ -748,7 +750,7 @@ private module FieldExprMatchingInput implements MatchingInputSig {
748750
class AccessPosition = DeclarationPosition;
749751

750752
class Access extends FieldExpr {
751-
Type getExplicitTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
753+
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) { none() }
752754

753755
AstNode getNodeAt(AccessPosition apos) {
754756
result = this.getExpr() and
@@ -781,10 +783,10 @@ private module FieldExprMatchingInput implements MatchingInputSig {
781783
if apos.isSelf()
782784
then
783785
// adjust for implicit deref
784-
path.startsWith(TRefTypeParameter(), pathAdj) and
786+
path.isCons(TRefTypeParameter(), pathAdj) and
785787
tAdj = t
786788
or
787-
not path.startsWith(TRefTypeParameter(), _) and
789+
not path.isCons(TRefTypeParameter(), _) and
788790
not (t = TRefType() and path.isEmpty()) and
789791
pathAdj = path and
790792
tAdj = t
@@ -824,7 +826,7 @@ private Type inferFieldExprType(AstNode n, TypePath path) {
824826
if receiverType = TRefType()
825827
then
826828
// adjust for implicit deref
827-
not path0.startsWith(TRefTypeParameter(), _) and
829+
not path0.isCons(TRefTypeParameter(), _) and
828830
not (path0.isEmpty() and result = TRefType()) and
829831
path = TypePath::cons(TRefTypeParameter(), path0)
830832
else path = path0
@@ -846,7 +848,7 @@ private Type inferRefExprType(Expr e, TypePath path) {
846848
or
847849
e = re and
848850
exists(TypePath exprPath | result = inferType(re.getExpr(), exprPath) |
849-
if exprPath.startsWith(TRefTypeParameter(), _)
851+
if exprPath.isCons(TRefTypeParameter(), _)
850852
then
851853
// `&x` simply means `x` when `x` already has reference type
852854
path = exprPath
@@ -975,7 +977,7 @@ private module Cached {
975977
Stages::TypeInference::backref() and
976978
result = inferAnnotatedType(n, path)
977979
or
978-
result = inferTypeSymmetry(n, path)
980+
result = inferTypeEquality(n, path)
979981
or
980982
result = inferImplicitSelfType(n, path)
981983
or

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class TypeReprMention extends TypeMention, TypeRepr {
7878

7979
class PathMention extends TypeMention, Path {
8080
override TypeMention getTypeArgument(int i) {
81-
result = this.getPart().getGenericArgList().getTypeArgument(i)
81+
result = this.getPart().getGenericArgList().getTypeArg(i)
8282
or
8383
// `Self` paths inside traits and `impl` blocks have implicit type arguments
8484
// that are the type parameters of the trait or impl. For example, in
@@ -93,7 +93,7 @@ class PathMention extends TypeMention, Path {
9393
//
9494
// the `Self` return type is shorthand for `Foo<T>`.
9595
exists(ImplOrTraitItemNode node | this = node.getASelfPath() |
96-
result = node.(ImplItemNode).getSelfPath().getPart().getGenericArgList().getTypeArgument(i)
96+
result = node.(ImplItemNode).getSelfPath().getPart().getGenericArgList().getTypeArg(i)
9797
or
9898
result = node.(Trait).getGenericParamList().getTypeParam(i)
9999
)
@@ -140,7 +140,7 @@ private predicate isImplSelfTypeParam(
140140
) {
141141
exists(PathMention path |
142142
selfPath = impl.getSelfPath() and
143-
path = selfPath.getPart().getGenericArgList().getTypeArgument(i).(PathTypeRepr).getPath() and
143+
path = selfPath.getPart().getGenericArgList().getTypeArg(i).(PathTypeRepr).getPath() and
144144
tp = path.resolveType()
145145
)
146146
}

0 commit comments

Comments
 (0)