Skip to content

Commit c6b8ae5

Browse files
authored
Merge pull request #25937 from hamishknight/all-the-sugar
2 parents dfb90b7 + 49bdc45 commit c6b8ae5

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/AST/Type.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,16 +1233,23 @@ CanType TypeBase::getCanonicalType(GenericSignature *sig) {
12331233
}
12341234

12351235
TypeBase *TypeBase::reconstituteSugar(bool Recursive) {
1236-
auto Func = [](Type Ty) -> Type {
1236+
auto Func = [Recursive](Type Ty) -> Type {
12371237
if (auto boundGeneric = dyn_cast<BoundGenericType>(Ty.getPointer())) {
1238+
1239+
auto getGenericArg = [&](unsigned i) -> Type {
1240+
auto arg = boundGeneric->getGenericArgs()[i];
1241+
if (Recursive)
1242+
arg = arg->reconstituteSugar(Recursive);
1243+
return arg;
1244+
};
1245+
12381246
auto &ctx = boundGeneric->getASTContext();
12391247
if (boundGeneric->getDecl() == ctx.getArrayDecl())
1240-
return ArraySliceType::get(boundGeneric->getGenericArgs()[0]);
1248+
return ArraySliceType::get(getGenericArg(0));
12411249
if (boundGeneric->getDecl() == ctx.getDictionaryDecl())
1242-
return DictionaryType::get(boundGeneric->getGenericArgs()[0],
1243-
boundGeneric->getGenericArgs()[1]);
1250+
return DictionaryType::get(getGenericArg(0), getGenericArg(1));
12441251
if (boundGeneric->getDecl() == ctx.getOptionalDecl())
1245-
return OptionalType::get(boundGeneric->getGenericArgs()[0]);
1252+
return OptionalType::get(getGenericArg(0));
12461253
}
12471254
return Ty;
12481255
};

test/Constraints/fixes.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ struct FooStruct {
261261
}
262262

263263
let e: BarStruct? = BarStruct()
264+
265+
func f() -> Optional<Optional<Int>> { return 29 }
264266
}
265267

266268
struct BarStruct {
@@ -290,6 +292,9 @@ let _: Int = thing?.e?.a() // expected-error {{value of optional type 'Int?' mus
290292
let _: Int? = thing?.e?.b // expected-error {{value of optional type 'Int??' must be unwrapped to a value of type 'Int?'}}
291293
// expected-note@-1{{coalesce}}
292294
// expected-note@-2{{force-unwrap}}
295+
let _: Int? = thing?.f() // expected-error {{value of optional type 'Int??' must be unwrapped to a value of type 'Int?'}}
296+
// expected-note@-1{{coalesce}}
297+
// expected-note@-2{{force-unwrap}}
293298

294299
// SR-9851 - https://bugs.swift.org/browse/SR-9851
295300
func coalesceWithParensRootExprFix() {

0 commit comments

Comments
 (0)