Skip to content

Commit e1ba1be

Browse files
authored
[flang] Account for accessibility in extensibility check (#128765)
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 161d002 commit e1ba1be

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
@@ -7268,17 +7268,20 @@ bool DeclarationVisitor::OkToAddComponent(
72687268
std::optional<parser::MessageFixedText> msg;
72697269
std::optional<common::UsageWarning> warning;
72707270
if (context().HasError(*prev)) { // don't pile on
7271-
} else if (extends) {
7272-
msg = "Type cannot be extended as it has a component named"
7273-
" '%s'"_err_en_US;
72747271
} else if (CheckAccessibleSymbol(currScope(), *prev)) {
72757272
// inaccessible component -- redeclaration is ok
7276-
if (context().ShouldWarn(
7277-
common::UsageWarning::RedeclaredInaccessibleComponent)) {
7273+
if (extends) {
7274+
// The parent type has a component of same name, but it remains
7275+
// extensible outside its module since that component is PRIVATE.
7276+
} else if (context().ShouldWarn(
7277+
common::UsageWarning::RedeclaredInaccessibleComponent)) {
72787278
msg =
72797279
"Component '%s' is inaccessibly declared in or as a parent of this derived type"_warn_en_US;
72807280
warning = common::UsageWarning::RedeclaredInaccessibleComponent;
72817281
}
7282+
} else if (extends) {
7283+
msg =
7284+
"Type cannot be extended as it has a component named '%s'"_err_en_US;
72827285
} else if (prev->test(Symbol::Flag::ParentComp)) {
72837286
msg =
72847287
"'%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)