Index: flang/lib/Semantics/check-declarations.cpp =================================================================== --- flang/lib/Semantics/check-declarations.cpp +++ flang/lib/Semantics/check-declarations.cpp @@ -104,7 +104,7 @@ } } bool IsResultOkToDiffer(const FunctionResult &); - void CheckBindCName(const Symbol &); + void CheckBindCScopeAndName(const Symbol &); // Check functions for defined I/O procedures void CheckDefinedIoProc( const Symbol &, const GenericDetails &, GenericKind::DefinedIo); @@ -236,7 +236,9 @@ if (symbol.attrs().test(Attr::VOLATILE)) { CheckVolatile(symbol, derived); } - CheckBindCName(symbol); + if (symbol.attrs().test(Attr::BIND_C)) { + CheckBindCScopeAndName(symbol); + } if (isDone) { return; // following checks do not apply } @@ -1869,8 +1871,13 @@ } } -// Check that BIND(C) names are distinct -void CheckHelper::CheckBindCName(const Symbol &symbol) { +// Check that BIND(C) names are distinct and BIND(C) variable declared in module +void CheckHelper::CheckBindCScopeAndName(const Symbol &symbol) { + if (symbol.has() && + symbol.owner().kind() != Scope::Kind::Module) { + messages_.Say(symbol.name(), + "A variable with BIND(C) attribute may only appear in the specification part of a module"_err_en_US); + } if (const std::string * name{DefinesBindCName(symbol)}) { auto pair{bindC_.emplace(*name, symbol)}; if (!pair.second) { Index: flang/test/Semantics/resolve113.f90 =================================================================== --- /dev/null +++ flang/test/Semantics/resolve113.f90 @@ -0,0 +1,20 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 + +module m + integer, bind(c, name="a") :: x1 + integer, bind(c) :: x2 +end + +subroutine sub() + !ERROR: A variable with BIND(C) attribute may only appear in the specification part of a module + integer, bind(c, name="b") :: x3 + !ERROR: A variable with BIND(C) attribute may only appear in the specification part of a module + integer, bind(c) :: x4 +end + +program main + !ERROR: A variable with BIND(C) attribute may only appear in the specification part of a module + integer, bind(c, name="c") :: x5 + !ERROR: A variable with BIND(C) attribute may only appear in the specification part of a module + integer, bind(c) :: x6 +end