diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md --- a/flang/docs/Extensions.md +++ b/flang/docs/Extensions.md @@ -277,6 +277,9 @@ * The character length of the `SOURCE=` or `MOLD=` in `ALLOCATE` may be distinct from the constant character length, if any, of an allocated object. +* When a name is brought into a scope by multiple ways, + such as USE-association as well as an `IMPORT` from its host, + it's an error only if the resolution is ambiguous. ### Extensions supported when enabled by options diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp --- a/flang/lib/Semantics/resolve-names.cpp +++ b/flang/lib/Semantics/resolve-names.cpp @@ -6752,8 +6752,19 @@ Say(std::move(*error)); } for (auto &name : x.names) { - if (FindSymbol(scope.parent(), name)) { + if (Symbol * outer{FindSymbol(scope.parent(), name)}) { scope.add_importName(name.source); + if (Symbol * symbol{FindInScope(name)}) { + if (outer->GetUltimate() == symbol->GetUltimate()) { + Say(name, + "The same '%s' is already present in this scope"_port_en_US); + } else { + Say(name, + "A distinct '%s' is already present in this scope"_err_en_US) + .Attach(symbol->name(), "Previous declaration of '%s'"_en_US) + .Attach(outer->name(), "Declaration of '%s' in host scope"_en_US); + } + } } else { Say(name, "'%s' not found in host scope"_err_en_US); } diff --git a/flang/test/Semantics/resolve118.f90 b/flang/test/Semantics/resolve118.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/resolve118.f90 @@ -0,0 +1,59 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 +! USE vs IMPORT +module m1 + type t + integer n + end type +end module + +module m2 + type t + real x + end type +end module + +module m3 + use m1 + interface + subroutine s1(x) + use m1 + !PORTABILITY: The same 't' is already present in this scope + import t + type(t) x + end + subroutine s2(x) + use m2 + !ERROR: A distinct 't' is already present in this scope + import t + type(t) x + end + end interface +end module + +module m4 + type t + complex z + end type + interface + subroutine s3(x) + use m1 + !ERROR: A distinct 't' is already present in this scope + import t + type(t) x + end + end interface +end module + +module m5 + interface + subroutine s4(x) + use m1 + !ERROR: A distinct 't' is already present in this scope + import t + type(t) x + end + end interface + contains + subroutine t + end +end module