Introduce safety checkin for ConcurrentValue conformance.

Introduce checking of ConcurrentValue conformances:
- For structs, check that each stored property conforms to ConcurrentValue
- For enums, check that each associated value conforms to ConcurrentValue
- For classes, check that each stored property is immutable and conforms
  to ConcurrentValue

Because all of the stored properties / associated values need to be
visible for this check to work, limit ConcurrentValue conformances to
be in the same source file as the type definition.

This checking can be disabled by conforming to a new marker protocol,
UnsafeConcurrentValue, that refines ConcurrentValue.
UnsafeConcurrentValue otherwise his no specific meaning. This allows
both "I know what I'm doing" for types that manage concurrent access
themselves as well as enabling retroactive conformance, both of which
are fundamentally unsafe but also quite necessary.

The bulk of this change ended up being to the standard library, because
all conformances of standard library types to the ConcurrentValue
protocol needed to be sunk down into the standard library so they
would benefit from the checking above. There were numerous little
mistakes in the initial pass through the stsandard library types that
have now been corrected.
This commit is contained in:
Doug Gregor
2021-02-04 03:36:26 -08:00
parent 9b166c1fde
commit 1a1f79c0de
67 changed files with 344 additions and 167 deletions

View File

@@ -19,7 +19,7 @@ extension Unicode.Scalar {
/// A value that provides access to properties of a Unicode scalar that are
/// defined by the Unicode standard.
public struct Properties {
public struct Properties: ConcurrentValue {
@usableFromInline
internal var _scalar: Unicode.Scalar
@@ -821,7 +821,7 @@ extension Unicode {
/// The general category of a scalar is its "first-order, most usual
/// categorization". It does not attempt to cover multiple uses of some
/// scalars, such as the use of letters to represent Roman numerals.
public enum GeneralCategory {
public enum GeneralCategory: ConcurrentValue {
/// An uppercase letter.
///
@@ -1208,7 +1208,7 @@ extension Unicode {
/// let overlayClassIsOverlay = overlayClass == .overlay
/// // overlayClassIsOverlay == true
public struct CanonicalCombiningClass:
Comparable, Hashable, RawRepresentable
Comparable, Hashable, RawRepresentable, ConcurrentValue
{
/// Base glyphs that occupy their own space and do not combine with others.
public static let notReordered = CanonicalCombiningClass(rawValue: 0)
@@ -1332,7 +1332,7 @@ extension Unicode {
/// Some letterlike scalars used in numeric systems, such as Greek or Latin
/// letters, do not have a non-nil numeric type, in order to prevent programs
/// from incorrectly interpreting them as numbers in non-numeric contexts.
public enum NumericType {
public enum NumericType: ConcurrentValue {
/// A digit that is commonly understood to form base-10 numbers.
///