@@ -4219,18 +4219,27 @@ bool ValueDecl::isUsableFromInline() const {
42194219}
42204220
42214221bool ValueDecl::isInterfacePackageEffectivelyPublic () const {
4222- // If a package decl has a @usableFromInline (or other inlinable)
4223- // attribute, and is defined in a module built from interface, it
4224- // can be referenced by another module that imports it even though
4225- // the defining interface module does not have package-name (such
4226- // as public or private interface); in such case, the decl is treated
4227- // as public and access checks in sema are skipped.
4228- // We might need to add another check here to ensure the interface
4229- // was part of the same package before the package-name was removed.
4230- return getFormalAccess () == AccessLevel::Package &&
4231- isUsableFromInline () &&
4232- getModuleContext ()->getPackageName ().empty () &&
4233- getModuleContext ()->isBuiltFromInterface ();
4222+ // A package decl with @usableFromInline (or other inlinable
4223+ // attributes) is essentially public, and can be printed in
4224+ // public (or private) interface file without package-name;
4225+ // it can be referenced by another module (without package-name)
4226+ // importing such interface module.
4227+ auto isCandidate = getFormalAccess () == AccessLevel::Package &&
4228+ isUsableFromInline () &&
4229+ getModuleContext ()->getPackageName ().empty ();
4230+ if (!isCandidate)
4231+ return false ;
4232+
4233+ // Treat the decl as public (1) if it's contained in an interface
4234+ // file, e.g. when running -typecheck-module-from-interface or
4235+ // -compile-module-from-interface.
4236+ isCandidate = false ;
4237+ if (auto srcFile = getDeclContext ()->getParentSourceFile ()) {
4238+ isCandidate = srcFile->Kind == SourceFileKind::Interface;
4239+ }
4240+ // Or (2) if the decl being referenced in a client file is defined
4241+ // in an interface module.
4242+ return isCandidate || getModuleContext ()->isBuiltFromInterface ();
42344243}
42354244
42364245bool ValueDecl::shouldHideFromEditor () const {
@@ -4512,9 +4521,9 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
45124521 if (!pkg) {
45134522 if (VD->isInterfacePackageEffectivelyPublic ())
45144523 return AccessScope::getPublic ();
4515- // Instead of reporting and failing early, return the scope of resultDC to
4516- // allow continuation ( should still non-zero exit later if in script mode)
4517- return AccessScope (resultDC);
4524+
4525+ // If reached here, should be treated as internal.
4526+ return AccessScope (resultDC-> getParentModule () );
45184527 } else {
45194528 return AccessScope (pkg);
45204529 }
0 commit comments