Skip to content

Commit 90d753a

Browse files
authored
[flang] Fix inheritance of IMPLICIT typing rules (#102692)
Interfaces don't inherit the IMPLICIT typing rules of their enclosing scope, and separate MODULE PROCEDUREs inherit the IMPLICIT typing rules of submodule in which they are defined, not the rules from their interface. Fixes #102558.
1 parent 10cc4a5 commit 90d753a

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ class ScopeHandler;
6262
// When inheritFromParent is set, defaults come from the parent rules.
6363
class ImplicitRules {
6464
public:
65-
ImplicitRules(SemanticsContext &context, ImplicitRules *parent)
66-
: parent_{parent}, context_{context} {
67-
inheritFromParent_ = parent != nullptr;
68-
}
65+
ImplicitRules(SemanticsContext &context, const ImplicitRules *parent)
66+
: parent_{parent}, context_{context},
67+
inheritFromParent_{parent != nullptr} {}
6968
bool isImplicitNoneType() const;
7069
bool isImplicitNoneExternal() const;
7170
void set_isImplicitNoneType(bool x) { isImplicitNoneType_ = x; }
@@ -82,7 +81,7 @@ class ImplicitRules {
8281
private:
8382
static char Incr(char ch);
8483

85-
ImplicitRules *parent_;
84+
const ImplicitRules *parent_;
8685
SemanticsContext &context_;
8786
bool inheritFromParent_{false}; // look in parent if not specified here
8887
bool isImplicitNoneType_{
@@ -3380,6 +3379,7 @@ bool ModuleVisitor::BeginSubmodule(
33803379
parentScope = &currScope();
33813380
}
33823381
BeginModule(name, true);
3382+
set_inheritFromParent(false); // submodules don't inherit parents' implicits
33833383
if (ancestor && !ancestor->AddSubmodule(name.source, currScope())) {
33843384
Say(name, "Module '%s' already has a submodule named '%s'"_err_en_US,
33853385
ancestorName.source, name.source);
@@ -4487,7 +4487,7 @@ Symbol &SubprogramVisitor::PushSubprogramScope(const parser::Name &name,
44874487
CHECK(context().HasError(genericSymbol));
44884488
}
44894489
}
4490-
set_inheritFromParent(hasModulePrefix);
4490+
set_inheritFromParent(false); // interfaces don't inherit, even if MODULE
44914491
}
44924492
if (Symbol * found{FindSymbol(name)};
44934493
found && found->has<HostAssocDetails>()) {

flang/test/Semantics/implicit15.f90

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
2+
!Test inheritance of implicit rules in submodules and separate module
3+
!procedures.
4+
5+
module m
6+
implicit integer(1)(a-z)
7+
interface
8+
module subroutine mp(da) ! integer(2)
9+
implicit integer(2)(a-z)
10+
end
11+
end interface
12+
save :: mv ! integer(1)
13+
end
14+
15+
submodule(m) sm1
16+
implicit integer(8)(a-z)
17+
save :: sm1v ! integer(8)
18+
interface
19+
module subroutine sm1p(da) ! default real
20+
end
21+
end interface
22+
end
23+
24+
submodule(m:sm1) sm2
25+
implicit integer(2)(a-c,e-z)
26+
save :: sm2v ! integer(2)
27+
contains
28+
module subroutine sm1p(da) ! default real
29+
save :: sm1pv ! inherited integer(2)
30+
!CHECK: PRINT *, 1_4, 8_4, 2_4, 4_4, 2_4
31+
print *, kind(mv), kind(sm1v), kind(sm2v), kind(da), kind(sm1pv)
32+
end
33+
end
34+
35+
submodule(m:sm2) sm3
36+
implicit integer(8)(a-z)
37+
save :: sm3v ! integer(8)
38+
contains
39+
module procedure mp
40+
save :: mpv ! inherited integer(8)
41+
call sm1p(1.)
42+
!CHECK: PRINT *, 1_4, 8_4, 2_4, 8_4, 2_4, 8_4
43+
print *, kind(mv), kind(sm1v), kind(sm2v), kind(sm3v), kind(da), kind(mpv)
44+
end
45+
end
46+
47+
program main
48+
use m
49+
call mp(1_2)
50+
end

0 commit comments

Comments
 (0)