Skip to content

Commit a627e35

Browse files
[Diagnostics] Use DeclDescriptiveKind on data flow diagnostics to improve diagnostic message
1 parent 00c2531 commit a627e35

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,30 @@ NOTE(designated_init_c_struct_fix,none,
250250

251251

252252
// Control flow diagnostics.
253-
ERROR(missing_return,none,
254-
"missing return in a %select{function|closure}1 expected to return %0",
255-
(Type, unsigned))
256-
ERROR(missing_return_last_expr,none,
257-
"missing return in a %select{function|closure}1 expected to return %0; "
253+
ERROR(missing_return_closure,none,
254+
"missing return in a closure expected to return %0",
255+
(Type))
256+
ERROR(missing_return_last_expr_closure,none,
257+
"missing return in a closure expected to return %0; "
258258
"did you mean to return the last expression?",
259-
(Type, unsigned))
260-
ERROR(missing_never_call,none,
261-
"%select{function|closure}1 with uninhabited return type %0 is missing "
259+
(Type))
260+
ERROR(missing_never_call_closure,none,
261+
"closure with uninhabited return type %0 is missing "
262262
"call to another never-returning function on all paths",
263-
(Type, unsigned))
263+
(Type))
264+
265+
ERROR(missing_return_decl,none,
266+
"missing return in a %1 expected to return %0",
267+
(Type, DescriptiveDeclKind))
268+
ERROR(missing_return_last_expr_decl,none,
269+
"missing return in a %1 expected to return %0; "
270+
"did you mean to return the last expression?",
271+
(Type, DescriptiveDeclKind))
272+
ERROR(missing_never_call_decl,none,
273+
"%1 with uninhabited return type %0 is missing "
274+
"call to another never-returning function on all paths",
275+
(Type, DescriptiveDeclKind))
276+
264277
ERROR(guard_body_must_not_fallthrough,none,
265278
"'guard' body must not fall through, consider using a 'return' or 'throw'"
266279
" to exit the scope", ())

lib/SILOptimizer/Mandatory/DataflowDiagnostics.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,38 @@ static void diagnoseMissingReturn(const UnreachableInst *UI,
6464
auto element = BS->getLastElement();
6565
if (auto expr = element.dyn_cast<Expr *>()) {
6666
if (expr->getType()->getRValueType()->isEqual(ResTy)) {
67-
Context.Diags.diagnose(
68-
expr->getStartLoc(),
69-
diag::missing_return_last_expr, ResTy,
70-
FLoc.isASTNode<ClosureExpr>() ? 1 : 0)
71-
.fixItInsert(expr->getStartLoc(), "return ");
67+
if (FLoc.isASTNode<ClosureExpr>()) {
68+
Context.Diags
69+
.diagnose(expr->getStartLoc(),
70+
diag::missing_return_last_expr_closure, ResTy)
71+
.fixItInsert(expr->getStartLoc(), "return ");
72+
} else {
73+
auto *DC = FLoc.getAsDeclContext();
74+
assert(DC && DC->getAsDecl() && "not a declaration?");
75+
Context.Diags
76+
.diagnose(expr->getStartLoc(),
77+
diag::missing_return_last_expr_decl, ResTy,
78+
DC->getAsDecl()->getDescriptiveKind())
79+
.fixItInsert(expr->getStartLoc(), "return ");
80+
}
7281
return;
7382
}
7483
}
7584
}
76-
auto diagID = F->isNoReturnFunction(F->getTypeExpansionContext())
77-
? diag::missing_never_call
78-
: diag::missing_return;
79-
diagnose(Context,
80-
L.getEndSourceLoc(),
81-
diagID, ResTy,
82-
FLoc.isASTNode<ClosureExpr>() ? 1 : 0);
85+
86+
bool isNoReturnFn = F->isNoReturnFunction(F->getTypeExpansionContext());
87+
if (FLoc.isASTNode<ClosureExpr>()) {
88+
auto diagID = isNoReturnFn ? diag::missing_never_call_closure
89+
: diag::missing_return_closure;
90+
diagnose(Context, L.getEndSourceLoc(), diagID, ResTy);
91+
} else {
92+
auto *DC = FLoc.getAsDeclContext();
93+
assert(DC && DC->getAsDecl() && "not a declaration?");
94+
auto diagID = isNoReturnFn ? diag::missing_never_call_decl
95+
: diag::missing_return_decl;
96+
diagnose(Context, L.getEndSourceLoc(), diagID, ResTy,
97+
DC->getAsDecl()->getDescriptiveKind());
98+
}
8399
}
84100

85101
static void diagnoseUnreachable(const SILInstruction *I,

0 commit comments

Comments
 (0)