Skip to content

Commit ff294bd

Browse files
authored
Merge pull request swiftlang#28780 from slavapestov/sr-75-cleanups
A handful of cleanups in preparation for building curry thunks in Sema
2 parents 7cdb285 + 7626f9d commit ff294bd

File tree

10 files changed

+64
-61
lines changed

10 files changed

+64
-61
lines changed

include/swift/AST/Expr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3748,7 +3748,8 @@ class AutoClosureExpr : public AbstractClosureExpr {
37483748
DeclContext *Parent)
37493749
: AbstractClosureExpr(ExprKind::AutoClosure, ResultTy, /*Implicit=*/true,
37503750
Discriminator, Parent) {
3751-
setBody(Body);
3751+
if (Body != nullptr)
3752+
setBody(Body);
37523753
}
37533754

37543755
SourceRange getSourceRange() const;

include/swift/AST/ParameterList.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ class alignas(ParamDecl *) ParameterList final :
103103
/// Change the DeclContext of any contained parameters to the specified
104104
/// DeclContext.
105105
void setDeclContextOfParamDecls(DeclContext *DC);
106-
107-
106+
108107
/// Flags used to indicate how ParameterList cloning should operate.
109108
enum CloneFlags {
110109
/// The cloned ParamDecls should be marked implicit.
111110
Implicit = 0x01,
112-
/// The cloned pattern is for an inherited constructor; mark default
113-
/// arguments as inherited, and mark unnamed arguments as named.
111+
/// Mark default arguments as inherited.
114112
Inherited = 0x02,
113+
/// Mark unnamed arguments as named.
114+
NamedArguments = 0x04,
115115
};
116116

117117
friend OptionSet<CloneFlags> operator|(CloneFlags flag1, CloneFlags flag2) {

lib/AST/Parameter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ ParameterList *ParameterList::clone(const ASTContext &C,
6767
if (options & Implicit)
6868
decl->setImplicit();
6969

70-
// If the argument isn't named, and we're cloning for an inherited
71-
// constructor, give the parameter a name so that silgen will produce a
72-
// value for it.
73-
if (decl->getName().empty() && (options & Inherited))
70+
// If the argument isn't named, give the parameter a name so that
71+
// silgen will produce a value for it.
72+
if (decl->getName().empty() && (options & NamedArguments))
7473
decl->setName(C.getIdentifier("argument"));
7574

7675
// If we're inheriting a default argument, mark it as such.

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,7 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
513513
emitStmt(ce->getBody());
514514
} else {
515515
auto *autoclosure = cast<AutoClosureExpr>(ace);
516-
// Closure expressions implicitly return the result of their body
517-
// expression.
518-
emitReturnExpr(ImplicitReturnLocation(ace),
519-
autoclosure->getSingleExpressionBody());
516+
emitStmt(autoclosure->getBody());
520517
}
521518
emitEpilog(ace);
522519
}

lib/SILGen/SILGenProlog.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,6 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
443443
uint16_t ArgNo = emitProlog(paramList, selfParam, resultType,
444444
DC, throws, throwsLoc);
445445

446-
// Emit an unreachable instruction if a parameter type is
447-
// uninhabited
448-
if (paramList) {
449-
for (auto *param : *paramList) {
450-
if (param->getType()->isStructurallyUninhabited()) {
451-
SILLocation unreachableLoc(param);
452-
unreachableLoc.markAsPrologue();
453-
B.createUnreachable(unreachableLoc);
454-
break;
455-
}
456-
}
457-
}
458-
459446
// Emit the capture argument variables. These are placed last because they
460447
// become the first curry level of the SIL function.
461448
assert((captureInfo.hasBeenComputed() ||
@@ -491,6 +478,19 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
491478
emitCaptureArguments(*this, DC->getGenericSignatureOfContext(),
492479
capture, ++ArgNo);
493480
}
481+
482+
// Emit an unreachable instruction if a parameter type is
483+
// uninhabited
484+
if (paramList) {
485+
for (auto *param : *paramList) {
486+
if (param->getType()->isStructurallyUninhabited()) {
487+
SILLocation unreachableLoc(param);
488+
unreachableLoc.markAsPrologue();
489+
B.createUnreachable(unreachableLoc);
490+
break;
491+
}
492+
}
493+
}
494494
}
495495

496496
static void emitIndirectResultParameters(SILGenFunction &SGF, Type resultType,

lib/Sema/CSApply.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,10 @@ namespace {
303303
///
304304
/// \param expr The expression to be coerced.
305305
/// \param toType The type to which the expression will be coerced.
306-
/// \param locator Locator describing where this conversion occurs.
307306
///
308307
/// \return The coerced expression, whose type will be equivalent to
309308
/// \c toType.
310-
Expr *coerceSuperclass(Expr *expr, Type toType,
311-
ConstraintLocatorBuilder locator);
309+
Expr *coerceSuperclass(Expr *expr, Type toType);
312310

313311
/// Coerce the given value to existential type.
314312
///
@@ -320,12 +318,10 @@ namespace {
320318
///
321319
/// \param expr The expression to be coerced.
322320
/// \param toType The type to which the expression will be coerced.
323-
/// \param locator Locator describing where this conversion occurs.
324321
///
325322
/// \return The coerced expression, whose type will be equivalent to
326323
/// \c toType.
327-
Expr *coerceExistential(Expr *expr, Type toType,
328-
ConstraintLocatorBuilder locator);
324+
Expr *coerceExistential(Expr *expr, Type toType);
329325

330326
/// Coerce an expression of (possibly unchecked) optional
331327
/// type to have a different (possibly unchecked) optional type.
@@ -4342,7 +4338,8 @@ namespace {
43424338
auto closureTy =
43434339
FunctionType::get({ FunctionType::Param(baseTy) }, leafTy);
43444340
auto closure = new (ctx)
4345-
AutoClosureExpr(E, leafTy, discriminator, cs.DC);
4341+
AutoClosureExpr(/*set body later*/nullptr, leafTy,
4342+
discriminator, cs.DC);
43464343
auto param = new (ctx) ParamDecl(
43474344
SourceLoc(),
43484345
/*argument label*/ SourceLoc(), Identifier(),
@@ -4356,7 +4353,8 @@ namespace {
43564353
auto outerClosureTy =
43574354
FunctionType::get({ FunctionType::Param(keyPathTy) }, closureTy);
43584355
auto outerClosure = new (ctx)
4359-
AutoClosureExpr(closure, closureTy, discriminator, cs.DC);
4356+
AutoClosureExpr(/*set body later*/nullptr, closureTy,
4357+
discriminator, cs.DC);
43604358
auto outerParam =
43614359
new (ctx) ParamDecl(SourceLoc(),
43624360
/*argument label*/ SourceLoc(), Identifier(),
@@ -4754,8 +4752,7 @@ static Type getMetatypeSuperclass(Type t) {
47544752
return t->getSuperclass();
47554753
}
47564754

4757-
Expr *ExprRewriter::coerceSuperclass(Expr *expr, Type toType,
4758-
ConstraintLocatorBuilder locator) {
4755+
Expr *ExprRewriter::coerceSuperclass(Expr *expr, Type toType) {
47594756
auto &ctx = cs.getASTContext();
47604757

47614758
auto fromType = cs.getType(expr);
@@ -4780,7 +4777,7 @@ Expr *ExprRewriter::coerceSuperclass(Expr *expr, Type toType,
47804777
new (ctx) ArchetypeToSuperExpr(expr, superclass));
47814778

47824779
if (!superclass->isEqual(toType))
4783-
return coerceSuperclass(expr, toType, locator);
4780+
return coerceSuperclass(expr, toType);
47844781

47854782
return expr;
47864783

@@ -4794,7 +4791,7 @@ Expr *ExprRewriter::coerceSuperclass(Expr *expr, Type toType,
47944791
auto *archetypeVal = cs.cacheType(new (ctx) OpaqueValueExpr(
47954792
expr->getSourceRange(), fromArchetype));
47964793

4797-
auto *result = coerceSuperclass(archetypeVal, toType, locator);
4794+
auto *result = coerceSuperclass(archetypeVal, toType);
47984795

47994796
return cs.cacheType(
48004797
new (ctx) OpenExistentialExpr(expr, archetypeVal, result,
@@ -4830,8 +4827,7 @@ collectExistentialConformances(Type fromType, Type toType,
48304827
return toType->getASTContext().AllocateCopy(conformances);
48314828
}
48324829

4833-
Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType,
4834-
ConstraintLocatorBuilder locator) {
4830+
Expr *ExprRewriter::coerceExistential(Expr *expr, Type toType) {
48354831
Type fromType = cs.getType(expr);
48364832
Type fromInstanceType = fromType;
48374833
Type toInstanceType = toType;
@@ -5823,11 +5819,11 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
58235819

58245820
case ConversionRestrictionKind::Superclass:
58255821
case ConversionRestrictionKind::ExistentialMetatypeToMetatype:
5826-
return coerceSuperclass(expr, toType, locator);
5822+
return coerceSuperclass(expr, toType);
58275823

58285824
case ConversionRestrictionKind::Existential:
58295825
case ConversionRestrictionKind::MetatypeToExistentialMetatype:
5830-
return coerceExistential(expr, toType, locator);
5826+
return coerceExistential(expr, toType);
58315827

58325828
case ConversionRestrictionKind::ClassMetatypeToAnyObject: {
58335829
assert(ctx.LangOpts.EnableObjCInterop &&
@@ -6060,7 +6056,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
60606056
fromSuperClass;
60616057
fromSuperClass = fromSuperClass->getSuperclass()) {
60626058
if (fromSuperClass->isEqual(toType)) {
6063-
return coerceSuperclass(expr, toType, locator);
6059+
return coerceSuperclass(expr, toType);
60646060
}
60656061
}
60666062
break;
@@ -6193,7 +6189,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
61936189
case TypeKind::ExistentialMetatype:
61946190
case TypeKind::ProtocolComposition:
61956191
case TypeKind::Protocol:
6196-
return coerceExistential(expr, toType, locator);
6192+
return coerceExistential(expr, toType);
61976193

61986194
// Coercion to Optional<T>.
61996195
case TypeKind::BoundGenericEnum: {

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
201201
ContextualTypePurpose CTP,
202202
Type suggestedType = Type());
203203

204-
/// For an expression being type checked with a CTP_CalleeResult contextual
205-
/// type, try to diagnose a problem.
206-
bool diagnoseCalleeResultContextualConversionError();
207-
208204
/// Attempt to produce a diagnostic for a mismatch between a call's
209205
/// type and its assumed contextual type.
210206
bool diagnoseCallContextualConversionErrors(ApplyExpr *callEpxr,

lib/Sema/CodeSynthesis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ createDesignatedInitOverride(ClassDecl *classDecl,
642642
// Create the initializer parameter patterns.
643643
OptionSet<ParameterList::CloneFlags> options
644644
= (ParameterList::Implicit |
645-
ParameterList::Inherited);
645+
ParameterList::Inherited |
646+
ParameterList::NamedArguments);
646647
auto *superclassParams = superclassCtor->getParameters();
647648
auto *bodyParams = superclassParams->clone(ctx, options);
648649

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class FindCapturedVars : public ASTWalker {
4242
OpaqueValueExpr *OpaqueValue = nullptr;
4343
SourceLoc CaptureLoc;
4444
DeclContext *CurDC;
45-
bool NoEscape, ObjC, IsGenericFunction;
45+
bool NoEscape, ObjC;
46+
bool HasGenericParamCaptures;
4647

4748
public:
4849
FindCapturedVars(SourceLoc CaptureLoc,
@@ -51,23 +52,23 @@ class FindCapturedVars : public ASTWalker {
5152
bool ObjC,
5253
bool IsGenericFunction)
5354
: Context(CurDC->getASTContext()), CaptureLoc(CaptureLoc), CurDC(CurDC),
54-
NoEscape(NoEscape), ObjC(ObjC), IsGenericFunction(IsGenericFunction) {}
55+
NoEscape(NoEscape), ObjC(ObjC), HasGenericParamCaptures(IsGenericFunction) {}
5556

5657
CaptureInfo getCaptureInfo() const {
5758
DynamicSelfType *dynamicSelfToRecord = nullptr;
58-
bool hasGenericParamCaptures = IsGenericFunction;
5959

6060
// Only local functions capture dynamic 'Self'.
6161
if (CurDC->getParent()->isLocalContext()) {
62-
if (GenericParamCaptureLoc.isValid())
63-
hasGenericParamCaptures = true;
64-
6562
if (DynamicSelfCaptureLoc.isValid())
6663
dynamicSelfToRecord = DynamicSelf;
6764
}
6865

6966
return CaptureInfo(Context, Captures, dynamicSelfToRecord, OpaqueValue,
70-
hasGenericParamCaptures);
67+
HasGenericParamCaptures);
68+
}
69+
70+
bool hasGenericParamCaptures() const {
71+
return HasGenericParamCaptures;
7172
}
7273

7374
SourceLoc getGenericParamCaptureLoc() const {
@@ -148,17 +149,19 @@ class FindCapturedVars : public ASTWalker {
148149
if ((t->is<ArchetypeType>() ||
149150
t->is<GenericTypeParamType>()) &&
150151
!t->isOpenedExistential() &&
151-
GenericParamCaptureLoc.isInvalid()) {
152+
!HasGenericParamCaptures) {
152153
GenericParamCaptureLoc = loc;
154+
HasGenericParamCaptures = true;
153155
}
154156
}));
155157
}
156158

157159
if (auto *gft = type->getAs<GenericFunctionType>()) {
158160
TypeCaptureWalker walker(ObjC, [&](Type t) {
159161
if (t->is<GenericTypeParamType>() &&
160-
GenericParamCaptureLoc.isInvalid()) {
162+
!HasGenericParamCaptures) {
161163
GenericParamCaptureLoc = loc;
164+
HasGenericParamCaptures = true;
162165
}
163166
});
164167

@@ -339,9 +342,12 @@ class FindCapturedVars : public ASTWalker {
339342
addCapture(CapturedValue(capture.getDecl(), Flags, capture.getLoc()));
340343
}
341344

342-
if (GenericParamCaptureLoc.isInvalid())
343-
if (captureInfo.hasGenericParamCaptures())
345+
if (!HasGenericParamCaptures) {
346+
if (captureInfo.hasGenericParamCaptures()) {
344347
GenericParamCaptureLoc = loc;
348+
HasGenericParamCaptures = true;
349+
}
350+
}
345351

346352
if (DynamicSelfCaptureLoc.isInvalid()) {
347353
if (captureInfo.hasDynamicSelfCapture()) {
@@ -631,7 +637,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
631637

632638
// Extensions of generic ObjC functions can't use generic parameters from
633639
// their context.
634-
if (AFD && finder.getGenericParamCaptureLoc().isValid()) {
640+
if (AFD && finder.hasGenericParamCaptures()) {
635641
if (auto Clas = AFD->getParent()->getSelfClassDecl()) {
636642
if (Clas->usesObjCGenericsModel()) {
637643
AFD->diagnose(diag::objc_generic_extension_using_type_parameter);

test/SILGen/functions_uninhabited_param.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ func foo(baz: Never) -> Int { // expected-note {{'baz' is uninhabited, so this f
88
}
99

1010
func bar(baz: Never) -> Int {} // ok
11+
12+
// We used to crash when emitting the closure below.
13+
enum E {
14+
static func f(_: E) {}
15+
}
16+
17+
let _: (E.Type) -> (E) -> () = { s in { e in s.f(e) } }

0 commit comments

Comments
 (0)