gpt4 book ai didi

Swift 编译器挂起(可能是泛型参数的类型推断)

转载 作者:可可西里 更新时间:2023-11-01 02:16:41 24 4
gpt4 key购买 nike

我最近写了一个函数和一些相关代码,充当一种开关表达式(按预期工作)和 posted it on CodeReview.SE

从那以后,我开始尝试改进我自己的代码,但似乎遇到了瓶颈,因为我的代码似乎让 playground 挂起,并且在放入项目时也无法编译。然而,当在项目中输入所有内容时,Xcode 不会提示,直到我真正尝试构建它。这是我拥有的:

func caze<T: Equatable, R>(vals: T..., @autoclosure(escaping) ret f: () -> R) -> (AnySequence<T>, () -> R) {
return (AnySequence(vals), f)
}

func caze<T, R where T: Equatable, T: ForwardIndexType>(range: Range<T>, @autoclosure(escaping) ret f: () -> R) -> (AnySequence<T>, () -> R) {
return (AnySequence(range), f)
}


func schwitch<T: Equatable, R>(value: T, _ cases: (AnySequence<T>, () -> R)..., @autoclosure def: () throws -> R) rethrows -> R {
for (vals, f) in cases {
if vals.contains(value) {
return f()
}
}
return try def()
}

func test() {
schwitch(5,
caze(0, ret: "hello"),
caze(1, 2, ret: "test"),
caze(3..<7, ret: "lol"),
def: "nop")

schwitch("helloo",
caze("hello", "Hello", ret: "test"),
def: "nop")
}

这是我在尝试构建项目时遇到的错误:

Command failed due to signal: Segmentation fault: 11

和堆栈跟踪:

0  swift                    0x000000010c7f366b llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43
1 swift 0x000000010c7f2956 llvm::sys::RunSignalHandlers() + 70
2 swift 0x000000010c7f3ccf SignalHandler(int) + 287
3 libsystem_platform.dylib 0x00007fff87f0752a _sigtramp + 26
4 libsystem_platform.dylib 0x00007f82f4bf0418 _sigtramp + 1825476360
5 swift 0x000000010ab9e994 (anonymous namespace)::ApplyClassifier::classifyRethrowsArgument(swift::Expr*, swift::Type) + 52
6 swift 0x000000010ab9ea2b (anonymous namespace)::ApplyClassifier::classifyRethrowsArgument(swift::Expr*, swift::Type) + 203
7 swift 0x000000010ab9f02a (anonymous namespace)::ApplyClassifier::classifyRethrowsArgument(swift::Expr*, swift::Type) + 1738
8 swift 0x000000010ab9e56c (anonymous namespace)::ApplyClassifier::classifyApply(swift::ApplyExpr*) + 652
9 swift 0x000000010ab9dc86 (anonymous namespace)::ErrorHandlingWalker<(anonymous namespace)::CheckErrorCoverage>::walkToExprPre(swift::Expr*) + 278
10 swift 0x000000010ac45854 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Stmt*) + 148
11 swift 0x000000010ac42ef7 swift::Stmt::walk(swift::ASTWalker&) + 87
12 swift 0x000000010ab9d9d5 swift::TypeChecker::checkFunctionErrorHandling(swift::AbstractFunctionDecl*) + 405
13 swift 0x000000010ab7ef1b typeCheckFunctionsAndExternalDecls(swift::TypeChecker&) + 1451
14 swift 0x000000010ab7f665 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int) + 1765
15 swift 0x000000010aa00eb4 swift::CompilerInstance::performSema() + 4580
16 swift 0x000000010a543596 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&) + 934
17 swift 0x000000010a54268d frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 2781
18 swift 0x000000010a53e0ac main + 1932
19 libdyld.dylib 0x00007fff912a35ad start + 1

问题似乎出在函数的使用上,因为如果我省略那部分,一切都可以正常编译。仅使用 caze 似乎也可以正常工作,因为以下内容也按预期工作:

caze(5, ret: "hello")
let test: (AnySequence<Int>, () -> String) = caze(0...5, ret: "hello") // Swift needs some help figuring out which function to use here

这仅仅是 Swift 编译器中的错误还是我遗漏了什么?

最佳答案

首先:正如 Bryan 所说,这肯定是一个编译器错误,它应该永远不会崩溃。


要解决这个问题:根本原因似乎是 rethrows,因为使用以下编译和运行没有任何问题。这与堆栈跟踪 5 - 7 classifyRethrowsArgument 相匹配。

func schwitch<T: Equatable, R>(value: T, cas cases: (AnySequence<T>, () -> R)..., @autoclosure def: () -> R) -> R {
for (vals, f) in cases {
if vals.contains(value) {
return f()
}
}
return def()
}

同样适用

func schwitch<T: Equatable, R>(value: T, cas cases: (AnySequence<T>, () -> R)..., @autoclosure def: () throws -> R) -> R {
for (vals, f) in cases {
if vals.contains(value) {
return f()
}
}
return try! def()
}

我知道这远不是一个完整的答案,但我想至少对我发现的内容给出一些提示。如果您不需要能够抛出此代码的默认情况应该适合您。我将尝试弄清楚为什么 rethrows 会导致问题。

TypeCheckError.cpp swift 编译器可能有助于解决这个问题。

关于Swift 编译器挂起(可能是泛型参数的类型推断),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com