Skip to content

[CSClosure] Fix handling of non-representivate variables #62773

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

Merged
merged 1 commit into from
Jan 4, 2023
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
14 changes: 13 additions & 1 deletion lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,22 @@ class TypeVariableRefFinder : public ASTWalker {
return;
}

// 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
// the outer context.
if (type->is<OpaqueTypeArchetypeType>())
return;

if (type->hasTypeVariable()) {
SmallPtrSet<TypeVariableType *, 4> typeVars;
type->getTypeVariables(typeVars);
ReferencedVars.insert(typeVars.begin(), typeVars.end());

// Some of the type variables could be non-representative, so
// we need to recurse into `inferTypeVariables` to property
// handle them.
for (auto *typeVar : typeVars)
inferVariables(typeVar);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-typecheck-verify-swift

struct Description: Hashable {
let name: String
let id: Int
}

struct Value {
let ID: Int?
}

func test(allValues: [Value]) {
// Type for `return nil` cannot be inferred at the moment because there is no join for result expressions.
let owners = Set(allValues.compactMap { // expected-error {{generic parameter 'Element' could not be inferred}}
// expected-note@-1 {{explicitly specify the generic arguments to fix this issue}}
guard let id = $0.ID else { return nil }
return Description(name: "", id: id)
})
}