Skip to content

Gracefully handle incorrect SwiftName arg count #78124

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 1 commit into from
Feb 12, 2025
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
35 changes: 35 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6674,6 +6674,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
ArrayRef<Identifier> argNames = name.getArgumentNames();

ParameterList *parameterList = nullptr;
DeclName nameBeforeAdjustment;
if (argNames.size() == 1 && decl->getNumParams() == 0) {
// Special case: We need to create an empty first parameter for our
// argument label
Expand All @@ -6690,6 +6691,25 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
dc, decl, {decl->param_begin(), decl->param_end()}, decl->isVariadic(),
allowNSUIntegerAsInt, argNames, /*genericParams=*/{},
/*resultType=*/nullptr, /*hasBoundsAnnotatedParam=*/nullptr);

if (name && parameterList && argNames.size() != parameterList->size()) {
// Remember that the name has changed.
nameBeforeAdjustment = name;

// Add or remove argument labels as needed to match `parameterList`.
SmallVector<Identifier, 16> newArgNames;
llvm::append_range(newArgNames, argNames);
while (newArgNames.size() > parameterList->size())
newArgNames.pop_back();
while (newArgNames.size() < parameterList->size()) {
auto param = parameterList->get(newArgNames.size());
newArgNames.push_back(param->getArgumentName());
}

// Construct the new name.
name = DeclName(Impl.SwiftContext, name.getBaseName(), newArgNames);
argNames = name.getArgumentNames();
}
}
if (!parameterList)
return nullptr;
Expand Down Expand Up @@ -6727,6 +6747,21 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
result->setIsObjC(false);
result->setIsDynamic(false);

if (nameBeforeAdjustment) {
SmallString<16> message;
llvm::raw_svector_ostream os(message);
os << "declared Swift name '" << nameBeforeAdjustment
<< "' was adjusted to '" << name
<< "' because it does not have the correct number of parameters ("
<< nameBeforeAdjustment.getArgumentNames().size() << " vs. "
<< name.getArgumentNames().size()
<< "); please report this to its maintainer";

auto attr = AvailableAttr::createUniversallyDeprecated(Impl.SwiftContext,
Impl.SwiftContext.AllocateCopy(message.str()), "");
result->getAttrs().add(attr);
}

finishFuncDecl(decl, result);
if (correctSwiftName)
markAsVariant(result, *correctSwiftName);
Expand Down
2 changes: 2 additions & 0 deletions test/ClangImporter/Inputs/custom-modules/SwiftName.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ typedef int my_int_t SWIFT_NAME(MyInt);
void spuriousAPINotedSwiftName(int);
void poorlyNamedFunction(const char *);

PointType readPoint(const char *path, void **errorOut) SWIFT_NAME(Point.init(path:));

struct BoxForConstants {
int dummy;
};
Expand Down
3 changes: 3 additions & 0 deletions test/ClangImporter/attr-swift_name_renaming.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func test() {
var p = Point()
var p2 = PointType() // FIXME: should provide Fix-It expected-error{{cannot find 'PointType' in scope}} {{none}}

// Initializers with incorrect argument count (rdar://141124373)
var p3 = Point(path: "/dev/zero", nil) // expected-warning {{'init(path:_:)' is deprecated: declared Swift name 'init(path:)' was adjusted to 'init(path:_:)' because it does not have the correct number of parameters (1 vs. 2); please report this to its maintainer}}

// Field name remapping
p.x = 7

Expand Down