Skip to content

Commit b38d967

Browse files
committed
Ban IUOs in more places.
We were allowing them in parens in some contexts, but shouldn't have. Also added tests for function types and tuple return types to ensure we're not allowing them in these places.
1 parent f54d8b4 commit b38d967

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,9 @@ Type TypeResolver::resolveTupleType(TupleTypeRepr *repr,
28452845
// ImmediateFunctionInput marker and install a FunctionInput one instead.
28462846
auto elementOptions = options;
28472847
if (repr->isParenType()) {
2848+
// We also want to disallow IUO within even a paren.
2849+
elementOptions -= TR_AllowIUO;
2850+
28482851
// If we have a single ParenType, don't clear the context bits; we
28492852
// still want to parse the type contained therein as if it were in
28502853
// parameter position, meaning function types are not @escaping by

test/Sema/diag_erroneous_iuo.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ let _: Generic<Int!, // expected-error {{implicitly unwrapped optionals are only
137137

138138
func vararg(_ first: Int, more: Int!...) { // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
139139
}
140+
141+
func iuoInTuple() -> (Int!) { // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
142+
return 1
143+
}
144+
145+
func iuoInTuple2() -> (Float, Int!) { // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
146+
return 1
147+
}
148+
149+
func takesFunc(_ fn: (Int!) -> Int) -> Int { // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
150+
return fn(0)
151+
}
152+
153+
func returnsFunc() -> (Int!) -> Int { // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
154+
return { $0 }
155+
}

0 commit comments

Comments
 (0)