Sema: Fix bug where lazy properties could become stored sometimes

If synthesizeWitnessAccessorsForStorage() got called on a lazy
property before maybeAddAccessorsToVariable(), we would build
it as a stored property instead.

This count result in bogus diagnostics, assertions and bad runtime
behavior.

Fixes <https://bugs.swift.org/browse/SR-1825>.
This commit is contained in:
Slava Pestov
2017-05-24 21:30:09 -07:00
parent e97123f9f7
commit 345a8b7eea
3 changed files with 28 additions and 7 deletions

View File

@@ -807,6 +807,9 @@ static bool doesStorageNeedSetter(AbstractStorageDecl *storage) {
static void addTrivialAccessorsToStorage(AbstractStorageDecl *storage,
TypeChecker &TC) {
assert(!storage->hasAccessorFunctions() && "already has accessors?");
assert(!storage->getAttrs().hasAttribute<LazyAttr>());
assert(!storage->getAttrs().hasAttribute<NSManagedAttr>());
auto *DC = storage->getDeclContext();
// Create the getter.
@@ -922,10 +925,7 @@ void TypeChecker::synthesizeWitnessAccessorsForStorage(
// If the decl is stored, convert it to StoredWithTrivialAccessors
// by synthesizing the full set of accessors.
if (!storage->hasAccessorFunctions()) {
if (storage->getAttrs().hasAttribute<NSManagedAttr>())
convertNSManagedStoredVarToComputed(cast<VarDecl>(storage), *this);
else
addTrivialAccessorsToStorage(storage, *this);
addTrivialAccessorsToStorage(storage, *this);
if (auto getter = storage->getGetter())
validateDecl(getter);