Property Wrapper, is this a bug?

protocol Copyable: AnyObject {
    func copy() -> Self
}

@propertyWrapper
struct CopyOnWrite<Value: Copyable> {
    init(wrappedValue: Value) {
        self.wrappedValue = wrappedValue
    }

    var wrappedValue: Value

    var projectedValue: Value {
        mutating get {
            if !isKnownUniquelyReferenced(&wrappedValue) {
                wrappedValue = wrappedValue.copy()
            }
            return wrappedValue
        }
        set {
            wrappedValue = newValue
        }
    }
}

final class Test: Copyable {
    let a: Int
    init(a: Int) {
        self.a = a
    }
    
    func copy() -> Self {
        Self.init(a: a)
    }
}

struct Demo1 {
    @CopyOnWrite var a = Test(a: 3) // compiler crash
}

// however this is OK
struct Demo2 {
    private var _a: CopyOnWrite<Test> = CopyOnWrite(wrappedValue: Test(a: 3))
    var a: Test {
        get {
            _a.wrappedValue
        }
        set {
            _a.wrappedValue = newValue
        }
    }
    var pa: Test {
        mutating get {
            if !isKnownUniquelyReferenced(&_a.wrappedValue) {
                _a.wrappedValue = _a.wrappedValue.copy()
            }
            return _a.wrappedValue
        }
        set {
            _a.wrappedValue = newValue
        }
    }
}

error message

(error_expr implicit type='<<error type>>' location=Stack dump:
0.	Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/harry/dev/swift/tdafas/tdafas/main.swift -emit-module-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main~partial.swiftmodule -emit-module-doc-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main~partial.swiftdoc -serialize-diagnostics-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main.dia -emit-dependencies-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main.d -emit-reference-dependencies-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main.swiftdeps -target x86_64-apple-macos10.14 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -I /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Products/Debug -F /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Products/Debug -enable-testing -g -module-cache-path /Users/harry/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -D DEBUG -serialize-debugging-options -Xcc -working-directory -Xcc /Users/harry/dev/swift/tdafas -enable-anonymous-context-mangled-names -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/tdafas-generated-files.hmap -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/tdafas-own-target-headers.hmap -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/tdafas-all-target-headers.hmap -Xcc -iquote -Xcc /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/tdafas-project-headers.hmap -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Products/Debug/include -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/DerivedSources-normal/x86_64 -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/DerivedSources/x86_64 -Xcc -I/Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/DerivedSources -Xcc -DDEBUG=1 -module-name tdafas -o /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Build/Intermediates.noindex/tdafas.build/Debug/tdafas.build/Objects-normal/x86_64/main.o -index-store-path /Users/harry/Library/Developer/Xcode/DerivedData/tdafas-gobaqsvxmwbbrlgnfunkbwryotjn/Index/DataStore -index-system-modules 
1.	While emitting SIL for getter for $a (at /Users/harry/dev/swift/tdafas/tdafas/main.swift:63:22)
2.	While silgen emitFunction SIL function "@$s6tdafas5Demo1V2$aAA4TestCvg".
 for getter for $a (at /Users/harry/dev/swift/tdafas/tdafas/main.swift:63:22)
0  swift                    0x0000000111d87013 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000111d867e6 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff7d831b5d _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2189223104
4  swift                    0x000000010e85c99a (anonymous namespace)::PrintExpr::printCommon(swift::Expr*, char const*) + 1562
5  swift                    0x000000010e84863d swift::ASTVisitor<(anonymous namespace)::PrintExpr, void, void, void, void, void, void>::visit(swift::Expr*) + 8749
6  swift                    0x000000010e85e084 swift::Expr::dump(llvm::raw_ostream&, unsigned int) const + 68
7  swift                    0x000000010de806e3 SILGenLValue::visitExpr(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) + 35
8  swift                    0x000000010de8052c swift::ASTVisitor<SILGenLValue, swift::Lowering::LValue, void, void, void, void, void, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions>::visit(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) + 12988
9  swift                    0x000000010de7d192 swift::Lowering::SILGenFunction::emitLValue(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) + 34
10 swift                    0x000000010dddcd41 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1425
11 swift                    0x000000010ddee445 (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus) && + 1061
12 swift                    0x000000010ddeb45b (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 1451
13 swift                    0x000000010dde8f13 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3059
14 swift                    0x000000010ddfa9d4 swift::Lowering::SILGenFunction::emitGetAccessor(swift::SILLocation, swift::SILDeclRef, swift::SubstitutionMap, swift::Lowering::ArgumentSource&&, bool, bool, swift::Lowering::PreparedArguments&&, swift::Lowering::SGFContext, bool) + 900
15 swift                    0x000000010de78b73 (anonymous namespace)::GetterSetterComponent::get(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) && + 275
16 swift                    0x000000010de72b7a swift::Lowering::SILGenFunction::emitLoadOfLValue(swift::SILLocation, swift::Lowering::LValue&&, swift::Lowering::SGFContext, bool) + 1018
17 swift                    0x000000010de42be5 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 29493
18 swift                    0x000000010deb7527 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 439
19 swift                    0x000000010deb2ff4 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 10132
20 swift                    0x000000010deb144b swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 3051
21 swift                    0x000000010de672fa swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 426
22 swift                    0x000000010ddcc3ac swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 1004
23 swift                    0x000000010dec1157 (anonymous namespace)::SILGenType::emitType() + 1319
24 swift                    0x000000010ddd7d6d swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 77
25 swift                    0x000000010ddd6fb6 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 1238
26 swift                    0x000000010ddd8a16 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*) + 1238
27 swift                    0x000000010d9dc8b9 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 28345
28 swift                    0x000000010d9d21f4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
29 swift                    0x000000010d95f763 main + 1219
30 libdyld.dylib            0x00007fff7d6463d5 start + 1
error: Segmentation fault: 11 (in target 'tdafas' from project 'tdafas')

Yes. This is a Swift compiler bug because it crashes (and it never should). If you don't mind it'd be great if you could report this at https://bugs.swift.org

I reported. SR-11393

Terms of Service

Privacy Policy

Cookie Policy