[flang] Fix USE rename
authorLeandro Lupori <leandro.lupori@linaro.org>
Fri, 10 Feb 2023 20:50:45 +0000 (20:50 +0000)
committerLeandro Lupori <leandro.lupori@linaro.org>
Tue, 14 Feb 2023 13:04:56 +0000 (10:04 -0300)
Fix USE rename when use-name and local-name are the same.
Previously, the associated symbol was being removed from scope.

Operator rename implementation needed no change, because, as it
doesn't call AddUseRename(), symbol erasure is skipped.

Fixes #60223

Reviewed By: klausler

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

flang/lib/Semantics/resolve-names.cpp
flang/test/Semantics/resolve116.f90 [new file with mode: 0644]

index 4cd6905..fd42514 100644 (file)
@@ -2718,7 +2718,7 @@ bool ModuleVisitor::Pre(const parser::Rename::Names &x) {
   const auto &useName{std::get<1>(x.t)};
   AddUseRename(useName.source);
   SymbolRename rename{AddUse(localName.source, useName.source)};
-  if (rename.use) {
+  if (rename.use && localName.source != useName.source) {
     EraseRenamedSymbol(*rename.use);
   }
   Resolve(useName, rename.use);
diff --git a/flang/test/Semantics/resolve116.f90 b/flang/test/Semantics/resolve116.f90
new file mode 100644 (file)
index 0000000..a46b677
--- /dev/null
@@ -0,0 +1,24 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Test rename to the same name.
+module m1
+  integer, allocatable :: a(:)
+
+  interface operator(.add.)
+    module procedure add
+  end interface
+
+contains
+  integer function add(a, b)
+    integer, intent(in) :: a, b
+
+    add = a + b
+  end function
+end
+
+program p1
+  use m1, a => a, operator(.add.) => operator(.add.)
+
+  allocate(a(10))
+  deallocate(a)
+  print *, 2 .add. 2
+end