Skip to content

Commit 159571c

Browse files
committed
[NFC] TypeLowering: Tweak verification.
Improved comments, and simplified assertion while making more stringent.
1 parent 32d5873 commit 159571c

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

lib/SIL/IR/TypeLowering.cpp

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,67 +3028,77 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
30283028
auto conformance = M.checkConformance(substType, bitwiseCopyableProtocol);
30293029

30303030
if (lowering.isTrivial() && !conformance) {
3031-
// A trivial type can only lack a conformance if one of its leaves is a
3032-
// public type that doesn't conform.
3031+
// A trivial type can lack a conformance in a few cases:
3032+
// (1) containing or being a resilient type
3033+
// (2) containing or being a generic type which doesn't conform
3034+
// unconditionally but in this particular instantiation is trivial
3035+
// (3) being a special type that's not worth forming a conformance for
3036+
// - ModuleType
3037+
// (4) being an unowned(unsafe) reference to a class/class-bound existential
3038+
// NOTE: ReferenceStorageType(C) does not conform HOWEVER the presence
3039+
// of an unowned(unsafe) field within an aggregate DOES NOT allow
3040+
// the aggregate to lack conformance. In other words, this must
3041+
// conform:
3042+
// struct S {
3043+
// unowned(unsafe) var o: AnyObject
3044+
// }
30333045
bool hasNoNonconformingNode = visitAggregateLeaves(
30343046
origType, substType, forExpansion,
3035-
/*isLeaf=*/
3047+
/*isLeafAggregate=*/
30363048
[&](auto ty, auto origTy, auto *field, auto index) -> bool {
3037-
// The field's type is an aggregate. Treat it as a leaf if it is
3038-
// public.
30393049
auto *nominal = ty.getAnyNominal();
3040-
// Non-nominal types are public iff their components are; visit the
3041-
// components.
3050+
// Non-nominal aggregates must not be responsible for non-conformance;
3051+
// walk into them.
30423052
if (!nominal)
30433053
return false;
30443054

3045-
// Nominals with generic parameters must be explicitly conformed to
3046-
// BitwiseCopyable.
3047-
auto *generic = ty.getAnyGeneric();
3048-
if (generic && generic->isGenericContext()) {
3055+
// Nominals with fields that are generic may not conform
3056+
// unconditionally (the only kind automatically derived currently)
3057+
// and so may lack a conformance (case (2)).
3058+
if (nominal->isGenericContext()) {
30493059
return true;
30503060
}
30513061

3052-
return nominal
3053-
->getFormalAccessScope(/*useDC=*/nullptr,
3054-
/*treatUsableFromInlineAsPublic=*/true)
3055-
.isPublic();
3062+
// Resilient trivial types may not conform (case (1)).
3063+
return nominal->isResilient();
30563064
},
30573065
/*visit=*/
30583066
[&](auto ty, auto origTy, auto *field, auto index) -> bool {
30593067
// Return false to indicate seeing a leaf which justifies the type
30603068
// being trivial but not conforming to BitwiseCopyable.
30613069

3070+
auto isTopLevel = !field;
3071+
30623072
// A BitwiseCopyable conformer appearing within its layout doesn't
30633073
// explain why substType doesn't itself conform.
30643074
if (M.checkConformance(ty, bitwiseCopyableProtocol))
30653075
return true;
30663076

3067-
// ModuleTypes are trivial but don't warrant being given a conformance
3068-
// to BitwiseCopyable.
3069-
if (auto mt = dyn_cast<ModuleType>(ty)) {
3070-
// If one were to appear within a type, its lack of conformance
3071-
// wouldn't result in the type's failure to conform. Conversely, if
3072-
// substType itself is the ModuleType, it is trivial and
3073-
// non-conformant; return false to indicate its the leaf which
3074-
// justifies substType being non-conformant.
3075-
return field;
3077+
// ModuleTypes are trivial but don't warrant being given a
3078+
// conformance to BitwiseCopyable (case (3)).
3079+
if (isa<ModuleType>(ty)) {
3080+
// These types should never appear within aggregates.
3081+
assert(isTopLevel && "aggregate containing marker type!?");
3082+
// If they did, though, they would not justify the aggregate's
3083+
// non-conformance.
3084+
// top-level -> justified to be trivial and non-conformant -> false
3085+
// leaf -> must not be responsible for non-conformance -> true
3086+
return !isTopLevel;
30763087
}
30773088

3078-
// Given a non-bitwise-copyable C, unowned(unsafe) C is still
3079-
// BitwiseCopyable.
3089+
// ReferenceStorageTypes with unmanaged ownership do not themselves
3090+
// conform (case (4)).
30803091
auto rst = dyn_cast<ReferenceStorageType>(ty);
30813092
if (rst && rst->getOwnership() == ReferenceOwnership::Unmanaged) {
3082-
// If a ReferenceStorageType appears as a component of an aggregate,
3083-
// that shouldn't stop the aggregate from being BitwiseCopyable. If
3084-
// the whole type is the ReferenceStorageType, however, it does not
3085-
// conform.
3086-
return field;
3093+
// top-level -> justified to be trivial and non-conformant -> false
3094+
// leaf -> must not be responsible for non-conformance -> true
3095+
return !isTopLevel;
30873096
}
30883097

30893098
auto *nominal = ty.getAnyNominal();
30903099

3091-
// Non-nominal types are trivial iff conforming.
3100+
// Non-nominal types (besides case (3) handled above) are trivial iff
3101+
// conforming.
30923102
if (!nominal) {
30933103
llvm::errs()
30943104
<< "Non-nominal type without conformance to _BitwiseCopyable:\n"
@@ -3099,20 +3109,14 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
30993109
return true;
31003110
}
31013111

3102-
/// A non-conforming generic nominal type justifies substType not
3103-
/// conforming.
3104-
auto *generic = ty.getAnyGeneric();
3105-
if (generic && generic->isGenericContext()) {
3112+
// A generic type may be trivial when instantiated with particular
3113+
// types but lack a conformance (case (3)).
3114+
if (nominal->isGenericContext()) {
31063115
return false;
31073116
}
31083117

3109-
// The field is trivial and the whole type is nonconforming. That's
3110-
// legal iff the type is public.
3111-
return !nominal
3112-
->getFormalAccessScope(
3113-
/*useDC=*/nullptr,
3114-
/*treatUsableFromInlineAsPublic=*/true)
3115-
.isPublic();
3118+
// Resilient trivial types may not conform (case (1)).
3119+
return !nominal->isResilient();
31163120
});
31173121
if (hasNoNonconformingNode) {
31183122
llvm::errs() << "Trivial type without a BitwiseCopyable conformance!?:\n"
@@ -3123,8 +3127,8 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
31233127
}
31243128

31253129
if (!lowering.isTrivial() && conformance) {
3126-
// A non-trivial type can only conform if at least one of its leaves is a
3127-
// type parameter that conforms.
3130+
// A non-trivial type can have a conformance in one case:
3131+
// (1) contains a conforming archetype
31283132
bool hasNoConformingArchetypeNode = visitAggregateLeaves(
31293133
origType, substType, forExpansion,
31303134
/*isLeaf=*/
@@ -3139,6 +3143,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
31393143
"leaf of non-trivial BitwiseCopyable type that doesn't "
31403144
"conform to BitwiseCopyable!?");
31413145

3146+
// An archetype may conform but be non-trivial (case (2)).
31423147
if (origTy.isTypeParameter())
31433148
return false;
31443149

@@ -3148,6 +3153,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
31483153
llvm::errs() << "Non-trivial type with _BitwiseCopyable conformance!?:\n"
31493154
<< substType << "\n";
31503155
conformance.print(llvm::errs());
3156+
llvm::errs() << "\n";
31513157
assert(false);
31523158
}
31533159
}

test/SILGen/bitwise_copyable.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ enum Context<T> {
3939
}
4040

4141
func doit() -> Context<Int>.Here { .init(t: 0) }
42+
43+
public enum E {
44+
case a
45+
}
46+
47+
func take<T : _BitwiseCopyable>(_ t: T) {}
48+
49+
func pass(_ e: E) { take(e) }

0 commit comments

Comments
 (0)