@@ -550,9 +550,7 @@ const Symbol *FindOverriddenBinding(
550550 if (const Symbol *
551551 overridden{parentScope->FindComponent (symbol.name ())}) {
552552 // 7.5.7.3 p1: only accessible bindings are overridden
553- if (!overridden->attrs ().test (Attr::PRIVATE) ||
554- FindModuleContaining (overridden->owner ()) ==
555- FindModuleContaining (symbol.owner ())) {
553+ if (IsAccessible (*overridden, symbol.owner ())) {
556554 return overridden;
557555 } else if (overridden->attrs ().test (Attr::DEFERRED)) {
558556 isInaccessibleDeferred = true ;
@@ -1126,23 +1124,31 @@ std::optional<common::CUDADataAttr> GetCUDADataAttr(const Symbol *symbol) {
11261124 return object ? object->cudaDataAttr () : std::nullopt ;
11271125}
11281126
1127+ bool IsAccessible (const Symbol &original, const Scope &scope) {
1128+ const Symbol &ultimate{original.GetUltimate ()};
1129+ if (ultimate.attrs ().test (Attr::PRIVATE)) {
1130+ const Scope *module {FindModuleContaining (ultimate.owner ())};
1131+ return !module || module ->Contains (scope);
1132+ } else {
1133+ return true ;
1134+ }
1135+ }
1136+
11291137std::optional<parser::MessageFormattedText> CheckAccessibleSymbol (
11301138 const Scope &scope, const Symbol &symbol) {
1131- if (symbol.attrs ().test (Attr::PRIVATE)) {
1132- if (FindModuleFileContaining (scope)) {
1133- // Don't enforce component accessibility checks in module files;
1134- // there may be forward-substituted named constants of derived type
1135- // whose structure constructors reference private components.
1136- } else if (const Scope *
1137- moduleScope{FindModuleContaining (symbol.owner ())}) {
1138- if (!moduleScope->Contains (scope)) {
1139- return parser::MessageFormattedText{
1140- " PRIVATE name '%s' is only accessible within module '%s'" _err_en_US,
1141- symbol.name (), moduleScope->GetName ().value ()};
1142- }
1143- }
1139+ if (IsAccessible (symbol, scope)) {
1140+ return std::nullopt ;
1141+ } else if (FindModuleFileContaining (scope)) {
1142+ // Don't enforce component accessibility checks in module files;
1143+ // there may be forward-substituted named constants of derived type
1144+ // whose structure constructors reference private components.
1145+ return std::nullopt ;
1146+ } else {
1147+ return parser::MessageFormattedText{
1148+ " PRIVATE name '%s' is only accessible within module '%s'" _err_en_US,
1149+ symbol.name (),
1150+ DEREF (FindModuleContaining (symbol.owner ())).GetName ().value ()};
11441151 }
1145- return std::nullopt ;
11461152}
11471153
11481154SymbolVector OrderParameterNames (const Symbol &typeSymbol) {
0 commit comments