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
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,80 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
hasNotTypeArgument(a, target, tp)
)
}

predicate baseTypeMatchAtTypeParameter(
Access a, AccessEnvironment e, AccessPosition apos, Declaration target, TypeParameter tp,
TypePath prefix, TypePath requiredPrefix
) {
exists(
TypePath pathToTypeParamInConstraint, TypePath pathToTp, TypePath pathToTypeParamInSub
|
argRootTypeSatisfiesTargetTypeCand(_, target, pragma[only_bind_into](apos), tp, pathToTp) and
SatisfiesParameterConstraint::satisfiesConstraintAtTypeParameter(MkRelevantAccess(a,
pragma[only_bind_into](apos), e),
MkRelevantTarget(target, pragma[only_bind_into](apos)), pathToTypeParamInConstraint,
pathToTypeParamInSub) and
hasNotTypeArgument(a, target, tp)
|
/*
* Example:
*
* ```swift
* class Base<B> {
* init(_ value: B) {}
* }
*
* class Derived<D>: Base<[D]> {
* init(_ value: D) { super.init([value]) }
* }
*
* func foo<T>(_ value: T, _ base: Base<T>) { }
*
* foo([2], Derived(<unknown>))
* ```
*
* - tp = T (bound by `foo`)
* - prefix = pathToTypeParamInSub = "D"
* - requiredPrefix = "Element"
* - pathToTypeParamInConstraint = "B.Element"
* - pathToTp = "B"
*/

pathToTypeParamInConstraint = pathToTp.appendInverse(requiredPrefix) and
prefix = pathToTypeParamInSub
or
/*
* Example:
*
* ```swift
* class Base<B> {
* init(_ value: B) {}
* }
*
* class Derived<D>: Base<D> {
* override init(_ value: D) { super.init(value) }
* }
*
* func foo<T>(_ value: T, _ base: Base<T?>) {}
*
* foo(2, Derived2(Optional.none))
* ```
*
* - tp = T (bound by `foo`)
* - prefix = "D.Wrapped"
* - pathToTypeParamInSub = "D"
* - requiredPrefix = ""
* - pathToTypeParamInConstraint = "B"
* - pathToTp = "B.Wrapped"
*/

exists(TypePath path0 |
pathToTp = pathToTypeParamInConstraint.appendInverse(path0) and
prefix = pathToTypeParamInSub.append(path0) and
requiredPrefix = TypePath::nil()
)
)
}
}

private module AccessConstraint {
Expand Down Expand Up @@ -1745,6 +1819,16 @@ module Make1<LocationSig Location, InputSig1<Location> Input1> {
)
)
or
exists(
Declaration target, TypePath prefix, TypePath requiredPrefix, TypePath suffix,
TypeParameter tp
|
AccessBaseType::baseTypeMatchAtTypeParameter(a, e, apos, target, tp, prefix,
requiredPrefix) and
typeMatch(a, e, target, requiredPrefix.appendInverse(suffix), result, tp) and
path = prefix.append(suffix)
)
or
exists(
Declaration target, TypePath prefix, TypeMention constraint,
TypePath pathToTypeParamInConstraint, TypePath pathToTypeParamInSub
Expand Down
5 changes: 5 additions & 0 deletions unified/ql/lib/codeql/unified/internal/FacadeAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,9 @@
/** Gets the number of parameters of this function. */
int getNumberOfParameters() { result = count(this.getAParameter()) }
}

class ArrayLiteral extends G::ArrayLiteral {

Check warning on line 323 in unified/ql/lib/codeql/unified/internal/FacadeAst.qll

View workflow job for this annotation

GitHub Actions / qldoc

Missing QLdoc for class FacadeAst::Unified::ArrayLiteral
/** Gets the number of elements in this array literal. */
int getNumberOfElements() { result = count(this.getAnElement()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,11 @@ private module Input3 implements InputSig3 {
not exists(n.(MemberAccessExpr).getBase().(TypeMention).getTypeAt(path))
) and
result instanceof UnknownType
or
hasResultValue(n) and
n.(ArrayLiteral).getNumberOfElements() = 0 and
path = TypePath::singleton(getArrayElementTypeParameter()) and
result instanceof UnknownType
}

pragma[nomagic]
Expand Down
52 changes: 47 additions & 5 deletions unified/ql/test/library-tests/type-inference/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,64 @@ class Derived<T1, T2>: Base<T2, T1> {
init(_ v1: T1, _ v2: T2) {
super.init(v2, v1) // $ type=v2:T2 type=v1:T1 target=Base.init
}

convenience init(_ v1: T1) {
fatalError()
}
}

class DerivedDerived<D>: Derived<D, Bool> {
init(_ v: D) {
super.init(v, true) // $ type=v:D target=Derived.init
class DerivedDerived<D>: Derived<[D], Bool> {
init(_ v: [D]) {
super.init(v, true) // $ type=v@Array<Element>:D target=Derived.init
}

convenience init() {
fatalError()
}
}

func foo<T1, T2, T3: Base<T1, T2>>(_ value1: T1, _ value2: T2, _ base: T3) -> T3 {
return base
}

func foo2<T1, T2>(_ value1: T1, _ value2: T2, _ base: Base<T1, T2>) {

}

func bar<A, B, C: Base<A, [B]>>(_ value1: A, _ value2: B, _ base: C) -> C {
return base

}

func bar2<A, B>(_ value1: A, _ value2: B, _ base: Base<A, [B]>) {}

func baz<A, B, C: Base<A, B?>>(_ value1: A, _ value2: B, _ base: C) -> C {
return base

}

func baz2<A, B>(_ value1: A, _ value2: B, _ base: Base<A, B?>) {}

func testDerived() {
let d = Derived(1, "x") // $ type=d@Derived<T1>:Int type=d@Derived<T2>:String target=Derived.init
let v1 = d.getValue1() // $ type=v1:String target=Base.getValue1
let v2 = d.getValue2() // $ type=v2:Int target=Base.getValue2

let dd = DerivedDerived("hello") // $ type=dd@DerivedDerived<D>:String target=DerivedDerived.init
let dd = DerivedDerived(["hello"]) // $ type=dd@DerivedDerived<D>:String target=DerivedDerived.init
let vv1 = dd.getValue1() // $ type=vv1:Bool target=Base.getValue1
let vv2 = dd.getValue2() // $ type=vv2:String target=Base.getValue2
let vv2 = dd.getValue2() // $ type=vv2@Array<Element>:String target=Base.getValue2

let x = foo(false, [2], DerivedDerived([])) // $ target=foo target=DerivedDerived.init type=x@DerivedDerived<D>:Int

foo2(false, [2], DerivedDerived([])) // $ target=foo2 target=DerivedDerived.init type=DerivedDerived(...)@DerivedDerived<D>:Int

let y = bar(false, 2, DerivedDerived([])) // $ type=y@DerivedDerived<D>:Int target=bar target=DerivedDerived.init

bar2(false, 2, DerivedDerived([])) // $ target=bar2 target=DerivedDerived.init type=DerivedDerived(...)@DerivedDerived<D>:Int

let w = baz(false, 2, Derived(Optional.none)) // $ type=w@Derived<T1>.Optional<Wrapped>:Int target=baz target=Derived.init field=Optional.none

baz2(false, 2, Derived(Optional.none)) // $ target=baz2 target=Derived.init field=Optional.none type=Derived(...)@Derived<T1>.Optional<Wrapped>:Int
}

// --- Generics and protocols ---
Expand Down
Loading
Loading