Skip to content

[Swiftify] Do not swiftify non-Swift-like counted_by exprs #81395

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 2 commits into from
May 9, 2025

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented May 9, 2025

__counted_by (and __sized_by) expressions can have arbitrary C syntax in them, such as:

void foo(int * __counted_by(*len) p, int *len);

When @_SwififyImport tries to generate Swift code for this, the expression *len leads to a syntax error, since it isn't valid Swift.

This patch adds a check to ensure we only attach the Swiftify macro to __counted_by expressions that are also syntactically valid in Swift.

rdar://150956352

@j-hui
Copy link
Contributor Author

j-hui commented May 9, 2025

@swift-ci please test

__counted_by (and __sized_by) expressions can have arbitrary C syntax
in them, such as:

    void foo(int * __counted_by(*len) p, int *len);

When @_SwififyImport tries to generate Swift code for this, the
expression `*len` leads to a syntax error, since it isn't valid Swift.

This patch adds a check to ensure we only attach the Swiftify macro to
__counted_by expressions that are also syntactically valid in Swift.

rdar://150956352
@j-hui j-hui force-pushed the dont-swiftify-invalid-expr branch from f585cf3 to e5b1f4a Compare May 9, 2025 04:13
@j-hui
Copy link
Contributor Author

j-hui commented May 9, 2025

@swift-ci please test

bool VisitBin ## BINOP(const clang::BinaryOperator *binop) { \
return this->Visit(binop->getLHS()) && this->Visit(binop->getRHS()); \
}
SUPPORTED_BINOP(Add)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we ensure that we have comprehensively considered all the valid cases involving operators?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't really, other than extensive testing. (E.g., I didn't realize we needed to whitelist ParenExpr in my initial draft of this.)

However, I do think this whitelist should cover the "obvious" cases, like basic arithmetic expressions, and even slightly weirder cases like bitwise arithmetic. The macro is not equipped to deal with anything much more complicated than that anyway, at least at the moment.

I'm erring on the side not Swiftifying imports, because if the user can't call the safe overload, they can always work around with the uglier unsafe code (what the macro should expand to anyway). The alternative, which is to produce a syntax error when encountering un-Swiftifiable code (what is happening now anyway), leads to un-compilable code from a macro they can't easily opt out of at a per-function granularity.

@j-hui
Copy link
Contributor Author

j-hui commented May 9, 2025

@swift-ci please test

@j-hui j-hui merged commit 027a178 into swiftlang:main May 9, 2025
5 checks passed
@j-hui j-hui deleted the dont-swiftify-invalid-expr branch May 9, 2025 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants