diff --git a/flang/lib/Semantics/rewrite-parse-tree.cpp b/flang/lib/Semantics/rewrite-parse-tree.cpp --- a/flang/lib/Semantics/rewrite-parse-tree.cpp +++ b/flang/lib/Semantics/rewrite-parse-tree.cpp @@ -110,6 +110,8 @@ return true; } +// Convert a syntactically ambiguous io-unit internal-file-variable to a +// file-unit-number. void RewriteMutator::Post(parser::IoUnit &x) { if (auto *var{std::get_if(&x.u)}) { const parser::Name &last{parser::GetLastName(*var)}; @@ -118,11 +120,13 @@ // If the Variable is not known to be character (any kind), transform // the I/O unit in situ to a FileUnitNumber so that automatic expression // constraint checking will be applied. + auto source{var->GetSource()}; auto expr{std::visit( [](auto &&indirection) { return parser::Expr{std::move(indirection)}; }, std::move(var->u))}; + expr.source = source; x.u = parser::FileUnitNumber{ parser::ScalarIntExpr{parser::IntExpr{std::move(expr)}}}; } diff --git a/flang/test/Semantics/io04.f90 b/flang/test/Semantics/io04.f90 --- a/flang/test/Semantics/io04.f90 +++ b/flang/test/Semantics/io04.f90 @@ -87,6 +87,9 @@ !ERROR: If UNIT=* appears, REC must not appear write(*, rec=13) 'Ok' + !ERROR: Must have INTEGER type, but is REAL(4) + write(unit, *) 'Ok' + !ERROR: If ADVANCE appears, UNIT=internal-file must not appear write(internal_file, advance='yes', fmt=1) 'Ok'