Skip to content

Commit e8bc662

Browse files
committed
Cleanups from Slava's comments
update module format
1 parent 6d00a57 commit e8bc662

File tree

3 files changed

+51
-59
lines changed

3 files changed

+51
-59
lines changed

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 477; // SILUndef serialized with ownership kind
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 478; // stored property default arg
5656

5757
using DeclIDField = BCFixed<31>;
5858

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3364,16 +3364,8 @@ void DelayedArgument::emitDefaultArgument(SILGenFunction &SGF,
33643364
var->getDeclContext()->getGenericSignatureOfContext();
33653365

33663366
if (genericEnv && typeGenericSig) {
3367-
subs = SubstitutionMap::get(
3368-
typeGenericSig,
3369-
[&](SubstitutableType *type) {
3370-
if (auto gp = type->getAs<GenericTypeParamType>()) {
3371-
return genericEnv->mapTypeIntoContext(gp);
3372-
}
3373-
3374-
return Type(type);
3375-
},
3376-
MakeAbstractConformanceForGenericType());
3367+
// Get the substitutions from the constructor's generic env.
3368+
subs = genericEnv->getForwardingSubstitutionMap();
33773369
}
33783370

33793371
value = SGF.emitApplyOfStoredPropertyInitializer(info.loc,

lib/Sema/CodeSynthesis.cpp

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,40 @@ void synthesizeAccessorBody(AbstractFunctionDecl *fn, void *) {
16431643
llvm_unreachable("bad synthesized function kind");
16441644
}
16451645

1646+
static void maybeAddMemberwiseDefaultArg(ParamDecl *arg, VarDecl *var,
1647+
SmallVectorImpl<DefaultArgumentInitializer *> &defaultInits,
1648+
unsigned paramSize, ASTContext &ctx) {
1649+
// First and foremost, if this is a constant don't bother.
1650+
if (var->isLet())
1651+
return;
1652+
1653+
// We can only provide default values for patterns binding a single variable.
1654+
// i.e. var (a, b) = getSomeTuple() is not allowed.
1655+
if (!var->getParentPattern()->getSingleVar())
1656+
return;
1657+
1658+
// If we don't have an initializer and we can't assign a default initializer
1659+
// in silgen, then we can't generate a default value. An example of a default
1660+
// initializer would be var x: Int? where the default is nil.
1661+
if (!var->getParentInitializer() &&
1662+
!var->getParentPatternBinding()->isDefaultInitializable())
1663+
return;
1664+
1665+
// We can add a default value now.
1666+
1667+
// Give this some bogus context right now, we'll fix it after making
1668+
// the constructor.
1669+
auto *initDC = new (ctx) DefaultArgumentInitializer(
1670+
arg->getDeclContext(), paramSize);
1671+
1672+
defaultInits.push_back(initDC);
1673+
1674+
// Set the default value to the variable. When we emit this in silgen
1675+
// we're going to call the variable's initializer expression.
1676+
arg->setStoredProperty(var);
1677+
arg->setDefaultArgumentKind(DefaultArgumentKind::StoredProperty);
1678+
}
1679+
16461680
/// Create an implicit struct or class constructor.
16471681
///
16481682
/// \param decl The struct or class for which a constructor will be created.
@@ -1655,7 +1689,7 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
16551689
ImplicitConstructorKind ICK) {
16561690
assert(!decl->hasClangNode());
16571691

1658-
ASTContext &C = tc.Context;
1692+
ASTContext &ctx = tc.Context;
16591693
SourceLoc Loc = decl->getLoc();
16601694
auto accessLevel = AccessLevel::Internal;
16611695

@@ -1700,59 +1734,24 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
17001734
varInterfaceType = OptionalType::get(varInterfaceType);
17011735

17021736
// Create the parameter.
1703-
auto *arg = new (C)
1737+
auto *arg = new (ctx)
17041738
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), Loc,
17051739
var->getName(), Loc, var->getName(), decl);
17061740
arg->setInterfaceType(varInterfaceType);
17071741
arg->setImplicit();
17081742

1709-
// If this is a var that has a default value, and is only binding one var,
1710-
// i.e, var (a, b) = (3, 3) is not allowed, lets assign a default value
1711-
// to the parameter with the same expression.
1712-
if (!var->isLet() && var->getParentPattern()->getSingleVar()) {
1713-
if (auto init = var->getParentInitializer()) {
1714-
// Give this some bogus context right now, we'll fix it after making
1715-
// the constructor.
1716-
auto *initDC = new (C) DefaultArgumentInitializer(
1717-
arg->getDeclContext(), params.size());
1718-
1719-
defaultInits.push_back(initDC);
1720-
1721-
// Set the default value to the variable. When we emit this in silgen
1722-
// we're going to call the variable's initializer expression.
1723-
arg->setStoredProperty(var);
1724-
arg->setDefaultArgumentKind(DefaultArgumentKind::StoredProperty);
1725-
}
1726-
}
1727-
1728-
// Now that we have default values for this synthesized constructor,
1729-
// if the property is an optional and does not have an initializer,
1730-
// and is not a let property because it can never be reassigned,
1731-
// assign nil as the default value.
1732-
if (var->getType()->getOptionalObjectType() &&
1733-
var->getParentPatternBinding()->isDefaultInitializable() &&
1734-
!var->isLet() &&
1735-
!var->getParentInitializer()) {
1736-
auto *initDC = new (C) DefaultArgumentInitializer(
1737-
arg->getDeclContext(), params.size());
1738-
1739-
defaultInits.push_back(initDC);
1740-
1741-
auto nil = new (C) NilLiteralExpr(SourceLoc(), /*implicit*/ true);
1742-
arg->setDefaultValue(nil);
1743-
arg->setDefaultArgumentKind(DefaultArgumentKind::NilLiteral);
1744-
}
1743+
maybeAddMemberwiseDefaultArg(arg, var, defaultInits, params.size(), ctx);
17451744

17461745
params.push_back(arg);
17471746
}
17481747
}
17491748

1750-
auto paramList = ParameterList::create(C, params);
1749+
auto paramList = ParameterList::create(ctx, params);
17511750

17521751
// Create the constructor.
1753-
DeclName name(C, DeclBaseName::createConstructor(), paramList);
1752+
DeclName name(ctx, DeclBaseName::createConstructor(), paramList);
17541753
auto *ctor =
1755-
new (C) ConstructorDecl(name, Loc,
1754+
new (ctx) ConstructorDecl(name, Loc,
17561755
OTK_None, /*FailabilityLoc=*/SourceLoc(),
17571756
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
17581757
paramList, /*GenericParams=*/nullptr, decl);
@@ -1761,20 +1760,21 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
17611760
ctor->setImplicit();
17621761
ctor->setAccess(accessLevel);
17631762

1764-
// Fix default argument init contexts now that we have a constructor
1765-
for (auto initDC : defaultInits) {
1766-
initDC->changeFunction(ctor, paramList);
1767-
}
1768-
1769-
if (ICK == ImplicitConstructorKind::Memberwise)
1763+
if (ICK == ImplicitConstructorKind::Memberwise) {
17701764
ctor->setIsMemberwiseInitializer();
17711765

1766+
// Fix default argument init contexts now that we have a constructor.
1767+
for (auto initDC : defaultInits) {
1768+
initDC->changeFunction(ctor, paramList);
1769+
}
1770+
}
1771+
17721772
// If we are defining a default initializer for a class that has a superclass,
17731773
// it overrides the default initializer of its superclass. Add an implicit
17741774
// 'override' attribute.
17751775
if (auto classDecl = dyn_cast<ClassDecl>(decl)) {
17761776
if (classDecl->getSuperclass())
1777-
ctor->getAttrs().add(new (C) OverrideAttr(/*IsImplicit=*/true));
1777+
ctor->getAttrs().add(new (ctx) OverrideAttr(/*IsImplicit=*/true));
17781778
}
17791779

17801780
return ctor;

0 commit comments

Comments
 (0)