[flang-commits] [flang] 5de49af - [flang] Abstract interfaces can't be designated or referenced

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Oct 29 18:10:50 PDT 2022


Author: Peter Klausler
Date: 2022-10-29T18:06:04-07:00
New Revision: 5de49af54036f1427ae8669b6482a198cbfeff2a

URL: https://github.com/llvm/llvm-project/commit/5de49af54036f1427ae8669b6482a198cbfeff2a
DIFF: https://github.com/llvm/llvm-project/commit/5de49af54036f1427ae8669b6482a198cbfeff2a.diff

LOG: [flang] Abstract interfaces can't be designated or referenced

Broaden the check for misuse of ABSTRACT procedure interfaces by
doing it in expression analysis rather than name resolution so that
cases like pointer assignment targets and actual arguments are also
diagnosed as errors.

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

Added: 
    flang/test/Semantics/abstract02.f90

Modified: 
    flang/lib/Semantics/expression.cpp
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Semantics/resolve20.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 603fb87d0950..47421efc560c 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -200,6 +200,10 @@ MaybeExpr ExpressionAnalyzer::Designate(DataRef &&ref) {
   const Symbol &last{ref.GetLastSymbol()};
   const Symbol &symbol{BypassGeneric(last).GetUltimate()};
   if (semantics::IsProcedure(symbol)) {
+    if (symbol.attrs().test(semantics::Attr::ABSTRACT)) {
+      Say("Abstract procedure interface '%s' may not be used as a designator"_err_en_US,
+          last.name());
+    }
     if (auto *component{std::get_if<Component>(&ref.u)}) {
       return Expr<SomeType>{ProcedureDesignator{std::move(*component)}};
     } else if (!std::holds_alternative<SymbolRef>(ref.u)) {
@@ -2340,6 +2344,10 @@ auto ExpressionAnalyzer::GetCalleeAndArguments(const parser::Name &name,
       // re-resolve name to the specific procedure
       name.symbol = const_cast<Symbol *>(resolution);
     }
+  } else if (IsProcedure(ultimate) &&
+      ultimate.attrs().test(semantics::Attr::ABSTRACT)) {
+    Say("Abstract procedure interface '%s' may not be referenced"_err_en_US,
+        name.source);
   } else {
     resolution = symbol;
   }

diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index f600ddc623bf..2c21896d807b 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -6981,10 +6981,7 @@ void ResolveNamesVisitor::HandleProcedureName(
     if (!symbol->has<GenericDetails>()) {
       CheckImplicitNoneExternal(name.source, *symbol);
     }
-    if (symbol->has<SubprogramDetails>() &&
-        symbol->attrs().test(Attr::ABSTRACT)) {
-      Say(name, "Abstract interface '%s' may not be called"_err_en_US);
-    } else if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
+    if (IsProcedure(*symbol) || symbol->has<DerivedTypeDetails>() ||
         symbol->has<AssocEntityDetails>()) {
       // Symbols with DerivedTypeDetails and AssocEntityDetails are accepted
       // here as procedure-designators because this means the related

diff  --git a/flang/test/Semantics/abstract02.f90 b/flang/test/Semantics/abstract02.f90
new file mode 100644
index 000000000000..29aad7b03e53
--- /dev/null
+++ b/flang/test/Semantics/abstract02.f90
@@ -0,0 +1,17 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Test misuse of abstract interfaces
+program test
+  abstract interface
+    subroutine abstract
+    end subroutine
+  end interface
+  procedure(abstract), pointer :: p
+  !ERROR: Abstract procedure interface 'abstract' may not be referenced
+  call abstract
+  !ERROR: Abstract procedure interface 'abstract' may not be used as a designator
+  p => abstract
+  !ERROR: Abstract procedure interface 'abstract' may not be used as a designator
+  call foo(abstract)
+  !ERROR: Abstract procedure interface 'abstract' may not be used as a designator
+  print *, associated(p, abstract)
+end

diff  --git a/flang/test/Semantics/resolve20.f90 b/flang/test/Semantics/resolve20.f90
index 9708d6efd46c..938decc49919 100644
--- a/flang/test/Semantics/resolve20.f90
+++ b/flang/test/Semantics/resolve20.f90
@@ -3,6 +3,8 @@ module m
   abstract interface
     subroutine foo
     end subroutine
+    subroutine foo2
+    end subroutine
   end interface
 
   procedure() :: a
@@ -70,9 +72,9 @@ function f()
   subroutine bar
   end subroutine
   subroutine test
-    !ERROR: Abstract interface 'foo' may not be called
-    call foo()
-    !ERROR: Abstract interface 'f' may not be called
+    !ERROR: Abstract procedure interface 'foo2' may not be referenced
+    call foo2()
+    !ERROR: Abstract procedure interface 'f' may not be referenced
     x = f()
   end subroutine
 end module


        


More information about the flang-commits mailing list