[flang-commits] [flang] [flang][Semantics] Preserve ambiguous USE-error through whole-module USE (PR #229707)

via flang-commits flang-commits at lists.llvm.org
Thu Oct 8 05:25:49 PDT 2026


================
@@ -4421,10 +4426,22 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
       // be copied.
       localSymbol->set_details(GenericDetails{});
       localSymbol->get<GenericDetails>().set_kind(useGeneric->kind());
+    } else if (const auto *useError{useSymbol.detailsIf<UseErrorDetails>()}) {
+      // The module being use-associated wholesale already has an ambiguous
+      // name at this position (e.g. two distinct USEs of the same name that
+      // are themselves never referenced there, which F2023 14.2.2 p8 allows).
+      // Propagate that ambiguity to the local symbol rather than silently
+      // resolving it to one of the ambiguous module's own USE details; the
+      // latter would produce a local UseDetails whose target module file
+      // omits the name, so it would not survive a module file round trip.
+      localSymbol->set_details(UseErrorDetails{*useError});
+      localSymbol->attrs() = useSymbol.attrs() & ~notInheritedFromUseAttrs;
+      localSymbol->implicitAttrs() =
+          localSymbol->attrs() & Attrs{Attr::ASYNCHRONOUS, Attr::VOLATILE};
+      return;
     } else { // just create UseDetails
       localSymbol->set_details(UseDetails{localName, useSymbol});
-      localSymbol->attrs() =
-          useSymbol.attrs() & ~Attrs{Attr::PUBLIC, Attr::PRIVATE, Attr::SAVE};
+      localSymbol->attrs() = useSymbol.attrs() & ~notInheritedFromUseAttrs;
       localSymbol->implicitAttrs() =
           localSymbol->attrs() & Attrs{Attr::ASYNCHRONOUS, Attr::VOLATILE};
       localSymbol->flags() = useSymbol.flags();
----------------
jeanPerier wrote:

So if there is more to keep in sync (local attrs...) than the attr(), I think it is better to define a small lambda above like (pseudo code):

```
àuto copyAttrsAndFlags{[](localSymbol, useSymbol) {
       localSymbol->attrs() = useSymbol.attrs() & ;
      localSymbol->implicitAttrs() = {Attr::PUBLIC, Attr::PRIVATE, Attr::SAVE}
      localSymbol->attrs() & Attrs{Attr::ASYNCHRONOUS, Attr::VOLATILE};
      localSymbol->flags() = useSymbol.flags();
}};
```
and to use it in the two places.

https://github.com/llvm/llvm-project/pull/229707


More information about the flang-commits mailing list