Swift: remove property wrapper AST inconsistencies

This commit is contained in:
Paolo Tranquilli
2023-05-25 11:27:38 +02:00
parent 5de8934525
commit 7878bc3cc1
6 changed files with 55 additions and 64 deletions

View File

@@ -28,7 +28,7 @@ private predicate shouldPrint(Locatable e) { any(PrintAstConfiguration config).s
/**
* An AST node that should be printed.
*/
private newtype TPrintAstNode = TLocatable(Locatable ast)
private newtype TPrintAstNode = TPrintLocatable(Locatable ast)
/**
* A node in the output tree.
@@ -60,6 +60,11 @@ class PrintAstNode extends TPrintAstNode {
* the property is `key`.
*/
string getProperty(string key) { none() }
/**
* Gets the underlying AST node, if any.
*/
abstract Locatable getAstNode();
}
private string prettyPrint(Locatable e) {
@@ -73,10 +78,10 @@ private class Unresolved extends Locatable {
/**
* A graph node representing a real Locatable node.
*/
class PrintLocatable extends PrintAstNode, TLocatable {
class PrintLocatable extends PrintAstNode, TPrintLocatable {
Locatable ast;
PrintLocatable() { this = TLocatable(ast) }
PrintLocatable() { this = TPrintLocatable(ast) }
override string toString() { result = prettyPrint(ast) }
@@ -87,9 +92,9 @@ class PrintLocatable extends PrintAstNode, TLocatable {
c = getChildAndAccessor(ast, i, accessor) and
(
// use even indexes for normal children, leaving odd slots for conversions if any
child = TLocatable(c) and index = 2 * i and label = accessor
child = TPrintLocatable(c) and index = 2 * i and label = accessor
or
child = TLocatable(c.getFullyUnresolved().(Unresolved)) and
child = TPrintLocatable(c.getFullyUnresolved().(Unresolved)) and
index = 2 * i + 1 and
(
if c instanceof Expr
@@ -100,6 +105,8 @@ class PrintLocatable extends PrintAstNode, TLocatable {
)
}
final override Locatable getAstNode() { result = ast }
final override Location getLocation() { result = ast.getLocation() }
}
@@ -112,17 +119,38 @@ class PrintUnresolved extends PrintLocatable {
override predicate hasChild(PrintAstNode child, int index, string label) {
// only print immediate unresolved children from the "parallel" AST
child = TLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved))
child = TPrintLocatable(getImmediateChildAndAccessor(ast, index, label).(Unresolved))
}
}
private predicate hasPropertyWrapperElement(VarDecl d, Locatable a) {
a = [d.getPropertyWrapperBackingVar(), d.getPropertyWrapperProjectionVar()] or
a = [d.getPropertyWrapperBackingVarBinding(), d.getPropertyWrapperProjectionVarBinding()]
}
/**
* A specialization of graph node for `VarDecl`, to add typing information.
* A specialization of graph node for `VarDecl`, to add typing information and deal with ambiguity
* over property wrapper children.
*/
class PrintVarDecl extends PrintLocatable {
override VarDecl ast;
override string getProperty(string key) { key = "Type" and result = ast.getType().toString() }
override predicate hasChild(PrintAstNode child, int index, string label) {
PrintLocatable.super.hasChild(child, index, label) and
// exclude property wrapper related children when they are already listed in the enclosing
// nominal type declaration or for a wrapped parameter for which this is a virtual local variable copy
not exists(Locatable childAst |
childAst = child.getAstNode() and
hasPropertyWrapperElement(ast, childAst) and
(
childAst = ast.getDeclaringDecl().getAMember()
or
ast instanceof ConcreteVarDecl and hasPropertyWrapperElement(any(ParamDecl p), childAst)
)
)
}
}
/**
@@ -135,3 +163,23 @@ class PrintFunction extends PrintLocatable {
key = "InterfaceType" and result = ast.getInterfaceType().toString()
}
}
/**
* A specialization of graph node for `PatternBindingDecl`, to solve ambiguity on `getInit`.
* When a property wrapper is involved, `getInit` may become shared between the explicit binding and
* the implicit compiler synthesized one.
*/
class PrintPatternBindingDecl extends PrintLocatable {
override PatternBindingDecl ast;
override predicate hasChild(PrintAstNode child, int index, string label) {
PrintLocatable.super.hasChild(child, index, label) and
// exclude `getInit` that are already the initializer of a variable that has this as a property wrapper backer
not exists(Expr init, VarDecl var |
init = child.getAstNode() and
init = ast.getAnInit() and
var.getPropertyWrapperBackingVarBinding() = ast and
var.getParentInitializer() = init
)
}
}

View File

@@ -1,13 +0,0 @@
doubleParents
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:54:6:54:15 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:54:4:54:15 | [CallExpr] call to X<T>.init(wrappedValue:) |
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:55:20:55:29 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:55:4:55:29 | [CallExpr] call to WrapperWithInit.init(wrappedValue:) |
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:56:25:56:34 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:56:4:56:34 | [CallExpr] call to WrapperWithProjected.init(wrappedValue:projectedValue:) |
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:57:32:57:41 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:57:4:57:41 | [CallExpr] call to WrapperWithProjectedAndInit.init(wrappedValue:) |
| var_decls.swift:23:1:25:1 | [StructDecl] Wrapped | getMember(2) | var_decls.swift:24:15:24:15 | [ConcreteVarDecl] wrapped | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| var_decls.swift:23:1:25:1 | [StructDecl] Wrapped | getMember(3) | var_decls.swift:24:15:24:15 | [ConcreteVarDecl] wrapped | getPropertyWrapperBackingVar() | var_decls.swift:24:15:24:15 | [ConcreteVarDecl] _wrapped |
| var_decls.swift:24:15:24:15 | [ConcreteVarDecl] wrapped | getPropertyWrapperBackingVar() | var_decls.swift:23:1:25:1 | [StructDecl] Wrapped | getMember(3) | var_decls.swift:24:15:24:15 | [ConcreteVarDecl] _wrapped |
| var_decls.swift:24:15:24:15 | [ConcreteVarDecl] wrapped | getPropertyWrapperBackingVarBinding() | var_decls.swift:23:1:25:1 | [StructDecl] Wrapped | getMember(2) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| var_decls.swift:54:6:54:15 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:54:4:54:15 | [CallExpr] call to X<T>.init(wrappedValue:) |
| var_decls.swift:55:20:55:29 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:55:4:55:29 | [CallExpr] call to WrapperWithInit.init(wrappedValue:) |
| var_decls.swift:56:25:56:34 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:56:4:56:34 | [CallExpr] call to WrapperWithProjected.init(wrappedValue:projectedValue:) |
| var_decls.swift:57:32:57:41 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | var_decls.swift:57:4:57:41 | [CallExpr] call to WrapperWithProjectedAndInit.init(wrappedValue:) |

View File

@@ -1,21 +0,0 @@
doubleParents
| param_decls.swift:48:18:48:18 | [ConcreteVarDecl] p1 | getPropertyWrapperBackingVar() | param_decls.swift:48:18:48:22 | [ParamDecl] p1 | getPropertyWrapperBackingVar() | param_decls.swift:48:18:48:18 | [ConcreteVarDecl] _p1 |
| param_decls.swift:48:18:48:18 | [ConcreteVarDecl] p1 | getPropertyWrapperBackingVarBinding() | param_decls.swift:48:18:48:22 | [ParamDecl] p1 | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:48:18:48:22 | [ParamDecl] p1 | getPropertyWrapperBackingVar() | param_decls.swift:48:18:48:18 | [ConcreteVarDecl] p1 | getPropertyWrapperBackingVar() | param_decls.swift:48:18:48:18 | [ConcreteVarDecl] _p1 |
| param_decls.swift:48:18:48:22 | [ParamDecl] p1 | getPropertyWrapperBackingVarBinding() | param_decls.swift:48:18:48:18 | [ConcreteVarDecl] p1 | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:49:26:49:26 | [ConcreteVarDecl] p2 | getPropertyWrapperBackingVar() | param_decls.swift:49:26:49:30 | [ParamDecl] p2 | getPropertyWrapperBackingVar() | param_decls.swift:49:26:49:26 | [ConcreteVarDecl] _p2 |
| param_decls.swift:49:26:49:26 | [ConcreteVarDecl] p2 | getPropertyWrapperBackingVarBinding() | param_decls.swift:49:26:49:30 | [ParamDecl] p2 | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:49:26:49:30 | [ParamDecl] p2 | getPropertyWrapperBackingVar() | param_decls.swift:49:26:49:26 | [ConcreteVarDecl] p2 | getPropertyWrapperBackingVar() | param_decls.swift:49:26:49:26 | [ConcreteVarDecl] _p2 |
| param_decls.swift:49:26:49:30 | [ParamDecl] p2 | getPropertyWrapperBackingVarBinding() | param_decls.swift:49:26:49:26 | [ConcreteVarDecl] p2 | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperBackingVar() | param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _p3 |
| param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperProjectionVar() | param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperProjectionVar() | param_decls.swift:50:31:50:31 | [ConcreteVarDecl] $p3 |
| param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperProjectionVarBinding() | param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperBackingVar() | param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _p3 |
| param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperProjectionVar() | param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperProjectionVar() | param_decls.swift:50:31:50:31 | [ConcreteVarDecl] $p3 |
| param_decls.swift:50:31:50:35 | [ParamDecl] p3 | getPropertyWrapperProjectionVarBinding() | param_decls.swift:50:31:50:31 | [ConcreteVarDecl] p3 | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperBackingVar() | param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _p4 |
| param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperProjectionVar() | param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperProjectionVar() | param_decls.swift:51:38:51:38 | [ConcreteVarDecl] $p4 |
| param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperProjectionVarBinding() | param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperBackingVar() | param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _p4 |
| param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperProjectionVar() | param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperProjectionVar() | param_decls.swift:51:38:51:38 | [ConcreteVarDecl] $p4 |
| param_decls.swift:51:38:51:42 | [ParamDecl] p4 | getPropertyWrapperProjectionVarBinding() | param_decls.swift:51:38:51:38 | [ConcreteVarDecl] p4 | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |

View File

@@ -1,13 +0,0 @@
doubleParents
| applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperBackingVar() | applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _x |
| applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] $x |
| applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperProjectionVarBinding() | applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperBackingVar() | applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _x |
| applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] $x |
| applied_property_wrapper.swift:12:19:12:22 | [ParamDecl] x | getPropertyWrapperProjectionVarBinding() | applied_property_wrapper.swift:12:19:12:19 | [ConcreteVarDecl] x | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperBackingVar() | applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _y |
| applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] $y |
| applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperProjectionVarBinding() | applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperBackingVar() | applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperBackingVar() | file://:0:0:0:0 | [ParamDecl] _y |
| applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperProjectionVar() | applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] $y |
| applied_property_wrapper.swift:17:26:17:29 | [ParamDecl] y | getPropertyWrapperProjectionVarBinding() | applied_property_wrapper.swift:17:26:17:26 | [ConcreteVarDecl] y | getPropertyWrapperProjectionVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |

View File

@@ -1,7 +0,0 @@
doubleParents
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | property_wrapper_value_placeholder.swift:12:12:12:26 | [PatternBindingDecl] var ... = ... | getInit(0) | property_wrapper_value_placeholder.swift:12:4:12:26 | [CallExpr] call to Wrapper.init(wrappedValue:) |
| property_wrapper_value_placeholder.swift:11:1:13:1 | [StructDecl] S | getMember(2) | property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] x | getPropertyWrapperBackingVarBinding() | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |
| property_wrapper_value_placeholder.swift:11:1:13:1 | [StructDecl] S | getMember(3) | property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] x | getPropertyWrapperBackingVar() | property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] _x |
| property_wrapper_value_placeholder.swift:12:12:12:26 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | property_wrapper_value_placeholder.swift:12:4:12:26 | [CallExpr] call to Wrapper.init(wrappedValue:) |
| property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] x | getPropertyWrapperBackingVar() | property_wrapper_value_placeholder.swift:11:1:13:1 | [StructDecl] S | getMember(3) | property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] _x |
| property_wrapper_value_placeholder.swift:12:16:12:16 | [ConcreteVarDecl] x | getPropertyWrapperBackingVarBinding() | property_wrapper_value_placeholder.swift:11:1:13:1 | [StructDecl] S | getMember(2) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... |

View File

@@ -1,3 +0,0 @@
doubleParents
| file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | test.swift:252:19:252:27 | [PatternBindingDecl] var ... = ... | getInit(0) | test.swift:252:6:252:27 | [CallExpr] call to DidSetSource.init(wrappedValue:) |
| test.swift:252:19:252:27 | [PatternBindingDecl] var ... = ... | getInit(0) | file://:0:0:0:0 | [PatternBindingDecl] var ... = ... | getInit(0) | test.swift:252:6:252:27 | [CallExpr] call to DidSetSource.init(wrappedValue:) |