Index: clang/lib/Sema/SemaModule.cpp =================================================================== --- clang/lib/Sema/SemaModule.cpp +++ clang/lib/Sema/SemaModule.cpp @@ -576,7 +576,8 @@ CurContext->addDecl(D); PushDeclContext(S, D); - D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); + if (getCurrentModule() && getCurrentModule()->isModulePurview()) + D->setModuleOwnershipKind(Decl::ModuleOwnershipKind::VisibleWhenImported); return D; } Index: clang/test/Modules/export-in-non-modules.cpp =================================================================== --- /dev/null +++ clang/test/Modules/export-in-non-modules.cpp @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -std=c++20 %s -fsyntax-only -verify +export struct Unit { // expected-error {{export declaration can only be used within a module interface unit after the module declaration}} + bool operator<(const Unit&); +};