Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14307] Closure property wrappers crash when applying to blocks #56666

Open
phausler opened this issue Mar 5, 2021 · 4 comments
Open

[SR-14307] Closure property wrappers crash when applying to blocks #56666

phausler opened this issue Mar 5, 2021 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@phausler
Copy link
Member

phausler commented Mar 5, 2021

Previous ID SR-14307
Radar rdar://problem/75116550
Original Reporter @phausler
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 62ddfd9778fcc39866283ce3b1edcf45

Issue Description:

The following code causes the compiler to crash

@propertyWrapper
public struct Weak<Object: AnyObject> {
  public weak var wrappedValue: Object?
  public init(wrappedValue: Object?) {
    self.wrappedValue = wrappedValue
  }
}

func withWeakReference<Object: AnyObject>(_ object: Object, _ apply: (Weak<Object>) -> Void) {
  let weakRef = Weak(wrappedValue: object)
  apply(weakRef)
}

class Foo { 
  func bar() {
    print("bar")
  }
}

let f = Foo()
withWeakReference(f) { (@Weak obj) in
  
}
0.  Program arguments: /Users/phausler/Documents/Public/build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -c -primary-file ./main.swift -emit-module-path /var/folders/hm/f1lnmhjn4110zjkgmbpkq1wc0000gn/T/main-d3c87f.swiftmodule -emit-module-doc-path /var/folders/hm/f1lnmhjn4110zjkgmbpkq1wc0000gn/T/main-d3c87f.swiftdoc -emit-module-source-info-path /var/folders/hm/f1lnmhjn4110zjkgmbpkq1wc0000gn/T/main-d3c87f.swiftsourceinfo -target x86_64-apple-darwin21.0.0 -enable-objc-interop -sdk /Users/phausler/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -color-diagnostics -enable-library-evolution -g -enable-experimental-concurrency -enable-anonymous-context-mangled-names -target-sdk-version 11.0 -module-name propertyWrapperTaskAttached -o /var/folders/hm/f1lnmhjn4110zjkgmbpkq1wc0000gn/T/main-d3c87f.o
1.  Swift version 5.4-dev (LLVM 3e5b885d17fe890, Swift 145196093320828)
2.  While evaluating request TypeCheckSourceFileRequest(source_file "./main.swift")
3.  While type-checking statement at [./main.swift:68:1 - line:70:1] RangeText="withWeakReference(f) { (@Weak obj) in

"
4.  While type-checking expression at [./main.swift:68:1 - line:70:1] RangeText="withWeakReference(f) { (@Weak obj) in

"
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           0x000000010c4ae8d7 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010c4ad848 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010c4aef06 SignalHandler(int) + 262
3  libsystem_platform.dylib 0x00007fff3265c4ad _sigtramp + 29
4  libsystem_platform.dylib 0x00007fa65d0526e8 _sigtramp + 18446743692172550744
5  libsystem_c.dylib        0x00007fff32598f0b abort + 118
6  libsystem_c.dylib        0x00007fff325982e3 err + 0
7  swift-frontend           0x000000010c9957c3 swift::ReferenceStorageType::get(swift::Type, swift::ReferenceOwnership, swift::ASTContext const&) (.cold.6) + 35
8  swift-frontend           0x00000001087dbe22 swift::ReferenceStorageType::get(swift::Type, swift::ReferenceOwnership, swift::ASTContext const&) + 658
9  swift-frontend           0x0000000108a8fefc swift::Type::transformRec(llvm::function_ref<llvm::Optional<swift::Type> (swift::TypeBase*)>) const + 5180
10 swift-frontend           0x0000000108a8cc69 substType(swift::Type, llvm::function_ref<swift::Type (swift::SubstitutableType*)>, llvm::function_ref<swift::ProtocolConformanceRef (swift::CanType, swift::Type, swift::ProtocolDecl*)>, swift::SubstOptions) + 297
11 swift-frontend           0x0000000108a8e85b swift::TypeBase::getTypeOfMember(swift::ModuleDecl*, swift::ValueDecl const*, swift::Type) + 475
12 swift-frontend           0x0000000108485ea7 generateWrappedPropertyTypeConstraints(swift::constraints::ConstraintSystem&, swift::Type, swift::VarDecl*, swift::Type) + 743
13 swift-frontend           0x00000001084888c1 swift::constraints::ConstraintSystem::applyPropertyWrapperToParameter(swift::Type, swift::Type, swift::ParamDecl*, swift::Identifier, swift::constraints::ConstraintKind, swift::constraints::ConstraintLocatorBuilder) + 497
14 swift-frontend           0x00000001084af8fa swift::constraints::ConstraintSystem::resolveClosure(swift::TypeVariableType*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 1562
15 swift-frontend           0x00000001084af078 swift::constraints::ConstraintSystem::matchTypesBindTypeVar(swift::TypeVariableType*, swift::Type, swift::constraints::ConstraintKind, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder, llvm::function_ref<swift::constraints::ConstraintSystem::TypeMatchResult ()>) + 1480
16 swift-frontend           0x00000001084a7656 swift::constraints::ConstraintSystem::matchTypes(swift::Type, swift::Type, swift::constraints::ConstraintKind, swift::OptionSet<swift::constraints::ConstraintSystem::TypeMatchFlags, unsigned int>, swift::constraints::ConstraintLocatorBuilder) + 1462
17 swift-frontend           0x000000010847db9b swift::constraints::TypeVariableBinding::attempt(swift::constraints::ConstraintSystem&) const + 395
18 swift-frontend           0x00000001084e64bc swift::constraints::BindingStep<swift::constraints::TypeVarBindingProducer>::take(bool) + 428
19 swift-frontend           0x00000001084d57c1 swift::constraints::ConstraintSystem::solveImpl(llvm::SmallVectorImpl<swift::constraints::Solution>&) + 433
20 swift-frontend           0x00000001084d7892 swift::constraints::ConstraintSystem::solve(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 50
21 swift-frontend           0x00000001084d73f5 swift::constraints::ConstraintSystem::solveImpl(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 453
22 swift-frontend           0x00000001084d6cf9 swift::constraints::ConstraintSystem::solve(swift::constraints::SolutionApplicationTarget&, swift::FreeTypeVariableBinding) + 105
23 swift-frontend           0x00000001085face2 swift::TypeChecker::typeCheckExpression(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 466
24 swift-frontend           0x00000001085faa8d swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 77
25 swift-frontend           0x00000001086ada73 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 227
26 swift-frontend           0x00000001086afa9c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 124
27 swift-frontend           0x00000001086aed60 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 128
28 swift-frontend           0x00000001086aede2 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 50
29 swift-frontend           0x00000001086e19af swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 223
30 swift-frontend           0x00000001086e291f llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 415
31 swift-frontend           0x00000001086e26d4 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultCached<swift::TypeCheckSourceFileRequest, (void*)0>(swift::TypeCheckSourceFileRequest const&) + 100
32 swift-frontend           0x00000001086e17f0 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 32
33 swift-frontend           0x00000001079f669a swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<void (swift::SourceFile&)>) + 90
34 swift-frontend           0x00000001079f658a swift::CompilerInstance::performSema() + 74
35 swift-frontend           0x00000001078c7a54 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4724
36 swift-frontend           0x0000000107861ba2 main + 514
37 dyld                     0x000000011f4ebc71 start + 417
38 dyld                     000000000000000000 start + 18446744068889330992
39 swift-frontend           0x000000010785f000 __dso_handle + 0
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)
@typesanitizer
Copy link

@swift-ci create

@hborla
Copy link
Member

hborla commented Mar 9, 2021

Ah, this code is invalid under the current proposal. You can't pass the backing storage directly to a wrapped parameter, even for closures. If you want to pass Weak<Object>, you'd need to expose it through projectedValue:

@propertyWrapper
public struct Weak<Object: AnyObject> {
  public weak var wrappedValue: Object?
  public init(wrappedValue: Object?) {
    self.wrappedValue = wrappedValue
  }

  public var projectedValue: Weak<Object> { self }

  public init(projectedValue: Weak<Object>) {
    self.wrappedValue = projectedValue.wrappedValue
  }
}


func withWeakReference<Object: AnyObject>(_ object: Object, _ apply: (Weak<Object>) -> Void) {
  let weakRef = Weak(wrappedValue: object)
  apply(weakRef)
}


class Foo {
  func bar() {
    print("bar")
  }
}


let f = Foo()
withWeakReference(f) { $obj in


}

Or just pass Object

func withWeakReference<Object: AnyObject>(_ object: Object, _ apply: (Object) -> Void) {
  apply(object)
}


class Foo {
  func bar() {
    print("bar")
  }
}


let f = Foo()
withWeakReference(f) { (@Weak obj) in


}

Note that type inference for closure parameters isn't fully implemented on main yet, but I have it working locally and it'll be part of #36344

@phausler
Copy link
Member Author

phausler commented Mar 9, 2021

invalid code shouldn't cause the compiler to crash.

@hborla
Copy link
Member

hborla commented Mar 9, 2021

I know that. I was letting you know in the meantime what the issue is until I can implement the fix for producing the right error message.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants