@@ -3028,67 +3028,77 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
3028
3028
auto conformance = M.checkConformance (substType, bitwiseCopyableProtocol);
3029
3029
3030
3030
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
+ // }
3033
3045
bool hasNoNonconformingNode = visitAggregateLeaves (
3034
3046
origType, substType, forExpansion,
3035
- /* isLeaf =*/
3047
+ /* isLeafAggregate =*/
3036
3048
[&](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.
3039
3049
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 .
3042
3052
if (!nominal)
3043
3053
return false ;
3044
3054
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 ()) {
3049
3059
return true ;
3050
3060
}
3051
3061
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 ();
3056
3064
},
3057
3065
/* visit=*/
3058
3066
[&](auto ty, auto origTy, auto *field, auto index) -> bool {
3059
3067
// Return false to indicate seeing a leaf which justifies the type
3060
3068
// being trivial but not conforming to BitwiseCopyable.
3061
3069
3070
+ auto isTopLevel = !field;
3071
+
3062
3072
// A BitwiseCopyable conformer appearing within its layout doesn't
3063
3073
// explain why substType doesn't itself conform.
3064
3074
if (M.checkConformance (ty, bitwiseCopyableProtocol))
3065
3075
return true ;
3066
3076
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;
3076
3087
}
3077
3088
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)) .
3080
3091
auto rst = dyn_cast<ReferenceStorageType>(ty);
3081
3092
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;
3087
3096
}
3088
3097
3089
3098
auto *nominal = ty.getAnyNominal ();
3090
3099
3091
- // Non-nominal types are trivial iff conforming.
3100
+ // Non-nominal types (besides case (3) handled above) are trivial iff
3101
+ // conforming.
3092
3102
if (!nominal) {
3093
3103
llvm::errs ()
3094
3104
<< " Non-nominal type without conformance to _BitwiseCopyable:\n "
@@ -3099,20 +3109,14 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
3099
3109
return true ;
3100
3110
}
3101
3111
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 ()) {
3106
3115
return false ;
3107
3116
}
3108
3117
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 ();
3116
3120
});
3117
3121
if (hasNoNonconformingNode) {
3118
3122
llvm::errs () << " Trivial type without a BitwiseCopyable conformance!?:\n "
@@ -3123,8 +3127,8 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
3123
3127
}
3124
3128
3125
3129
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
3128
3132
bool hasNoConformingArchetypeNode = visitAggregateLeaves (
3129
3133
origType, substType, forExpansion,
3130
3134
/* isLeaf=*/
@@ -3139,6 +3143,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
3139
3143
" leaf of non-trivial BitwiseCopyable type that doesn't "
3140
3144
" conform to BitwiseCopyable!?" );
3141
3145
3146
+ // An archetype may conform but be non-trivial (case (2)).
3142
3147
if (origTy.isTypeParameter ())
3143
3148
return false ;
3144
3149
@@ -3148,6 +3153,7 @@ void TypeConverter::verifyTrivialLowering(const TypeLowering &lowering,
3148
3153
llvm::errs () << " Non-trivial type with _BitwiseCopyable conformance!?:\n "
3149
3154
<< substType << " \n " ;
3150
3155
conformance.print (llvm::errs ());
3156
+ llvm::errs () << " \n " ;
3151
3157
assert (false );
3152
3158
}
3153
3159
}
0 commit comments