Skip to content

[Rename] Also syntactically rename a macro’s definition #65399

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
Apr 25, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/ASTWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ class ASTWalker {
/// TODO: Consider changing this to false by default.
virtual bool shouldWalkSerializedTopLevelInternalDecls() { return true; }

/// Whether to walk into the definition of a \c MacroDecl if it hasn't been
/// type-checked yet.
virtual bool shouldWalkIntoUncheckedMacroDefinitions() { return false; }

/// walkToParameterListPre - This method is called when first visiting a
/// ParameterList, before walking into its parameters.
///
Expand Down
1 change: 1 addition & 0 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ class NameMatcher: public ASTWalker {
PreWalkAction walkToTypeReprPre(TypeRepr *T) override;
PreWalkResult<Pattern *> walkToPatternPre(Pattern *P) override;
bool shouldWalkIntoGenericParams() override { return true; }
bool shouldWalkIntoUncheckedMacroDefinitions() override { return true; }

PreWalkResult<ArgumentList *>
walkToArgumentListPre(ArgumentList *ArgList) override;
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
if (auto def = MD->definition) {
// Don't walk into unchecked definitions.
if (auto expansion = dyn_cast<MacroExpansionExpr>(def)) {
if (!expansion->getType().isNull()) {
if (!expansion->getType().isNull() ||
Walker.shouldWalkIntoUncheckedMacroDefinitions()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose this is safer, but do you know what expansion->getType().isNull() was added for? We can walk over un-typechecked ASTs, so it seems like clients should be checking this instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

It was added in aac0406. @DougGregor Do you remember why you added it?

Copy link
Member Author

Choose a reason for hiding this comment

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

@DougGregor and I just chatted about it and he added this check because of a verified crash and thinks that we can fix it in a more principled way than what he did.

I’m going to merge this as-is because it’s a low-risk change this way that we can cherry-pick to 5.9 and will provide a better fix in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Tracking as rdar://108560477 for reference.

if (auto newDef = doIt(def))
MD->definition = newDef;
else
Expand Down
11 changes: 11 additions & 0 deletions test/refactoring/SyntacticRename/macro-definition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %empty-directory(%t.ranges)
// RUN: %refactor -find-rename-ranges -source-filename %s -pos="test" -is-function-like -old-name "StringifyMacro" | %FileCheck %s

// CHECK: struct /*test:def*/<base>StringifyMacro</base> {}
// CHECK: @freestanding(expression)
// CHECK: macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacroMacros", type: "/*test:ref*/<base>StringifyMacro</base>")

struct /*test:def*/StringifyMacro {}

@freestanding(expression)
macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MyMacroMacros", type: "/*test:ref*/StringifyMacro")