Skip to content

Commit 388230e

Browse files
authored
Merge pull request #59835 from DougGregor/preconcurrency-sendable-function-conversion-5.7
Introduce function conversions for @preconcurrency-adjusted declaration references
2 parents d5b6caf + 9d111b1 commit 388230e

16 files changed

+532
-306
lines changed

include/swift/AST/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4553,7 +4553,7 @@ class ApplyExpr : public Expr {
45534553
Bits.ApplyExpr.ShouldApplyDistributedThunk = flag;
45544554
}
45554555

4556-
ValueDecl *getCalledValue() const;
4556+
ValueDecl *getCalledValue(bool skipFunctionConversions = false) const;
45574557

45584558
static bool classof(const Expr *E) {
45594559
return E->getKind() >= ExprKind::First_ApplyExpr &&

include/swift/Sema/ConstraintSystem.h

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,19 @@ struct SelectedOverload {
614614
/// we're referencing a member.
615615
const Type openedFullType;
616616

617+
/// The opened type of the base of the reference to this overload, adjusted
618+
/// for `@preconcurrency` or other contextual type-altering attributes.
619+
const Type adjustedOpenedFullType;
620+
617621
/// The opened type produced by referring to this overload.
618622
const Type openedType;
619623

624+
/// The opened type produced by referring to this overload, adjusted for
625+
/// `@preconcurrency` or other contextual type-altering attributes.
626+
const Type adjustedOpenedType;
627+
620628
/// The type that this overload binds. Note that this may differ from
621-
/// openedType, for example it will include any IUO unwrapping that has taken
629+
/// adjustedOpenedType, for example it will include any IUO unwrapping that has taken
622630
/// place.
623631
const Type boundType;
624632
};
@@ -2471,6 +2479,31 @@ struct GetClosureType {
24712479
Type operator()(const AbstractClosureExpr *expr) const;
24722480
};
24732481

2482+
/// Describes the type produced when referencing a declaration.
2483+
struct DeclReferenceType {
2484+
/// The "opened" type, which is the type of the declaration where any
2485+
/// generic parameters have been replaced with type variables.
2486+
///
2487+
/// The mapping from generic parameters to type variables will have been
2488+
/// recorded by \c recordOpenedTypes when this type is produced.
2489+
Type openedType;
2490+
2491+
/// The opened type, after performing contextual type adjustments such as
2492+
/// removing concurrency-related annotations for a `@preconcurrency`
2493+
/// operation.
2494+
Type adjustedOpenedType;
2495+
2496+
/// The type of the reference, based on the original opened type. This is the
2497+
/// type that the expression used to form the declaration reference would
2498+
/// have if no adjustments had been applied.
2499+
Type referenceType;
2500+
2501+
/// The type of the reference, which is the adjusted opened type after
2502+
/// (e.g.) applying the base of a member access. This is the type of the
2503+
/// expression used to form the declaration reference.
2504+
Type adjustedReferenceType;
2505+
};
2506+
24742507
/// Describes a system of constraints on type variables, the
24752508
/// solution of which assigns concrete types to each of the type variables.
24762509
/// Constraint systems are typically generated given an (untyped) expression.
@@ -4423,10 +4456,11 @@ class ConstraintSystem {
44234456
const OpenedTypeMap &replacements);
44244457

44254458
/// Wrapper over swift::adjustFunctionTypeForConcurrency that passes along
4426-
/// the appropriate closure-type extraction function.
4459+
/// the appropriate closure-type and opening extraction functions.
44274460
AnyFunctionType *adjustFunctionTypeForConcurrency(
44284461
AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
4429-
unsigned numApplies, bool isMainDispatchQueue);
4462+
unsigned numApplies, bool isMainDispatchQueue,
4463+
OpenedTypeMap &replacements);
44304464

44314465
/// Retrieve the type of a reference to the given value declaration.
44324466
///
@@ -4436,9 +4470,8 @@ class ConstraintSystem {
44364470
///
44374471
/// \param decl The declarations whose type is being computed.
44384472
///
4439-
/// \returns a pair containing the full opened type (if applicable) and
4440-
/// opened type of a reference to declaration.
4441-
std::pair<Type, Type> getTypeOfReference(
4473+
/// \returns a description of the type of this declaration reference.
4474+
DeclReferenceType getTypeOfReference(
44424475
ValueDecl *decl,
44434476
FunctionRefKind functionRefKind,
44444477
ConstraintLocatorBuilder locator,
@@ -4486,6 +4519,14 @@ class ConstraintSystem {
44864519
return Type();
44874520
});
44884521

4522+
/// Given the opened type and a pile of information about a member reference,
4523+
/// determine the reference type of the member reference.
4524+
Type getMemberReferenceTypeFromOpenedType(
4525+
Type &openedType, Type baseObjTy, ValueDecl *value, DeclContext *outerDC,
4526+
ConstraintLocator *locator, bool hasAppliedSelf,
4527+
bool isStaticMemberRefOnProtocol, bool isDynamicResult,
4528+
OpenedTypeMap &replacements);
4529+
44894530
/// Retrieve the type of a reference to the given value declaration,
44904531
/// as a member with a base of the given type.
44914532
///
@@ -4496,9 +4537,8 @@ class ConstraintSystem {
44964537
/// \param isDynamicResult Indicates that this declaration was found via
44974538
/// dynamic lookup.
44984539
///
4499-
/// \returns a pair containing the full opened type (which includes the opened
4500-
/// base) and opened type of a reference to this member.
4501-
std::pair<Type, Type> getTypeOfMemberReference(
4540+
/// \returns a description of the type of this declaration reference.
4541+
DeclReferenceType getTypeOfMemberReference(
45024542
Type baseTy, ValueDecl *decl, DeclContext *useDC,
45034543
bool isDynamicResult,
45044544
FunctionRefKind functionRefKind,

lib/AST/Expr.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ DictionaryExpr *DictionaryExpr::create(ASTContext &C, SourceLoc LBracketLoc,
14131413
Ty);
14141414
}
14151415

1416-
static ValueDecl *getCalledValue(Expr *E) {
1416+
static ValueDecl *getCalledValue(Expr *E, bool skipFunctionConversions) {
14171417
if (auto *DRE = dyn_cast<DeclRefExpr>(E))
14181418
return DRE->getDecl();
14191419

@@ -1422,11 +1422,16 @@ static ValueDecl *getCalledValue(Expr *E) {
14221422

14231423
// Look through SelfApplyExpr.
14241424
if (auto *SAE = dyn_cast<SelfApplyExpr>(E))
1425-
return SAE->getCalledValue();
1425+
return SAE->getCalledValue(skipFunctionConversions);
1426+
1427+
if (skipFunctionConversions) {
1428+
if (auto fnConv = dyn_cast<FunctionConversionExpr>(E))
1429+
return getCalledValue(fnConv->getSubExpr(), skipFunctionConversions);
1430+
}
14261431

14271432
Expr *E2 = E->getValueProvidingExpr();
14281433
if (E != E2)
1429-
return getCalledValue(E2);
1434+
return getCalledValue(E2, skipFunctionConversions);
14301435

14311436
return nullptr;
14321437
}
@@ -1468,8 +1473,8 @@ Expr *DefaultArgumentExpr::getCallerSideDefaultExpr() const {
14681473
new (ctx) ErrorExpr(getSourceRange(), getType()));
14691474
}
14701475

1471-
ValueDecl *ApplyExpr::getCalledValue() const {
1472-
return ::getCalledValue(Fn);
1476+
ValueDecl *ApplyExpr::getCalledValue(bool skipFunctionConversions) const {
1477+
return ::getCalledValue(Fn, skipFunctionConversions);
14731478
}
14741479

14751480
SubscriptExpr::SubscriptExpr(Expr *base, ArgumentList *argList,
@@ -1906,6 +1911,14 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
19061911
return expr;
19071912
};
19081913

1914+
auto maybeUnwrapConversions = [](Expr *expr) {
1915+
if (auto *covariantReturn = dyn_cast<CovariantReturnConversionExpr>(expr))
1916+
expr = covariantReturn->getSubExpr();
1917+
if (auto *functionConversion = dyn_cast<FunctionConversionExpr>(expr))
1918+
expr = functionConversion->getSubExpr();
1919+
return expr;
1920+
};
1921+
19091922
switch (getThunkKind()) {
19101923
case AutoClosureExpr::Kind::None:
19111924
case AutoClosureExpr::Kind::AsyncLet:
@@ -1916,6 +1929,7 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
19161929
body = body->getSemanticsProvidingExpr();
19171930
body = maybeUnwrapOpenExistential(body);
19181931
body = maybeUnwrapOptionalEval(body);
1932+
body = maybeUnwrapConversions(body);
19191933

19201934
if (auto *outerCall = dyn_cast<ApplyExpr>(body)) {
19211935
return outerCall->getFn();
@@ -1934,9 +1948,10 @@ Expr *AutoClosureExpr::getUnwrappedCurryThunkExpr() const {
19341948
innerBody = innerBody->getSemanticsProvidingExpr();
19351949
innerBody = maybeUnwrapOpenExistential(innerBody);
19361950
innerBody = maybeUnwrapOptionalEval(innerBody);
1937-
1951+
innerBody = maybeUnwrapConversions(innerBody);
19381952
if (auto *outerCall = dyn_cast<ApplyExpr>(innerBody)) {
1939-
if (auto *innerCall = dyn_cast<ApplyExpr>(outerCall->getFn())) {
1953+
auto outerFn = maybeUnwrapConversions(outerCall->getFn());
1954+
if (auto *innerCall = dyn_cast<ApplyExpr>(outerFn)) {
19401955
return innerCall->getFn();
19411956
}
19421957
}

lib/SILGen/SILGenPoly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ collectExistentialConformances(ModuleDecl *M, CanType fromType, CanType toType)
183183
SmallVector<ProtocolConformanceRef, 4> conformances;
184184
for (auto proto : protocols) {
185185
auto conformance =
186-
M->lookupConformance(fromType, proto);
186+
M->lookupConformance(fromType, proto, /*allowMissing=*/true);
187187
assert(conformance);
188188
conformances.push_back(conformance);
189189
}

0 commit comments

Comments
 (0)