Skip to content

Commit 5fe29a7

Browse files
authored
Merge pull request #26219 from theblixguy/fix/SR-11060
[CSDiag] Add a new diagnostic for @propertyWrapper implicit init call missing arguments
2 parents e41595e + ab1f059 commit 5fe29a7

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,6 +4523,9 @@ WARNING(property_wrapper_init_initialValue,none,
45234523
())
45244524
ERROR(property_wrapper_projection_value_missing,none,
45254525
"could not find projection value property %0", (Identifier))
4526+
ERROR(property_wrapper_missing_arg_init, none, "missing argument for parameter "
4527+
"%0 in property wrapper initializer; add 'wrappedValue' and %0 "
4528+
"arguments in '@%1(...)'", (Identifier, StringRef))
45264529

45274530
//------------------------------------------------------------------------------
45284531
// MARK: function builder diagnostics

lib/Sema/CSDiag.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,13 +3219,20 @@ class ArgumentMatcher : public MatchCallArgumentListener {
32193219

32203220
assert(insertLoc.isValid() && "missing argument after trailing closure?");
32213221

3222-
if (name.empty())
3222+
if (name.empty()) {
32233223
TC.diagnose(insertLoc, diag::missing_argument_positional,
32243224
missingParamIdx + 1)
32253225
.fixItInsert(insertLoc, insertText.str());
3226-
else
3227-
TC.diagnose(insertLoc, diag::missing_argument_named, name)
3228-
.fixItInsert(insertLoc, insertText.str());
3226+
} else {
3227+
if (isPropertyWrapperImplicitInit()) {
3228+
auto TE = cast<TypeExpr>(FnExpr);
3229+
TC.diagnose(TE->getLoc(), diag::property_wrapper_missing_arg_init, name,
3230+
TE->getInstanceType()->getString());
3231+
} else {
3232+
TC.diagnose(insertLoc, diag::missing_argument_named, name)
3233+
.fixItInsert(insertLoc, insertText.str());
3234+
}
3235+
}
32293236

32303237
auto candidate = CandidateInfo[0];
32313238
if (candidate.getDecl())
@@ -3235,6 +3242,27 @@ class ArgumentMatcher : public MatchCallArgumentListener {
32353242
Diagnosed = true;
32363243
}
32373244

3245+
bool isPropertyWrapperImplicitInit() {
3246+
auto TE = dyn_cast<TypeExpr>(FnExpr);
3247+
if (!TE)
3248+
return false;
3249+
3250+
auto instanceTy = TE->getInstanceType();
3251+
if (!instanceTy)
3252+
return false;
3253+
3254+
auto nominalDecl = instanceTy->getAnyNominal();
3255+
if (!(nominalDecl &&
3256+
nominalDecl->getAttrs().hasAttribute<PropertyWrapperAttr>()))
3257+
return false;
3258+
3259+
if (auto *parentExpr = CandidateInfo.CS.getParentExpr(FnExpr)) {
3260+
return parentExpr->isImplicit() && isa<CallExpr>(parentExpr);
3261+
}
3262+
3263+
return false;
3264+
}
3265+
32383266
bool missingLabel(unsigned paramIdx) override {
32393267
return false;
32403268
}

test/decl/var/property_wrappers.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,19 @@ struct InvalidPropertyDelegateUse {
10911091
self.x.foo() // expected-error {{value of type 'Int' has no member 'foo'}}
10921092
}
10931093
}
1094+
1095+
// SR-11060
1096+
1097+
class SR_11060_Class {
1098+
@SR_11060_Wrapper var property: Int = 1234 // expected-error {{missing argument for parameter 'string' in property wrapper initializer; add 'wrappedValue' and 'string' arguments in '@SR_11060_Wrapper(...)'}}
1099+
}
1100+
1101+
@propertyWrapper
1102+
struct SR_11060_Wrapper {
1103+
var wrappedValue: Int
1104+
1105+
init(wrappedValue: Int, string: String) { // expected-note {{'init(wrappedValue:string:)' declared here}}
1106+
self.wrappedValue = wrappedValue
1107+
}
1108+
}
1109+

0 commit comments

Comments
 (0)