Skip to content

Commit 4bbbc6c

Browse files
paldepindCopilot
andcommitted
Unified: Extract attributed types as modified_type_expr
Preserve the base type of attributed types (`inout T`, `sending T`, `@escaping () -> T`) and keep their specifiers and attributes as modifiers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent faf13ad commit 4bbbc6c

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎unified/extractor/ast_types.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ supertypes:
4141
- bulk_importing_pattern
4242
- ignore_pattern
4343
- generic_type_expr
44+
- modified_type_expr
4445
- inferred_type_expr
4546
# An element of an `unresolved_operator_sequence`: either an operand (`expr`)
4647
# or one of the infix operators separating the operands.
@@ -553,5 +554,11 @@ named:
553554
base: expr
554555
type_argument*: expr
555556

557+
# A type with a modifier. In Swift this can be specifiers or attributes, such
558+
# as `inout T`, `sending T` or `@escaping () -> Void`.
559+
modified_type_expr:
560+
modifier*: modifier
561+
type: expr
562+
556563
# A modifier such as 'static', 'public', or 'async'. For now this is just a leaf node with a string value.
557564
modifier:

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,24 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
11641164
// becomes a `modifier`; its source text is the modifier spelling.
11651165
rule!((attribute) @m => (modifier #{m})),
11661166
rule!((declModifier) @m => (modifier #{m})),
1167+
// Type specifiers (`inout`, `sending`, ...) are mapped to modifier.
1168+
rule!((simpleTypeSpecifier) @m => (modifier #{m})),
1169+
rule!((lifetimeTypeSpecifier) @m => (modifier #{m})),
1170+
rule!((nonisolatedTypeSpecifier) @m => (modifier #{m})),
1171+
// An attributed type is mapped to a modified type of its base type.
1172+
rule!(
1173+
(attributedType
1174+
specifiers: _* @specs
1175+
attributes: _* @attrs
1176+
lateSpecifiers: _* @late
1177+
baseType: @base)
1178+
=>
1179+
(modified_type_expr
1180+
modifier: {specs}
1181+
modifier: {attrs}
1182+
modifier: {late}
1183+
type: {base})
1184+
),
11671185
// Preserve the `super` keyword as a dedicated expression, normally used
11681186
// as the base of a member access (`super.foo`).
11691187
rule!((superExpr superKeyword: @@keyword) => (super_expr #{keyword})),
@@ -1471,7 +1489,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
14711489
// currently map them to the unified AST. Syntax nested inside one of
14721490
// these roots is discarded with its parent and needs no separate rule.
14731491
rule!((actorDecl) => (unsupported_node)),
1474-
rule!((attributedType) => (unsupported_node)),
14751492
rule!((borrowExpr) => (unsupported_node)),
14761493
rule!((classRestrictionType) => (unsupported_node)),
14771494
rule!((compositionType) => (unsupported_node)),

‎unified/ql/lib/codeql/unified/internal/ExprPositions.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ predicate isInTypeContext(Expr expr) {
3232
or
3333
expr = any(GenericTypeExpr gte).getATypeArgument()
3434
or
35+
expr = any(ModifiedTypeExpr m).getType()
36+
or
3537
expr.getParent() instanceof TypeConstraint
3638
or
3739
exists(Identifier id | id = NameBinding::getStaticBindingTargetFromRef(expr) |

0 commit comments

Comments
 (0)