Skip to content

[AST] Paren'd reference to an IUO function crashes the compiler in SILGen #26831

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 14 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,17 @@ namespace {
CS.getConstraintLocator(expr, ConstraintLocator::FunctionResult),
TVO_CanBindToNoEscape);

// If we have something like '(s.foo)()', where 's.foo()' returns an IUO,
// then we need to change the return type to be an optional, so if the
// user is assigning the value to a variable of type T, then we can
// inform the user that it needs to be T? instead.
if (auto overload = CS.findSelectedOverloadFor(expr->getSemanticFn())) {
if (overload->Choice.isImplicitlyUnwrappedValueOrReturnValue() &&
expr->getSemanticFn() != expr->getFn()) {
resultType = OptionalType::get(resultType);
}
}

// A direct call to a ClosureExpr makes it noescape.
FunctionType::ExtInfo extInfo;
if (isa<ClosureExpr>(fnExpr->getSemanticsProvidingExpr()))
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/iuo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,15 @@ var y: Int = 2

let r = sr6988(x: x, y: y)
let _: Int = r

// SR-10492

struct SR_10492_S {
func foo() -> Int! { return 0 }
}

let sr_10492_s = SR_10492_S()
let sr_10492_int1: Int = (sr_10492_s.foo)() // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-1 {{coalesce}}{{44-44= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap}}{{44-44=!}}
let sr_10492_int2: Int? = (sr_10492_s.foo)() // Okay
8 changes: 8 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr10492.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: not %target-swift-frontend -emit-silgen %s

struct S {
func foo() -> Int! { return 0 }
}

let s = S()
let x: Int = (s.foo)()