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-6516] error: reference to invalid associated type 'Context' of type 'S' #49066

Closed
weissi opened this issue Dec 1, 2017 · 7 comments · Fixed by #69826
Closed

[SR-6516] error: reference to invalid associated type 'Context' of type 'S' #49066

weissi opened this issue Dec 1, 2017 · 7 comments · Fixed by #69826
Labels
associated type inference bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances swift 6.0 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error

Comments

@weissi
Copy link
Member

weissi commented Dec 1, 2017

Previous ID SR-6516
Radar rdar://problem/38913692
Original Reporter @weissi
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: ab85d612bfcfa69795091d50765d64c6

relates to:

Issue Description:

This issue is related to #48118 and the '[swift-evolution] [RFC] Associated type inference' thread that Doug started.

So first of all I must say I'm delighted that there's ongoing work in this area and we seem to be getting closer to fixing this issue which is blocking us pretty badly.

The code below is almost the same as the one in #48118 but I changed the typealias Context = ... to an associatedtype Context = .... In Swift 4.0 that doesn't make any difference and crashes the compiler.

In a recent (4.1-dev (LLVM 5b54bd1e96, Clang 03ed64977b, Swift 88a7a55e83)) Swift 4.1 dev snapshot, however we get a different error which also seems bogus:

test2.swift:33:25: error: reference to invalid associated type 'Context' of type 'S'
        public func f2(_ x: Context, _ y: PB) {

That's really odd as it just works fine for f1 but breaks for f2, I'm confused.

public struct Foo<A, B, C> {}

public protocol P {
    associatedtype PA
    associatedtype PB = Never
    associatedtype PC = Never

    associatedtype Context = Foo<PA, PB, PC>

    func f1(_ x: Context, _ y: PA)
    func f2(_ x: Context, _ y: PB)
    func f3(_ x: Context, _ y: PC)
    func f4(_ x: Context)
}

public extension P {
    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
    public func f3(_ x: Context, _ y: PC) {
    }
    public func f4(_ x: Context) {
    }
}

public struct S: P {
    public typealias PA = String
    public typealias PB = Int

    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
}

Also quite interestingly, in a recent (LLVM 20c8fee562, Clang b227f55990, Swift 603e5359fd) Swift master dev snapshot I get the following messages for the code above:

$ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-11-28-a.xctoolchain/usr/bin/swiftc test2.swift 
    test2.swift:33:25: error: reference to invalid associated type 'Context' of type 'S'
        public func f2(_ x: Context, _ y: PB) {
                            ^
    test2.swift:33:17: warning: instance method 'f2' nearly matches defaulted requirement 'f2' of protocol 'P'
        public func f2(_ x: Context, _ y: PB) {
                    ^
    test2.swift:33:17: note: move 'f2' to an extension to silence this warning
        public func f2(_ x: Context, _ y: PB) {
                    ^
    test2.swift:11:10: note: requirement 'f2' declared here
        func f2(_ x: Context, _ y: PB)
             ^

very odd, on top of the error that the 4.1 dev snapshot shows too it also gives me a near miss warning that

public func f2(_ x: Context, _ y: PB)

is almost

func f2(_ x: Context, _ y: PB)

to which I'd say that this isn't a near miss but an exact hit?

@belkadan
Copy link
Contributor

belkadan commented Dec 1, 2017

I don't think associated types have ever been able to reference other associated types in their defaults. (There are other bugs about this.)

cc @DougGregor

@DougGregor
Copy link
Member

It's reasonable to want to do this, and IIRC we have code in the associated type inference logic to try to support it.

@weissi
Copy link
Member Author

weissi commented Mar 27, 2018

@swift-ci create

@weissi
Copy link
Member Author

weissi commented Apr 20, 2018

update: still the same on 4.1 release:

$ swiftc -c test.swift
test.swift:33:25: error: reference to invalid associated type 'Context' of type 'S'
    public func f2(_ x: Context, _ y: PB) {
                        ^

@MaxDesiatov
Copy link
Member

MaxDesiatov commented Jan 29, 2019

Reproducible for me with this simple code with Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1) and Apple Swift version 5.0 (swiftlang-1001.0.45.7 clang-1001.0.37.7):

public struct Foo<A, B> {}

public protocol P {
    associatedtype PA
    associatedtype PB

    associatedtype Context = Foo<PA, PB>

    func f2(_ x: Context, _ y: PB)
}

public struct S: P {
    public typealias PA = String
    public typealias PB = Int

    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
}

Interestingly enough, commenting out S.f1 makes the error go away, which is super weird. Also, placing S.f2 definition before S.f1 makes the problem go away too, so this second snippet compiles perfectly fine:

public struct Foo<A, B> {}

public protocol P {
    associatedtype PA
    associatedtype PB

    associatedtype Context = Foo<PA, PB>

    func f2(_ x: Context, _ y: PB)
}

public struct S: P {
    public typealias PA = String
    public typealias PB = Int

    public func f2(_ x: Context, _ y: PB) {
    }
    public func f1(_ x: Context, _ y: PA) {
    }
}

@weissi
Copy link
Member Author

weissi commented Aug 27, 2019

still an issue with 5.0.2

$ cat test.swift 
public struct Foo<A, B> {}

public protocol P {
    associatedtype PA
    associatedtype PB

    associatedtype Context = Foo<PA, PB>

    func f2(_ x: Context, _ y: PB)
}

public struct S: P {
    public typealias PA = String
    public typealias PB = Int

    public func f1(_ x: Context, _ y: PA) {
    }
    public func f2(_ x: Context, _ y: PB) {
    }
}
$ swiftc test.swift 
test.swift:18:25: error: reference to invalid associated type 'Context' of type 'S'
    public func f2(_ x: Context, _ y: PB) {
                        ^

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
slavapestov added a commit to slavapestov/swift that referenced this issue Nov 13, 2023
…request cycle

This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / apple#48680
- rdar://38913692 / apple#49066
- rdar://56672411
- apple#50010
- rdar://81587765 / apple#57355
- rdar://117442510
slavapestov added a commit to slavapestov/swift that referenced this issue Nov 14, 2023
…request cycle

This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / apple#48680
- rdar://38913692 / apple#49066
- rdar://56672411
- apple#50010
- rdar://81587765 / apple#57355
- rdar://117442510
slavapestov added a commit to slavapestov/swift that referenced this issue Nov 14, 2023
…request cycle

This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / apple#48680
- rdar://38913692 / apple#49066
- rdar://56672411
- apple#50010
- rdar://81587765 / apple#57355
- rdar://117442510
slavapestov added a commit to slavapestov/swift that referenced this issue Nov 14, 2023
…request cycle

This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / apple#48680
- rdar://38913692 / apple#49066
- rdar://56672411
- apple#50010
- rdar://81587765 / apple#57355
- rdar://117442510
@slavapestov
Copy link
Member

Fixed by #69826

slavapestov added a commit to slavapestov/swift that referenced this issue Nov 17, 2023
…request cycle

This implements a structural walk over the TypeRepr to catch
situations where we attempt to infer `A` from `func f(_: A)`,
which references the concrete `A` that will be synthesized
in the conforming type.

Fixes:
- rdar://34956654 / apple#48680
- rdar://38913692 / apple#49066
- rdar://56672411
- apple#50010
- rdar://81587765 / apple#57355
- rdar://117442510
@AnthonyLatsis AnthonyLatsis added type checker Area → compiler: Semantic analysis conformances Feature → protocol: protocol conformances associated type inference unexpected error Bug: Unexpected error swift 6.0 labels Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
associated type inference bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances swift 6.0 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants