Skip to content

Commit 4a9722e

Browse files
committed
[Sema] Prevent duplicate diagnostics for opaque return type
1 parent 5a91cea commit 4a9722e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
7979
.diagnose(repr->getLoc(), diag::opaque_type_in_protocol_requirement)
8080
.fixItInsert(fixitLoc, result)
8181
.fixItReplace(repr->getSourceRange(), placeholder);
82+
repr->setInvalid();
8283

8384
return nullptr;
8485
}
@@ -162,6 +163,7 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
162163
.diagnose(currentRepr->getLoc(), diag::opaque_of_optional_rewrite)
163164
.fixItReplaceChars(currentRepr->getStartLoc(),
164165
currentRepr->getEndLoc(), stream.str());
166+
repr->setInvalid();
165167
return nullptr;
166168
}
167169
}
@@ -199,6 +201,7 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
199201
// Error out if the constraint type isn't a class or existential type.
200202
ctx.Diags.diagnose(currentRepr->getLoc(),
201203
diag::opaque_type_invalid_constraint);
204+
currentRepr->setInvalid();
202205
return nullptr;
203206
}
204207

@@ -246,6 +249,7 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
246249
ctx.Diags.diagnose(repr->getLoc(),
247250
diag::opaque_type_in_parameter,
248251
false, interfaceType);
252+
repr->setInvalid();
249253
return true;
250254
}
251255
}

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,8 +2365,10 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
23652365
bool isInExistential = diagnoseDisallowedExistential(opaqueRepr);
23662366

23672367
if (auto opaqueDecl = dyn_cast<OpaqueTypeDecl>(DC)) {
2368-
if (auto ordinal = opaqueDecl->getAnonymousOpaqueParamOrdinal(opaqueRepr))
2369-
return getIdentityOpaqueTypeArchetypeType(opaqueDecl, *ordinal);
2368+
if (auto ordinal = opaqueDecl->getAnonymousOpaqueParamOrdinal(opaqueRepr)){
2369+
if(!isInExistential)
2370+
return getIdentityOpaqueTypeArchetypeType(opaqueDecl, *ordinal);
2371+
}
23702372
}
23712373

23722374
// Check whether any of the generic parameters in the context represents
@@ -2382,7 +2384,7 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
23822384
}
23832385
}
23842386

2385-
if (!isInExistential){
2387+
if (!repr->isInvalid()){
23862388
// We are not inside an `OpaqueTypeDecl`, so diagnose an error.
23872389
if (!(options & TypeResolutionFlags::SilenceErrors)) {
23882390
diagnose(opaqueRepr->getOpaqueLoc(),
@@ -4712,6 +4714,7 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
47124714
if (constraintType->hasError())
47134715
return ErrorType::get(getASTContext());
47144716

4717+
//TO-DO: generalize this and emit the same erorr for some P?
47154718
if (!constraintType->isConstraintType()) {
47164719
// Emit a tailored diagnostic for the incorrect optional
47174720
// syntax 'any P?' with a fix-it to add parenthesis.

0 commit comments

Comments
 (0)