Skip to content

Commit 0df8c63

Browse files
committed
try the approach we use for vars
1 parent 78c1307 commit 0df8c63

File tree

5 files changed

+31
-115
lines changed

5 files changed

+31
-115
lines changed

lib/SILGen/SILGenDecl.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
#include "llvm/Support/ErrorHandling.h"
4141
#include <iterator>
4242

43-
#define DEBUG_TYPE "SILGenDecl"
44-
4543
using namespace swift;
4644
using namespace Lowering;
4745

@@ -810,12 +808,6 @@ class LetValueInitialization : public Initialization {
810808
lowering->getLoweredType().isMoveOnly(/*orWrapped=*/false);
811809
}
812810

813-
// auto parentInit = vd->getParentInitializer();
814-
// if (isa<CallExpr>(parentInit)) {
815-
// auto fn = cast<CallExpr>(parentInit)->getSemanticFn();
816-
// isUninitialized = isa<AbstractClosureExpr>(fn);
817-
// }
818-
819811
// Make sure that we have a non-address only type when binding a
820812
// @_noImplicitCopy let.
821813
if (lowering->isAddressOnly() && vd->isNoImplicitCopy()) {
@@ -824,10 +816,6 @@ class LetValueInitialization : public Initialization {
824816
}
825817

826818
if (needsTemporaryBuffer) {
827-
LLVM_DEBUG({
828-
llvm::dbgs() << "JQ: need tmp buffer\n";
829-
vd->dump(llvm::dbgs());
830-
});
831819
bool lexicalLifetimesEnabled =
832820
SGF.getASTContext().SILOpts.supportsLexicalLifetimes(SGF.getModule());
833821
auto lifetime = SGF.F.getLifetime(vd, lowering->getLoweredType());
@@ -837,14 +825,13 @@ class LetValueInitialization : public Initialization {
837825
DoesNotHaveDynamicLifetime,
838826
isLexical, IsFromVarDecl);
839827

840-
// Ensure DI always checks this to avoid cases where an address-only
841-
// value is referenced in a closure that is part of its initializer.
842-
if (true || isUninitialized)
828+
if (isUninitialized)
843829
address = SGF.B.createMarkUninitializedVar(vd, address);
844830

845831
DestroyCleanup = SGF.enterDormantTemporaryCleanup(address, *lowering);
846-
SGF.VarLocs[vd] = SILGenFunction::VarLoc(address,
847-
SILAccessEnforcement::Unknown);
832+
833+
// We do not register the address in VarLocs yet so that the capture-
834+
// before-definition machinery will diagnose these cases.
848835
}
849836
// Push a cleanup to destroy the let declaration. This has to be
850837
// inactive until the variable is initialized: if control flow exits the
@@ -1022,7 +1009,16 @@ class LetValueInitialization : public Initialization {
10221009
void finishInitialization(SILGenFunction &SGF) override {
10231010
assert(!DidFinish &&
10241011
"called LetValueInit::finishInitialization twice!");
1025-
assert(SGF.VarLocs.count(vd) && "Didn't bind a value to this let!");
1012+
1013+
if (!address) {
1014+
// We should have already in the non-address case.
1015+
assert(SGF.VarLocs.count(vd) && "Didn't bind a value to this let!");
1016+
} else {
1017+
// We delayed the emission to catch forward declaration uses.
1018+
assert(SGF.VarLocs.count(vd) == 0 && "Should not have emitted the address-only let!");
1019+
SGF.VarLocs[vd] = SILGenFunction::VarLoc(address,
1020+
SILAccessEnforcement::Unknown);
1021+
}
10261022

10271023
// Deactivate any cleanups we made when splitting the tuple.
10281024
for (auto cleanup : SplitCleanups)

lib/SILGen/SILGenFunction.cpp

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
590590
break;
591591
}
592592

593-
LLVM_DEBUG({
594-
llvm::dbgs() << "JQ: emit captures running\n";
595-
for (auto capture : captureInfo.getCaptures()) {
596-
llvm::dbgs() << " cap: '" << capture.getDecl()->getName() << "'\n";
597-
}
598-
llvm::dbgs()
599-
<< " canGuarantee: '" << canGuarantee << "'\n"
600-
<< " capCanEscape: '" << captureCanEscape << "'\n";
601-
});
602-
603593
auto expansion = getTypeExpansionContext();
604594

605595
for (auto capture : captureInfo.getCaptures()) {
@@ -619,7 +609,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
619609
if (capture.isOpaqueValue() || capture.isPackElement()) {
620610
capturedArgs.push_back(
621611
emitRValueAsSingleValue(capture.getExpr()).ensurePlusOne(*this, loc));
622-
LLVM_DEBUG(llvm::dbgs() << "JQ: cap emit early ret: opaque || pack\n");
623612
continue;
624613
}
625614

@@ -643,52 +632,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
643632
auto valueType = FunctionDC->mapTypeIntoContext(
644633
interfaceType->getReferenceStorageReferent());
645634

646-
LLVM_DEBUG(
647-
{
648-
llvm::dbgs() << "=== DEBUG: VarLocs contents for capture of '"
649-
<< vd->getBaseIdentifier() << "' ===\n";
650-
llvm::dbgs() << "Total entries in VarLocs: " << VarLocs.size() << "\n";
651-
652-
for (auto &entry : VarLocs) {
653-
auto *var = entry.first;
654-
auto &loc = entry.second;
655-
656-
llvm::dbgs() << " - Variable: " << var->getBaseIdentifier() << "\n";
657-
// llvm::errs() << " Type: " << var->getType() << "\n";
658-
llvm::dbgs() << " Value type: " << loc.value->getType() << "\n";
659-
llvm::dbgs() << " Value kind: ";
660-
661-
if (isa<SILUndef>(loc.value)) {
662-
llvm::dbgs() << "SILUndef (UNINITIALIZED)\n";
663-
} else if (isa<SILArgument>(loc.value)) {
664-
llvm::dbgs() << "SILArgument\n";
665-
} else if (isa<AllocStackInst>(loc.value)) {
666-
llvm::dbgs() << "AllocStackInst\n";
667-
} else if (isa<AllocBoxInst>(loc.value)) {
668-
llvm::dbgs() << "AllocBoxInst\n";
669-
} else {
670-
llvm::dbgs() << "some other inst\n";
671-
}
672-
673-
if (loc.box) {
674-
llvm::dbgs() << " Has box: yes\n";
675-
}
676-
677-
llvm::dbgs() << "\n";
678-
}
679-
680-
llvm::dbgs() << "Looking for variable: " << vd->getBaseIdentifier() << "\n";
681-
auto found = VarLocs.find(vd);
682-
if (found == VarLocs.end()) {
683-
llvm::dbgs() << " Result: NOT FOUND in VarLocs\n";
684-
} else {
685-
llvm::dbgs() << " Result: FOUND in VarLocs\n";
686-
llvm::dbgs() << " Value is undef: "
687-
<< (isa<SILUndef>(found->second.value) ? "YES" : "NO") << "\n";
688-
}
689-
llvm::dbgs() << "===================================\n\n";
690-
});
691-
692635
//
693636
// If we haven't emitted the captured value yet, we're forming a closure
694637
// to a local function before all of its captures have been emitted. Eg,
@@ -752,12 +695,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
752695
// expansion context without opaque archetype substitution.
753696
auto getAddressValue = [&](SILValue entryValue, bool forceCopy,
754697
bool forLValue) -> SILValue {
755-
LLVM_DEBUG({
756-
llvm::dbgs() << "JQ: get addr value, force copy: " << forceCopy
757-
<< ", for lval: " << forLValue << "\n";
758-
entryValue->getDefiningInstruction()->print(llvm::dbgs());
759-
});
760-
761698
if (!SGM.M.useLoweredAddresses() && !forLValue && !isPack) {
762699
// In opaque values mode, addresses aren't used except by lvalues.
763700
auto &lowering = getTypeLowering(entryValue->getType());
@@ -838,11 +775,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
838775
auto &Entry = found->second;
839776
auto val = Entry.value;
840777

841-
LLVM_DEBUG({
842-
auto capKind = SGM.Types.getDeclCaptureKind(capture, expansion);
843-
llvm::dbgs() << "JQ: cap kind:: " << (unsigned)capKind << "\n";
844-
});
845-
846778
switch (SGM.Types.getDeclCaptureKind(capture, expansion)) {
847779
case CaptureKind::Constant: {
848780
assert(!isPack);
@@ -923,12 +855,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
923855
}
924856
capturedArgs.push_back(ManagedValue::forOwnedAddressRValue(
925857
addr, CleanupHandle::invalid()));
926-
927-
// LLVM_DEBUG({
928-
// llvm::dbgs() << "JQ: adding escape to mark for: \n";
929-
// val->getDefiningInstruction()->print(llvm::dbgs());
930-
// });
931-
// escapesToMark.push_back(val);
932858
}
933859
break;
934860
}

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -823,27 +823,6 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
823823
continue;
824824
}
825825

826-
if (false || (isa<UnconditionalCheckedCastAddrInst>(User) ||
827-
isa<CheckedCastAddrBranchInst>(User))) {
828-
LLVM_DEBUG({
829-
llvm::dbgs() << "JQ: DI found unchecked addr cast:\n"
830-
<< Op;
831-
});
832-
833-
DIUseKind Kind;
834-
Kind = DIUseKind::Initialization;
835-
// if (Op->getOperandNumber() == 0)
836-
// Kind = DIUseKind::Load;
837-
// else
838-
// Kind = InStructSubElement ? DIUseKind::PartialStore
839-
// : DIUseKind::Initialization;
840-
841-
// TODO: what about enum sub elt? do we need to even special case things here?
842-
843-
addElementUses(BaseEltNo, PointeeType, User, Kind);
844-
continue;
845-
}
846-
847826
// Look through mark_unresolved_non_copyable_value. To us, it is not
848827
// interesting.
849828
if (auto *mmi = dyn_cast<MarkUnresolvedNonCopyableValueInst>(User)) {

test/SILGen/capture_order.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,19 @@ func func77933460() {
192192
// expected-note@-3 {{captured value declared here}}
193193
// expected-warning@-4 {{variable 'obj' was never mutated; consider changing to 'let' constant}}
194194
}
195+
196+
// MARK: -
197+
198+
func address_only_lets() {
199+
let bad1 = { () -> Any in bad1 }()
200+
// expected-error@-1 {{closure captures 'bad1' before it is declared}}
201+
// expected-note@-2 {{captured here}}
202+
// expected-note@-3 {{captured value declared here}}
203+
204+
205+
func indirect(_ i: () -> Any) -> Any { i() }
206+
let bad2 = indirect { bad2 }
207+
// expected-error@-1 {{closure captures 'bad2' before it is declared}}
208+
// expected-note@-2 {{captured here}}
209+
// expected-note@-3 {{captured value declared here}}
210+
}

test/SILGen/consume_operator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ func testLoadableVar() {
4242

4343
// CHECK-LABEL: sil hidden [ossa] @$s7consume18testAddressOnlyLetyyxmAA1PRzlF : $@convention(thin) <T where T : P> (@thick T.Type) -> () {
4444
// CHECK: [[BOX:%.*]] = alloc_stack [lexical] [var_decl] $T
45-
// CHECK: [[MUI:%.*]] = mark_uninitialized [var] [[BOX]]
4645
//
4746
// CHECK: [[STACK:%.*]] = alloc_stack $T
48-
// CHECK: mark_unresolved_move_addr [[MUI]] to [[STACK]]
47+
// CHECK: mark_unresolved_move_addr [[BOX]] to [[STACK]]
4948
//
5049
// CHECK: } // end sil function '$s7consume18testAddressOnlyLetyyxmAA1PRzlF'
5150
func testAddressOnlyLet<T : P>(_ t: T.Type) {

0 commit comments

Comments
 (0)