Skip to content

Commit f4afb03

Browse files
committed
[AST] Enforce that composeTuple drops parameter flags
Callers may either assert that the parameter flags are empty, or ask for them to be dropped.
1 parent 5c29bd2 commit f4afb03

File tree

11 files changed

+42
-37
lines changed

11 files changed

+42
-37
lines changed

include/swift/AST/Types.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,11 @@ class ParameterTypeFlags {
21232123
uint16_t toRaw() const { return value.toRaw(); }
21242124
};
21252125

2126+
/// A type that indicates how parameter flags should be handled in an operation
2127+
/// that requires the conversion into a type that doesn't support them, such as
2128+
/// tuples. They must either be dropped, or be enforced to not be present.
2129+
enum class ParameterFlagHandling { Drop, AssertEmpty };
2130+
21262131
class YieldTypeFlags {
21272132
enum YieldFlags : uint8_t {
21282133
None = 0,
@@ -3127,10 +3132,9 @@ class AnyFunctionType : public TypeBase {
31273132
public:
31283133
/// Take an array of parameters and turn it into a tuple or paren type.
31293134
///
3130-
/// \param wantParamFlags Whether to preserve the parameter flags from the
3131-
/// given set of parameters.
3135+
/// \param paramFlagHandling How to handle the parameter flags.
31323136
static Type composeTuple(ASTContext &ctx, ArrayRef<Param> params,
3133-
bool wantParamFlags = true);
3137+
ParameterFlagHandling paramFlagHandling);
31343138

31353139
/// Given two arrays of parameters determine if they are equal in their
31363140
/// canonicalized form. Internal labels and type sugar is *not* taken into

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,8 +1601,8 @@ SwiftDeclCollector::constructTypeNode(Type T, TypeInitInfo Info) {
16011601
// Still, return type first
16021602
Root->addChild(constructTypeNode(Fun->getResult()));
16031603

1604-
auto Input = AnyFunctionType::composeTuple(Fun->getASTContext(),
1605-
Fun->getParams());
1604+
auto Input = AnyFunctionType::composeTuple(
1605+
Fun->getASTContext(), Fun->getParams(), ParameterFlagHandling::Drop);
16061606
Root->addChild(constructTypeNode(Input));
16071607
return Root;
16081608
}

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,12 +3623,17 @@ Type AnyFunctionType::Param::getParameterType(bool forCanonical,
36233623
}
36243624

36253625
Type AnyFunctionType::composeTuple(ASTContext &ctx, ArrayRef<Param> params,
3626-
bool wantParamFlags) {
3626+
ParameterFlagHandling paramFlagHandling) {
36273627
SmallVector<TupleTypeElt, 4> elements;
36283628
for (const auto &param : params) {
3629-
auto flags = wantParamFlags ? param.getParameterFlags()
3630-
: ParameterTypeFlags();
3631-
elements.emplace_back(param.getParameterType(), param.getLabel(), flags);
3629+
switch (paramFlagHandling) {
3630+
case ParameterFlagHandling::Drop:
3631+
break;
3632+
case ParameterFlagHandling::AssertEmpty:
3633+
assert(param.getParameterFlags().isNone());
3634+
break;
3635+
}
3636+
elements.emplace_back(param.getParameterType(), param.getLabel());
36323637
}
36333638
return TupleType::get(elements, ctx);
36343639
}

lib/AST/Decl.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8813,13 +8813,8 @@ Type EnumElementDecl::getArgumentInterfaceType() const {
88138813
auto funcTy = interfaceType->castTo<AnyFunctionType>();
88148814
funcTy = funcTy->getResult()->castTo<FunctionType>();
88158815

8816-
auto &ctx = getASTContext();
8817-
SmallVector<TupleTypeElt, 4> elements;
8818-
for (const auto &param : funcTy->getParams()) {
8819-
Type eltType = param.getParameterType(/*canonicalVararg=*/false, &ctx);
8820-
elements.emplace_back(eltType, param.getLabel());
8821-
}
8822-
return TupleType::get(elements, ctx);
8816+
return AnyFunctionType::composeTuple(getASTContext(), funcTy->getParams(),
8817+
ParameterFlagHandling::Drop);
88238818
}
88248819

88258820
void EnumElementDecl::setParameterList(ParameterList *params) {

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4187,7 +4187,8 @@ RValue CallEmission::applyEnumElementConstructor(SGFContext C) {
41874187
std::move(*callSite).forward());
41884188

41894189
auto payloadTy = AnyFunctionType::composeTuple(SGF.getASTContext(),
4190-
resultFnType->getParams());
4190+
resultFnType->getParams(),
4191+
ParameterFlagHandling::Drop);
41914192
auto arg = RValue(SGF, argVals, payloadTy->getCanonicalType());
41924193
payload = ArgumentSource(uncurriedLoc, std::move(arg));
41934194
formalResultType = cast<FunctionType>(formalResultType).getResult();

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,9 +2765,8 @@ loadIndexValuesForKeyPathComponent(SILGenFunction &SGF, SILLocation loc,
27652765
return indexValues;
27662766
}
27672767

2768-
auto indexLoweredTy =
2769-
SGF.getLoweredType(
2770-
AnyFunctionType::composeTuple(SGF.getASTContext(), indexParams));
2768+
auto indexLoweredTy = SGF.getLoweredType(AnyFunctionType::composeTuple(
2769+
SGF.getASTContext(), indexParams, ParameterFlagHandling::AssertEmpty));
27712770

27722771
auto addr = SGF.B.createPointerToAddress(loc, pointer,
27732772
indexLoweredTy.getAddressType(),

lib/Sema/CSRanking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,9 @@ getConstructorParamsAsTuples(ASTContext &ctx, Type boundTy1, Type boundTy2) {
848848
}
849849

850850
auto tuple1 = AnyFunctionType::composeTuple(ctx, initParams1,
851-
/*wantParamFlags*/ false);
851+
ParameterFlagHandling::Drop);
852852
auto tuple2 = AnyFunctionType::composeTuple(ctx, initParams2,
853-
/*wantParamFlags*/ false);
853+
ParameterFlagHandling::Drop);
854854
return TypeBindingsToCompare(tuple1, tuple2);
855855
}
856856

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,9 +2650,10 @@ static bool fixMissingArguments(ConstraintSystem &cs, ASTNode anchor,
26502650
// If the argument was a single "tuple", let's bind newly
26512651
// synthesized arguments to it.
26522652
if (argumentTuple) {
2653-
cs.addConstraint(ConstraintKind::Bind, *argumentTuple,
2654-
FunctionType::composeTuple(ctx, args),
2655-
cs.getConstraintLocator(anchor));
2653+
cs.addConstraint(
2654+
ConstraintKind::Bind, *argumentTuple,
2655+
FunctionType::composeTuple(ctx, args, ParameterFlagHandling::Drop),
2656+
cs.getConstraintLocator(anchor));
26562657
}
26572658

26582659
return false;
@@ -2885,7 +2886,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
28852886
// canImplodeParams makes sure we're not dealing with vargs, inout, etc,
28862887
// we may still have e.g ownership flags left over, which we can drop.
28872888
auto input = AnyFunctionType::composeTuple(getASTContext(), params,
2888-
/*wantParamFlags*/ false);
2889+
ParameterFlagHandling::Drop);
28892890
params.clear();
28902891
// If fixes are disabled let's do an easy thing and implode
28912892
// tuple directly into parameters list.
@@ -6985,8 +6986,8 @@ ConstraintSystem::simplifyConstructionConstraint(
69856986
}
69866987

69876988
// Tuple construction is simply tuple conversion.
6988-
Type argType = AnyFunctionType::composeTuple(getASTContext(),
6989-
fnType->getParams());
6989+
Type argType = AnyFunctionType::composeTuple(
6990+
getASTContext(), fnType->getParams(), ParameterFlagHandling::Drop);
69906991
Type resultType = fnType->getResult();
69916992

69926993
ConstraintLocatorBuilder builder(locator);
@@ -7970,9 +7971,8 @@ ConstraintSystem::simplifyBindTupleOfFunctionParamsConstraint(
79707971
if (!funcTy)
79717972
return SolutionKind::Error;
79727973

7973-
auto tupleTy =
7974-
AnyFunctionType::composeTuple(getASTContext(), funcTy->getParams(),
7975-
/*wantParamFlags*/ false);
7974+
auto tupleTy = AnyFunctionType::composeTuple(
7975+
getASTContext(), funcTy->getParams(), ParameterFlagHandling::Drop);
79767976

79777977
addConstraint(ConstraintKind::Bind, tupleTy, second,
79787978
locator.withPathElement(ConstraintLocator::FunctionArgument));

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ static bool noteFixableMismatchedTypes(ValueDecl *decl, const ValueDecl *base) {
490490
auto *fnType = baseTy->getAs<AnyFunctionType>();
491491
baseTy = fnType->getResult();
492492
Type argTy = FunctionType::composeTuple(
493-
ctx, baseTy->getAs<AnyFunctionType>()->getParams());
493+
ctx, baseTy->getAs<AnyFunctionType>()->getParams(),
494+
ParameterFlagHandling::Drop);
494495
auto diagKind = diag::override_type_mismatch_with_fixits_init;
495496
unsigned numArgs = baseInit->getParameters()->size();
496497
return computeFixitsForOverriddenDeclaration(

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,10 +2267,10 @@ static void addAssocTypeDeductionString(llvm::SmallString<128> &str,
22672267
static Type getTypeForDisplay(ModuleDecl *module, ValueDecl *decl) {
22682268
// For a constructor, we only care about the parameter types.
22692269
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
2270-
return AnyFunctionType::composeTuple(module->getASTContext(),
2271-
ctor->getMethodInterfaceType()
2272-
->castTo<FunctionType>()
2273-
->getParams());
2270+
return AnyFunctionType::composeTuple(
2271+
module->getASTContext(),
2272+
ctor->getMethodInterfaceType()->castTo<FunctionType>()->getParams(),
2273+
ParameterFlagHandling::Drop);
22742274
}
22752275

22762276
Type type = decl->getInterfaceType();

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func r22263468(_ a : String?) {
573573
// TODO(diagnostics): This is a regression from diagnosing missing optional unwrap for `a`, we have to
574574
// re-think the way errors in tuple elements are detected because it's currently impossible to detect
575575
// exactly what went wrong here and aggregate fixes for different elements at the same time.
576-
_ = MyTuple(42, a) // expected-error {{tuple type '(_const Int, String?)' is not convertible to tuple type 'MyTuple' (aka '(Int, String)')}}
576+
_ = MyTuple(42, a) // expected-error {{tuple type '(Int, String?)' is not convertible to tuple type 'MyTuple' (aka '(Int, String)')}}
577577
}
578578

579579
// rdar://71829040 - "ambiguous without more context" error for tuple type mismatch.

0 commit comments

Comments
 (0)