Index: flang/docs/Extensions.md =================================================================== --- flang/docs/Extensions.md +++ flang/docs/Extensions.md @@ -490,3 +490,10 @@ application codes that expect exterior symbols whose names match components to be visible in a derived-type definition's default initialization expressions, and so f18 follows that precedent. + +* 19.3.1p1 "Within its scope, a local identifier of an entity of class (1) + or class (4) shall not be the same as a global identifier used in that scope..." + is read so as to allow the name of a module, submodule, main program, + or `BLOCK DATA` subprogram to also be the name of an local entity in its + scope, with a portability warning, since that global name is not actually + capable of being "used" in its scope. Index: flang/lib/Semantics/check-declarations.cpp =================================================================== --- flang/lib/Semantics/check-declarations.cpp +++ flang/lib/Semantics/check-declarations.cpp @@ -1805,6 +1805,31 @@ if (scope.kind() == Scope::Kind::BlockData) { CheckBlockData(scope); } + if (auto name{scope.GetName()}) { + auto iter{scope.find(*name)}; + if (iter != scope.end()) { + const char *kind{nullptr}; + switch (scope.kind()) { + case Scope::Kind::Module: + kind = scope.symbol()->get().isSubmodule() + ? "submodule" + : "module"; + break; + case Scope::Kind::MainProgram: + kind = "main program"; + break; + case Scope::Kind::BlockData: + kind = "BLOCK DATA subprogram"; + break; + default:; + } + if (kind) { + messages_.Say(iter->second->name(), + "Name '%s' declared in a %s should not have the same name as the %s"_port_en_US, + *name, kind, kind); + } + } + } CheckGenericOps(scope); } } Index: flang/test/Semantics/OpenMP/omp-declare-target03.f90 =================================================================== --- flang/test/Semantics/OpenMP/omp-declare-target03.f90 +++ flang/test/Semantics/OpenMP/omp-declare-target03.f90 @@ -12,6 +12,7 @@ !ERROR: The module name or main program name cannot be in a DECLARE TARGET directive !$omp declare target (mod1) + !PORTABILITY: Name 'main' declared in a main program should not have the same name as the main program !ERROR: The module name or main program name cannot be in a DECLARE TARGET directive !$omp declare target (main) end Index: flang/test/Semantics/OpenMP/omp-threadprivate03.f90 =================================================================== --- flang/test/Semantics/OpenMP/omp-threadprivate03.f90 +++ flang/test/Semantics/OpenMP/omp-threadprivate03.f90 @@ -13,6 +13,7 @@ !ERROR: The module name or main program name cannot be in a THREADPRIVATE directive !$omp threadprivate(mod1) + !PORTABILITY: Name 'main' declared in a main program should not have the same name as the main program !ERROR: The module name or main program name cannot be in a THREADPRIVATE directive !$omp threadprivate(main) Index: flang/test/Semantics/bind-c02.f90 =================================================================== --- flang/test/Semantics/bind-c02.f90 +++ flang/test/Semantics/bind-c02.f90 @@ -18,6 +18,7 @@ !ERROR: Only variable and named common block can be in BIND statement bind(c) :: sub + !PORTABILITY: Name 'm' declared in a module should not have the same name as the module bind(c) :: m ! no error for implicit type variable type my_type Index: flang/test/Semantics/resolve05.f90 =================================================================== --- flang/test/Semantics/resolve05.f90 +++ flang/test/Semantics/resolve05.f90 @@ -1,12 +1,20 @@ ! RUN: %python %S/test_errors.py %s %flang_fc1 program p - integer :: p ! this is ok + !PORTABILITY: Name 'p' declared in a main program should not have the same name as the main program + integer :: p end module m - integer :: m ! this is ok + !PORTABILITY: Name 'm' declared in a module should not have the same name as the module + integer :: m end submodule(m) sm - integer :: sm ! this is ok + !PORTABILITY: Name 'sm' declared in a submodule should not have the same name as the submodule + integer :: sm +end +block data bd + !PORTABILITY: Name 'bd' declared in a BLOCK DATA subprogram should not have the same name as the BLOCK DATA subprogram + type bd + end type end module m2 type :: t Index: flang/test/Semantics/resolve20.f90 =================================================================== --- flang/test/Semantics/resolve20.f90 +++ flang/test/Semantics/resolve20.f90 @@ -37,7 +37,8 @@ type :: bad3 end type - type :: m ! the name of a module can be used as a local identifier + !PORTABILITY: Name 'm' declared in a module should not have the same name as the module + type :: m end type m !ERROR: EXTERNAL attribute was already specified on 'a'