Skip to content

Commit 74cd0b0

Browse files
committed
[Typechecker] Enumify classifyOptionalityIssues()
1 parent 6b9133b commit 74cd0b0

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,7 @@ SourceLoc OptionalAdjustment::getOptionalityLoc(TypeRepr *tyR) const {
19301930
}
19311931

19321932
/// Classify the provided optionality issues for use in diagnostics.
1933-
/// FIXME: Enumify this
1934-
static unsigned classifyOptionalityIssues(
1933+
static OptionalAdjustmentPosition classifyOptionalityIssues(
19351934
const SmallVectorImpl<OptionalAdjustment> &adjustments,
19361935
ValueDecl *requirement) {
19371936
unsigned numParameterAdjustments = 0;
@@ -1944,21 +1943,20 @@ static unsigned classifyOptionalityIssues(
19441943
}
19451944

19461945
if (hasNonParameterAdjustment) {
1947-
// Both return and parameter adjustments.
19481946
if (numParameterAdjustments > 0)
1949-
return 4;
1947+
return OptionalAdjustmentPosition::ParamAndReturn;
19501948

1951-
// The type of a variable.
19521949
if (isa<VarDecl>(requirement))
1953-
return 0;
1950+
return OptionalAdjustmentPosition::VarType;
19541951

1955-
// The result type of something.
1956-
return 1;
1952+
return OptionalAdjustmentPosition::Result;
19571953
}
19581954

19591955
// Only parameter adjustments.
19601956
assert(numParameterAdjustments > 0 && "No adjustments?");
1961-
return numParameterAdjustments == 1 ? 2 : 3;
1957+
return numParameterAdjustments == 1
1958+
? OptionalAdjustmentPosition::Param
1959+
: OptionalAdjustmentPosition::MultipleParam;
19621960
}
19631961

19641962
static void addOptionalityFixIts(
@@ -2078,10 +2076,11 @@ diagnoseMatch(ModuleDecl *module, NormalProtocolConformance *conformance,
20782076

20792077
case MatchKind::OptionalityConflict: {
20802078
auto &adjustments = match.OptionalAdjustments;
2081-
auto diag = diags.diagnose(match.Witness,
2079+
auto issues =
2080+
static_cast<unsigned>(classifyOptionalityIssues(adjustments, req));
2081+
auto diag = diags.diagnose(match.Witness,
20822082
diag::protocol_witness_optionality_conflict,
2083-
classifyOptionalityIssues(adjustments, req),
2084-
withAssocTypes);
2083+
issues, withAssocTypes);
20852084
addOptionalityFixIts(adjustments,
20862085
match.Witness->getASTContext(),
20872086
match.Witness,
@@ -3134,14 +3133,14 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
31343133
auto &diags = ctx.Diags;
31353134
{
31363135
SourceLoc diagLoc = getLocForDiagnosingWitness(conformance,witness);
3136+
auto issues = static_cast<unsigned>(
3137+
classifyOptionalityIssues(adjustments, requirement));
31373138
auto diag = diags.diagnose(
31383139
diagLoc,
31393140
hasAnyError(adjustments)
3140-
? diag::err_protocol_witness_optionality
3141-
: diag::warn_protocol_witness_optionality,
3142-
classifyOptionalityIssues(adjustments, requirement),
3143-
witness->getFullName(),
3144-
proto->getFullName());
3141+
? diag::err_protocol_witness_optionality
3142+
: diag::warn_protocol_witness_optionality,
3143+
issues, witness->getFullName(), proto->getFullName());
31453144
if (diagLoc == witness->getLoc()) {
31463145
addOptionalityFixIts(adjustments, ctx, witness, diag);
31473146
} else {

lib/Sema/TypeCheckProtocol.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ class OptionalAdjustment {
347347
SourceLoc getOptionalityLoc(TypeRepr *tyR) const;
348348
};
349349

350+
/// Describes the position for optional adjustment made to a witness
351+
enum class OptionalAdjustmentPosition : unsigned {
352+
/// The type of a variable.
353+
VarType = 0,
354+
/// The result type of something.
355+
Result = 1,
356+
/// The parameter type of something.
357+
Param = 2,
358+
/// The parameter types of something.
359+
MultipleParam = 3,
360+
/// Both return and parameter adjustments.
361+
ParamAndReturn = 4,
362+
};
363+
350364
/// Describes a match between a requirement and a witness.
351365
struct RequirementMatch {
352366
RequirementMatch(ValueDecl *witness, MatchKind kind,

0 commit comments

Comments
 (0)