Skip to content

[AST] Properly recurse when reconstituting sugar #25937

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
Jul 5, 2019
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
17 changes: 12 additions & 5 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,16 +1233,23 @@ CanType TypeBase::getCanonicalType(GenericSignature *sig) {
}

TypeBase *TypeBase::reconstituteSugar(bool Recursive) {
auto Func = [](Type Ty) -> Type {
auto Func = [Recursive](Type Ty) -> Type {
if (auto boundGeneric = dyn_cast<BoundGenericType>(Ty.getPointer())) {

auto getGenericArg = [&](unsigned i) -> Type {
auto arg = boundGeneric->getGenericArgs()[i];
if (Recursive)
arg = arg->reconstituteSugar(Recursive);
return arg;
};

auto &ctx = boundGeneric->getASTContext();
if (boundGeneric->getDecl() == ctx.getArrayDecl())
return ArraySliceType::get(boundGeneric->getGenericArgs()[0]);
return ArraySliceType::get(getGenericArg(0));
if (boundGeneric->getDecl() == ctx.getDictionaryDecl())
return DictionaryType::get(boundGeneric->getGenericArgs()[0],
boundGeneric->getGenericArgs()[1]);
return DictionaryType::get(getGenericArg(0), getGenericArg(1));
if (boundGeneric->getDecl() == ctx.getOptionalDecl())
return OptionalType::get(boundGeneric->getGenericArgs()[0]);
return OptionalType::get(getGenericArg(0));
}
return Ty;
};
Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/fixes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ struct FooStruct {
}

let e: BarStruct? = BarStruct()

func f() -> Optional<Optional<Int>> { return 29 }
}

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

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