Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
522 changes: 520 additions & 2 deletions shared/typeinference/codeql/typeinference/internal/TypeInference.qll

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

private import codeql.util.Unit
private import codeql.typeinference.internal.TypeInference
private import codeql.typeinference.internal.TypeInference as TypeInferenceInternal
private import codeql.unified.internal.ParameterEx
private import codeql.unified.internal.ExprPositions
private import codeql.unified.internal.NameBinding
Expand All @@ -33,6 +34,10 @@ private import TypeInferencePlugin as Plugin

class Type = T::Type;

class Variance = TypeInferenceInternal::Variance;

class MatchingRelation = TypeInferenceInternal::MatchingRelation;

private module Input1 implements InputSig1<Location> {
private import Type as T

Expand All @@ -44,6 +49,10 @@ private module Input1 implements InputSig1<Location> {

class TypeAbstraction = TA::TypeAbstraction;

Variance getTypeParameterVariance(TypeParameter tp) {
result = any(Plugin::TypeInferencePlugin pl).getTypeParameterVariance(tp)
}

int getTypeParameterId(TypeParameter tp) {
CachedStage::ref() and
tp =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class TypeInferencePlugin extends Unit {
/** Gets the type parameter representing the element type of an array. */
abstract TypeParameter getArrayElementTypeParameter();

/** Gets a non-invariant variance of a type parameter; absence means invariance. */
Variance getTypeParameterVariance(TypeParameter tp) { none() }

/**
* Gets the type of the given function expression.
*
Expand All @@ -36,6 +39,9 @@ class TypeInferencePlugin extends Unit {
bindingset[fe]
abstract Type getFunctionExprType(FunctionExpr fe);

/** Gets an intermediate constructor in the type tree of a function signature. */
Type getFunctionExprIntermediateType(FunctionExpr fe, TypePath path) { none() }

/**
* Gets the type path for the `i`th parameter of the given function type
* when the function type has the given arity.
Expand Down Expand Up @@ -128,6 +134,10 @@ TypePath getFunctionExprReturnTypePath(Type t) {
result = any(TypeInferencePlugin pl).getFunctionExprReturnTypePath(t)
}

Type getFunctionExprIntermediateType(FunctionExpr fe, TypePath path) {
result = any(TypeInferencePlugin pl).getFunctionExprIntermediateType(fe, path)
}

class TupleType extends Type {
int arity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ TypeParameter getTupleTypeParameter(int arity, int i) {
private predicate inferTypeAlias = inferType/2;

private class SwiftTypeInferencePlugin extends Plugin::TypeInferencePlugin {
override Variance getTypeParameterVariance(TypeParameter tp) {
tp = getFunctionArgsTypeParameter() and result.isContravariant()
or
(
tp = getFunctionReturnTypeParameter()
or
tp instanceof ParentTypeParameterType
or
tp = any(TupleType t).getATypeParameter()
or
tp = any(ArrayType t).getATypeParameter()
or
tp = any(OptionalType t).getATypeParameter()
) and
result.isCovariant()
}

override Type getBoolType() { result instanceof BoolType }

override Type getIntType() { result instanceof IntType }
Expand All @@ -115,6 +132,11 @@ private class SwiftTypeInferencePlugin extends Plugin::TypeInferencePlugin {
exists(fe)
}

override Type getFunctionExprIntermediateType(FunctionExpr fe, TypePath path) {
result = this.getTupleType(fe.getNumberOfParameters()) and
path = TypePath::singleton(getFunctionArgsTypeParameter())
}

bindingset[t]
override TypePath getFunctionExprParameterTypePath(Type t, int arity, int i) {
result = functionParameterPath(arity, i) and exists(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class ExprTypeMention extends TypeMentionImpl, Expr {
result = this.getUnexpandedType() and
path.isEmpty()
or
result = Plugin::getFunctionExprIntermediateType(this.(FunctionExpr), path)
or
exists(
ClassLikeDeclaration c, AssociatedTypeDeclaration a, ClassLikeDeclaration base,
AssociatedTypeParameterType baseTp, string name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ var y7 = innerMost.getOuter() // $ target=Outer.Inner.InnerMost.getOuter $ MISS

class SubOuter<TSubOuter>: Outer<[TSubOuter]> {
func f(_ x: Inner<String>) -> [TSubOuter] {
let res = x.getOuter() // $ target=Outer.Inner.getOuter $ MISSING: type=res@Array<Element>:TSubOuter -- this is an instance of variance sub typing
let res = x.getOuter() // $ target=Outer.Inner.getOuter type=res@Array<Element>:TSubOuter
return res
}
}
Expand Down Expand Up @@ -321,10 +321,10 @@ class SubSub: Sub {
x3: Element2, // $ type=x3@Base2.Nested<(parent:Nested)>:Sub
x4: SubSub.Element2 // $ type=x4@Base2.Nested<(parent:Nested)>:Sub
) {
let a = x1.getX() // $ target=Base2.Nested.getX $ MISSING: type=a:String -- this is an instance of variance sub typing
let b = x2.getX() // $ target=Base2.Nested.getX $ MISSING: type=b:String -- this is an instance of variance sub typing
let c = x3.getX() // $ target=Base2.Nested.getX $ MISSING: type=c:String -- this is an instance of variance sub typing
let d = x4.getX() // $ target=Base2.Nested.getX $ MISSING: type=d:String -- this is an instance of variance sub typing
let a = x1.getX() // $ target=Base2.Nested.getX type=a:String
let b = x2.getX() // $ target=Base2.Nested.getX type=b:String
let c = x3.getX() // $ target=Base2.Nested.getX type=c:String
let d = x4.getX() // $ target=Base2.Nested.getX type=d:String
}
}

Expand Down
Loading
Loading