Skip to content

Commit 9406edc

Browse files
committed
[Diagnostics] Tailored diagnostic for missing argument in property wrapper init
1 parent 60bcc94 commit 9406edc

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,6 +3740,10 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
37403740
if (label.empty()) {
37413741
emitDiagnostic(insertLoc, diag::missing_argument_positional, position + 1)
37423742
.fixItInsert(insertLoc, insertText.str());
3743+
} else if (isPropertyWrapperInitialization()) {
3744+
auto *TE = cast<TypeExpr>(fnExpr);
3745+
emitDiagnostic(TE->getLoc(), diag::property_wrapper_missing_arg_init, label,
3746+
resolveType(TE->getInstanceType())->getString());
37433747
} else {
37443748
emitDiagnostic(insertLoc, diag::missing_argument_named, label)
37453749
.fixItInsert(insertLoc, insertText.str());
@@ -3880,6 +3884,23 @@ bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const {
38803884
return true;
38813885
}
38823886

3887+
bool MissingArgumentsFailure::isPropertyWrapperInitialization() const {
3888+
auto *call = dyn_cast<CallExpr>(getRawAnchor());
3889+
if (!(call && call->isImplicit()))
3890+
return false;
3891+
3892+
auto TE = dyn_cast<TypeExpr>(call->getFn());
3893+
if (!TE)
3894+
return false;
3895+
3896+
auto instanceTy = TE->getInstanceType();
3897+
if (!instanceTy)
3898+
return false;
3899+
3900+
auto *NTD = resolveType(instanceTy)->getAnyNominal();
3901+
return NTD && NTD->getAttrs().hasAttribute<PropertyWrapperAttr>();
3902+
}
3903+
38833904
bool ClosureParamDestructuringFailure::diagnoseAsError() {
38843905
auto *closure = cast<ClosureExpr>(getAnchor());
38853906
auto params = closure->getParameters();

lib/Sema/CSDiagnostics.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,11 @@ class MissingArgumentsFailure final : public FailureDiagnostic {
12071207
/// Diagnose cases when instead of multiple distinct arguments
12081208
/// call got a single tuple argument with expected arity/types.
12091209
bool diagnoseInvalidTupleDestructuring() const;
1210+
1211+
/// Determine whether missing arguments are associated with
1212+
/// an implicit call to a property wrapper initializer e.g.
1213+
/// `@Foo(answer: 42) var question = "ultimate question"`
1214+
bool isPropertyWrapperInitialization() const;
12101215
};
12111216

12121217
class OutOfOrderArgumentFailure final : public FailureDiagnostic {

0 commit comments

Comments
 (0)