Skip to content

[AccessScope] Treat PackageUnit as enclosing scope of ModuleDecl #64230

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
Mar 14, 2023
Merged

Conversation

elsh
Copy link
Contributor

@elsh elsh commented Mar 9, 2023

  • Create PackageUnit when package-name input is non-nil as a property of ModuleDecl
  • Treat PackageUnit as the enclosing scope of ModuleDecl
  • Create an access scope with PackageUnit as context for the package access level
  • Add a PackageUnit context check in access scope functions
  • Return Module Decl from PackageUnit context (weakly referenced) in DeclContext::getModuleScopeContext and DeclContext::getParentModule to avoid breaking existing code
  • Add doc comments and fix tests

Resolves rdar://104987295, rdar://105187216, rdar://104723918

@elsh elsh changed the title Hook up PackageUnit to ModuleDecl Add PackageUnit to ModuleDecl Mar 9, 2023
@elsh elsh changed the title Add PackageUnit to ModuleDecl Associate PackageUnit with ModuleDecl Mar 9, 2023
@elsh
Copy link
Contributor Author

elsh commented Mar 9, 2023

@swift-ci smoke test

@xedin xedin removed their request for review March 9, 2023 18:34
Comment on lines 175 to 184
create(Identifier name, ModuleDecl &src, ASTContext &ctx) {
return new (ctx) PackageUnit(name, src);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is…pretty weird. Wouldn’t we prefer to have one instance of PackageUnit shared by all of the ModuleDecls in the same package? That way you could == the DeclContext pointers to see if they match, just like other DeclContexts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found this way easier to look up the module from a package unit in DeclContext::getModuleScopeContext and DeclContext::getParentModule; otherwise will have to compare ModuleDecl pointers in an array

Copy link
Contributor

@beccadax beccadax Mar 9, 2023

Choose a reason for hiding this comment

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

That kind of just moves the question. Why do getParentModule() and getModuleScopeContext() return the module when you call them on the package? That seems, if anything, backwards from what you’d want. (And yes, I know you’re trying to avoid making the package a parent of the module, but making it a child instead seems pretty strange.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing code assumes ModuleDecl is the top level context as long as there's non-null DeclContext (otherwise public) and performs isa/cast without checking if it's null, which is everywhere. With a package context now introduced, those existing functions are still called on the target DeclContext that may or may not be package, and will crash on isa/cast if the returned context is not module. Either we'll have to add assert or null check before casting or add package specific handling before each; by returning the module mapped to a package as in this PR, the changes required are minimal.

Copy link
Contributor Author

@elsh elsh Mar 10, 2023

Choose a reason for hiding this comment

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

Also added doc comments in AccessScope, DeclContext, PackageUnit, ModuleDecl with more info.

Copy link
Contributor Author

@elsh elsh Mar 10, 2023

Choose a reason for hiding this comment

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

I actually tried other PackageUnit-ModuleDecl mappings first but there were issues with each as follows.

  1. Set PackageUnit as ModuleDecl's parent context (most clean, straightforward hierarchy)
    • Can only pass PackageUnit to ModuleDecl's ctor directly for the main module; for loaded modules, a PackageUnit needs to be created and set as a new DeclContext.
    • setDeclContext does not seem to actually override the Context field in DeclContext once set (not sure why), thus looking up the new context returns garbage.
    • Still need a way to look up ModuleDecl from PackageUnit for the existing module getters in DeclContext to avoid crashes as mentioned in the previous comment.
  2. Create a PackageUnit-ModuleDecl map in ASTContext (reasonable, no circular reference b/t the two classes)
    • Adding the entry to the map when PackageUnit is created from either class results in calling DeclContext::getASTContext() which calls DeclContext::getParentModule(), which then calls DeclContext::getASTContext(), resulting in a cycle.

Thus, resorted to having PackageUnit as a property in ModuleDecl and have a weak reference back to ModuleDecl from PackageUnit. Lmk if if the way it references is not actually a weak reference though.

@elsh
Copy link
Contributor Author

elsh commented Mar 9, 2023

@swift-ci smoke test

@elsh elsh force-pushed the es-dbg branch 2 times, most recently from 95dae6e to 6726d07 Compare March 10, 2023 00:53
@elsh
Copy link
Contributor Author

elsh commented Mar 10, 2023

@swift-ci smoke test

* Weakly reference ModuleDecl from PackageUnit
* Add PackageUnit decl context getter and use it for a package AccessScope
* Return module decl referenced by PackageUnit in getModuleScopeContext and getParentModule
* Handle package acl in access scope checkers
* Remove AccessLimitKind
* Fix tests
Resolves rdar://104987295, rdar://105187216, rdar://104723918
@elsh
Copy link
Contributor Author

elsh commented Mar 11, 2023

@swift-ci smoke test

@elsh elsh changed the title Associate PackageUnit with ModuleDecl Set PackageUnit as enclosing scope of ModuleDecl Mar 13, 2023
@elsh elsh changed the title Set PackageUnit as enclosing scope of ModuleDecl Treat PackageUnit as enclosing scope of ModuleDecl Mar 13, 2023
@elsh elsh changed the title Treat PackageUnit as enclosing scope of ModuleDecl [AccessScope] Treat PackageUnit as enclosing scope of ModuleDecl Mar 13, 2023
@@ -1679,6 +1679,7 @@ ERROR(access_control_open_bad_decl,none,
WARNING(access_control_non_objc_open_member,none,
"non-'@objc' %0 in extensions cannot be overridden; use 'public' instead",
(DescriptiveDeclKind))
ERROR(access_control_requires_package_name, none, "decl has a package access level but no -package-name was passed", ())
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a forgotten long line.

@elsh
Copy link
Contributor Author

elsh commented Mar 14, 2023

@xymus Thanks for the review. Your comments are addressed in #64373.

@elsh elsh merged commit 1924376 into main Mar 14, 2023
@elsh elsh deleted the es-dbg branch March 14, 2023 00:00
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