Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Last active October 27, 2017 08:36
Show Gist options
  • Save ukitaka/34e3132db8a69283e612a77b3e375548 to your computer and use it in GitHub Desktop.
Save ukitaka/34e3132db8a69283e612a77b3e375548 to your computer and use it in GitHub Desktop.
Opening existentials
protocol Animal {
func bark()
}
struct Dog: Animal {
func bark() {
print("わんわん")
}
}
let a: Animal = Dog()
a.bark()
(found solution 0 0 0 0 0 0 0 0 0 0 0 0 0)
---Constraint solving for the expression at [proto.swift:11:17 - line:11:21]---
(increasing score due to function conversion)
(overload set choice binding ($T1) -> $T0 := () -> Dog)
---Initial constraints for the given expression---
(call_expr type='Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:21] arg_labels=
(type_expr type='Dog.Type' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] typerepr='Dog')
(tuple_expr type='()' location=proto.swift:11:20 range=[proto.swift:11:20 - line:11:21]))
Score: 0 0 0 0 1 0 0 0 0 0 0 0 0
Contextual Type: Animal at [proto.swift:11:8 - line:11:8]
Type Variables:
#0 = $T0 [inout allowed] as Dog
#1 = $T1 [lvalue allowed] [inout allowed] as ()
Active Constraints:
Inactive Constraints:
Resolved overloads:
selected overload set choice Dog.Type.init: ($T1) -> $T0 == () -> Dog
(found solution 0 0 0 0 1 0 0 0 0 0 0 0 0)
---Solution---
Fixed score: 0 0 0 0 1 0 0 0 0 0 0 0 0
Type variables:
$T0 as Dog
$T1 as ()
Overload choices:
locator@0x7fdd0c8c8720 [Call@proto.swift:11:17 -> apply function -> constructor member] with proto.(file).Dog.init()@proto.swift:5:8 as Dog.Type.init: () -> Dog
Constraint restrictions:
Dog to Animal is [existential]
Disjunction choices:
Conformances:
At locator@0x7fdd0c8c8690 [Call@proto.swift:11:17]
(normal_conformance type=Dog protocol=Animal
(value req=bark() witness=proto.(file).Dog.bark()@proto.swift:6:8))
---Type-checked expression---
(call_expr type='Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:21] arg_labels=
(constructor_ref_call_expr type='() -> Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17]
(declref_expr implicit type='(Dog.Type) -> () -> Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] decl=proto.(file).Dog.init()@proto.swift:5:8 function_ref=single)
(type_expr type='Dog.Type' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] typerepr='Dog'))
(tuple_expr type='()' location=proto.swift:11:20 range=[proto.swift:11:20 - line:11:21]))
---Constraint solving for the expression at [proto.swift:7:6 - line:7:26]---
---Initial constraints for the given expression---
(call_expr type='()' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:26] arg_labels=_:
(overloaded_decl_ref_expr type='$T0' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:6] name=print #decls=6 function_ref=single
Swift.(file).print(_:separator:terminator:)
Swift.(file).print(_:separator:terminator:to:)
Swift.(file).print(_:separator:terminator:toStream:)
Swift.(file).print(_:appendNewline:)
Swift.(file).print
Swift.(file).print(_:_:appendNewline:))
(paren_expr type='($T1)' location=proto.swift:7:12 range=[proto.swift:7:11 - line:7:26]
(string_literal_expr type='$T1' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] encoding=utf8 value="わんわん" builtin_initializer=**NULL** initializer=**NULL**)))
Score: 0 0 0 0 0 0 0 0 0 0 0 0 0
Type Variables:
#0 = $T0 [lvalue allowed] [inout allowed]
#1 = $T1 [inout allowed]
Active Constraints:
Inactive Constraints:
$T1 literal conforms to ExpressibleByStringLiteral [[locator@0x7fdd0d8062b0 [StringLiteral@proto.swift:7:12]]];
($T1) -> () applicable fn $T0 [[locator@0x7fdd0d806390 [Call@proto.swift:7:6 -> apply function]]];
disjunction [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]:$T0 bound to decl Swift.(file).print(_:separator:terminator:) : (Any..., String, String) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]; or $T0 bound to decl Swift.(file).print(_:separator:terminator:to:) : <Target where Target : TextOutputStream> (Any..., separator: String, terminator: String, to: inout Target) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]; or $T0 bound to decl Swift.(file).print(_:separator:terminator:toStream:) : <Target where Target : TextOutputStream> (Any..., separator: String, terminator: String, toStream: inout Target) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]; or $T0 bound to decl Swift.(file).print(_:appendNewline:) : <T> (T, appendNewline: Bool) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]; or $T0 bound to decl Swift.(file).print : <T> (T, inout TextOutputStream) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]]; or $T0 bound to decl Swift.(file).print(_:_:appendNewline:) : <T> (T, inout TextOutputStream, appendNewline: Bool) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]];
($T1 fully_bound literal=3 involves_type_vars bindings=(subtypes of) (default from ExpressibleByStringLiteral) String)
(assuming $T0 bound to decl Swift.(file).print(_:separator:terminator:) : (Any..., String, String) -> () [[locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6]]];
(overload set choice binding $T0 := (Any..., String, String) -> ())
($T1 literal=3 bindings=(subtypes of) (default from ExpressibleByStringLiteral) String (subtypes of) Any)
Active bindings: $T1 := String $T1 := Any
(trying $T1 := String
(increasing score due to empty-existential conversion)
(found solution 0 0 0 0 0 0 0 0 0 0 1 0 0)
)
(trying $T1 := Any
(increasing score due to non-default literal)
(solution is worse than the best solution)
)
)
---Solution---
Fixed score: 0 0 0 0 0 0 0 0 0 0 1 0 0
Type variables:
$T0 as (Any..., String, String) -> ()
$T1 as String
Overload choices:
locator@0x7fdd0d806000 [OverloadedDeclRef@proto.swift:7:6] with Swift.(file).print(_:separator:terminator:) as print: (Any..., String, String) -> ()
Constraint restrictions:
String to Any is [existential]
Disjunction choices:
Conformances:
At locator@0x7fdd0d8062b0 [StringLiteral@proto.swift:7:12]
(normal_conformance type=String protocol=ExpressibleByStringLiteral lazy
(normal_conformance type=String protocol=ExpressibleByExtendedGraphemeClusterLiteral lazy
(normal_conformance type=String protocol=ExpressibleByUnicodeScalarLiteral lazy
(normal_conformance type=String protocol=_ExpressibleByBuiltinUnicodeScalarLiteral lazy))
(normal_conformance type=String protocol=_ExpressibleByBuiltinExtendedGraphemeClusterLiteral lazy
(normal_conformance type=String protocol=_ExpressibleByBuiltinUnicodeScalarLiteral
(value req=init(_builtinUnicodeScalarLiteral:) witness=Swift.(file).String.init(_builtinUnicodeScalarLiteral:)))))
(normal_conformance type=String protocol=_ExpressibleByBuiltinStringLiteral lazy
(normal_conformance type=String protocol=_ExpressibleByBuiltinExtendedGraphemeClusterLiteral
(value req=init(_builtinExtendedGraphemeClusterLiteral:utf8CodeUnitCount:isASCII:) witness=Swift.(file).String.init(_builtinExtendedGraphemeClusterLiteral:utf8CodeUnitCount:isASCII:))
(normal_conformance type=String protocol=_ExpressibleByBuiltinUnicodeScalarLiteral
(value req=init(_builtinUnicodeScalarLiteral:) witness=Swift.(file).String.init(_builtinUnicodeScalarLiteral:))))))
---Constraint solving for the expression at [proto.swift:7:12 - line:7:12]---
(overload set choice binding $T0 := (RawPointer, Word) -> String)
---Initial constraints for the given expression---
(unresolved_dot_expr implicit type='(RawPointer, Word) -> String' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] field 'init(_builtinUTF16StringLiteral:utf16CodeUnitCount:)' function_ref=unapplied
(type_expr implicit type='String.Type' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] typerepr='String'))
Score: 0 0 0 0 0 0 0 0 0 0 0 0 0
Type Variables:
#0 = $T0 [lvalue allowed] [inout allowed] as (RawPointer, Word) -> String
Active Constraints:
Inactive Constraints:
Resolved overloads:
selected overload set choice String.Type.init: $T0 == (RawPointer, Word) -> String
(found solution 0 0 0 0 0 0 0 0 0 0 0 0 0)
---Solution---
Fixed score: 0 0 0 0 0 0 0 0 0 0 0 0 0
Type variables:
$T0 as (RawPointer, Word) -> String
Overload choices:
locator@0x7fdd0d86f218 [UnresolvedDot@proto.swift:7:12 -> member] with Swift.(file).String.init(_builtinUTF16StringLiteral:utf16CodeUnitCount:) as String.Type.init: (RawPointer, Word) -> String
Constraint restrictions:
Disjunction choices:
---Type-checked expression---
(constructor_ref_call_expr implicit type='(RawPointer, Word) -> String' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12]
(declref_expr implicit type='(String.Type) -> (RawPointer, Word) -> String' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] decl=Swift.(file).String.init(_builtinUTF16StringLiteral:utf16CodeUnitCount:) function_ref=unapplied)
(type_expr implicit type='String.Type' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] typerepr='String'))
---Type-checked expression---
(call_expr type='()' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:26] arg_labels=_:
(declref_expr type='(Any..., String, String) -> ()' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:6] decl=Swift.(file).print(_:separator:terminator:) function_ref=single)
(tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' location=proto.swift:7:12 range=[proto.swift:7:11 - line:7:26] source_is_scalar elements=[-2, -1, -1] variadic_sources=[0] default_args_owner=Swift.(file).print(_:separator:terminator:)
(paren_expr type='Any' location=proto.swift:7:12 range=[proto.swift:7:11 - line:7:26]
(erasure_expr implicit type='Any' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12]
(string_literal_expr type='String' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] encoding=utf16 value="わんわん" builtin_initializer=Swift.(file).String.init(_builtinUTF16StringLiteral:utf16CodeUnitCount:) initializer=**NULL**)))))
---Constraint solving for the expression at [proto.swift:13:1 - line:13:8]---
(overload set choice binding $T0 := Animal)
(overload set choice binding $T1 := () -> ())
---Initial constraints for the given expression---
(call_expr type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8] arg_labels=
(unresolved_dot_expr type='() -> ()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:3] field 'bark' function_ref=single
(declref_expr type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] decl=proto.(file).a@proto.swift:11:5 direct_to_storage function_ref=unapplied))
(tuple_expr type='()' location=proto.swift:13:7 range=[proto.swift:13:7 - line:13:8]))
Score: 0 0 0 0 0 0 0 0 0 0 0 0 0
Type Variables:
#0 = $T0 [lvalue allowed] [inout allowed] as Animal
#1 = $T1 [lvalue allowed] [inout allowed] as () -> ()
#2 = $T2 as Animal
#3 = $T3 [inout allowed] as ()
Active Constraints:
Inactive Constraints:
Resolved overloads:
selected overload set choice Animal.bark: $T1 == () -> ()
selected overload set choice a: $T0 == Animal
Opened types:
locator@0x7fdd0f000ea8 [UnresolvedDot@proto.swift:13:3 -> member] opens τ_0_0 -> $T2
Opened existential types:
locator@0x7fdd0f000ea8 [UnresolvedDot@proto.swift:13:3 -> member] opens to Animal
(found solution 0 0 0 0 0 0 0 0 0 0 0 0 0)
---Solution---
Fixed score: 0 0 0 0 0 0 0 0 0 0 0 0 0
Type variables:
$T3 as ()
$T0 as Animal
$T1 as () -> ()
$T2 as Animal
Overload choices:
locator@0x7fdd0f000ea8 [UnresolvedDot@proto.swift:13:3 -> member] with proto.(file).Animal.bark()@proto.swift:2:8 as Animal.bark: () -> ()
locator@0x7fdd0f000e18 [DeclRef@proto.swift:13:1] with proto.(file).a@proto.swift:11:5 as a: Animal
Constraint restrictions:
Disjunction choices:
Opened types:
locator@0x7fdd0f000ea8 [UnresolvedDot@proto.swift:13:3 -> member] opens τ_0_0 -> $T2
Opened existential types:
locator@0x7fdd0f000ea8 [UnresolvedDot@proto.swift:13:3 -> member] opens to Animal
---Type-checked expression---
(open_existential_expr implicit type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8]
(opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fdd0d933960)
(declref_expr type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] decl=proto.(file).a@proto.swift:11:5 direct_to_storage function_ref=unapplied)
(call_expr type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8] arg_labels=
(dot_syntax_call_expr type='() -> ()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:3]
(declref_expr type='(Animal) -> () -> ()' location=proto.swift:13:3 range=[proto.swift:13:3 - line:13:3] decl=proto.(file).Animal.bark()@proto.swift:2:8 [with Animal[abstract:Animal]] function_ref=single)
(opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fdd0d933960))
(tuple_expr type='()' location=proto.swift:13:7 range=[proto.swift:13:7 - line:13:8])))
(source_file
(protocol "Animal" requirement signature=<null>
(func_decl "bark()"
(parameter_list
(parameter "self"))
(parameter_list)))
(struct_decl "Dog" inherits: <null>
(func_decl "bark()"
(parameter_list
(parameter "self"))
(parameter_list)
(brace_stmt
(call_expr type='<null>' arg_labels=_:
(unresolved_decl_ref_expr type='<null>' name=print function_ref=unapplied)
(paren_expr type='<null>'
(string_literal_expr type='<null>' encoding=utf8 value="わんわん" builtin_initializer=**NULL** initializer=**NULL**))))))
(top_level_code_decl
(brace_stmt
(pattern_binding_decl
(pattern_typed
(pattern_named 'a')
(type_ident
(component id='Animal' bind=none)))
(call_expr type='<null>' arg_labels=
(unresolved_decl_ref_expr type='<null>' name=Dog function_ref=unapplied)
(tuple_expr type='()' location=proto.swift:11:20 range=[proto.swift:11:20 - line:11:21])))
))
(var_decl "a" type='<null type>' let storage_kind=stored)
(top_level_code_decl
(brace_stmt
(call_expr type='<null>' arg_labels=
(unresolved_dot_expr type='<null>' field 'bark' function_ref=unapplied
(unresolved_decl_ref_expr type='<null>' name=a function_ref=unapplied))
(tuple_expr type='()' location=proto.swift:13:7 range=[proto.swift:13:7 - line:13:8])))))
(source_file
(protocol "Animal" <Self : Animal> interface type='Animal.Protocol' access=internal @_fixed_layout requirement signature=<Self>
(func_decl "bark()" interface type='<Self where Self : Animal> (Self) -> () -> ()' access=internal
(parameter_list
(parameter "self" type='Self' interface type='Self'))
(parameter_list)))
(struct_decl "Dog" interface type='Dog.Type' access=internal @_fixed_layout inherits: Animal
(func_decl "bark()" interface type='(Dog) -> () -> ()' access=internal
(parameter_list
(parameter "self" type='Dog' interface type='Dog'))
(parameter_list)
(brace_stmt
(call_expr type='()' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:26] nothrow arg_labels=_:
(declref_expr type='(Any..., String, String) -> ()' location=proto.swift:7:6 range=[proto.swift:7:6 - line:7:6] decl=Swift.(file).print(_:separator:terminator:) function_ref=single)
(tuple_shuffle_expr implicit type='(Any..., separator: String, terminator: String)' location=proto.swift:7:12 range=[proto.swift:7:11 - line:7:26] source_is_scalar elements=[-2, -1, -1] variadic_sources=[0] default_args_owner=Swift.(file).print(_:separator:terminator:)
(paren_expr type='Any' location=proto.swift:7:12 range=[proto.swift:7:11 - line:7:26]
(erasure_expr implicit type='Any' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12]
(string_literal_expr type='String' location=proto.swift:7:12 range=[proto.swift:7:12 - line:7:12] encoding=utf16 value="わんわん" builtin_initializer=Swift.(file).String.init(_builtinUTF16StringLiteral:utf16CodeUnitCount:) initializer=**NULL**)))))))
(constructor_decl implicit "init()" interface type='(Dog.Type) -> () -> Dog' access=internal designated
(parameter_list
(parameter "self" type='inout Dog' interface type='inout Dog' mutable))
(parameter_list)
(brace_stmt
(return_stmt implicit))))
(top_level_code_decl
(brace_stmt
(pattern_binding_decl
(pattern_typed type='Animal'
(pattern_named type='Animal' 'a')
(type_ident
(component id='Animal' bind=proto.(file).Animal@proto.swift:1:10)))
(erasure_expr implicit type='Animal' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:21]
(normal_conformance type=Dog protocol=Animal
(value req=bark() witness=proto.(file).Dog.bark()@proto.swift:6:8))
(call_expr type='Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:21] nothrow arg_labels=
(constructor_ref_call_expr type='() -> Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] nothrow
(declref_expr implicit type='(Dog.Type) -> () -> Dog' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] decl=proto.(file).Dog.init()@proto.swift:5:8 function_ref=single)
(type_expr type='Dog.Type' location=proto.swift:11:17 range=[proto.swift:11:17 - line:11:17] typerepr='Dog'))
(tuple_expr type='()' location=proto.swift:11:20 range=[proto.swift:11:20 - line:11:21]))))
))
(var_decl "a" type='Animal' interface type='Animal' access=internal let storage_kind=stored)
(top_level_code_decl
(brace_stmt
(open_existential_expr implicit type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8]
(opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fef860f07b0)
(declref_expr type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] decl=proto.(file).a@proto.swift:11:5 direct_to_storage function_ref=unapplied)
(call_expr type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8] nothrow arg_labels=
(dot_syntax_call_expr type='() -> ()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:3] nothrow
(declref_expr type='(Animal) -> () -> ()' location=proto.swift:13:3 range=[proto.swift:13:3 - line:13:3] decl=proto.(file).Animal.bark()@proto.swift:2:8 [with Animal[abstract:Animal]] function_ref=single)
(opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fef860f07b0))
(tuple_expr type='()' location=proto.swift:13:7 range=[proto.swift:13:7 - line:13:8]))))))
@ukitaka
Copy link
Author

ukitaka commented Oct 27, 2017

a.bark() の呼び出し部分で open_existential_expr が確認できる

(open_existential_expr implicit type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8]
        (opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fef860f07b0)
        (declref_expr type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] decl=proto.(file).a@proto.swift:11:5 direct_to_storage function_ref=unapplied)
        (call_expr type='()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:8] nothrow arg_labels=
          (dot_syntax_call_expr type='() -> ()' location=proto.swift:13:3 range=[proto.swift:13:1 - line:13:3] nothrow
            (declref_expr type='(Animal) -> () -> ()' location=proto.swift:13:3 range=[proto.swift:13:3 - line:13:3] decl=proto.(file).Animal.bark()@proto.swift:2:8 [with Animal[abstract:Animal]] function_ref=single)
            (opaque_value_expr implicit type='Animal' location=proto.swift:13:1 range=[proto.swift:13:1 - line:13:1] @ 0x7fef860f07b0))
          (tuple_expr type='()' location=proto.swift:13:7 range=[proto.swift:13:7 - line:13:8]))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment