Files
clang-p2996/clang/test/CXX/module/module.import/p2.cpp
Chuanqi Xu 82034aca30 [C++20] [Modules] Warn for importing implementation partition unit in interface units (#108493)
Recently, there are multiple false positive issue reports about the
reachability of implementation partition units:
- https://github.com/llvm/llvm-project/issues/105882
- https://github.com/llvm/llvm-project/issues/101348
- https://lists.isocpp.org/core/2024/08/16232.php

And according to our use experience for modules, we find it is a pretty
good practice to not import implementation partition units in the
interface units. It can help developers to have a pretty good mental
model for when to use an implementation partition unit: that any unit in
the module but not in the module interfaces can be in the implementation
partition unit.

So I think it is good to add the diagnostics.
2024-09-14 14:45:50 +08:00

79 lines
2.5 KiB
C++

// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
// RUN: %clang_cc1 -std=c++20 %t/impl.cppm -emit-module-interface -o %t/M-impl.pcm
// RUN: %clang_cc1 -std=c++20 %t/M.cppm -emit-module-interface -fprebuilt-module-path=%t -o %t/M.pcm
// RUN: %clang_cc1 -std=c++20 %t/Use.cpp -fprebuilt-module-path=%t -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/UseInPartA.cppm -fprebuilt-module-path=%t -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/UseInAnotherModule.cppm -fprebuilt-module-path=%t -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/Private.cppm -emit-module-interface -fprebuilt-module-path=%t -o %t/A.pcm
// RUN: %clang_cc1 -std=c++20 %t/TryUseFromPrivate.cpp -fprebuilt-module-path=%t -verify -fsyntax-only
// RUN: %clang_cc1 -std=c++20 %t/Global.cppm -emit-module-interface -fprebuilt-module-path=%t -o %t/C.pcm
// RUN: %clang_cc1 -std=c++20 %t/UseGlobal.cpp -fprebuilt-module-path=%t -verify -fsyntax-only
//--- impl.cppm
module M:impl;
class A {};
//--- M.cppm
export module M;
import :impl;
export A f();
//--- Use.cpp
import M;
void test() {
A a; // expected-error {{definition of 'A' must be imported from module 'M' before it is required}}
// expected-error@-1 {{definition of 'A' must be imported from module 'M' before it is required}} expected-error@-1 {{}}
// expected-note@impl.cppm:2 {{declaration here is not visible}}
// expected-note@impl.cppm:2 {{definition here is not reachable}} expected-note@impl.cppm:2 {{}}
}
//--- UseInPartA.cppm
export module M:partA;
import :impl; // expected-warning {{importing an implementation partition unit in a module interface is not recommended.}}
void test() {
A a;
}
//--- UseInAnotherModule.cppm
export module B;
import M;
void test() {
A a; // expected-error {{declaration of 'A' must be imported from module 'M'}}
// expected-error@-1 {{definition of 'A' must be imported from module 'M'}} expected-error@-1 {{}}
// expected-note@impl.cppm:2 {{declaration here is not visible}}
// expected-note@impl.cppm:2 {{definition here is not reachable}} expected-note@impl.cppm:2 {{}}
}
//--- Private.cppm
export module A;
module :private;
class A {};
void test() {
A a;
}
//--- TryUseFromPrivate.cpp
import A;
void test() {
A a; // expected-error {{unknown type name 'A'}}
}
//--- Global.cppm
module;
class A{};
export module C;
void test() {
A a;
}
//--- UseGlobal.cpp
import C;
void test() {
A a; // expected-error {{'A' must be declared before it is used}}
// expected-note@Global.cppm:2 {{declaration here is not visible}}
}