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-11319] Unnecessary error with mutually-dependent generic protocols, crash when implementing #53720

Closed
swift-ci opened this issue Aug 16, 2019 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11319
Radar https://feedbackassistant.apple.com/feedback/7046528
Original Reporter BenLeggiero (JIRA User)
Type Bug
Status Closed
Resolution Duplicate

Attachment: Download

Environment
  • macOS 10.14.6 (18G87)

  • Xcode 10.3 (10G8)

  • Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)

  • Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 2a8daebc5237695929abd3fac8ed5fdd

duplicates:

  • SR-10438 Self constraint working on Swift 4.2 but not Swift 5

Issue Description:

When:

  • Some protocol A has 2 associated types BType and CType, both of which depend on A such that:

  • Some protocol B has 1 associated type AType which is an A

  • Some protocol C has 1 associated type AType which is an A

And:

  • Protocol A has at least 1 requirement involving type BType andor CType

SSCCE

protocol A {
    associatedtype BType: B where BType.AType == Self
    associatedtype CType: C where CType.AType == Self
    
    
    var b: BType { get }
    var c: CType { get set }

    func bGetter() -> BType
    mutating func cSetter(_ newC: CType)
    
    subscript (b: BType) -> CType { get set }
}



protocol B {
    associatedtype AType: A
}



protocol C {
    associatedtype AType: A
}

Expected Behavior

The compiler sees that this is OK as long as the concrete implementations satisfy the constraints, such as:

struct AImpl: A {
    
    typealias BType = BImpl
    typealias CType = CImpl
    
    
    
    let b: BImpl
    var c: CImpl
    
    
    func bGetter() -> BImpl {
        return b
    }
    
    
    mutating func cSetter(_ newC: CImpl) {
        c = newC
    }
    
    
    subscript(b: BImpl) -> CImpl {
        get {
            return c
        }
        set {
            c = newValue
        }
    }
}



struct BImpl: B {
    typealias AType = AImpl
}



struct CImpl: C {
    typealias AType = AImpl
}

Actual Behavior

The Swift compiler emits the following errors on protocol A:

/Path/To/main.swift:17:20: error: getter requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    var b: BType { get }
                   ^
/Path/To/main.swift:18:20: error: getter requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    var c: CType { get set }
                   ^
/Path/To/main.swift:18:24: error: setter requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    var c: CType { get set }
                       ^
/Path/To/main.swift:18:9: error: _modify accessor requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    var c: CType { get set }
        ^
/Path/To/main.swift:20:10: error: instance method requirement 'bGetter()' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    func bGetter() -> BType
         ^
/Path/To/main.swift:21:10: error: instance method requirement 'cSetter' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    func cSetter(_ newC: CType)
         ^
/Path/To/main.swift:23:5: error: subscript requirement 'subscript(_:)' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    subscript (b: BType) -> CType { get set }
    ^
/Path/To/main.swift:23:37: error: getter requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    subscript (b: BType) -> CType { get set }
                                    ^
/Path/To/main.swift:23:41: error: setter requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    subscript (b: BType) -> CType { get set }
                                        ^
/Path/To/main.swift:23:5: error: _modify accessor requirement '_' cannot add constraint 'Self.BType == Self.CType.AType.BType.AType.BType' on 'Self'
    subscript (b: BType) -> CType { get set }
    ^

And crashes with the following report when these protocols are implemented as demonstrated above:

Process:               swift [3264]
Path:                  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
Identifier:            swift
Version:               1001.0.82.4
Code Type:             X86-64 (Native)
Parent Process:        com.apple.dt.SKAgent [736]
Responsible:           swift [3264]
User ID:               1008033411

Date/Time:             2019-08-16 09:22:32.408 -0600
OS Version:            Mac OS X 10.14.6 (18G87)
Report Version:        12
Bridge OS Version:     3.6 (16P6568)
Anonymous UUID:        03231FE7-6C8E-2045-C20A-027EB8765946


Time Awake Since Boot: 3400 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x000000000000000a
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [3264]

VM Regions Near 0xa:
--> 
    __TEXT                 000000010167d000-0000000106272000 [ 76.0M] r-x/rwx SM=COW  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   swift                           0x0000000102c62c10 swift::GenericSignature::get(swift::ArrayRefView<swift::Type, swift::GenericTypeParamType*, swift::GenericTypeParamType* swift::staticCastHelper<swift::GenericTypeParamType>(swift::Type const&), true>, llvm::ArrayRef<swift::Requirement>, bool) + 160
1   swift                           0x0000000102d70660 swift::GenericSignatureBuilder::computeGenericSignature(swift::SourceLoc, bool, bool) && + 3904
2   swift                           0x0000000102daa6f9 swift::RequirementEnvironment::RequirementEnvironment(swift::DeclContext*, swift::GenericSignature*, swift::ProtocolDecl*, swift::ClassDecl*, swift::ProtocolConformance*) + 2441
3   swift                           0x00000001029cecc8 swift::matchWitness(swift::TypeChecker&, llvm::DenseMap<std::__1::pair<swift::GenericSignature const*, swift::ClassDecl const*>, swift::RequirementEnvironment, llvm::DenseMapInfo<std::__1::pair<swift::GenericSignature const*, swift::ClassDecl const*> >, llvm::detail::DenseMapPair<std::__1::pair<swift::GenericSignature const*, swift::ClassDecl const*>, swift::RequirementEnvironment> >&, swift::ProtocolDecl*, swift::ProtocolConformance*, swift::DeclContext*, swift::ValueDecl*, swift::ValueDecl*) + 584
4   swift                           0x00000001029d14bf swift::WitnessChecker::findBestWitness(swift::ValueDecl*, bool*, swift::NormalProtocolConformance*, llvm::SmallVectorImpl<swift::RequirementMatch>&, unsigned int&, unsigned int&, bool&) + 1615
5   swift                           0x00000001029dbf98 swift::ConformanceChecker::resolveWitnessTryingAllStrategies(swift::ValueDecl*) + 680
6   swift                           0x00000001029daf7e swift::ConformanceChecker::resolveValueWitnesses() + 334
7   swift                           0x00000001029d607c swift::MultiConformanceChecker::checkIndividualConformance(swift::NormalProtocolConformance*, bool) + 11740
8   swift                           0x00000001029d2db3 swift::MultiConformanceChecker::checkAllConformances() + 147
9   swift                           0x00000001029ebc8b swift::TypeChecker::checkConformancesInContext(swift::DeclContext*, swift::IterableDeclContext*) + 7515
10  swift                           0x0000000102a31e45 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1589
11  swift                           0x00000001022103f6 swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 4614
12  swift                           0x000000010170e3f6 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 838
13  swift                           0x000000010170a7de swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
14  swift                           0x00000001016a887e main + 1246
15  libdyld.dylib                   0x00007fff59e7f3d5 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x00007ffeee578570  rdx: 0x0000000000000000
  rdi: 0x0000000000000000  rsi: 0x00007f9b13478808  rbp: 0x00007ffeee578550  rsp: 0x00007ffeee5783d0
   r8: 0x0000000000000000   r9: 0x00007f9b160014e8  r10: 0x00007f9b148e8fb0  r11: 0x00007f9b00000001
  r12: 0x0000000000000000  r13: 0x00007f9b13478808  r14: 0x0000000000000000  r15: 0x00007ffeee578570
  rip: 0x0000000102c62c10  rfl: 0x0000000000010246  cr2: 0x000000000000000a
  
Logical CPU:     4
Error Code:      0x00000004
Trap Number:     14


Binary Images:
       0x10167d000 -        0x106271fff +swift (1001.0.82.4) <8E747054-0048-30D7-A5F0-EE951EC91CE7> /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
       0x10d2ad000 -        0x10d3176ef  dyld (655.1.1) <D3E77331-ACE5-349D-A7CC-433D626D4A5B> /usr/lib/dyld
    0x7fff56e66000 -     0x7fff56e67ffb  libSystem.B.dylib (1252.250.1) <E6C586CE-EEFA-3B4B-9316-AC4322AF0E75> /usr/lib/libSystem.B.dylib
    0x7fff570a9000 -     0x7fff570fcff7  libc++.1.dylib (400.9.4) <9A60A190-6C34-339F-BB3D-AACE942009A4> /usr/lib/libc++.1.dylib
    0x7fff570fd000 -     0x7fff57112ff7  libc++abi.dylib (400.17) <38C09CED-9090-3719-90F3-04A2749F5428> /usr/lib/libc++abi.dylib
    0x7fff579e2000 -     0x7fff579ffff7  libedit.3.dylib (50.200.2) <8FBA9760-CFF6-3F53-8DBE-AEC526337D7A> /usr/lib/libedit.3.dylib
    0x7fff580e7000 -     0x7fff58117fff  libncurses.5.4.dylib (53.200.3) <767B4D3D-CF4C-33DF-B282-0CAC013A7AD0> /usr/lib/libncurses.5.4.dylib
    0x7fff5869d000 -     0x7fff58e22fdf  libobjc.A.dylib (756.2) <7C312627-43CB-3234-9324-4DEA92D59F50> /usr/lib/libobjc.A.dylib
    0x7fff5950c000 -     0x7fff5951eff7  libz.1.dylib (70.200.4) <B048FC1F-058F-3A08-A1FE-81D5308CB3E6> /usr/lib/libz.1.dylib
    0x7fff59d02000 -     0x7fff59d06ff3  libcache.dylib (81) <1987D1E1-DB11-3291-B12A-EBD55848E02D> /usr/lib/system/libcache.dylib
    0x7fff59d07000 -     0x7fff59d11ff3  libcommonCrypto.dylib (60118.250.2) <1765BB6E-6784-3653-B16B-CB839721DC9A> /usr/lib/system/libcommonCrypto.dylib
    0x7fff59d12000 -     0x7fff59d19ff7  libcompiler_rt.dylib (63.4) <5212BA7B-B7EA-37B4-AF6E-AC4F507EDFB8> /usr/lib/system/libcompiler_rt.dylib
    0x7fff59d1a000 -     0x7fff59d23ff7  libcopyfile.dylib (146.250.1) <98CD00CD-9B91-3B5C-A9DB-842638050FA8> /usr/lib/system/libcopyfile.dylib
    0x7fff59d24000 -     0x7fff59da8fc3  libcorecrypto.dylib (602.260.2) <01464D24-570C-3B83-9D18-467769E0FCDD> /usr/lib/system/libcorecrypto.dylib
    0x7fff59e2f000 -     0x7fff59e68ff7  libdispatch.dylib (1008.270.1) <97273678-E94C-3C8C-89F6-2E2020F4B43B> /usr/lib/system/libdispatch.dylib
    0x7fff59e69000 -     0x7fff59e95ff7  libdyld.dylib (655.1.1) <002418CC-AD11-3D10-865B-015591D24E6C> /usr/lib/system/libdyld.dylib
    0x7fff59e96000 -     0x7fff59e96ffb  libkeymgr.dylib (30) <0D0F9CA2-8D5A-3273-8723-59987B5827F2> /usr/lib/system/libkeymgr.dylib
    0x7fff59ea4000 -     0x7fff59ea4ff7  liblaunch.dylib (1336.261.2) <2B07E27E-D404-3E98-9D28-BCA641E5C479> /usr/lib/system/liblaunch.dylib
    0x7fff59ea5000 -     0x7fff59eaafff  libmacho.dylib (927.0.3) <A377D608-77AB-3F6E-90F0-B4F251A5C12F> /usr/lib/system/libmacho.dylib
    0x7fff59eab000 -     0x7fff59eadffb  libquarantine.dylib (86.220.1) <6D0BC770-7348-3608-9254-F7FFBD347634> /usr/lib/system/libquarantine.dylib
    0x7fff59eae000 -     0x7fff59eafff7  libremovefile.dylib (45.200.2) <9FBEB2FF-EEBE-31BC-BCFC-C71F8D0E99B6> /usr/lib/system/libremovefile.dylib
    0x7fff59eb0000 -     0x7fff59ec7ff3  libsystem_asl.dylib (356.200.4) <A62A7249-38B8-33FA-9875-F1852590796C> /usr/lib/system/libsystem_asl.dylib
    0x7fff59ec8000 -     0x7fff59ec8ff7  libsystem_blocks.dylib (73) <A453E8EE-860D-3CED-B5DC-BE54E9DB4348> /usr/lib/system/libsystem_blocks.dylib
    0x7fff59ec9000 -     0x7fff59f50fff  libsystem_c.dylib (1272.250.1) <7EDACF78-2FA3-35B8-B051-D70475A35117> /usr/lib/system/libsystem_c.dylib
    0x7fff59f51000 -     0x7fff59f54ffb  libsystem_configuration.dylib (963.270.3) <2B4A836D-68A4-33E6-8D48-CD4486B03387> /usr/lib/system/libsystem_configuration.dylib
    0x7fff59f55000 -     0x7fff59f58ff7  libsystem_coreservices.dylib (66) <719F75A4-74C5-3BA6-A09E-0C5A3E5889D7> /usr/lib/system/libsystem_coreservices.dylib
    0x7fff59f59000 -     0x7fff59f5ffff  libsystem_darwin.dylib (1272.250.1) <EC9B39A5-9592-3577-8997-7DC721D20D8C> /usr/lib/system/libsystem_darwin.dylib
    0x7fff59f60000 -     0x7fff59f66ff7  libsystem_dnssd.dylib (878.270.2) <E9A5ACCF-E35F-3909-AF0A-2A37CD217276> /usr/lib/system/libsystem_dnssd.dylib
    0x7fff59f67000 -     0x7fff59fb2ffb  libsystem_info.dylib (517.200.9) <D09D5AE0-2FDC-3A6D-93EC-729F931B1457> /usr/lib/system/libsystem_info.dylib
    0x7fff59fb3000 -     0x7fff59fdbff7  libsystem_kernel.dylib (4903.270.47) <4195838C-EFEF-3CC9-B459-75032AF7EA1A> /usr/lib/system/libsystem_kernel.dylib
    0x7fff59fdc000 -     0x7fff5a027ff7  libsystem_m.dylib (3158.200.7) <F19B6DB7-014F-3820-831F-389CCDA06EF6> /usr/lib/system/libsystem_m.dylib
    0x7fff5a028000 -     0x7fff5a052fff  libsystem_malloc.dylib (166.270.1) <011F3AD0-8E6A-3A89-AE64-6E5F6840F30A> /usr/lib/system/libsystem_malloc.dylib
    0x7fff5a053000 -     0x7fff5a05dff7  libsystem_networkextension.dylib (767.250.2) <FF06F13A-AEFE-3A27-A073-910EF78AEA36> /usr/lib/system/libsystem_networkextension.dylib
    0x7fff5a05e000 -     0x7fff5a065fff  libsystem_notify.dylib (172.200.21) <145B5CFC-CF73-33CE-BD3D-E8DDE268FFDE> /usr/lib/system/libsystem_notify.dylib
    0x7fff5a066000 -     0x7fff5a06ffef  libsystem_platform.dylib (177.270.1) <9D1FE5E4-EB7D-3B3F-A8D1-A96D9CF1348C> /usr/lib/system/libsystem_platform.dylib
    0x7fff5a070000 -     0x7fff5a07aff7  libsystem_pthread.dylib (330.250.2) <2D5C08FF-484F-3D59-9132-CE1DCB3F76D7> /usr/lib/system/libsystem_pthread.dylib
    0x7fff5a07b000 -     0x7fff5a07eff7  libsystem_sandbox.dylib (851.270.1) <9494594B-5199-3186-82AB-5FF8BED6EE16> /usr/lib/system/libsystem_sandbox.dylib
    0x7fff5a07f000 -     0x7fff5a081ff3  libsystem_secinit.dylib (30.260.2) <EF1EA47B-7B22-35E8-BD9B-F7003DCB96AE> /usr/lib/system/libsystem_secinit.dylib
    0x7fff5a082000 -     0x7fff5a089ff3  libsystem_symptoms.dylib (820.267.1) <03F1C2DD-0F5A-3D9D-88F6-B26C0F94EB52> /usr/lib/system/libsystem_symptoms.dylib
    0x7fff5a08a000 -     0x7fff5a09ffff  libsystem_trace.dylib (906.260.1) <FC761C3B-5434-3A52-912D-F1B15FAA8EB2> /usr/lib/system/libsystem_trace.dylib
    0x7fff5a0a1000 -     0x7fff5a0a6ffb  libunwind.dylib (35.4) <24A97A67-F017-3CFC-B0D0-6BD0224B1336> /usr/lib/system/libunwind.dylib
    0x7fff5a0a7000 -     0x7fff5a0d6fff  libxpc.dylib (1336.261.2) <7DEE2300-6D8E-3C00-9C63-E3E80D56B0C4> /usr/lib/system/libxpc.dylib

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 2271
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=318.9M resident=0K(0%) swapped_out_or_unallocated=318.9M(100%)
Writable regions: Total=129.8M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=129.8M(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Kernel Alloc Once                    8K        1 
MALLOC                           121.4M       21 
MALLOC guard page                   16K        4 
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE (reserved)             256K        1         reserved VM address space (unallocated)
__DATA                            5604K       43 
__LINKEDIT                       230.9M        3 
__TEXT                            88.0M       42 
mapped file                       18.1M       12 
shared memory                        8K        2 
===========                     =======  ======= 
TOTAL                            528.1M      131 
TOTAL, minus reserved VM space   527.8M      131 
@belkadan
Copy link
Contributor

cc @DougGregor

@theblixguy
Copy link
Collaborator

Works on Swift 5.1, as long as you mark `cSetter` as `mutating` in protocol A (and in the implementation).

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

@theblixguy I did just try it in Xcode 11.0.0-Beta.5 and it seems this bug was fixed. Unfortunate, as our codebase is all Swift 5.0.1. Are there plans of releasing a 5.0.2 or similar to fix bugs like this?

@swift-ci
Copy link
Collaborator Author

Comment by Ky (JIRA)

Seems this is a duplicate of SR-10438. Will defer to that one for future updates

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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
Projects
None yet
Development

No branches or pull requests

3 participants