Skip to content

Commit 601a1dd

Browse files
committed
[TypeChecker] Mark type repr as invalid if IUO appears in incorrect position
Mark IUO repr in incorrect position as invalid. This is the only way to indicate that something went wrong without supressing checking other reprs in the same type. For example: ```swift struct S<T, U> { ... } _ = S<Int!, String!>(...) ``` Compiler should diagnose both `Int!` and `String!` as invalid, but returning `ErrorType` from here would stop type resolution after `Int!`.
1 parent 874172b commit 601a1dd

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3790,6 +3790,19 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
37903790
if (doDiag && !options.contains(TypeResolutionFlags::SilenceErrors)) {
37913791
// Prior to Swift 5, we allow 'as T!' and turn it into a disjunction.
37923792
if (getASTContext().isSwiftVersionAtLeast(5)) {
3793+
// Mark this repr as invalid. This is the only way to indicate that
3794+
// something went wrong without supressing checking other reprs in
3795+
// the same type. For example:
3796+
//
3797+
// struct S<T, U> { ... }
3798+
//
3799+
// _ = S<Int!, String!>(...)
3800+
//
3801+
// Compiler should diagnose both `Int!` and `String!` as invalid,
3802+
// but returning `ErrorType` from here would stop type resolution
3803+
// after `Int!`.
3804+
repr->setInvalid();
3805+
37933806
diagnose(repr->getStartLoc(),
37943807
diag::implicitly_unwrapped_optional_in_illegal_position)
37953808
.fixItReplace(repr->getExclamationLoc(), "?");

test/Sema/diag_erroneous_iuo.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ func genericFunctionSigil<T>(
6161
}
6262

6363
func genericFunctionSigilArray<T>(
64-
// FIXME: We validate these types multiple times resulting in multiple diagnostics
6564
iuo: [T!] // expected-error {{'!' is not allowed here; perhaps '?' was intended?}}{{10-11=?}}
66-
// expected-error@-1 {{'!' is not allowed here; perhaps '?' was intended?}}{{10-11=?}}
6765
) -> [T!] { // expected-error {{'!' is not allowed here; perhaps '?' was intended?}}{{8-9=?}}
68-
// expected-error@-1 {{'!' is not allowed here; perhaps '?' was intended?}}{{8-9=?}}
6966
return iuo
7067
}
7168

0 commit comments

Comments
 (0)