Skip to content

Commit 4281605

Browse files
committed
Swift: extract existential types
1 parent 631156d commit 4281605

File tree

10 files changed

+41
-7
lines changed

10 files changed

+41
-7
lines changed

swift/codegen/schema.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ ProtocolCompositionType:
115115

116116
ExistentialType:
117117
_extends: Type
118+
constraint: Type
118119

119120
ReferenceStorageType:
120121
_extends: Type

swift/extractor/visitors/TypeVisitor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,11 @@ void TypeVisitor::fillArchetypeType(const swift::ArchetypeType& type, ArchetypeT
253253
entry.superclass = dispatcher_.fetchOptionalLabel(type.getSuperclass());
254254
fillType(type, entry);
255255
}
256+
codeql::ExistentialType TypeVisitor::translateExistentialType(const swift::ExistentialType& type) {
257+
codeql::ExistentialType entry{dispatcher_.assignNewLabel(type)};
258+
entry.constraint = dispatcher_.fetchLabel(type.getConstraintType());
259+
fillType(type, entry);
260+
return entry;
261+
}
256262

257263
} // namespace codeql

swift/extractor/visitors/TypeVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
3838
codeql::PrimaryArchetypeType translatePrimaryArchetypeType(
3939
const swift::PrimaryArchetypeType& type);
4040
codeql::NestedArchetypeType translateNestedArchetypeType(const swift::NestedArchetypeType& type);
41+
codeql::ExistentialType translateExistentialType(const swift::ExistentialType& type);
4142

4243
private:
4344
void fillType(const swift::TypeBase& type, codeql::Type& entry);
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// generated by codegen/codegen.py, remove this comment if you wish to edit this file
21
private import codeql.swift.generated.type.ExistentialType
2+
private import codeql.swift.elements.type.ProtocolType
33

4-
class ExistentialType extends ExistentialTypeBase { }
4+
class ExistentialType extends ExistentialTypeBase {
5+
override ProtocolType getConstraint() { result = super.getConstraint() }
6+
}

swift/ql/lib/codeql/swift/generated/type/ExistentialType.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@ import codeql.swift.elements.type.Type
33

44
class ExistentialTypeBase extends @existential_type, Type {
55
override string getAPrimaryQlClass() { result = "ExistentialType" }
6+
7+
Type getConstraint() {
8+
exists(Type x |
9+
existential_types(this, x) and
10+
result = x.resolve()
11+
)
12+
}
613
}

swift/ql/lib/swift.dbscheme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ protocol_composition_types( //dir=type
273273
);
274274

275275
existential_types( //dir=type
276-
unique int id: @existential_type
276+
unique int id: @existential_type,
277+
int constraint: @type ref
277278
);
278279

279280
@reference_storage_type =
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| ExplicitExistential | getDiagnosticsName: | ExplicitExistential | getCanonicalType: | ExplicitExistential | getConstraint: | ExplicitExistential |
2+
| ImplicitExistential | getDiagnosticsName: | ImplicitExistential | getCanonicalType: | ImplicitExistential | getConstraint: | ImplicitExistential |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// generated by codegen/codegen.py
2+
import codeql.swift.elements
3+
import TestUtils
4+
5+
from ExistentialType x, string getDiagnosticsName, Type getCanonicalType, Type getConstraint
6+
where
7+
toBeTested(x) and
8+
not x.isUnknown() and
9+
getDiagnosticsName = x.getDiagnosticsName() and
10+
getCanonicalType = x.getCanonicalType() and
11+
getConstraint = x.getConstraint()
12+
select x, "getDiagnosticsName:", getDiagnosticsName, "getCanonicalType:", getCanonicalType,
13+
"getConstraint:", getConstraint

swift/ql/test/extractor-tests/generated/type/ExistentialType/MISSING_SOURCE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
protocol ExplicitExistential {}
2+
protocol ImplicitExistential {}
3+
4+
func foo1(_: any ExplicitExistential) {}
5+
func foo2(_: ImplicitExistential) {} // valid for now, will be an error in some future swift release

0 commit comments

Comments
 (0)