[flang] Resolve USE vs IMPORT conflicts
authorPeter Klausler <pklausler@nvidia.com>
Wed, 29 Mar 2023 15:56:31 +0000 (08:56 -0700)
committerPeter Klausler <pklausler@nvidia.com>
Mon, 3 Apr 2023 15:50:49 +0000 (08:50 -0700)
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

flang/docs/Extensions.md
flang/lib/Semantics/resolve-names.cpp
flang/test/Semantics/resolve118.f90 [new file with mode: 0644]

index 0ed89e0..e254745 100644 (file)
@@ -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
 
index 341bde8..35f7124 100644 (file)
@@ -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 (file)
index 0000000..e311757
--- /dev/null
@@ -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