Skip to content

[MiscDiagnostics] Fix a crash in OpaqueUnderlyingTypeChecker #42039

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
Mar 28, 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
29 changes: 16 additions & 13 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2646,15 +2646,19 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
// TODO [OPAQUE SUPPORT]: diagnose multiple opaque types
SubstitutionMap underlyingSubs = Candidates.front().second;
if (Candidates.size() > 1) {
unsigned mismatchIndex = OpaqueDecl->getOpaqueGenericParams().size();
for (auto genericParam : OpaqueDecl->getOpaqueGenericParams()) {
unsigned index = genericParam->getIndex();
Type underlyingType = Candidates[0].second.getReplacementTypes()[index];
Optional<std::pair<unsigned, GenericTypeParamType *>> mismatch;

auto opaqueParams = OpaqueDecl->getOpaqueGenericParams();
for (auto index : indices(opaqueParams)) {
auto *genericParam = opaqueParams[index];

Type underlyingType = Type(genericParam).subst(underlyingSubs);
bool found = false;
for (const auto &candidate : Candidates) {
Type otherType = candidate.second.getReplacementTypes()[index];
Type otherType = Type(genericParam).subst(candidate.second);

if (!underlyingType->isEqual(otherType)) {
mismatchIndex = index;
mismatch.emplace(index, genericParam);
found = true;
break;
}
Expand All @@ -2663,28 +2667,27 @@ class OpaqueUnderlyingTypeChecker : public ASTWalker {
if (found)
break;
}
assert(mismatchIndex < OpaqueDecl->getOpaqueGenericParams().size());
assert(mismatch.hasValue());

if (auto genericParam =
OpaqueDecl->getExplicitGenericParam(mismatchIndex)) {
OpaqueDecl->getExplicitGenericParam(mismatch->first)) {
Implementation->diagnose(
diag::opaque_type_mismatched_underlying_type_candidates_named,
genericParam->getName())
.highlight(genericParam->getLoc());
} else {
TypeRepr *opaqueRepr =
OpaqueDecl->getOpaqueReturnTypeReprs()[mismatchIndex];
OpaqueDecl->getOpaqueReturnTypeReprs()[mismatch->first];
Implementation->diagnose(
diag::opaque_type_mismatched_underlying_type_candidates,
opaqueRepr)
.highlight(opaqueRepr->getSourceRange());
}

for (auto candidate : Candidates) {
Ctx.Diags.diagnose(
candidate.first->getLoc(),
diag::opaque_type_underlying_type_candidate_here,
candidate.second.getReplacementTypes()[mismatchIndex]);
Ctx.Diags.diagnose(candidate.first->getLoc(),
diag::opaque_type_underlying_type_candidate_here,
Type(mismatch->second).subst(candidate.second));
}
return;
}
Expand Down
14 changes: 14 additions & 0 deletions test/type/opaque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,17 @@ func takesOpaqueProtocol<T : OpaqueProtocol>(generic: T) {

func opaquePlaceholderFunc() -> some _ { 1 } // expected-error {{type placeholder not allowed here}}
var opaquePlaceholderVar: some _ = 1 // expected-error {{type placeholder not allowed here}}

// rdar://90456579 - crash in `OpaqueUnderlyingTypeChecker`
func test_diagnostic_with_contextual_generic_params() {
struct S {
func test<T: Q>(t: T) -> some Q {
// expected-error@-1 {{function declares an opaque return type 'some Q', but the return statements in its body do not have matching underlying types}}
if true {
return t // expected-note {{return statement has underlying type 'T'}}
}
return "" // String conforms to `Q`
// expected-note@-1 {{return statement has underlying type 'String'}}
}
}
}