Skip to content

Commit f6f1be5

Browse files
authored
Merge pull request #59303 from gottesmm/pr-cd7072441becdbf718aaf324a1d402ad5d6803b4
[move-only] Two small fixes
2 parents 9507315 + 7690329 commit f6f1be5

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

include/swift/SIL/SILType.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ enum class SILValueCategory : uint8_t {
8686
Address,
8787
};
8888

89+
class SILPrinter;
90+
8991
/// SILType - A Swift type that has been lowered to a SIL representation type.
9092
/// In addition to the Swift type system, SIL adds "address" types that can
9193
/// reference any Swift type (but cannot take the address of an address). *T
@@ -113,6 +115,8 @@ class SILType {
113115

114116
friend class Lowering::TypeConverter;
115117
friend struct llvm::DenseMapInfo<SILType>;
118+
friend class SILPrinter;
119+
116120
public:
117121
SILType() = default;
118122

@@ -184,8 +188,11 @@ class SILType {
184188
/// type. This is done under the assumption that in all cases where we are
185189
/// performing these AST queries on SILType, we are not interested in the
186190
/// move only-ness of the value (which we can query separately anyways).
187-
CanType getASTType() const { return withoutMoveOnly().getRawASTType(); }
191+
CanType getASTType() const {
192+
return removingMoveOnlyWrapper().getRawASTType();
193+
}
188194

195+
private:
189196
/// Returns the canonical AST type references by this SIL type without looking
190197
/// through move only. Should only be used by internal utilities of SILType.
191198
CanType getRawASTType() const { return CanType(value.getPointer()); }
@@ -597,33 +604,34 @@ class SILType {
597604
///
598605
/// Canonical way to check if a SILType is move only. Using is/getAs/castTo
599606
/// will look through moveonly-ness.
600-
bool isMoveOnly() const { return getRawASTType()->is<SILMoveOnlyType>(); }
607+
bool isMoveOnlyWrapped() const {
608+
return getRawASTType()->is<SILMoveOnlyType>();
609+
}
601610

602-
/// Return *this if already move only... otherwise, wrap the current type
603-
/// within a move only type wrapper and return that. Idempotent!
604-
SILType asMoveOnly() const {
605-
if (isMoveOnly())
611+
/// If this is already a move only wrapped type, return *this. Otherwise, wrap
612+
/// the copyable type in the mov eonly wrapper.
613+
SILType addingMoveOnlyWrapper() const {
614+
if (isMoveOnlyWrapped())
606615
return *this;
607616
auto newType = SILMoveOnlyType::get(getRawASTType());
608617
return SILType::getPrimitiveType(newType, getCategory());
609618
}
610619

611-
/// Return this SILType, removing moveonly-ness.
612-
///
613-
/// Is idempotent.
614-
SILType withoutMoveOnly() const {
615-
if (!isMoveOnly())
620+
/// If this is already a copyable type, just return *this. Otherwise, if this
621+
/// is a move only wrapped copyable type, return the inner type.
622+
SILType removingMoveOnlyWrapper() const {
623+
if (!isMoveOnlyWrapped())
616624
return *this;
617625
auto moveOnly = getRawASTType()->castTo<SILMoveOnlyType>();
618626
return SILType::getPrimitiveType(moveOnly->getInnerType(), getCategory());
619627
}
620628

621-
/// If \p otherType is move only, return this type that is move only as
622-
/// well. Otherwise, returns self. Useful for propagating "move only"-ness
629+
/// If \p otherType is move only wrapped, return this type that is move only
630+
/// as well. Otherwise, returns self. Useful for propagating "move only"-ness
623631
/// from a parent type to a subtype.
624-
SILType copyMoveOnly(SILType otherType) const {
625-
if (otherType.isMoveOnly()) {
626-
return asMoveOnly();
632+
SILType copyingMoveOnlyWrapper(SILType otherType) const {
633+
if (otherType.isMoveOnlyWrapped()) {
634+
return addingMoveOnlyWrapper();
627635
}
628636
return *this;
629637
}

lib/SIL/IR/SILPrinter.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,6 @@ static void printSILFunctionNameAndType(llvm::raw_ostream &OS,
532532
}
533533

534534
namespace {
535-
536-
class SILPrinter;
537535

538536
// 1. Accumulate opcode-specific comments in this stream.
539537
// 2. Start emitting comments: lineComments.start()
@@ -598,6 +596,10 @@ class LineComments : public raw_ostream {
598596
}
599597
};
600598

599+
} // namespace
600+
601+
namespace swift {
602+
601603
/// SILPrinter class - This holds the internal implementation details of
602604
/// printing SIL structures.
603605
class SILPrinter : public SILInstructionVisitor<SILPrinter> {
@@ -2740,7 +2742,8 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
27402742
}
27412743
}
27422744
};
2743-
} // end anonymous namespace
2745+
2746+
} // namespace swift
27442747

27452748
static void printBlockID(raw_ostream &OS, SILBasicBlock *bb) {
27462749
SILPrintContext Ctx(OS);

lib/SIL/IR/SILType.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ SILType SILType::getOptionalType(SILType type) {
9292
auto optType = BoundGenericEnumType::get(ctx.getOptionalDecl(), Type(),
9393
{ type.getASTType() });
9494
return getPrimitiveType(CanType(optType), type.getCategory())
95-
.copyMoveOnly(type);
95+
.copyingMoveOnlyWrapper(type);
9696
}
9797

9898
SILType SILType::getEmptyTupleType(const ASTContext &C) {
@@ -296,7 +296,7 @@ SILType SILType::getFieldType(VarDecl *field, TypeConverter &TC,
296296

297297
// If this type is not a class type, then we propagate "move only"-ness to the
298298
// field. Example:
299-
if (!getClassOrBoundGenericClass() && isMoveOnly())
299+
if (!getClassOrBoundGenericClass() && isMoveOnlyWrapped())
300300
loweredTy = SILMoveOnlyType::get(loweredTy);
301301

302302
if (isAddress() || getClassOrBoundGenericClass() != nullptr) {
@@ -318,7 +318,7 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, TypeConverter &TC,
318318

319319
if (auto objectType = getASTType().getOptionalObjectType()) {
320320
assert(elt == TC.Context.getOptionalSomeDecl());
321-
return SILType(objectType, getCategory()).copyMoveOnly(*this);
321+
return SILType(objectType, getCategory()).copyingMoveOnlyWrapper(*this);
322322
}
323323

324324
// If the case is indirect, then the payload is boxed.
@@ -333,7 +333,7 @@ SILType SILType::getEnumElementType(EnumElementDecl *elt, TypeConverter &TC,
333333
auto loweredTy = TC.getLoweredRValueType(
334334
context, TC.getAbstractionPattern(elt), substEltTy);
335335

336-
return SILType(loweredTy, getCategory()).copyMoveOnly(*this);
336+
return SILType(loweredTy, getCategory()).copyingMoveOnlyWrapper(*this);
337337
}
338338

339339
SILType SILType::getEnumElementType(EnumElementDecl *elt, SILModule &M,
@@ -438,15 +438,15 @@ bool SILType::aggregateHasUnreferenceableStorage() const {
438438

439439
SILType SILType::getOptionalObjectType() const {
440440
if (auto objectTy = getASTType().getOptionalObjectType()) {
441-
return SILType(objectTy, getCategory()).copyMoveOnly(*this);
441+
return SILType(objectTy, getCategory()).copyingMoveOnlyWrapper(*this);
442442
}
443443

444444
return SILType();
445445
}
446446

447447
SILType SILType::unwrapOptionalType() const {
448-
if (auto objectTy = withoutMoveOnly().getOptionalObjectType()) {
449-
return objectTy.copyMoveOnly(*this);
448+
if (auto objectTy = removingMoveOnlyWrapper().getOptionalObjectType()) {
449+
return objectTy.copyingMoveOnlyWrapper(*this);
450450
}
451451

452452
return *this;

0 commit comments

Comments
 (0)