@@ -719,20 +719,33 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
719
719
720
720
bool hadError = false ;
721
721
722
+ auto &TL = decl->getTypeLoc ();
723
+
722
724
// We might have a null typeLoc if this is a closure parameter list,
723
725
// where parameters are allowed to elide their types.
724
- if (!decl-> getTypeLoc () .isNull ()) {
725
- hadError |= TC.validateType (decl-> getTypeLoc () , DC,
726
+ if (!TL .isNull ()) {
727
+ hadError |= TC.validateType (TL , DC,
726
728
elementOptions, &resolver);
727
729
}
728
730
729
- Type Ty = decl->getTypeLoc ().getType ();
731
+ // If this is declared with '!' indicating that it is an Optional
732
+ // that we should implicitly unwrap if doing so is required to type
733
+ // check, then add an attribute to the decl.
734
+ if (elementOptions.contains (TypeResolutionFlags::AllowIUO)
735
+ && TL.getTypeRepr () && TL.getTypeRepr ()->getKind () ==
736
+ TypeReprKind::ImplicitlyUnwrappedOptional) {
737
+ auto &C = DC->getASTContext ();
738
+ decl->getAttrs ().add (
739
+ new (C) ImplicitlyUnwrappedOptionalAttr (/* implicit= */ true ));
740
+ }
741
+
742
+ Type Ty = TL.getType ();
730
743
if (decl->isVariadic () && !Ty.isNull () && !hadError) {
731
744
Ty = TC.getArraySliceType (decl->getStartLoc (), Ty);
732
745
if (Ty.isNull ()) {
733
746
hadError = true ;
734
747
}
735
- decl-> getTypeLoc () .setType (Ty);
748
+ TL .setType (Ty);
736
749
}
737
750
738
751
// If the user did not explicitly write 'let', 'var', or 'inout', we'll let
@@ -751,7 +764,7 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
751
764
}
752
765
753
766
if (hadError)
754
- decl-> getTypeLoc () .setType (ErrorType::get (TC.Context ), /* validated*/ true );
767
+ TL .setType (ErrorType::get (TC.Context ), /* validated*/ true );
755
768
756
769
return hadError;
757
770
}
0 commit comments