Skip to content

Commit 73a492d

Browse files
committed
For Swift 3/4 mode, do not treat IUOs in illegal positions as Optionals.
Instead, just emit a deprecation warning and suggest switching to Optionals. The old behavior caused several projects to break, so before we change the behavior here we need to investigate whether we can mitigate most of those breaks.
1 parent 32a8dbc commit 73a492d

File tree

6 files changed

+71
-86
lines changed

6 files changed

+71
-86
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,11 +3117,11 @@ WARNING(implicitly_unwrapped_optional_spelling_deprecated,none,
31173117
WARNING(implicitly_unwrapped_optional_spelling_deprecated_with_fixit,none,
31183118
"the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name", ())
31193119

3120-
WARNING(implicitly_unwrapped_optional_spelling_decay_to_optional,none,
3121-
"'ImplicitlyUnwrappedOptional' is not allowed here; interpreting this as 'Optional' instead", ())
3120+
WARNING(implicitly_unwrapped_optional_spelling_suggest_optional,none,
3121+
"using 'ImplicitlyUnwrappedOptional' in this location is deprecated and will be removed in a future release; consider changing this to 'Optional' instead", ())
31223122

3123-
WARNING(implicitly_unwrapped_optional_in_illegal_position_decay_to_optional,none,
3124-
"'!' is not allowed here; interpreting this as '?' instead", ())
3123+
WARNING(implicitly_unwrapped_optional_in_illegal_position_suggest_optional,none,
3124+
"using '!' in this location is deprecated and will be removed in a future release; consider changing this to '?' instead", ())
31253125

31263126
ERROR(implicitly_unwrapped_optional_spelling_error,none,
31273127
"the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use an explicit type followed by '!'", ())

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,14 +1130,6 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
11301130

11311131
auto id = comp->getIdentifier();
11321132

1133-
// If we're compiling for Swift version < 5 and we have a mention of
1134-
// ImplicitlyUnwrappedOptional where it is not allowed, treat it as
1135-
// if it was spelled Optional.
1136-
if (id == TC.Context.Id_ImplicitlyUnwrappedOptional
1137-
&& !options.contains(TypeResolutionFlags::AllowIUO)
1138-
&& !TC.Context.isSwiftVersionAtLeast(5))
1139-
id = TC.Context.Id_Optional;
1140-
11411133
NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
11421134
if (options.contains(TypeResolutionFlags::KnownNonCascadingDependency))
11431135
lookupOptions |= NameLookupFlags::KnownPrivate;
@@ -1239,7 +1231,7 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
12391231
}
12401232
} else if (isa<GenericIdentTypeRepr>(comp)) {
12411233
Diagnostic diag =
1242-
diag::implicitly_unwrapped_optional_spelling_decay_to_optional;
1234+
diag::implicitly_unwrapped_optional_spelling_suggest_optional;
12431235

12441236
if (TC.Context.isSwiftVersionAtLeast(5))
12451237
diag = diag::implicitly_unwrapped_optional_spelling_in_illegal_position;
@@ -1258,7 +1250,7 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
12581250
genericTyR->getAngleBrackets().End.getAdvancedLoc(1));
12591251
} else {
12601252
Diagnostic diag =
1261-
diag::implicitly_unwrapped_optional_spelling_decay_to_optional;
1253+
diag::implicitly_unwrapped_optional_spelling_suggest_optional;
12621254

12631255
if (TC.Context.isSwiftVersionAtLeast(5))
12641256
diag = diag::
@@ -2847,7 +2839,7 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
28472839
TypeResolutionOptions options) {
28482840
if (!options.contains(TypeResolutionFlags::AllowIUO)) {
28492841
Diagnostic diag = diag::
2850-
implicitly_unwrapped_optional_in_illegal_position_decay_to_optional;
2842+
implicitly_unwrapped_optional_in_illegal_position_suggest_optional;
28512843

28522844
if (TC.Context.isSwiftVersionAtLeast(5))
28532845
diag = diag::implicitly_unwrapped_optional_in_illegal_position;
@@ -2865,12 +2857,8 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
28652857
if (!baseTy || baseTy->hasError()) return baseTy;
28662858

28672859
Type uncheckedOptionalTy;
2868-
if (!options.contains(TypeResolutionFlags::AllowIUO))
2869-
// Treat IUOs in illegal positions as optionals.
2870-
uncheckedOptionalTy = TC.getOptionalType(repr->getExclamationLoc(), baseTy);
2871-
else
2872-
uncheckedOptionalTy = TC.getImplicitlyUnwrappedOptionalType(
2873-
repr->getExclamationLoc(), baseTy);
2860+
uncheckedOptionalTy =
2861+
TC.getImplicitlyUnwrappedOptionalType(repr->getExclamationLoc(), baseTy);
28742862

28752863
if (!uncheckedOptionalTy)
28762864
return ErrorType::get(Context);

test/Migrator/Inputs/API.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@
319319
"NodeKind": "Function",
320320
"NodeAnnotation": "WrapOptional",
321321
"ChildIndex": "1",
322-
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
322+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
323323
"LeftComment": "",
324324
"RightUsr": "",
325325
"RightComment": "",
@@ -330,7 +330,7 @@
330330
"NodeKind": "Function",
331331
"NodeAnnotation": "WrapOptional",
332332
"ChildIndex": "1",
333-
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
333+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
334334
"LeftComment": "",
335335
"RightUsr": "",
336336
"RightComment": "",
@@ -341,7 +341,7 @@
341341
"NodeKind": "Function",
342342
"NodeAnnotation": "WrapOptional",
343343
"ChildIndex": "1",
344-
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
344+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
345345
"LeftComment": "",
346346
"RightUsr": "",
347347
"RightComment": "",
@@ -352,7 +352,7 @@
352352
"NodeKind": "Function",
353353
"NodeAnnotation": "ImplicitOptionalToOptional",
354354
"ChildIndex": "1:0",
355-
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
355+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
356356
"LeftComment": "",
357357
"RightUsr": "",
358358
"RightComment": "",
@@ -363,7 +363,7 @@
363363
"NodeKind": "Function",
364364
"NodeAnnotation": "UnwrapOptional",
365365
"ChildIndex": "1:1:0",
366-
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
366+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
367367
"LeftComment": "",
368368
"RightUsr": "",
369369
"RightComment": "",

0 commit comments

Comments
 (0)