Skip to content

For Swift 3/4 mode, do not treat IUOs in illegal positions as Optionals. #13259

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
Dec 5, 2017
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
8 changes: 4 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3117,11 +3117,11 @@ WARNING(implicitly_unwrapped_optional_spelling_deprecated,none,
WARNING(implicitly_unwrapped_optional_spelling_deprecated_with_fixit,none,
"the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name", ())

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

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

ERROR(implicitly_unwrapped_optional_spelling_error,none,
"the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use an explicit type followed by '!'", ())
Expand Down
22 changes: 5 additions & 17 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,14 +1130,6 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,

auto id = comp->getIdentifier();

// If we're compiling for Swift version < 5 and we have a mention of
// ImplicitlyUnwrappedOptional where it is not allowed, treat it as
// if it was spelled Optional.
if (id == TC.Context.Id_ImplicitlyUnwrappedOptional
&& !options.contains(TypeResolutionFlags::AllowIUO)
&& !TC.Context.isSwiftVersionAtLeast(5))
id = TC.Context.Id_Optional;

NameLookupOptions lookupOptions = defaultUnqualifiedLookupOptions;
if (options.contains(TypeResolutionFlags::KnownNonCascadingDependency))
lookupOptions |= NameLookupFlags::KnownPrivate;
Expand Down Expand Up @@ -1239,7 +1231,7 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
}
} else if (isa<GenericIdentTypeRepr>(comp)) {
Diagnostic diag =
diag::implicitly_unwrapped_optional_spelling_decay_to_optional;
diag::implicitly_unwrapped_optional_spelling_suggest_optional;

if (TC.Context.isSwiftVersionAtLeast(5))
diag = diag::implicitly_unwrapped_optional_spelling_in_illegal_position;
Expand All @@ -1258,7 +1250,7 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
genericTyR->getAngleBrackets().End.getAdvancedLoc(1));
} else {
Diagnostic diag =
diag::implicitly_unwrapped_optional_spelling_decay_to_optional;
diag::implicitly_unwrapped_optional_spelling_suggest_optional;

if (TC.Context.isSwiftVersionAtLeast(5))
diag = diag::
Expand Down Expand Up @@ -2847,7 +2839,7 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
TypeResolutionOptions options) {
if (!options.contains(TypeResolutionFlags::AllowIUO)) {
Diagnostic diag = diag::
implicitly_unwrapped_optional_in_illegal_position_decay_to_optional;
implicitly_unwrapped_optional_in_illegal_position_suggest_optional;

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

Type uncheckedOptionalTy;
if (!options.contains(TypeResolutionFlags::AllowIUO))
// Treat IUOs in illegal positions as optionals.
uncheckedOptionalTy = TC.getOptionalType(repr->getExclamationLoc(), baseTy);
else
uncheckedOptionalTy = TC.getImplicitlyUnwrappedOptionalType(
repr->getExclamationLoc(), baseTy);
uncheckedOptionalTy =
TC.getImplicitlyUnwrappedOptionalType(repr->getExclamationLoc(), baseTy);

if (!uncheckedOptionalTy)
return ErrorType::get(Context);
Expand Down
10 changes: 5 additions & 5 deletions test/Migrator/Inputs/API.json
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
"NodeKind": "Function",
"NodeAnnotation": "WrapOptional",
"ChildIndex": "1",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
"LeftComment": "",
"RightUsr": "",
"RightComment": "",
Expand All @@ -330,7 +330,7 @@
"NodeKind": "Function",
"NodeAnnotation": "WrapOptional",
"ChildIndex": "1",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
"LeftComment": "",
"RightUsr": "",
"RightComment": "",
Expand All @@ -341,7 +341,7 @@
"NodeKind": "Function",
"NodeAnnotation": "WrapOptional",
"ChildIndex": "1",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
"LeftComment": "",
"RightUsr": "",
"RightComment": "",
Expand All @@ -352,7 +352,7 @@
"NodeKind": "Function",
"NodeAnnotation": "ImplicitOptionalToOptional",
"ChildIndex": "1:0",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
"LeftComment": "",
"RightUsr": "",
"RightComment": "",
Expand All @@ -363,7 +363,7 @@
"NodeKind": "Function",
"NodeAnnotation": "UnwrapOptional",
"ChildIndex": "1:1:0",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySSSgAE_SStc1x_tF",
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
"LeftComment": "",
"RightUsr": "",
"RightComment": "",
Expand Down
Loading