Skip to content

Commit 0447d84

Browse files
committed
[flang] Account for accessibility in extensibility check
A derived type with a component of the same name as the type is not extensible... unless the extension occurs in another module where the conflicting component is inaccessible. Fixes #126114.
1 parent 9884803 commit 0447d84

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7294,17 +7294,20 @@ bool DeclarationVisitor::OkToAddComponent(
72947294
std::optional<parser::MessageFixedText> msg;
72957295
std::optional<common::UsageWarning> warning;
72967296
if (context().HasError(*prev)) { // don't pile on
7297-
} else if (extends) {
7298-
msg = "Type cannot be extended as it has a component named"
7299-
" '%s'"_err_en_US;
73007297
} else if (CheckAccessibleSymbol(currScope(), *prev)) {
73017298
// inaccessible component -- redeclaration is ok
7302-
if (context().ShouldWarn(
7303-
common::UsageWarning::RedeclaredInaccessibleComponent)) {
7299+
if (extends) {
7300+
// The parent type has a component of same name, but it remains
7301+
// extensible outside its module since that component is PRIVATE.
7302+
} else if (context().ShouldWarn(
7303+
common::UsageWarning::RedeclaredInaccessibleComponent)) {
73047304
msg =
73057305
"Component '%s' is inaccessibly declared in or as a parent of this derived type"_warn_en_US;
73067306
warning = common::UsageWarning::RedeclaredInaccessibleComponent;
73077307
}
7308+
} else if (extends) {
7309+
msg =
7310+
"Type cannot be extended as it has a component named '%s'"_err_en_US;
73087311
} else if (prev->test(Symbol::Flag::ParentComp)) {
73097312
msg =
73107313
"'%s' is a parent type of this type and so cannot be a component"_err_en_US;

flang/test/Semantics/resolve34.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module m4
4545
type, extends(t1) :: t2
4646
end type
4747
end
48+
module m4a
49+
use m4
50+
type, extends(t1) :: t3 ! ok, inaccessible component
51+
end type
52+
end
4853

4954
module m5
5055
type :: t1

0 commit comments

Comments
 (0)