Skip to content

Commit 92a12c0

Browse files
committed
improve diagnostic for inferred uninhabited
1 parent 21d657a commit 92a12c0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,12 @@ ERROR(ambiguous_enum_pattern_type,none,
33033303
WARNING(type_inferred_to_undesirable_type,none,
33043304
"%select{variable|constant}2 %0 inferred to have type %1, "
33053305
"which may be unexpected", (Identifier, Type, bool))
3306+
WARNING(type_inferred_to_uninhabited_type,none,
3307+
"%select{variable|constant}2 %0 inferred to have type %1, "
3308+
"which is an enum with no cases", (Identifier, Type, bool))
3309+
WARNING(type_inferred_to_uninhabited_tuple_type,none,
3310+
"%select{variable|constant}2 %0 inferred to have type %1, "
3311+
"which contains an enum with no cases", (Identifier, Type, bool))
33063312
NOTE(add_explicit_type_annotation_to_silence,none,
33073313
"add an explicit type annotation to silence this warning", ())
33083314

lib/Sema/TypeCheckPattern.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
11001100
Type diagTy = type->getOptionalObjectType();
11011101
if (!diagTy) diagTy = type;
11021102

1103+
auto diag = diag::type_inferred_to_undesirable_type;
11031104
bool shouldRequireType = false;
11041105
if (NP->isImplicit()) {
11051106
// If the whole pattern is implicit, the user didn't write it.
@@ -1111,14 +1112,22 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
11111112
shouldRequireType = true;
11121113
} else if (diagTy->isStructurallyUninhabited()) {
11131114
shouldRequireType = true;
1115+
diag = diag::type_inferred_to_uninhabited_type;
1116+
1117+
if (diagTy->is<TupleType>()) {
1118+
diag = diag::type_inferred_to_uninhabited_tuple_type;
1119+
} else {
1120+
assert((diagTy->is<EnumType>() || diagTy->is<BoundGenericEnumType>()) &&
1121+
"unknown structurally uninhabited type");
1122+
}
11141123
}
11151124

11161125
if (shouldRequireType &&
11171126
!options.is(TypeResolverContext::ForEachStmt) &&
11181127
!options.is(TypeResolverContext::EditorPlaceholderExpr) &&
11191128
!(options & TypeResolutionFlags::FromNonInferredPattern)) {
1120-
diagnose(NP->getLoc(), diag::type_inferred_to_undesirable_type,
1121-
NP->getDecl()->getName(), type, NP->getDecl()->isLet());
1129+
diagnose(NP->getLoc(), diag, NP->getDecl()->getName(), type,
1130+
NP->getDecl()->isLet());
11221131
diagnose(NP->getLoc(), diag::add_explicit_type_annotation_to_silence);
11231132
}
11241133

0 commit comments

Comments
 (0)