swiftc -dump-ast
揭示了一些可能的原因。
让我们从简化的代码开始:
let string = "Hello"
let num = 42
var varString = "Hi Again"
var varNum = 234
typealias SomeTuple = (string: String, int: Int)
func foo(x:SomeTuple) {}
错误案例
foo(string: "Hello", int: 42)
(top_level_code_decl
(brace_stmt
(call_expr type='()' location=test.swift:9:1 range=[test.swift:9:1 - line:9:29]
(declref_expr type='(SomeTuple) -> ()' location=test.swift:9:1 range=[test.swift:9:1 - line:9:1] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:2:6 specialized=no)
(tuple_expr type='<<error type>>' location=test.swift:9:4 range=[test.swift:9:4 - line:9:29] names=string,int
(string_literal_expr type='<<error type>>' location=test.swift:9:13 range=[test.swift:9:13 - line:9:13] encoding=utf8 value="Hello")
(integer_literal_expr type='<<error type>>' location=test.swift:9:27 range=[test.swift:9:27 - line:9:27] value=42))))
在这种情况下,编译器无法推断最终类型string_literal_expr
and integer_literal_expr
. string_literal_expr
can be String
, StaticString
, Selector
或诸如此类。
foo(string: varString, int: varNum)
(top_level_code_decl
(brace_stmt
(call_expr type='()' location=test.swift:10:1 range=[test.swift:10:1 - line:10:35]
(declref_expr type='(SomeTuple) -> ()' location=test.swift:10:1 range=[test.swift:10:1 - line:10:1] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:2:6 specialized=no)
(tuple_expr type='(string: @lvalue String, int: @lvalue Int)' location=test.swift:10:4 range=[test.swift:10:4 - line:10:35] names=string,int
(declref_expr type='@lvalue String' location=test.swift:10:13 range=[test.swift:10:13 - line:10:13] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:6:5 direct_to_storage specialized=no)
(declref_expr type='@lvalue Int' location=test.swift:10:29 range=[test.swift:10:29 - line:10:29] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:7:5 direct_to_storage specialized=no))))
在这种情况下,编译器解释为(string: varString, int: varNum)
as (string: @lvalue String, int: @lvalue Int)
。并且它不匹配(string: String, int: Int)
.
成功案例
foo(string: "Hello" as String, int: 42 as Int)
(top_level_code_decl
(brace_stmt
(call_expr type='()' location=test.swift:13:1 range=[test.swift:13:1 - line:13:46]
(declref_expr type='(SomeTuple) -> ()' location=test.swift:13:1 range=[test.swift:13:1 - line:13:1] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:2:6 specialized=no)
(tuple_expr type='(string: String, int: Int)' location=test.swift:13:4 range=[test.swift:13:4 - line:13:46] names=string,int
(coerce_expr type='String' location=test.swift:13:21 range=[test.swift:13:13 - line:13:24] writtenType=String
(call_expr implicit type='String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13]
(constructor_ref_call_expr implicit type='(_builtinStringLiteral: RawPointer, byteSize: Word, isASCII: Int1) -> String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13]
(declref_expr implicit type='String.Type -> (_builtinStringLiteral: RawPointer, byteSize: Word, isASCII: Int1) -> String' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] decl=Swift.(file).String.init(_builtinStringLiteral:byteSize:isASCII:) specialized=no)
(type_expr implicit type='String.Type' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] typerepr='<<IMPLICIT>>'))
(string_literal_expr type='(_builtinStringLiteral: Builtin.RawPointer, byteSize: Builtin.Word, isASCII: Builtin.Int1)' location=test.swift:13:13 range=[test.swift:13:13 - line:13:13] encoding=utf8 value="Hello")))
(coerce_expr type='Int' location=test.swift:13:40 range=[test.swift:13:37 - line:13:43] writtenType=Int
(call_expr implicit type='Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37]
(constructor_ref_call_expr implicit type='(_builtinIntegerLiteral: Int2048) -> Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37]
(declref_expr implicit type='Int.Type -> (_builtinIntegerLiteral: Int2048) -> Int' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] decl=Swift.(file).Int.init(_builtinIntegerLiteral:) specialized=no)
(type_expr implicit type='Int.Type' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] typerepr='<<IMPLICIT>>'))
(tuple_expr implicit type='(_builtinIntegerLiteral: Int2048)' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] names=_builtinIntegerLiteral
(integer_literal_expr type='Int2048' location=test.swift:13:37 range=[test.swift:13:37 - line:13:37] value=42)))))))
显式转换原因coerce_expr
。它构建了String
and Int
正确。
foo(string: string, int: num)
(top_level_code_decl
(brace_stmt
(call_expr type='()' location=test.swift:12:1 range=[test.swift:12:1 - line:12:29]
(declref_expr type='(SomeTuple) -> ()' location=test.swift:12:1 range=[test.swift:12:1 - line:12:1] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:2:6 specialized=no)
(tuple_expr type='(string: String, int: Int)' location=test.swift:12:4 range=[test.swift:12:4 - line:12:29] names=string,int
(declref_expr type='String' location=test.swift:12:13 range=[test.swift:12:13 - line:12:13] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:4:5 direct_to_storage specialized=no)
(declref_expr type='Int' location=test.swift:12:26 range=[test.swift:12:26 - line:12:26] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:5:5 direct_to_storage specialized=no))))
let
常数是String
and Int
, 它没有@lvalue
。因此它们可以按原样应用。
foo((string: varString, int: varNum))
(top_level_code_decl
(brace_stmt
(call_expr type='()' location=test.swift:15:1 range=[test.swift:15:1 - line:15:37]
(declref_expr type='(SomeTuple) -> ()' location=test.swift:15:1 range=[test.swift:15:1 - line:15:1] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:2:6 specialized=no)
(paren_expr type='(SomeTuple)' location=test.swift:15:5 range=[test.swift:15:4 - line:15:37]
(tuple_expr type='(string: String, int: Int)' location=test.swift:15:5 range=[test.swift:15:5 - line:15:36] names=string,int
(load_expr implicit type='String' location=test.swift:15:14 range=[test.swift:15:14 - line:15:14]
(declref_expr type='@lvalue String' location=test.swift:15:14 range=[test.swift:15:14 - line:15:14] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:6:5 direct_to_storage specialized=no))
(load_expr implicit type='Int' location=test.swift:15:30 range=[test.swift:15:30 - line:15:30]
(declref_expr type='@lvalue Int' location=test.swift:15:30 range=[test.swift:15:30 - line:15:30] decl=test.(file)[email protected] /cdn-cgi/l/email-protection:7:5 direct_to_storage specialized=no)))))))
在这种情况下,相比foo(string: varString, int: varNum)
, load_expr
被插入,并且它转换@lvalue String
to String
, and @lvalue Int
to Int