Skip to content

Fix wrong missing-return error message for an initializer returning an implicitly unwrapped optional #14272

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

Merged
merged 1 commit into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ SILGenFunction::emitPreconditionOptionalHasValue(SILLocation loc,
SGFContext());
}

B.createUnreachable(loc);
B.createUnreachable(ArtificialUnreachableLocation());
B.clearInsertionPoint();
B.emitBlock(contBB);

Expand Down
11 changes: 10 additions & 1 deletion test/SILOptimizer/return.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func whileLoop(flag: Bool) -> Int {
struct S {}
extension S:ExpressibleByStringLiteral {
init!(stringLiteral:String) {
} // expected-error {{missing return in a function expected to return 'S!'}}
} // no error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graydon This error is printed since your fix #11765
But I think the error message was wrong. Please confirm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eeckstein Perhaps! I have no idea whether an IUO initializer with no body is legal, I was mainly concerned with making it stop crashing in #11765 by handling the case, and the existing diagnostic seemed to fit my understanding of the meaning of an IUO initializer with no body. But by all means confirm with someone who knows IUO semantics better than me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning successfully (i.e. a non-optional, properly-initialized S) would be the correct semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it returns an S (implicitly, not with a return statement)

}

func whileTrueLoop() -> Int {
Expand Down Expand Up @@ -150,3 +150,12 @@ func testCleanupCodeEmptyTuple(fn: @autoclosure () -> Bool = false,
exit()
}
} // no warning

protocol InitProtocol {
init(_ x: Int)
}

struct StructWithIUOinit : InitProtocol {
init!(_ x: Int) { } // no missing-return error
}