Skip to content

[Type checker] Delete unnecessary, bogus optimization for initializers. #9483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,10 @@ class alignas(1 << DeclAlignInBits) Decl {
/// to this declaration.
unsigned AddedImplicitInitializers : 1;

/// \brief Whether or not this declaration has a failable initializer member,
/// and whether or not we've actually searched for one.
unsigned HasFailableInits : 1;

/// Whether we have already searched for failable initializers.
unsigned SearchedForFailableInits : 1;

/// Whether there is are lazily-loaded conformances for this nominal type.
unsigned HasLazyConformances : 1;
};
enum { NumNominalTypeDeclBits = NumGenericTypeDeclBits + 5 };
enum { NumNominalTypeDeclBits = NumGenericTypeDeclBits + 3 };
static_assert(NumNominalTypeDeclBits <= 32, "fits in an unsigned");

class ProtocolDeclBitfields {
Expand Down Expand Up @@ -2744,8 +2737,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
NominalTypeDeclBits.HasDelayedMembers = false;
NominalTypeDeclBits.AddedImplicitInitializers = false;
ExtensionGeneration = 0;
NominalTypeDeclBits.SearchedForFailableInits = false;
NominalTypeDeclBits.HasFailableInits = false;
NominalTypeDeclBits.HasLazyConformances = false;
}

Expand Down Expand Up @@ -2795,19 +2786,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
NominalTypeDeclBits.AddedImplicitInitializers = true;
}

bool getHasFailableInits() const {
return NominalTypeDeclBits.HasFailableInits;
}
void setHasFailableInits(bool failable = true) {
NominalTypeDeclBits.HasFailableInits = failable;
}
void setSearchedForFailableInits(bool searched = true) {
NominalTypeDeclBits.SearchedForFailableInits = searched;
}
bool getSearchedForFailableInits() const {
return NominalTypeDeclBits.SearchedForFailableInits;
}

/// Compute the type of this nominal type.
void computeType();

Expand Down
69 changes: 0 additions & 69 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,56 +678,6 @@ namespace {
}
}

/// Determine whether or not a given NominalTypeDecl has a failable
/// initializer member.
bool hasFailableInits(NominalTypeDecl *NTD,
ConstraintSystem *CS) {

// TODO: Note that we search manually, rather than invoking lookupMember
// on the ConstraintSystem object. Because this is a hot path, this keeps
// the overhead of the check low, and is twice as fast.
if (!NTD->getSearchedForFailableInits()) {
// Set flag before recursing to catch circularity.
NTD->setSearchedForFailableInits();

for (auto member : NTD->getMembers()) {
if (auto CD = dyn_cast<ConstructorDecl>(member)) {
if (CD->getFailability()) {
NTD->setHasFailableInits();
break;
}
}
}

if (!NTD->getHasFailableInits()) {
for (auto extension : NTD->getExtensions()) {
for (auto member : extension->getMembers()) {
if (auto CD = dyn_cast<ConstructorDecl>(member)) {
if (CD->getFailability()) {
NTD->setHasFailableInits();
break;
}
}
}
}

if (!NTD->getHasFailableInits()) {
for (auto parentTyLoc : NTD->getInherited()) {
if (auto nominalType =
parentTyLoc.getType()->getAs<NominalType>()) {
if (hasFailableInits(nominalType->getDecl(), CS)) {
NTD->setHasFailableInits();
break;
}
}
}
}
}
}

return NTD->getHasFailableInits();
}

size_t getOperandCount(Type t) {
size_t nOperands = 0;

Expand Down Expand Up @@ -2385,25 +2335,6 @@ namespace {
if (auto fnType = CS.getType(fnExpr)->getAs<AnyFunctionType>()) {
outputTy = fnType->getResult();
}
} else if (auto TE = dyn_cast<TypeExpr>(fnExpr)) {
outputTy = CS.getInstanceType(TE);
NominalTypeDecl *NTD = nullptr;

if (auto nominalType = outputTy->getAs<NominalType>()) {
NTD = nominalType->getDecl();
} else if (auto bgT = outputTy->getAs<BoundGenericType>()) {
NTD = bgT->getDecl();
}

if (NTD) {
if (!(isa<ClassDecl>(NTD) || isa<StructDecl>(NTD)) ||
hasFailableInits(NTD, &CS)) {
outputTy = Type();
}
} else {
outputTy = Type();
}

} else if (auto OSR = dyn_cast<OverloadSetRefExpr>(fnExpr)) {
if (auto FD = dyn_cast<FuncDecl>(OSR->getDecls()[0])) {

Expand Down
13 changes: 13 additions & 0 deletions test/Constraints/construction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,16 @@ Int(i32 - 2 + 1) // expected-warning{{unused}}
let xx: UInt64 = 100
let yy = ((xx + 10) - 5) / 5
let zy = (xx + (10 - 5)) / 5

// rdar://problem/30588177
struct S3 {
init() { }
}

let s3a = S3()

extension S3 {
init?(maybe: S3) { return nil }
}

let s3b = S3(maybe: s3a)