Skip to content

Commit e458b53

Browse files
Merge pull request #61180 from LucianoPAlmeida/crash-default
[Sema] Do not attempt openning same generic argument twice while checking default parameter
2 parents 7054460 + 6c2fad4 commit e458b53

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
548548
assert(!type->is<UnboundGenericType>());
549549

550550
if (auto *GP = type->getAs<GenericTypeParamType>()) {
551+
auto openedVar = genericParameters.find(getCanonicalGenericParamTy(GP));
552+
if (openedVar != genericParameters.end()) {
553+
return openedVar->second;
554+
}
551555
return cs.openGenericParameter(DC->getParent(), GP, genericParameters,
552556
locator);
553557
}

test/Constraints/type_inference_from_default_exprs.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,20 @@ class BaseStorage<T> : Storage, StorageType {
233233

234234
final class CustomStorage<T>: BaseStorage<T> { // Ok - no crash typechecking inherited init
235235
}
236+
237+
// https://github.com/apple/swift/issues/61061
238+
239+
struct S61061<T> where T:Hashable { // expected-note{{'T' declared as parameter to type 'S61061'}}
240+
init(x:[[T: T]] = [:]) {} // expected-error{{default argument value of type '[AnyHashable : Any]' cannot be converted to type '[[T : T]]'}}
241+
// expected-error@-1{{generic parameter 'T' could not be inferred}}
242+
}
243+
244+
struct S61061_1<T> where T:Hashable { // expected-note{{'T' declared as parameter to type 'S61061_1'}}
245+
init(x:[(T, T)] = [:]) {} // expected-error{{default argument value of type '[AnyHashable : Any]' cannot be converted to type '[(T, T)]'}}
246+
// expected-error@-1{{generic parameter 'T' could not be inferred}}
247+
}
248+
249+
// TODO(diagnostics): Should produce a conflicting types inferred for generic argument 'T'
250+
struct S61061_2<T> where T:Hashable {
251+
init(x:[(T, T)] = [(1, "")]) {} // expected-error{{type of expression is ambiguous without more context}}
252+
}

0 commit comments

Comments
 (0)