Skip to content
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

[Macros] Don't visit macro-generated extensions in visitAuxiliaryDecls. #66914

Merged
merged 2 commits into from
Jun 26, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented Jun 24, 2023

Macro-generated extensions are hoisted to file scope, because extensions are not valid in nested scopes. Callers of visitAuxiliaryDecls assume that the auxiliary decls are in the same decl context as the original, which is clearly not the case for extensions, and it leads to issues like visiting extension at the wrong time during SILGen. The extensions are already added exposed via getTopLevelDecls(), so we don't need to visit them as auxiliary decls.

Resolves: #66916, rdar://111000256

@hborla hborla requested review from slavapestov and xedin as code owners June 24, 2023 02:55
@hborla hborla requested a review from DougGregor June 24, 2023 02:55
@hborla
Copy link
Member Author

hborla commented Jun 24, 2023

@swift-ci please smoke test

// hoisting behavior of macro-generated extensions, we simply expose the
// extension to 'getTopLevelDecls()' and type check the extension eagerly
// here.
TypeChecker::typeCheckDecl(extension);
Copy link
Member

Choose a reason for hiding this comment

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

Could we instead visit these at the end of TypeCheckDeclPrimary's visitation? Doing a complete check of the extension seems unnecessary here.

(Also, you have typos "visitAuxiliarDecls" and "understsand").

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose it's better to visit expanded extensions in TypeCheckDeclPrimary as opposed to visiting the top-level decls of SF->getSynthesizedFile() at the end of TypeCheckSourceFileRequest::evaluate because the former approach will definitely invoke the request if it hasn't already happened?

Copy link
Member

Choose a reason for hiding this comment

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

I hadn't thought of TypeCheckSourceFileRequest::evaluate. Visiting the top-level decls of the synthesized file there should work.

@hborla hborla force-pushed the conformance-macro-in-extension branch from 82bc829 to b27efa0 Compare June 24, 2023 05:53
@hborla hborla requested a review from bnbarham as a code owner June 24, 2023 05:53
@hborla hborla force-pushed the conformance-macro-in-extension branch from b27efa0 to 6fe4845 Compare June 24, 2023 05:56
…ls'.

Macro-generated extensions are hoisted to file scope, because extensions are
not valid in nested scopes. Callers of 'visitAuxiliaryDecls' assume that the
auxiliary decls are in the same decl context as the original, which is clearly
not the case for extensions, and it leads to issues like visiting extension at
the wrong time during SILGen. The extensions are already added to the top-level
decls, so we don't need to visit them as auxiliary decls, and we can type-check
macro-expanded decls at the end of visitation in TypeCheckDeclPrimary.
@hborla hborla force-pushed the conformance-macro-in-extension branch from 6fe4845 to 83030a9 Compare June 24, 2023 06:25
…s of

the synthesized source file instead of a separate API to gather the
extensions.

This is how macro-generated extensions are visited everywhere else.
@hborla
Copy link
Member Author

hborla commented Jun 26, 2023

@swift-ci please smoke test

@hborla hborla merged commit da8e84c into swiftlang:main Jun 26, 2023
@hborla hborla deleted the conformance-macro-in-extension branch June 26, 2023 22:45
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.

@Observable Macro causes compilation error when in an extension of a view
2 participants