@@ -367,7 +367,6 @@ Type TypeChecker::resolveTypeInContext(
367
367
static TypeResolutionOptions
368
368
adjustOptionsForGenericArgs (TypeResolutionOptions options) {
369
369
options -= TypeResolutionFlags::SILType;
370
- options -= TypeResolutionFlags::ImmediateFunctionInput;
371
370
options -= TypeResolutionFlags::FunctionInput;
372
371
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
373
372
options -= TypeResolutionFlags::AllowUnavailableProtocol;
@@ -1416,8 +1415,7 @@ static Type applyNonEscapingFromContext(DeclContext *DC,
1416
1415
// Remember whether this is a function parameter.
1417
1416
bool defaultNoEscape =
1418
1417
!options.contains (TypeResolutionFlags::EnumCase) &&
1419
- (options.contains (TypeResolutionFlags::FunctionInput) ||
1420
- options.contains (TypeResolutionFlags::ImmediateFunctionInput));
1418
+ options.contains (TypeResolutionFlags::FunctionInput);
1421
1419
1422
1420
// Desugar here
1423
1421
auto *funcTy = ty->castTo <FunctionType>();
@@ -1553,6 +1551,12 @@ namespace {
1553
1551
TypeResolutionOptions options,
1554
1552
FunctionType::ExtInfo extInfo
1555
1553
= FunctionType::ExtInfo());
1554
+ bool
1555
+ resolveASTFunctionTypeParams (TupleTypeRepr *inputRepr,
1556
+ TypeResolutionOptions options,
1557
+ bool requiresMappingOut,
1558
+ SmallVectorImpl<AnyFunctionType::Param> &ps);
1559
+
1556
1560
Type resolveSILFunctionType (FunctionTypeRepr *repr,
1557
1561
TypeResolutionOptions options,
1558
1562
SILCoroutineKind coroutineKind
@@ -1636,7 +1640,6 @@ Type TypeResolver::resolveType(TypeRepr *repr, TypeResolutionOptions options) {
1636
1640
if (!isa<SpecifierTypeRepr>(repr) && !isa<TupleTypeRepr>(repr) &&
1637
1641
!isa<AttributedTypeRepr>(repr) && !isa<FunctionTypeRepr>(repr) &&
1638
1642
!isa<IdentTypeRepr>(repr)) {
1639
- options -= TypeResolutionFlags::ImmediateFunctionInput;
1640
1643
options -= TypeResolutionFlags::FunctionInput;
1641
1644
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
1642
1645
}
@@ -1741,8 +1744,7 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
1741
1744
};
1742
1745
1743
1746
// Remember whether this is a function parameter.
1744
- bool isParam = options.contains (TypeResolutionFlags::FunctionInput) ||
1745
- options.contains (TypeResolutionFlags::ImmediateFunctionInput);
1747
+ bool isParam = options.contains (TypeResolutionFlags::FunctionInput);
1746
1748
1747
1749
bool isVariadicFunctionParam =
1748
1750
!options.contains (TypeResolutionFlags::EnumCase) &&
@@ -1772,7 +1774,6 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
1772
1774
// The instance type is not a SIL type.
1773
1775
auto instanceOptions = options;
1774
1776
instanceOptions -= TypeResolutionFlags::SILType;
1775
- instanceOptions -= TypeResolutionFlags::ImmediateFunctionInput;
1776
1777
instanceOptions -= TypeResolutionFlags::FunctionInput;
1777
1778
instanceOptions -= TypeResolutionFlags::TypeAliasUnderlyingType;
1778
1779
@@ -1989,7 +1990,6 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
1989
1990
}
1990
1991
1991
1992
auto instanceOptions = options;
1992
- instanceOptions -= TypeResolutionFlags::ImmediateFunctionInput;
1993
1993
instanceOptions -= TypeResolutionFlags::FunctionInput;
1994
1994
instanceOptions -= TypeResolutionFlags::TypeAliasUnderlyingType;
1995
1995
@@ -2114,17 +2114,78 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
2114
2114
return ty;
2115
2115
}
2116
2116
2117
+ bool TypeResolver::resolveASTFunctionTypeParams (
2118
+ TupleTypeRepr *inputRepr, TypeResolutionOptions options,
2119
+ bool requiresMappingOut,
2120
+ SmallVectorImpl<AnyFunctionType::Param> &elements) {
2121
+ elements.reserve (inputRepr->getNumElements ());
2122
+
2123
+ const auto elementOptions = withoutContext (options, true )
2124
+ | TypeResolutionFlags::FunctionInput;
2125
+ for (unsigned i = 0 , end = inputRepr->getNumElements (); i != end; ++i) {
2126
+ auto *eltTypeRepr = inputRepr->getElementType (i);
2127
+
2128
+ // If the element is a variadic parameter, resolve the parameter type as if
2129
+ // it were in non-parameter position, since we want functions to be
2130
+ // @escaping in this case.
2131
+ auto thisElementOptions = elementOptions;
2132
+ bool variadic = false ;
2133
+ if (inputRepr->hasEllipsis () &&
2134
+ elements.size () == inputRepr->getEllipsisIndex ()) {
2135
+ thisElementOptions = withoutContext (elementOptions);
2136
+ thisElementOptions |= TypeResolutionFlags::VariadicFunctionInput;
2137
+ variadic = true ;
2138
+ }
2139
+
2140
+ Type ty = resolveType (eltTypeRepr, thisElementOptions);
2141
+ if (!ty) return true ;
2142
+
2143
+ if (ty->hasError ()) {
2144
+ elements.emplace_back (ErrorType::get (Context), Identifier (),
2145
+ ParameterTypeFlags ());
2146
+ continue ;
2147
+ }
2148
+
2149
+ // Parameters of polymorphic functions speak in terms of interface types.
2150
+ if (requiresMappingOut) {
2151
+ ty = ty->mapTypeOutOfContext ();
2152
+ }
2153
+
2154
+ ValueOwnership ownership;
2155
+ switch (eltTypeRepr->getKind ()) {
2156
+ case TypeReprKind::Shared:
2157
+ ownership = ValueOwnership::Shared;
2158
+ break ;
2159
+ case TypeReprKind::InOut:
2160
+ ownership = ValueOwnership::InOut;
2161
+ break ;
2162
+ case TypeReprKind::Owned:
2163
+ ownership = ValueOwnership::Owned;
2164
+ break ;
2165
+ default :
2166
+ ownership = ValueOwnership::Default;
2167
+ break ;
2168
+ }
2169
+ ParameterTypeFlags paramFlags =
2170
+ ParameterTypeFlags::fromParameterType (ty, variadic, ownership);
2171
+ elements.emplace_back (ty->getInOutObjectType (), Identifier (), paramFlags);
2172
+ }
2173
+
2174
+ return false ;
2175
+ }
2176
+
2117
2177
Type TypeResolver::resolveASTFunctionType (FunctionTypeRepr *repr,
2118
2178
TypeResolutionOptions options,
2119
2179
FunctionType::ExtInfo extInfo) {
2120
- options -= TypeResolutionFlags::ImmediateFunctionInput;
2121
2180
options -= TypeResolutionFlags::FunctionInput;
2122
2181
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
2123
2182
options -= TypeResolutionFlags::AllowIUO;
2124
2183
2125
- Type inputTy = resolveType (repr->getArgsTypeRepr (),
2126
- options | TypeResolutionFlags::ImmediateFunctionInput);
2127
- if (!inputTy || inputTy->hasError ()) return inputTy;
2184
+ SmallVector<AnyFunctionType::Param, 8 > params;
2185
+ if (resolveASTFunctionTypeParams (repr->getArgsTypeRepr (), options,
2186
+ repr->getGenericEnvironment () != nullptr , params)) {
2187
+ return Type ();
2188
+ }
2128
2189
2129
2190
Type outputTy = resolveType (repr->getResultTypeRepr (), options);
2130
2191
if (!outputTy || outputTy->hasError ()) return outputTy;
@@ -2152,13 +2213,12 @@ Type TypeResolver::resolveASTFunctionType(FunctionTypeRepr *repr,
2152
2213
2153
2214
// SIL uses polymorphic function types to resolve overloaded member functions.
2154
2215
if (auto genericEnv = repr->getGenericEnvironment ()) {
2155
- inputTy = inputTy->mapTypeOutOfContext ();
2156
2216
outputTy = outputTy->mapTypeOutOfContext ();
2157
2217
return GenericFunctionType::get (genericEnv->getGenericSignature (),
2158
- inputTy , outputTy, extInfo);
2218
+ params , outputTy, extInfo);
2159
2219
}
2160
2220
2161
- auto fnTy = FunctionType::get (inputTy , outputTy, extInfo);
2221
+ auto fnTy = FunctionType::get (params , outputTy, extInfo);
2162
2222
// If the type is a block or C function pointer, it must be representable in
2163
2223
// ObjC.
2164
2224
switch (auto rep = extInfo.getRepresentation ()) {
@@ -2169,7 +2229,7 @@ Type TypeResolver::resolveASTFunctionType(FunctionTypeRepr *repr,
2169
2229
rep == AnyFunctionType::Representation::Block ? " block" : " c" ;
2170
2230
auto extInfo2 =
2171
2231
extInfo.withRepresentation (AnyFunctionType::Representation::Swift);
2172
- auto simpleFnTy = FunctionType::get (inputTy , outputTy, extInfo2);
2232
+ auto simpleFnTy = FunctionType::get (params , outputTy, extInfo2);
2173
2233
TC.diagnose (repr->getStartLoc (), diag::objc_convention_invalid,
2174
2234
simpleFnTy, strName);
2175
2235
}
@@ -2266,7 +2326,6 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2266
2326
SILFunctionType::ExtInfo extInfo,
2267
2327
ParameterConvention callee,
2268
2328
TypeRepr *witnessMethodProtocol) {
2269
- options -= TypeResolutionFlags::ImmediateFunctionInput;
2270
2329
options -= TypeResolutionFlags::FunctionInput;
2271
2330
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
2272
2331
@@ -2305,7 +2364,7 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
2305
2364
2306
2365
for (auto elt : argsTuple->getElements ()) {
2307
2366
auto param = resolveSILParameter (elt.Type ,
2308
- options | TypeResolutionFlags::ImmediateFunctionInput );
2367
+ options | TypeResolutionFlags::FunctionInput );
2309
2368
params.push_back (param);
2310
2369
if (!param.getType ()) return nullptr ;
2311
2370
@@ -2409,17 +2468,16 @@ SILYieldInfo TypeResolver::resolveSILYield(TypeAttributes &attrs,
2409
2468
TypeResolutionOptions options) {
2410
2469
AttributedTypeRepr attrRepr (attrs, repr);
2411
2470
SILParameterInfo paramInfo =
2412
- resolveSILParameter (&attrRepr, options |
2413
- TypeResolutionFlags::ImmediateFunctionInput );
2471
+ resolveSILParameter (&attrRepr,
2472
+ options | TypeResolutionFlags::FunctionInput );
2414
2473
return SILYieldInfo (paramInfo.getType (), paramInfo.getConvention ());
2415
2474
}
2416
2475
2417
2476
SILParameterInfo TypeResolver::resolveSILParameter (
2418
2477
TypeRepr *repr,
2419
2478
TypeResolutionOptions options) {
2420
- assert ((options & TypeResolutionFlags::FunctionInput)
2421
- | (options & TypeResolutionFlags::ImmediateFunctionInput) &&
2422
- " Parameters should be marked as inputs" );
2479
+ assert (options.contains (TypeResolutionFlags::FunctionInput)
2480
+ && " Parameters should be marked as inputs" );
2423
2481
auto convention = DefaultParameterConvention;
2424
2482
Type type;
2425
2483
bool hadError = false ;
@@ -2587,8 +2645,7 @@ Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
2587
2645
// function parameters.
2588
2646
if ((options & TypeResolutionFlags::SubscriptParameters) ||
2589
2647
(options & TypeResolutionFlags::EnumCase) ||
2590
- (!(options & TypeResolutionFlags::FunctionInput) &&
2591
- !(options & TypeResolutionFlags::ImmediateFunctionInput))) {
2648
+ (!(options & TypeResolutionFlags::FunctionInput))) {
2592
2649
2593
2650
decltype (diag::attr_only_on_parameters) diagID;
2594
2651
if (options & TypeResolutionFlags::SubscriptParameters) {
@@ -2618,7 +2675,6 @@ Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
2618
2675
}
2619
2676
2620
2677
// Anything within the inout isn't a parameter anymore.
2621
- options -= TypeResolutionFlags::ImmediateFunctionInput;
2622
2678
options -= TypeResolutionFlags::FunctionInput;
2623
2679
options -= TypeResolutionFlags::TypeAliasUnderlyingType;
2624
2680
@@ -2742,112 +2798,48 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
2742
2798
2743
2799
Type TypeResolver::resolveTupleType (TupleTypeRepr *repr,
2744
2800
TypeResolutionOptions options) {
2745
- bool isImmediateFunctionInput = options.contains (
2746
- TypeResolutionFlags::ImmediateFunctionInput);
2747
2801
SmallVector<TupleTypeElt, 8 > elements;
2748
2802
elements.reserve (repr->getNumElements ());
2749
2803
2750
- // If this is the top level of a function input list, peel off the
2751
- // ImmediateFunctionInput marker and install a FunctionInput one instead.
2804
+
2752
2805
auto elementOptions = options;
2753
2806
if (repr->isParenType ()) {
2754
2807
// We also want to disallow IUO within even a paren.
2755
2808
elementOptions -= TypeResolutionFlags::AllowIUO;
2756
-
2757
- // If we have a single ParenType, don't clear the context bits; we
2758
- // still want to parse the type contained therein as if it were in
2759
- // parameter position, meaning function types are not @escaping by
2760
- // default. We still want to reduce `ImmediateFunctionInput` to
2761
- // `FunctionInput` so that e.g. ((foo: Int)) -> Int is considered a
2762
- // tuple argument rather than a labeled Int argument.
2763
- if (isImmediateFunctionInput) {
2764
- elementOptions = withoutContext (elementOptions, true );
2765
- elementOptions |= TypeResolutionFlags::FunctionInput;
2766
- }
2767
2809
} else {
2768
2810
elementOptions = withoutContext (elementOptions, true );
2769
- if (isImmediateFunctionInput)
2770
- elementOptions |= TypeResolutionFlags::FunctionInput;
2771
2811
}
2772
2812
2773
- bool complained = false ;
2774
-
2775
2813
// Variadic tuples are not permitted.
2776
- if (repr-> hasEllipsis () &&
2777
- !isImmediateFunctionInput ) {
2814
+ bool complained = false ;
2815
+ if (repr-> hasEllipsis () ) {
2778
2816
TC.diagnose (repr->getEllipsisLoc (), diag::tuple_ellipsis);
2779
2817
repr->removeEllipsis ();
2780
2818
complained = true ;
2781
2819
}
2782
2820
2783
2821
for (unsigned i = 0 , end = repr->getNumElements (); i != end; ++i) {
2784
2822
auto *tyR = repr->getElementType (i);
2785
- Type ty;
2786
- Identifier name;
2787
- bool variadic = false ;
2788
2823
2789
- // If the element has a label, stash the label.
2790
- // FIXME: Preserve and serialize parameter names in function types, maybe
2791
- // with a new sugar type.
2792
- if (!isImmediateFunctionInput)
2793
- name = repr->getElementName (i);
2794
-
2795
- // If the element is a variadic parameter, resolve the parameter type as if
2796
- // it were in non-parameter position, since we want functions to be
2797
- // @escaping in this case.
2798
- auto thisElementOptions = elementOptions;
2799
- if (repr->hasEllipsis () &&
2800
- elements.size () == repr->getEllipsisIndex ()) {
2801
- thisElementOptions = withoutContext (elementOptions);
2802
- thisElementOptions |= TypeResolutionFlags::VariadicFunctionInput;
2803
- variadic = true ;
2804
- }
2805
-
2806
- ty = resolveType (tyR, thisElementOptions);
2824
+ Type ty = resolveType (tyR, elementOptions);
2807
2825
if (!ty || ty->hasError ()) return ty;
2808
2826
2809
- // If the element is a variadic parameter, the underlying type is actually
2810
- // an ArraySlice of the element type.
2811
- if (variadic)
2812
- ty = TC.getArraySliceType (repr->getEllipsisLoc (), ty);
2813
-
2814
- ParameterTypeFlags paramFlags;
2815
- if (isImmediateFunctionInput) {
2816
- ValueOwnership ownership;
2817
- switch (tyR->getKind ()) {
2818
- case TypeReprKind::Shared:
2819
- ownership = ValueOwnership::Shared;
2820
- break ;
2821
- case TypeReprKind::InOut:
2822
- ownership = ValueOwnership::InOut;
2823
- break ;
2824
- case TypeReprKind::Owned:
2825
- ownership = ValueOwnership::Owned;
2826
- break ;
2827
- default :
2828
- ownership = ValueOwnership::Default;
2829
- break ;
2830
- }
2831
- paramFlags =
2832
- ParameterTypeFlags::fromParameterType (ty, variadic, ownership);
2833
- }
2834
- elements.emplace_back (ty->getInOutObjectType (), name, paramFlags);
2827
+ elements.emplace_back (ty->getInOutObjectType (),
2828
+ repr->getElementName (i), ParameterTypeFlags ());
2835
2829
}
2836
2830
2837
2831
// Single-element labeled tuples are not permitted outside of declarations
2838
2832
// or SIL, either.
2839
- if (!isImmediateFunctionInput) {
2840
- if (elements.size () == 1 && elements[0 ].hasName ()
2841
- && !(options & TypeResolutionFlags::SILType)) {
2842
- if (!complained) {
2843
- TC.diagnose (repr->getElementNameLoc (0 ),
2844
- diag::tuple_single_element)
2845
- .fixItRemoveChars (repr->getElementNameLoc (0 ),
2846
- repr->getElementType (0 )->getStartLoc ());
2847
- }
2848
-
2849
- elements[0 ] = TupleTypeElt (elements[0 ].getType ());
2833
+ if (elements.size () == 1 && elements[0 ].hasName ()
2834
+ && !(options & TypeResolutionFlags::SILType)) {
2835
+ if (!complained) {
2836
+ TC.diagnose (repr->getElementNameLoc (0 ),
2837
+ diag::tuple_single_element)
2838
+ .fixItRemoveChars (repr->getElementNameLoc (0 ),
2839
+ repr->getElementType (0 )->getStartLoc ());
2850
2840
}
2841
+
2842
+ elements[0 ] = TupleTypeElt (elements[0 ].getType ());
2851
2843
}
2852
2844
2853
2845
return TupleType::get (elements, Context);
0 commit comments