Skip to content

Commit f604b28

Browse files
committed
Swift: Resolve type aliases (1) in base class declarations, not before them and (2) consistently. This is not behaviour preserving.
1 parent c78f5ce commit f604b28

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

swift/ql/lib/codeql/swift/elements/decl/TypeDecl.qll

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ class TypeDecl extends Generated::TypeDecl {
3535
deprecated Type getBaseType(int index) { result = this.getInheritedType(index) }
3636

3737
/**
38-
* Gets any of the base types of this type declaration.
38+
* Gets any of the base types of this type declaration. Expands type aliases, for example
39+
* in the following code, `B` has base type `A`.
40+
* ```
41+
* typealias A_alias = A
42+
*
43+
* class B : A_alias {}
44+
* ```
3945
*/
4046
Type getABaseType() {
41-
// TODO generalize this to resolve `TypeAliasDecl`s and consider bases added by extensions
42-
result = this.getAnInheritedType()
47+
// TODO generalize this to consider bases added by extensions
48+
result = this.getAnInheritedType().getUnderlyingType()
4349
}
4450

4551
/**
@@ -51,7 +57,13 @@ class TypeDecl extends Generated::TypeDecl {
5157
}
5258

5359
/**
54-
* Gets the declaration of any of the base types of this type declaration.
60+
* Gets the declaration of any of the base types of this type declaration. Expands type
61+
* aliases, for example in the following code, `B` has base type decl `A`.
62+
* ```
63+
* typealias A_alias = A
64+
*
65+
* class B : A_alias {}
66+
* ```
5567
*/
5668
TypeDecl getABaseTypeDecl() { result = this.getABaseType().(AnyGenericType).getDeclaration() }
5769

@@ -63,7 +75,13 @@ class TypeDecl extends Generated::TypeDecl {
6375
deprecated TypeDecl getDerivedTypeDecl(int i) { result.getBaseTypeDecl(i) = this }
6476

6577
/**
66-
* Gets the declaration of any type derived from this type declaration.
78+
* Gets the declaration of any type derived from this type declaration. Expands type aliases,
79+
* for example in the following code, `B` is derived from `A`.
80+
* ```
81+
* typealias A_alias = A
82+
*
83+
* class B : A_alias {}
84+
* ```
6785
*/
6886
TypeDecl getADerivedTypeDecl() { result.getABaseTypeDecl() = this }
6987

swift/ql/lib/codeql/swift/elements/type/Type.qll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ class Type extends Generated::Type {
4343
Type getUnderlyingType() { result = this }
4444

4545
/**
46-
* Gets any base type of this type. For a `typealias`, this is a base type
47-
* of the aliased type. For example in the following code, both `B` and
48-
* `B_alias` have base type `A`.
46+
* Gets any base type of this type. Expands type aliases, for example in the following
47+
* code, `B` has base type `A`.
4948
* ```
50-
* class A {}
49+
* typealias A_alias = A
5150
*
52-
* class B : A {}
53-
*
54-
* typealias B_alias = B
51+
* class B : A_alias {}
5552
* ```
5653
*/
5754
Type getABaseType() { result = this.(AnyGenericType).getDeclaration().getABaseType() }
5855

56+
/**
57+
* Gets a type derived from this type. Expands type aliases, for example in the following
58+
* code, `B` derives from type `A`.
59+
* ```
60+
* typealias A_alias = A
61+
*
62+
* class B : A_alias {}
63+
* ```
64+
*/
5965
Type getADerivedType() { result.getABaseType() = this }
6066
}

swift/ql/lib/codeql/swift/elements/type/TypeAliasType.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@ class TypeAliasType extends Generated::TypeAliasType {
1919
Type getAliasedType() { result = this.getDecl().getAliasedType() }
2020

2121
override Type getUnderlyingType() { result = this.getAliasedType().getUnderlyingType() }
22-
23-
override Type getABaseType() { result = this.getAliasedType().getABaseType() }
2422
}

swift/ql/test/library-tests/elements/type/nominaltype/nominaltype.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias, getUnderlyingType:A |
55
| nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias, getUnderlyingType:A? |
66
| nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getFullName:B1, getName:B1, getUnderlyingType:B1 |
7-
| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2, getUnderlyingType:B2 |
8-
| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getABaseType:A, getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 |
9-
| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getABaseType:A_alias, getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 |
7+
| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A, getFullName:B2, getName:B2, getUnderlyingType:B2 |
8+
| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias, getUnderlyingType:B1 |
9+
| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias, getUnderlyingType:B2 |
1010
| nominaltype.swift:93:6:93:6 | p | P | getFullName:P, getName:P, getUnderlyingType:P |
1111
| nominaltype.swift:94:6:94:6 | p_alias | P_alias | getFullName:P_alias, getName:P_alias, getUnderlyingType:P_alias |
1212
| nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getFullName:C1, getName:C1, getUnderlyingType:C1 |
13-
| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2, getUnderlyingType:C2 |
13+
| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P, getFullName:C2, getName:C2, getUnderlyingType:C2 |
1414
| nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer, getUnderlyingType:Outer |
1515
| nominaltype.swift:98:6:98:6 | oi | Outer.Inner | getFullName:Outer.Inner, getName:Inner, getUnderlyingType:Outer.Inner |
1616
| nominaltype.swift:99:6:99:6 | oia | Outer.Inner.InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias, getUnderlyingType:A |

swift/ql/test/library-tests/elements/type/nominaltype/nominaltypedecl.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
| nominaltype.swift:87:6:87:6 | a_alias | A_alias | getAliasedType:A, getFullName:A_alias, getName:A_alias |
44
| nominaltype.swift:88:6:88:6 | a_optional_alias | A_optional_alias | getAliasedType:A?, getFullName:A_optional_alias, getName:A_optional_alias |
55
| nominaltype.swift:89:6:89:6 | b1 | B1 | getABaseType:A, getABaseTypeDecl:A, getFullName:B1, getName:B1 |
6-
| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A_alias, getFullName:B2, getName:B2 |
6+
| nominaltype.swift:90:6:90:6 | b2 | B2 | getABaseType:A, getABaseTypeDecl:A, getFullName:B2, getName:B2 |
77
| nominaltype.swift:91:6:91:6 | b1_alias | B1_alias | getAliasedType:B1, getFullName:B1_alias, getName:B1_alias |
88
| nominaltype.swift:92:6:92:6 | b2_alias | B2_alias | getAliasedType:B2, getFullName:B2_alias, getName:B2_alias |
99
| nominaltype.swift:95:6:95:6 | c1 | C1 | getABaseType:P, getABaseTypeDecl:P, getFullName:C1, getName:C1 |
10-
| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P_alias, getFullName:C2, getName:C2 |
10+
| nominaltype.swift:96:6:96:6 | c2 | C2 | getABaseType:P, getABaseTypeDecl:P, getFullName:C2, getName:C2 |
1111
| nominaltype.swift:97:6:97:6 | o | Outer | getFullName:Outer, getName:Outer |
1212
| nominaltype.swift:98:6:98:6 | oi | Inner | getFullName:Outer.Inner, getName:Inner |
1313
| nominaltype.swift:99:6:99:6 | oia | InnerAlias | getAliasedType:A, getFullName:Outer.Inner.InnerAlias, getName:InnerAlias |

0 commit comments

Comments
 (0)