Skip to content

[Sema] Do not attempt openning same generic argument twice while checking default parameter #61180

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 2 commits into from
Sep 20, 2022
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
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
assert(!type->is<UnboundGenericType>());

if (auto *GP = type->getAs<GenericTypeParamType>()) {
auto openedVar = genericParameters.find(getCanonicalGenericParamTy(GP));
if (openedVar != genericParameters.end()) {
return openedVar->second;
}
return cs.openGenericParameter(DC->getParent(), GP, genericParameters,
locator);
}
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/type_inference_from_default_exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,20 @@ class BaseStorage<T> : Storage, StorageType {

final class CustomStorage<T>: BaseStorage<T> { // Ok - no crash typechecking inherited init
}

// https://github.com/apple/swift/issues/61061

struct S61061<T> where T:Hashable { // expected-note{{'T' declared as parameter to type 'S61061'}}
init(x:[[T: T]] = [:]) {} // expected-error{{default argument value of type '[AnyHashable : Any]' cannot be converted to type '[[T : T]]'}}
// expected-error@-1{{generic parameter 'T' could not be inferred}}
}

struct S61061_1<T> where T:Hashable { // expected-note{{'T' declared as parameter to type 'S61061_1'}}
init(x:[(T, T)] = [:]) {} // expected-error{{default argument value of type '[AnyHashable : Any]' cannot be converted to type '[(T, T)]'}}
// expected-error@-1{{generic parameter 'T' could not be inferred}}
}

// TODO(diagnostics): Should produce a conflicting types inferred for generic argument 'T'
struct S61061_2<T> where T:Hashable {
init(x:[(T, T)] = [(1, "")]) {} // expected-error{{type of expression is ambiguous without more context}}
}