New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-8788] Rethrowing/convenience initializer does not compile #51296
Comments
@xedin, think the fix is straightforward enough for a StarterBug? |
It seems like a good StarterBug candidate indeed! |
Comment by Jacob G (JIRA) In that case, I'll see if I can fix it myself |
PMFJI I've hit the similar issue in my project: enum MyError: Error {
case someError
}
func letsTry(_ trial:@autoclosure () throws -> Void) -> Void {
do {
try trial()
} catch {
print("There is an error.")
}
}
func thrower() throws {
throw MyError.someError
}
let nonThrowingClosure1 = { () -> Void in letsTry(try thrower()) }
nonThrowingClosure1()
// -> error: Call can throw but is not marked with 'try'
let nonThrowingClosure2: () -> Void = { letsTry(try thrower()) }
// -> error: Invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '() -> Void' In this case, |
Comment by Jacob G (JIRA) @YOCKOW It seems to me that the compiler is confused by the @xedin I am having trouble finding the cause of the main issue described. I started looking at |
Coder-256 (JIRA User) To understand how everything works you need to enable type-checker logging, to do that you can pass `-Xfrontend -debug-constraints` to `swiftc` invocation. That would print a bunch of information about expression AST and deduced solutions (types, declarations). I'd suggest you to save `class Foo` declaration into separate file in order to avoid noise. Code related to solver is contained in `lib/Sema/CS*` files, and top-level entry point is `typeCheckExpression` located in `TypeCheckConstraints.cpp`. For example from description, unlike one @YOCKOW provided, which involves `@autoclosure`, I think it actually makes sense that `try` is requested on `self.init` because closure is marked as `throws` and `init` is marked as `rethrows` which makes it always throwing regardless whether argument throws or not, you'll see that in the output of the solver, that type of the closure is deduced to be `throwing` based on parameter type. Example from @YOCKOW is more interesting and looks like real problem because `nonThrowingClosure1` is deduced to be throwing although `letsTry` doesn't propagate errors. |
Coder-256 (JIRA User), @xedin |
Comment by Jacob G (JIRA) @slavapestov Thank you! |
Environment
Swift 4.2, Xcode 10.0 (10A255), macOS 10.13.6 (17G2307)
Additional Detail from JIRA
md5: 35970f5be3170943946b15431fb6980a
Issue Description:
This code should, in theory, compile:
However, the compiler emits the following error on the
self.init
line:This is incorrect because the initializer rethrows and is passed a non-throwing closure.
The text was updated successfully, but these errors were encountered: