Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ class TypeVariableRefFinder : public ASTWalker {
return;
}

// Desugar type before collecting type variables, otherwise
// we can bring in scope unrelated type variables passed
// into the closure (via parameter/result) from contextual type.
// For example `Typealias<$T, $U>.Context` which desugars into
// `_Context<$U>` would bring in `$T` that could be inferrable
// only after the body of the closure is solved.
type = type->getDesugaredType();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think getDesugaredType() only unwraps one level of sugar, but you might have type aliases in nested position. Maybe you should use getCanonicalType() instead? Are other callers of getTypeVariables() also affected by this? Perhaps getTypeVariables() should canonicalize the type itself first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other callers of getTypeVariables() don't really care because only closures are solved in isolation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, getDesugaredType would only unwrap N levels of sugar if they occur in sequence... I'll switch this to getCanonicalType().


// Don't walk into the opaque archetypes because they are not
// transparent in this context - `some P` could reference a
// type variables as substitutions which are visible only to
Expand Down
51 changes: 51 additions & 0 deletions validation-test/Sema/SwiftUI/rdar107835060.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15 -swift-version 5 -disable-availability-checking

// REQUIRES: objc_interop
// REQUIRES: OS=macosx

import SwiftUI

protocol Model<ReturnType> {
associatedtype ReturnType
}

struct AnyModel<ReturnType>: Model {
}

protocol ContentProtocol : View {
associatedtype _Context
}

struct CollectionContext<Data: RandomAccessCollection> {
let offset: Data.Index
}

struct ContinuousContent<Data: RandomAccessCollection, Content: View> : ContentProtocol
where Data.Element: Model, Data.Element.ReturnType: Sequence, Data.Index: Hashable {

typealias _Context = CollectionContext<Data>

var body: some View { EmptyView() }
}

struct TestView<Data, Content: View> : View {
typealias Context = Content._Context where Content: ContentProtocol

init<R, C>(_ data: Data,
@ViewBuilder shelfContent: @escaping (Context) -> C)
where Data.Element == any Model<R>,
Content == ContinuousContent<LazyMapCollection<Data, AnyModel<R>>, C> {
}

var body: some View { EmptyView() }
}

@ViewBuilder
func test(values: [any Model<[Int]>]) -> some View {
TestView(values) { context in
VStack {
if context.offset == 0 {
}
}
}
}