Skip to content

Commit f9558c6

Browse files
committed
[C++20] [Modules] Handle import decl before module declaration without being in GMF
Close #67627 In a module unit, all the declaration before the modoule declaration should live in the GMF.
1 parent eb38edc commit f9558c6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang/lib/Parse/Parser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,10 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
25642564
SeenError = false;
25652565
break;
25662566
case Sema::ModuleImportState::FirstDecl:
2567+
// If we found an import decl as the first declaration, we must be not in
2568+
// a C++20 module unit or we are in an invalid state.
2569+
ImportState = Sema::ModuleImportState::NotACXX20Module;
2570+
[[fallthrough]];
25672571
case Sema::ModuleImportState::NotACXX20Module:
25682572
// We can only import a partition within a module purview.
25692573
if (IsPartition)

clang/test/Modules/pr67627.cppm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
6+
// RUN: %clang_cc1 -std=c++20 %t/B.cppm -fmodule-file=A=%t/A.pcm -fsyntax-only -verify
7+
8+
//--- A.cppm
9+
export module A;
10+
11+
//--- B.cppm
12+
import A; // expected-note {{add 'module;' to the start of the file to introduce a global module fragment}}
13+
export module B; // expected-error {{module declaration must occur at the start of the translation unit}}

0 commit comments

Comments
 (0)