[flang-commits] [flang] 32a793b - [flang] Resolve USE vs IMPORT conflicts

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Apr 3 09:00:02 PDT 2023


Author: Peter Klausler
Date: 2023-04-03T08:50:49-07:00
New Revision: 32a793b687a59717bc1c0211ebeb1fcd613d1bc9

URL: https://github.com/llvm/llvm-project/commit/32a793b687a59717bc1c0211ebeb1fcd613d1bc9
DIFF: https://github.com/llvm/llvm-project/commit/32a793b687a59717bc1c0211ebeb1fcd613d1bc9.diff

LOG: [flang] Resolve USE vs IMPORT conflicts

When the same name is pulled into a scope more than once via
USE and IMPORT, emit an error if its resolutions are ambiguous,
or (as an extension like some other compilers) emit a portability
warning when the names all resolve to the same symbol.

Differential Revision: https://reviews.llvm.org/D147388

Added: 
    flang/test/Semantics/resolve118.f90

Modified: 
    flang/docs/Extensions.md
    flang/lib/Semantics/resolve-names.cpp

Removed: 
    


################################################################################
diff  --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 0ed89e07c2dfd..e25474577e35b 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -277,6 +277,9 @@ end
 * 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
index 341bde8baab4f..35f71242d7831 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6752,8 +6752,19 @@ bool ResolveNamesVisitor::Pre(const parser::ImportStmt &x) {
     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
index 0000000000000..e31175799b028
--- /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


        


More information about the flang-commits mailing list