swiftlang / swift

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

[SR-12618] Compiler crashes when property wrapper with autoclosure is initialized #55062

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12618
Radar rdar://problem/62894484
Original Reporter Koninis (JIRA User)
Type Bug
Environment Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 8fd48a17dbccfaadc96b152428b1c903

Issue Description:

The follow code crashes the compiler:

@propertyWrapper
enum Lazy<Value> {
  case uninitialized(() -> Value)
  case initialized(Value)
  init(wrappedValue: @autoclosure @escaping () -> Value) {
    self = .uninitialized(wrappedValue)
  }
  var wrappedValue: Value {
    mutating get {
      switch self {
      case .uninitialized(let initializer):
        let value = initializer()
        return value
      case .initialized(let value):
        return value
      }
    }
    set {
    }
  }
}
func getBalanceFromDataBase() -> Int {
    return 100_000
}
struct BankAccount {
    @Lazy var balance: Int
    init() {
        balance = getBalanceFromDataBase()
    }
}
beccadax commented 4 years ago

@swift-ci create

LucianoPAlmeida commented 3 years ago

I just checked this doesn't crash the compiler on Xcode 12.4

struct BankAccount {
    @Lazy var balance: Int
    init() {
        balance = getBalanceFromDataBase() // error:  'self' used before all stored properties are initialized
    }
}

Just diagnostic issue that is probably fixed on main and also 5.4 by https://github.com/apple/swift/pull/35218
So maybe we could resolve this one? cc @hborla =]

hborla commented 3 years ago

Ah! I actually suspect that this is not fixed on main. I bet the constraint system and SILGen don't know what to do for property wrapper assignment when the argument type is different between the setter and initializer due to `autoclosure`. The PR above is specific to when the wrapper has a nonmutating setter

LucianoPAlmeida commented 3 years ago

You right, just confirmed on a near main branch the crash is fixed but incorrect diagnostic is still there...

romikabi commented 2 years ago

I have been running into similar issue when using a property wrapper with init(wrappedValue: @escaping @autoclosure () -> T) for a function argument.


file.swift

import Foundation

@propertyWrapper
struct Foo<T> {
  var wrappedValue: T {
    factory()
  }

  init(wrappedValue: @escaping @autoclosure () -> T) {
    self.factory = wrappedValue
  }

  private let factory: () -> T
}

func bar<T>(@Foo _: @escaping @autoclosure () -> T) {}

swift file.swift

output ``` Stack dump: 0. Program arguments: /Applications/Xcode-13.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode-13.4.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -color-diagnostics -new-driver-path /Applications/Xcode-13.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -resource-dir /Applications/Xcode-13.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name file -target-sdk-version 12.3 1. Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12) 2. Compiling with the current language version 3. While evaluating request ASTLoweringRequest(Lowering AST to SIL for module file) 4. While silgen emitFunction SIL function "@$s4file3baryyxyXAlF". for 'bar(_:)' (at file.swift:16:1) 5. While silgen closureexpr SIL function "@$s4file3baryyxyXAlFxycycfu65534_". for expression at [ - ] 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 0x000000010674fed7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39 1 swift-frontend 0x000000010674ef28 llvm::sys::RunSignalHandlers() + 248 2 swift-frontend 0x0000000106750530 SignalHandler(int) + 288 3 libsystem_platform.dylib 0x00007ff804b22dfd _sigtramp + 29 4 libsystem_platform.dylib 0x00007ff7beb11001 _sigtramp + 18446744072535073313 5 swift-frontend 0x000000010164cc7d (anonymous namespace)::SubstFunctionTypePatternVisitor::handleTypeParameterInAbstractionPattern(swift::Lowering::AbstractionPattern, swift::Type) + 317 6 swift-frontend 0x000000010164c3f1 swift::TypeVisitor<(anonymous namespace)::SubstFunctionTypePatternVisitor, swift::CanType, swift::Lowering::AbstractionPattern>::visit(swift::Type, swift::Lowering::AbstractionPattern) + 81 7 swift-frontend 0x000000010164c0c7 (anonymous namespace)::SubstFunctionTypePatternVisitor::handleUnabstractedFunctionType(swift::AnyFunctionType*, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern) + 183 8 swift-frontend 0x000000010164bd4e swift::Lowering::AbstractionPattern::getSubstFunctionTypePattern(swift::CanTypeWrapper, swift::Lowering::TypeConverter&, swift::Lowering::AbstractionPattern, swift::CanType) const + 286 9 swift-frontend 0x000000010167a7f0 getSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, swift::SILExtInfoBuilder, (anonymous namespace)::Conventions const&, swift::ForeignInfo const&, llvm::Optional, llvm::Optional, llvm::Optional, swift::ProtocolConformanceRef, llvm::Optional) + 1584 10 swift-frontend 0x000000010167a14e getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, swift::SILExtInfoBuilder, llvm::Optional, llvm::Optional, llvm::Optional, swift::ProtocolConformanceRef, llvm::Optional)::$_12::operator()((anonymous namespace)::Conventions const&) const + 734 11 swift-frontend 0x0000000101672b43 getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, swift::SILExtInfoBuilder, llvm::Optional, llvm::Optional, llvm::Optional, swift::ProtocolConformanceRef, llvm::Optional) + 595 12 swift-frontend 0x000000010167289a swift::getNativeSILFunctionType(swift::Lowering::TypeConverter&, swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, swift::SILExtInfo, llvm::Optional, llvm::Optional, llvm::Optional, swift::ProtocolConformanceRef) + 122 13 swift-frontend 0x0000000101708588 swift::Lowering::TypeConverter::computeLoweredRValueType(swift::TypeExpansionContext, swift::Lowering::AbstractionPattern, swift::CanType)::LoweredRValueTypeVisitor::visitAnyFunctionType(swift::CanTypeWrapper) + 744 14 swift-frontend 0x00000001016fbaaa swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type, swift::TypeExpansionContext) + 698 15 swift-frontend 0x0000000101c370f8 swift::Lowering::SILGenFunction::emitGlobalVariableRef(swift::SILLocation, swift::VarDecl*, llvm::Optional) + 1016 16 swift-frontend 0x0000000101c42190 swift::Lowering::LValue::addNonMemberVarComponent(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::VarDecl*, swift::SubstitutionMap, swift::Lowering::LValueOptions, swift::Lowering::SGFAccessKind, swift::AccessStrategy, swift::CanType, llvm::Optional) + 1424 17 swift-frontend 0x0000000101c42789 swift::Lowering::SILGenFunction::emitRValueForNonMemberVarDecl(swift::SILLocation, swift::ConcreteDeclRef, swift::CanType, swift::AccessSemantics, swift::Lowering::SGFContext) + 1001 18 swift-frontend 0x0000000101c05036 swift::Lowering::SILGenFunction::emitRValueForDecl(swift::SILLocation, swift::ConcreteDeclRef, swift::Type, swift::AccessSemantics, swift::Lowering::SGFContext) + 278 19 swift-frontend 0x0000000101c13b6b swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 3707 20 swift-frontend 0x0000000101c0469c swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional) + 124 21 swift-frontend 0x0000000101c8b585 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 1765 22 swift-frontend 0x0000000101c31273 swift::Lowering::SILGenFunction::emitClosure(swift::AbstractClosureExpr*) + 771 23 swift-frontend 0x0000000101b8d90d swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 7981 24 swift-frontend 0x0000000101c25e24 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 340 25 swift-frontend 0x0000000101c16126 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 13366 26 swift-frontend 0x0000000101c078b8 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 40 27 swift-frontend 0x0000000101beb41c swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref) + 444 28 swift-frontend 0x0000000101bb268a (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 3322 29 swift-frontend 0x0000000101b9d1bc (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 172 30 swift-frontend 0x0000000101bbb86a (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, swift::ForeignInfo const&) && + 842 31 swift-frontend 0x0000000101bbb23f (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper, swift::ForeignInfo const&, llvm::SmallVectorImpl&, llvm::Optional&) + 1071 32 swift-frontend 0x0000000101ba366a (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 4698 33 swift-frontend 0x0000000101ba00ae swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2238 34 swift-frontend 0x0000000101c17547 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 18519 35 swift-frontend 0x0000000101c0469c swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, llvm::Optional) + 124 36 swift-frontend 0x0000000101bf1ce5 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 2677 37 swift-frontend 0x0000000101b926cd swift::ASTVisitor::visit(swift::Decl*) + 125 38 swift-frontend 0x0000000101c82bcf swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 303 39 swift-frontend 0x0000000101c309f1 swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 817 40 swift-frontend 0x0000000101b8e3cd swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 10733 41 swift-frontend 0x0000000101b90959 emitOrDelayFunction(swift::Lowering::SILGenModule&, swift::SILDeclRef, bool) + 217 42 swift-frontend 0x0000000101b95c1d swift::ASTVisitor::visit(swift::Decl*) + 1053 43 swift-frontend 0x0000000101b93809 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 4297 44 swift-frontend 0x0000000101c826b3 swift::SimpleRequest > (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 195 45 swift-frontend 0x0000000101b97ef9 llvm::Expected swift::Evaluator::getResultUncached(swift::ASTLoweringRequest const&) + 649 46 swift-frontend 0x0000000101479903 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 21011 47 swift-frontend 0x00000001014375d4 swift::mainEntry(int, char const**) + 1108 48 dyld 0x000000010ad3f51e start + 462 [1] 6220 segmentation fault swift file.swift ```