swiftlang / swift

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

[SR-11358] Segmentation Fault when compiling a property wrapper with a mutating get projectedValue #53759

Open swift-ci opened 5 years ago

swift-ci commented 5 years ago
Previous ID SR-11358
Radar None
Original Reporter kevlario (JIRA User)
Type Bug

Attachment: Download

Environment Xcode 11 beta 6, on mac OS mojave 10.14.5
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash, PropertyWrappers | |Assignee | None | |Priority | Medium | md5: b0ae3adefac5b0119769526719ea31a4

Issue Description:

When compiling a project that defines a property wrapper with a `projectedValue` property that happens to have a `mutating` getter, I hit a segmentation fault. changing the getter to not be `mutating` (and removing any code that made `mutating` a requirement) lets the compiler continue.

To reproduce, compile the SPM package attached to this issue, including the tests, (either in Xcode itself or with `swift build --build-tests`) and you'll see the attached segfault. If you change CopyOnWrite.swift by removing mutating and commenting the mutating-required code out, the compiler succeeds.

Edit: See the comment below, this is a smaller sample that exhibits the behavior:

@propertyWrapper
struct Thing<Value> {
    var wrappedValue: Value
    public var projectedValue: Value {
        mutating get { wrappedValue } // if this is not mutating, no segfault
    }
}

class Bar {}
struct Foo { // if this is a class, there's no segfault
    @Thing var name: Bar = Bar()
}
theblixguy commented 5 years ago

This does not crash for me on master.

belkadan commented 5 years ago

@DougGregor, do you have the dup for this?

swift-ci commented 5 years ago

Comment by Kevin Lundberg (JIRA)

I attached a segfault for yesterday's 5.1 snapshot which looks like it had a slightly different assertion error than the compiler in beta 6 has. The development snapshot also has the bug with a slightly different assertion error too, which is also attached

swift-ci commented 5 years ago

Comment by Kevin Lundberg (JIRA)

I have a smaller example which exhibits the crash (using the compiler in Xcode 11 GM Seed), added to the description above.

It seems that structs can't handle generated code for projected values that are mutating gets. changing Foo to a class doesn't crash, or changing projectedValue's get to not be mutating also doesn't crash.

theblixguy commented 5 years ago

Might be fixed by https://github.com/apple/swift/pull/27109

It’s been cherry picked to 5.1 as well, could you verify using the next snapshot?

swift-ci commented 5 years ago

Comment by Kevin Lundberg (JIRA)

Thanks for the suggestion! I just tried using the 5.1 snapshot from yesterday, however it still crashes. Not with a segfault 11, but with an abort trap 6 with an assertion failure:

Assertion failed: (!selfParam.isIndirectMutating() && "passing unmaterialized r-value as inout argument"), function prepareAccessorObjectBaseArg, file /Users/buildnode/jenkins/workspace/oss-swift-5.1-package-osx/swift/lib/SILGen/SILGenApply.cpp, line 5943.

I've attached the full stack dump here, from compiling the updated reduced reproduction case: segfault-5.1-snapshot-9-11.txt

Edit: I just tried the trunk snapshot from 9/11 though, and that DID work. the crash on the 5/1 snapshot from yesterday and from 8/23 looks the same, so its probable that whatever fix was made to master wasn't applied to 5.1 by the time the most recent snapshot was made.

swift-ci commented 5 years ago

Comment by Kevin Lundberg (JIRA)

And yeah, it does look like this and SR-11393 are the same issue