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-12839] Underscore variable with same name as published var crashes compiler #55285

Closed
amomchilov opened this issue May 20, 2020 · 8 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers

Comments

@amomchilov
Copy link
Contributor

Previous ID SR-12839
Radar rdar://problem/63496795
Original Reporter @amomchilov
Type Bug
Status Resolved
Resolution Done
Environment

macOS Catalina Version 10.15.4 (19E287)
Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, PropertyWrappers
Assignee @theblixguy
Priority Medium

md5: ae50e303b3b717be892e6a7c67dfffd4

is duplicated by:

  • SR-13033 Seg fault with property wrapper and underscored variables

Issue Description:

This code's a compiler crasher:

import Combine

class C {
    @Published var published = 123
    var _published = 123
}

Crash log:

 Untitled 5.swift:5:6: error: invalid redeclaration of '_published'
         var _published = 123
             ^
 Untitled 5.swift:4:17: note: '_published' previously declared here
         @Published var published = 123
                        ^
 <unknown>:0: error: cannot convert value of type 'KeyPath<_, _>' to expected argument type 'ReferenceWritableKeyPath<_, _>'
 Stack dump:
 0.    Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file Untitled 5.swift -target x86_64-apple-macosx10.15 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -module-name main -o /var/folders/jm/q8pkbqns0ylccg0q9zbb815r0000gn/T/Untitled 5-b05225.o 
 1.    Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
 2.    While evaluating request TypeCheckSourceFileRequest(source_file "Untitled 5.swift", 0)
 3.    While evaluating request TypeCheckFunctionBodyUntilRequest(main.(file).C._@Untitled 5.swift:4:17, )
 4.    While type-checking getter for published (at Untitled 5.swift:4:17)
 5.    While evaluating request ParseAbstractFunctionBodyRequest(main.(file).C._@Untitled 5.swift:4:17)
 0  swift                    0x000000010ab7a4ea PrintStackTraceSignalHandler(void*) + 42
 1  swift                    0x000000010ab79cc0 SignalHandler(int) + 352
 2  libsystem_platform.dylib 0x00007fff690685fd _sigtramp + 29
 3  libsystem_platform.dylib 0x00007fd3f68c2ac0 _sigtramp + 2374345952
 4  swift                    0x0000000107581e26 synthesizeAccessorBody(swift::AbstractFunctionDecl*, void*) + 1702
 5  swift                    0x00000001077b52c8 swift::SimpleRequest<swift::ParseAbstractFunctionBodyRequest, swift::BraceStmt* (swift::AbstractFunctionDecl*), (swift::CacheKind)2>::evaluateRequest(swift::ParseAbstractFunctionBodyRequest const&, swift::Evaluator&) + 168
 6  swift                    0x00000001078ccf5b swift::ParseAbstractFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::ParseAbstractFunctionBodyRequest>(swift::Evaluator&, swift::ParseAbstractFunctionBodyRequest, swift::ParseAbstractFunctionBodyRequest::OutputType) + 1211
 7  swift                    0x000000010756f634 swift::SimpleRequest<swift::TypeCheckFunctionBodyUntilRequest, bool (swift::AbstractFunctionDecl*, swift::SourceLoc), (swift::CacheKind)1>::evaluateRequest(swift::TypeCheckFunctionBodyUntilRequest const&, swift::Evaluator&) + 756
 8  swift                    0x00000001075766ee swift::TypeCheckFunctionBodyUntilRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyUntilRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyUntilRequest, swift::TypeCheckFunctionBodyUntilRequest::OutputType) + 718
 9  swift                    0x00000001075a524e swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*, unsigned int) const + 6270
 10 swift                    0x00000001075746c7 swift::SimpleRequest<swift::TypeCheckSourceFileRequest, bool (swift::SourceFile*, unsigned int), (swift::CacheKind)2>::evaluateRequest(swift::TypeCheckSourceFileRequest const&, swift::Evaluator&) + 23
 11 swift                    0x00000001075a2b4e swift::performTypeChecking(swift::SourceFile&, unsigned int) + 1006
 12 swift                    0x0000000106bd6bcb swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 5915
 13 swift                    0x00000001068c2d99 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 10281
 14 swift                    0x0000000106843f53 main + 1283
 15 libdyld.dylib            0x00007fff68e6fcc9 start + 1
 <unknown>:0: error: unable to execute command: Segmentation fault: 11
 <unknown>:0: error: compile command failed due to signal 11 (use -v to see invocation)

Interestingly, the crash doesn't occur if you use your own @propertyWrapper:

@propertyWrapper struct S {
    var wrappedValue = 123
    var projectedValue: S { self } // Doesn't matter if this is here or not
}

class C {
    @S var s = 456
    var _s  = 789
}

Compiler error (but no crash):

Untitled 6.swift:8:6: error: invalid redeclaration of '_s'
         var _s  = 789
             ^
 Untitled 6.swift:7:9: note: '_s' previously declared here
         @S var s = 456
                ^
@amomchilov
Copy link
Contributor Author

IIRC, property wrappers were initially going to to use a prefix `_`, but that was changed to `$`. Is this just some old remnant of that?

@swift-ci
Copy link
Collaborator

Comment by cukr (JIRA)

Property wrappers use both prefixes: `_s` is the property wrapper itself (with private access level), `$s` is the projected value, and `s` is the wrapped value

@theblixguy
Copy link
Collaborator

Reproducer that doesn't depend on Combine:

@propertyWrapper 
struct Wrapper {
  var wrappedValue: Int
}

struct UsesWrapper {
  @Wrapper var foo1 = 123
  var _foo1 = 123
}

@amomchilov
Copy link
Contributor Author

cukier (JIRA User) Oh I didn't know that. Perhaps the message could be clarified, because I didn't think I had anything with an `_`.

And it definitely shouldn't crash the compiler in the `@Published` case 🙂

@theblixguy
Copy link
Collaborator

It’s a regression... you’d previously get a redeclaration error (which can be improved further) and now you get a crash instead. I’ll take a look at this shortly. It seems to have regressed after the initial 5.2 release.

@theblixguy
Copy link
Collaborator

Here's a fix: #31915

@hborla
Copy link
Member

hborla commented May 21, 2020

@swift-ci create

@theblixguy
Copy link
Collaborator

Fixed on master by #31915. I have also cherry-picked it to 5.3 branch: #31938

@amomchilov Could you please verify using the next available snapshots from swift.org? Thanks!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
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 crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

5 participants