Skip to content

Commit 7ce4191

Browse files
committed
[Sema] Prevent duplicate diagnostics for opaque return type
1 parent e249fe8 commit 7ce4191

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
@@ -2278,8 +2278,10 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
22782278
bool isInExistential = diagnoseDisallowedExistential(opaqueRepr);
22792279

22802280
if (auto opaqueDecl = dyn_cast<OpaqueTypeDecl>(DC)) {
2281-
if (auto ordinal = opaqueDecl->getAnonymousOpaqueParamOrdinal(opaqueRepr))
2282-
return getIdentityOpaqueTypeArchetypeType(opaqueDecl, *ordinal);
2281+
if (auto ordinal = opaqueDecl->getAnonymousOpaqueParamOrdinal(opaqueRepr)){
2282+
if(!isInExistential)
2283+
return getIdentityOpaqueTypeArchetypeType(opaqueDecl, *ordinal);
2284+
}
22832285
}
22842286

22852287
// Check whether any of the generic parameters in the context represents
@@ -2295,7 +2297,7 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
22952297
}
22962298
}
22972299

2298-
if (!isInExistential){
2300+
if (!repr->isInvalid()){
22992301
// We are not inside an `OpaqueTypeDecl`, so diagnose an error.
23002302
if (!(options & TypeResolutionFlags::SilenceErrors)) {
23012303
diagnose(opaqueRepr->getOpaqueLoc(),
@@ -4515,6 +4517,7 @@ TypeResolver::resolveExistentialType(ExistentialTypeRepr *repr,
45154517
if (constraintType->hasError())
45164518
return ErrorType::get(getASTContext());
45174519

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

0 commit comments

Comments
 (0)