Skip to content

[CSDiag] Add a new diagnostic for @propertyWrapper implicit init call missing arguments #26219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4521,6 +4521,9 @@ WARNING(property_wrapper_init_initialValue,none,
())
ERROR(property_wrapper_projection_value_missing,none,
"could not find projection value property %0", (Identifier))
ERROR(property_wrapper_missing_arg_init, none, "missing argument for parameter "
"%0 in property wrapper initializer; add 'wrappedValue' and %0 "
"arguments in '@%1(...)'", (Identifier, StringRef))

//------------------------------------------------------------------------------
// MARK: function builder diagnostics
Expand Down
36 changes: 32 additions & 4 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3329,13 +3329,20 @@ class ArgumentMatcher : public MatchCallArgumentListener {

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

if (name.empty())
if (name.empty()) {
TC.diagnose(insertLoc, diag::missing_argument_positional,
missingParamIdx + 1)
.fixItInsert(insertLoc, insertText.str());
else
TC.diagnose(insertLoc, diag::missing_argument_named, name)
.fixItInsert(insertLoc, insertText.str());
} else {
if (isPropertyWrapperImplicitInit()) {
auto TE = cast<TypeExpr>(FnExpr);
TC.diagnose(TE->getLoc(), diag::property_wrapper_missing_arg_init, name,
TE->getInstanceType()->getString());
} else {
TC.diagnose(insertLoc, diag::missing_argument_named, name)
.fixItInsert(insertLoc, insertText.str());
}
}

auto candidate = CandidateInfo[0];
if (candidate.getDecl())
Expand All @@ -3345,6 +3352,27 @@ class ArgumentMatcher : public MatchCallArgumentListener {
Diagnosed = true;
}

bool isPropertyWrapperImplicitInit() {
auto TE = dyn_cast<TypeExpr>(FnExpr);
if (!TE)
return false;

auto instanceTy = TE->getInstanceType();
if (!instanceTy)
return false;

auto nominalDecl = instanceTy->getAnyNominal();
if (!(nominalDecl &&
nominalDecl->getAttrs().hasAttribute<PropertyWrapperAttr>()))
return false;

if (auto *parentExpr = CandidateInfo.CS.getParentExpr(FnExpr)) {
return parentExpr->isImplicit() && isa<CallExpr>(parentExpr);
}

return false;
}

bool missingLabel(unsigned paramIdx) override {
return false;
}
Expand Down
16 changes: 16 additions & 0 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,19 @@ struct InvalidPropertyDelegateUse {
self.x.foo() // expected-error {{value of type 'Int' has no member 'foo'}}
}
}

// SR-11060

class SR_11060_Class {
@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(...)'}}
}

@propertyWrapper
struct SR_11060_Wrapper {
var wrappedValue: Int

init(wrappedValue: Int, string: String) { // expected-note {{'init(wrappedValue:string:)' declared here}}
self.wrappedValue = wrappedValue
}
}