Skip to content

Commit 1a38032

Browse files
authored
Merge pull request #27035 from theblixguy/chore/enumify-classifyOptionalityIssues
[Typechecker] Enumify classifyOptionalityIssues()
2 parents 07bcf5a + d577f4b commit 1a38032

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,9 +1927,29 @@ SourceLoc OptionalAdjustment::getOptionalityLoc(TypeRepr *tyR) const {
19271927
return SourceLoc();
19281928
}
19291929

1930+
namespace {
1931+
/// Describes the position for optional adjustment made to a witness.
1932+
///
1933+
/// This is used by the following diagnostics:
1934+
/// 1) 'err_protocol_witness_optionality',
1935+
/// 2) 'warn_protocol_witness_optionality'
1936+
/// 3) 'protocol_witness_optionality_conflict'
1937+
enum class OptionalAdjustmentPosition : unsigned {
1938+
/// The type of a variable.
1939+
VarType = 0,
1940+
/// The result type of something.
1941+
Result = 1,
1942+
/// The parameter type of something.
1943+
Param = 2,
1944+
/// The parameter types of something.
1945+
MultipleParam = 3,
1946+
/// Both return and parameter adjustments.
1947+
ParamAndReturn = 4,
1948+
};
1949+
} // end anonymous namespace
1950+
19301951
/// Classify the provided optionality issues for use in diagnostics.
1931-
/// FIXME: Enumify this
1932-
static unsigned classifyOptionalityIssues(
1952+
static OptionalAdjustmentPosition classifyOptionalityIssues(
19331953
const SmallVectorImpl<OptionalAdjustment> &adjustments,
19341954
ValueDecl *requirement) {
19351955
unsigned numParameterAdjustments = 0;
@@ -1942,21 +1962,20 @@ static unsigned classifyOptionalityIssues(
19421962
}
19431963

19441964
if (hasNonParameterAdjustment) {
1945-
// Both return and parameter adjustments.
19461965
if (numParameterAdjustments > 0)
1947-
return 4;
1966+
return OptionalAdjustmentPosition::ParamAndReturn;
19481967

1949-
// The type of a variable.
19501968
if (isa<VarDecl>(requirement))
1951-
return 0;
1969+
return OptionalAdjustmentPosition::VarType;
19521970

1953-
// The result type of something.
1954-
return 1;
1971+
return OptionalAdjustmentPosition::Result;
19551972
}
19561973

19571974
// Only parameter adjustments.
19581975
assert(numParameterAdjustments > 0 && "No adjustments?");
1959-
return numParameterAdjustments == 1 ? 2 : 3;
1976+
return numParameterAdjustments == 1
1977+
? OptionalAdjustmentPosition::Param
1978+
: OptionalAdjustmentPosition::MultipleParam;
19601979
}
19611980

19621981
static void addOptionalityFixIts(
@@ -2076,10 +2095,11 @@ diagnoseMatch(ModuleDecl *module, NormalProtocolConformance *conformance,
20762095

20772096
case MatchKind::OptionalityConflict: {
20782097
auto &adjustments = match.OptionalAdjustments;
2079-
auto diag = diags.diagnose(match.Witness,
2098+
auto issues =
2099+
static_cast<unsigned>(classifyOptionalityIssues(adjustments, req));
2100+
auto diag = diags.diagnose(match.Witness,
20802101
diag::protocol_witness_optionality_conflict,
2081-
classifyOptionalityIssues(adjustments, req),
2082-
withAssocTypes);
2102+
issues, withAssocTypes);
20832103
addOptionalityFixIts(adjustments,
20842104
match.Witness->getASTContext(),
20852105
match.Witness,
@@ -3167,14 +3187,14 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
31673187
auto &diags = ctx.Diags;
31683188
{
31693189
SourceLoc diagLoc = getLocForDiagnosingWitness(conformance,witness);
3190+
auto issues = static_cast<unsigned>(
3191+
classifyOptionalityIssues(adjustments, requirement));
31703192
auto diag = diags.diagnose(
31713193
diagLoc,
31723194
hasAnyError(adjustments)
3173-
? diag::err_protocol_witness_optionality
3174-
: diag::warn_protocol_witness_optionality,
3175-
classifyOptionalityIssues(adjustments, requirement),
3176-
witness->getFullName(),
3177-
proto->getFullName());
3195+
? diag::err_protocol_witness_optionality
3196+
: diag::warn_protocol_witness_optionality,
3197+
issues, witness->getFullName(), proto->getFullName());
31783198
if (diagLoc == witness->getLoc()) {
31793199
addOptionalityFixIts(adjustments, ctx, witness, diag);
31803200
} else {

0 commit comments

Comments
 (0)