swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.28k stars 10.33k forks source link

[SR-15115] propertyWrapper _enclosingInstance without setter crashes swiftc #57441

Open mayoff opened 3 years ago

mayoff commented 3 years ago
Previous ID SR-15115
Radar rdar://problem/82404190
Original Reporter @mayoff
Type Bug
Environment Xcode 13b5 Apple Swift version 5.5 (swiftlang-1300.0.29.102 clang-1300.0.28.1) macOS 11.5.2 (20G95)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | None | |Priority | Medium | md5: a8ac5f48b8e4501926af3c48669cc2eb

Issue Description:

Source code:

@propertyWrapper
public struct Wrapper<Value> {
    private let value: Value

    public init(wrappedValue: Value) {
    value = wrappedValue
    }

    @available(*, unavailable, message: "@Published is only available on properties of classes")
    public var wrappedValue: Value {
        get { fatalError() }
        set { fatalError() }
    }

    public static subscript<EnclosingSelf>(
    _enclosingInstance object: EnclosingSelf,
    wrapped wrappedKeyPath: Swift.ReferenceWritableKeyPath<EnclosingSelf, Value>,
    storage storageKeyPath: Swift.ReferenceWritableKeyPath<EnclosingSelf, Wrapper<Value>>
    ) -> Value where EnclosingSelf : AnyObject {
        get { object[keyPath: storageKeyPath].value }
//  set { }
    }
}

class MyClass {
    @Wrapper
    var property: Int = 123
}

let outer = MyClass()

Compiler crash:

:; xcrun swiftc -c /tmp/wrapper.swift 
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.  Program arguments: /Applications/Xcode-13b5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /tmp/wrapper.swift -target x86_64-apple-macosx11.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode-13b5.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode-13b5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -resource-dir /Applications/Xcode-13b5.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name wrapper -target-sdk-version 12.0.0 -o wrapper.o
1.  Apple Swift version 5.5 (swiftlang-1300.0.29.102 clang-1300.0.28.1)
2.  
3.  While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/tmp/wrapper.swift")
4.  While silgen emitFunction SIL function "@$s7wrapper7MyClassC8propertySivs".
 for setter for property (at /tmp/wrapper.swift:28:9)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001110d4797 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x00000001110d3728 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x00000001110d4da6 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007fff20525d7d _sigtramp + 29
4  libsystem_platform.dylib 0x00007f9f7ac8e800 _sigtramp + 18446743662910409376
5  swift-frontend           0x000000010c962b60 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) + 9696
6  swift-frontend           0x000000010c960475 swift::Lowering::SILGenFunction::emitLValue(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) + 37
7  swift-frontend           0x000000010c93e994 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 43188
8  swift-frontend           0x000000010c9289f1 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 705
9  swift-frontend           0x000000010c9a4969 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 201
10 swift-frontend           0x000000010c953768 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 776
11 swift-frontend           0x000000010c8b6240 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 9296
12 swift-frontend           0x000000010c8b8688 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 376
13 swift-frontend           0x000000010c8b3dd8 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 216
14 swift-frontend           0x000000010c9ba37d void llvm::function_ref<void (swift::AccessorDecl*)>::callback_fn<(anonymous namespace)::SILGenType::visitAccessors(swift::AbstractStorageDecl*)::'lambda'(swift::AccessorDecl*)>(long, swift::AccessorDecl*) + 29
15 swift-frontend           0x000000010d44f231 swift::AbstractStorageDecl::visitEmittedAccessors(llvm::function_ref<void (swift::AccessorDecl*)>) const + 321
16 swift-frontend           0x000000010c9ba310 (anonymous namespace)::SILGenType::visitVarDecl(swift::VarDecl*) + 448
17 swift-frontend           0x000000010c9b561c (anonymous namespace)::SILGenType::emitType() + 220
18 swift-frontend           0x000000010c8bd921 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 97
19 swift-frontend           0x000000010c8bb3ec swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 4188
20 swift-frontend           0x000000010c9a4495 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 197
21 swift-frontend           0x000000010c8c03ac llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) + 652
22 swift-frontend           0x000000010c1ff778 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 13304
23 swift-frontend           0x000000010c141548 main + 1032
24 libdyld.dylib            0x00007fff204fbf3d start + 1
Illegal instruction: 4

Uncommenting the setter (set { }) eliminates the crash.

typesanitizer commented 3 years ago

This triggers an ASTVerifier failure.

type is not an l-value in LHS of assignment: Int

@swift-ci create

AnthonyLatsis commented 1 year ago

Stack trace update:

type is not an l-value in LHS of assignment: Int
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: bin/swift-frontend /Users/mac/Desktop/test.swift -emit-silgen
1.  Swift version 5.9-dev (LLVM 91080e65df791d6, Swift c1d5118c21d)
2.  Compiling with the current language version
3.  While walking into 'MyClass' (at /Users/mac/Desktop/test.swift:94:1)
4.  While walking into body of setter for property (at /Users/mac/Desktop/test.swift:96:9)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000106447847 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000106446b25 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x0000000106447e90 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff815a94dfd _sigtramp + 29
4  libsystem_platform.dylib 0x00007ff7bf5dbdb8 _sigtramp + 18446744072261758936
5  libsystem_c.dylib        0x00007ff8159cad24 abort + 123
6  swift-frontend           0x0000000106c20003 (anonymous namespace)::Verifier::checkLValue(swift::Type, char const*) (.cold.3) + 131
7  swift-frontend           0x0000000101f76b54 (anonymous namespace)::Verifier::checkLValue(swift::Type, char const*) + 116
8  swift-frontend           0x0000000101f6cbbc (anonymous namespace)::Verifier::walkToExprPost(swift::Expr*) + 20812
9  swift-frontend           0x0000000101f7aa19 (anonymous namespace)::Traversal::doIt(swift::Expr*) + 137
10 swift-frontend           0x0000000101f7c493 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Stmt*) + 195
11 swift-frontend           0x0000000101f7ab0d (anonymous namespace)::Traversal::doIt(swift::Stmt*) + 109
12 swift-frontend           0x0000000101f7f1b8 (anonymous namespace)::Traversal::visitAbstractFunctionDecl(swift::AbstractFunctionDecl*) + 712
13 swift-frontend           0x0000000101f7b0b5 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 293
14 swift-frontend           0x0000000101f7da70 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Decl*) + 1040
15 swift-frontend           0x0000000101f7b0b5 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 293
16 swift-frontend           0x0000000101f7cbe6 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Pattern*) + 198
17 swift-frontend           0x0000000101f7ac2d (anonymous namespace)::Traversal::doIt(swift::Pattern*) + 109
18 swift-frontend           0x0000000101f7cc00 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Pattern*) + 224
19 swift-frontend           0x0000000101f7ac2d (anonymous namespace)::Traversal::doIt(swift::Pattern*) + 109
20 swift-frontend           0x0000000101f7e4a1 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Decl*) + 3649
21 swift-frontend           0x0000000101f7b0b5 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 293
22 swift-frontend           0x0000000101f7ebed (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) + 653
23 swift-frontend           0x0000000101f7b0b5 (anonymous namespace)::Traversal::doIt(swift::Decl*) + 293
24 swift-frontend           0x0000000101f7af7b swift::Decl::walk(swift::ASTWalker&) + 27
25 swift-frontend           0x000000010213dca2 swift::SourceFile::walk(swift::ASTWalker&) + 322
26 swift-frontend           0x0000000101f666a0 swift::verify(swift::SourceFile&) + 80
27 swift-frontend           0x000000010225bf4a swift::TypeCheckSourceFileRequest::cacheResult(std::__1::tuple<>) const + 74
28 swift-frontend           0x0000000101cf8950 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 96
29 swift-frontend           0x0000000101cf669f swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 31
30 swift-frontend           0x0000000100bb4e1c bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_7>(long, swift::SourceFile&) + 12
31 swift-frontend           0x0000000100bab93e swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 174
32 swift-frontend           0x0000000100bab86b swift::CompilerInstance::performSema() + 75
33 swift-frontend           0x0000000100994c83 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 659
34 swift-frontend           0x0000000100993b20 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3232
35 swift-frontend           0x00000001009590b3 swift::mainEntry(int, char const**) + 1203
36 dyld                     0x000000012786052e start + 462
Abort trap: 6
fwcd commented 1 week ago

Ran into this today using Xcode 15.4/Swift 5.10