Skip to content

Commit e1663b0

Browse files
committed
Auto merge of #157721 - clubby789:test-lint-machinery, r=<try>
compiletest: Add testing for lint machinery at runtime try-job: x86_64-gnu-stable try-job: dist-x86_64-msvc
2 parents 485ec3f + 9c08712 commit e1663b0

22 files changed

Lines changed: 341 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,13 @@ dependencies = [
879879
"windows 0.61.3",
880880
]
881881

882+
[[package]]
883+
name = "compiletest-lint-driver"
884+
version = "0.1.0"
885+
dependencies = [
886+
"libloading 0.9.0",
887+
]
888+
882889
[[package]]
883890
name = "console"
884891
version = "0.16.3"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ members = [
1515
"src/tools/clippy/clippy_dev",
1616
"src/tools/collect-license-metadata",
1717
"src/tools/compiletest",
18+
"src/tools/compiletest-lint-driver",
1819
"src/tools/coverage-dump",
1920
"src/tools/features-status-dump",
2021
"src/tools/generate-copyright",

src/bootstrap/src/core/build_steps/check.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,13 @@ tool_check_step!(Compiletest {
877877
default: false,
878878
});
879879

880+
tool_check_step!(CompiletestLintDriver {
881+
path: "src/tools/compiletest-lint-driver",
882+
mode: Mode::ToolRustcPrivate,
883+
allow_features: "rustc_private",
884+
default: false,
885+
});
886+
880887
// As with compiletest, rustdoc-gui-test is automatically built when running
881888
// relevant tests. So being able to check it is mainly useful for people
882889
// working on on rustdoc-gui-test itself, or on its compiletest dependency.

src/bootstrap/src/core/build_steps/clippy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ lint_any!(
498498
Clippy, "src/tools/clippy", "clippy", Mode::ToolRustcPrivate;
499499
CollectLicenseMetadata, "src/tools/collect-license-metadata", "collect-license-metadata", Mode::ToolTarget;
500500
Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTarget;
501+
CompiletestLintDriver, "src/tools/compiletest-lint-driver", "compiletest-lint-driver", Mode::ToolRustcPrivate;
501502
CoverageDump, "src/tools/coverage-dump", "coverage-dump", Mode::ToolTarget;
502503
Jsondocck, "src/tools/jsondocck", "jsondocck", Mode::ToolTarget;
503504
Jsondoclint, "src/tools/jsondoclint", "jsondoclint", Mode::ToolTarget;

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,12 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20582058
builder.ensure(compile::Rustc::new(test_compiler, target));
20592059
}
20602060

2061+
let lint_driver_compiler = query_compiler.unwrap_or(test_compiler);
2062+
if suite == "ui-fulldeps" {
2063+
builder.std(lint_driver_compiler, target);
2064+
builder.std(lint_driver_compiler, lint_driver_compiler.host);
2065+
}
2066+
20612067
if suite == "debuginfo" {
20622068
builder.ensure(dist::DebuggerScripts {
20632069
sysroot: builder.sysroot(test_compiler).to_path_buf(),
@@ -2096,6 +2102,17 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20962102
cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(test_compiler));
20972103
cmd.arg("--run-lib-path").arg(builder.sysroot_target_libdir(test_compiler, target));
20982104
cmd.arg("--rustc-path").arg(builder.rustc(test_compiler));
2105+
if suite == "ui-fulldeps" {
2106+
let lint_driver: PathBuf = builder
2107+
.ensure(tool::CompiletestLintDriver {
2108+
compiler: test_compiler,
2109+
target: test_compiler.host,
2110+
})
2111+
.tool_path;
2112+
cmd.arg("--rustc-lint-driver-path").arg(lint_driver);
2113+
cmd.arg("--rustc-lint-driver-lib-path").arg(builder.rustc_libdir(lint_driver_compiler));
2114+
cmd.arg("--rustc-lint-driver-sysroot").arg(builder.sysroot(lint_driver_compiler));
2115+
}
20992116
if let Some(query_compiler) = query_compiler {
21002117
cmd.arg("--query-rustc-path").arg(builder.rustc(query_compiler));
21012118
}

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,47 @@ impl Step for ErrorIndex {
634634
}
635635
}
636636

637+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
638+
pub struct CompiletestLintDriver {
639+
pub compiler: Compiler,
640+
pub target: TargetSelection,
641+
}
642+
643+
impl Step for CompiletestLintDriver {
644+
type Output = ToolBuildResult;
645+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
646+
run.path("src/tools/compiletest-lint-driver")
647+
}
648+
649+
fn make_run(run: RunConfig<'_>) {
650+
run.builder.ensure(CompiletestLintDriver {
651+
compiler: run.builder.compiler(0, run.builder.config.host_target),
652+
target: run.target,
653+
});
654+
}
655+
656+
fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
657+
builder.std(self.compiler, self.target);
658+
builder.ensure(compile::Rustc::new(self.compiler, self.target));
659+
builder.ensure(ToolBuild {
660+
build_compiler: self.compiler,
661+
target: self.target,
662+
tool: "compiletest-lint-driver",
663+
mode: Mode::ToolRustcPrivate,
664+
path: "src/tools/compiletest-lint-driver",
665+
source_type: SourceType::InTree,
666+
extra_features: Vec::new(),
667+
allow_features: "rustc_private",
668+
cargo_args: Vec::new(),
669+
artifact_kind: ToolArtifactKind::Binary,
670+
})
671+
}
672+
673+
fn metadata(&self) -> Option<StepMetadata> {
674+
Some(StepMetadata::build("CompiletestLintDriver", self.target).built_by(self.compiler))
675+
}
676+
}
677+
637678
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
638679
pub struct RemoteTestServer {
639680
pub build_compiler: Compiler,

src/bootstrap/src/core/builder/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ impl<'a> Builder<'a> {
776776
tool::Linkchecker,
777777
tool::CargoTest,
778778
tool::Compiletest,
779+
tool::CompiletestLintDriver,
779780
tool::RemoteTestServer,
780781
tool::RemoteTestClient,
781782
tool::RustInstaller,
@@ -815,6 +816,7 @@ impl<'a> Builder<'a> {
815816
clippy::CodegenGcc,
816817
clippy::CollectLicenseMetadata,
817818
clippy::Compiletest,
819+
clippy::CompiletestLintDriver,
818820
clippy::CoverageDump,
819821
clippy::Jsondocck,
820822
clippy::Jsondoclint,
@@ -848,6 +850,7 @@ impl<'a> Builder<'a> {
848850
check::Bootstrap,
849851
check::RunMakeSupport,
850852
check::Compiletest,
853+
check::CompiletestLintDriver,
851854
check::RustdocGuiTest,
852855
check::FeaturesStatusDump,
853856
check::CoverageDump,

src/bootstrap/src/core/builder/tests.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,7 @@ mod snapshot {
20982098
[test] compiletest-assembly-llvm 1 <host>
20992099
[test] compiletest-incremental 1 <host>
21002100
[test] compiletest-debuginfo 1 <host>
2101+
[build] rustc 0 <host> -> CompiletestLintDriver 1 <host>
21012102
[test] compiletest-ui-fulldeps 1 <host>
21022103
[build] rustdoc 1 <host>
21032104
[test] compiletest-rustdoc-html 1 <host>
@@ -2171,12 +2172,13 @@ mod snapshot {
21712172
insta::assert_snapshot!(
21722173
ctx.config("test")
21732174
.args(&["ui", "ui-fulldeps", "run-make", "rustdoc-html", "rustdoc-gui", "incremental"])
2174-
.render_steps(), @r"
2175+
.render_steps(), @"
21752176
[build] llvm <host>
21762177
[build] rustc 0 <host> -> rustc 1 <host>
21772178
[build] rustc 1 <host> -> std 1 <host>
21782179
[build] rustc 0 <host> -> Compiletest 1 <host>
21792180
[test] compiletest-ui 1 <host>
2181+
[build] rustc 0 <host> -> CompiletestLintDriver 1 <host>
21802182
[test] compiletest-ui-fulldeps 1 <host>
21812183
[build] rustc 0 <host> -> RunMakeSupport 1 <host>
21822184
[build] rustdoc 1 <host>
@@ -2195,7 +2197,7 @@ mod snapshot {
21952197
ctx.config("test")
21962198
.args(&["ui", "ui-fulldeps", "run-make", "rustdoc-html", "rustdoc-gui", "incremental"])
21972199
.stage(2)
2198-
.render_steps(), @r"
2200+
.render_steps(), @"
21992201
[build] llvm <host>
22002202
[build] rustc 0 <host> -> rustc 1 <host>
22012203
[build] rustc 1 <host> -> std 1 <host>
@@ -2204,6 +2206,7 @@ mod snapshot {
22042206
[build] rustc 0 <host> -> Compiletest 1 <host>
22052207
[test] compiletest-ui 2 <host>
22062208
[build] rustc 2 <host> -> rustc 3 <host>
2209+
[build] rustc 2 <host> -> CompiletestLintDriver 3 <host>
22072210
[test] compiletest-ui-fulldeps 2 <host>
22082211
[build] rustc 0 <host> -> RunMakeSupport 1 <host>
22092212
[build] rustdoc 2 <host>
@@ -2224,7 +2227,7 @@ mod snapshot {
22242227
.targets(&[TEST_TRIPLE_1])
22252228
.args(&["ui", "ui-fulldeps", "run-make", "rustdoc", "rustdoc-gui", "incremental"])
22262229
.stage(2)
2227-
.render_steps(), @r"
2230+
.render_steps(), @"
22282231
[build] llvm <host>
22292232
[build] rustc 0 <host> -> rustc 1 <host>
22302233
[build] rustc 1 <host> -> std 1 <host>
@@ -2236,6 +2239,8 @@ mod snapshot {
22362239
[test] compiletest-ui 2 <target1>
22372240
[build] llvm <target1>
22382241
[build] rustc 2 <host> -> rustc 3 <target1>
2242+
[build] rustc 2 <host> -> rustc 3 <host>
2243+
[build] rustc 2 <host> -> CompiletestLintDriver 3 <host>
22392244
[test] compiletest-ui-fulldeps 2 <target1>
22402245
[build] rustc 0 <host> -> RunMakeSupport 1 <host>
22412246
[build] rustdoc 2 <host>
@@ -2279,6 +2284,7 @@ mod snapshot {
22792284
[test] compiletest-incremental 2 <host>
22802285
[test] compiletest-debuginfo 2 <host>
22812286
[build] rustc 2 <host> -> rustc 3 <host>
2287+
[build] rustc 2 <host> -> CompiletestLintDriver 3 <host>
22822288
[test] compiletest-ui-fulldeps 2 <host>
22832289
[build] rustdoc 2 <host>
22842290
[test] compiletest-rustdoc-html 2 <host>

src/doc/rustc-dev-guide/src/tests/compiletest.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,64 @@ pub fn identity(ts: TokenStream) -> TokenStream {
741741
> the `aux-build` header, and use `#![crate_type="proc_macro"]`, and `//@
742742
> force-host` and `//@ no-prefer-dynamic` headers in the proc-macro.
743743
744+
### Auxiliary lint
745+
746+
By using an `aux-lint` directive in a `ui-fulldeps` test, you can build a 'lint library' file which will be loaded into rustc's lints when running your test.
747+
748+
```rs
749+
// tests/ui-fulldeps/lint_test.rs
750+
751+
//@ aux-lint: my_lint.rs
752+
753+
fn main() {
754+
trigger_lint(); //~ ERROR this triggered the lint
755+
}
756+
```
757+
758+
```rs
759+
// tests/ui-fulldeps/auxiliary/my_lint.rs
760+
761+
#![feature(rustc_private)]
762+
763+
extern crate rustc_driver;
764+
extern crate rustc_errors;
765+
extern crate rustc_hir;
766+
extern crate rustc_lint;
767+
extern crate rustc_macros;
768+
extern crate rustc_session;
769+
770+
use rustc_lint::{LateContext, LateLintPass, LintContext};
771+
use rustc_macros::Diagnostic;
772+
use rustc_session::{declare_lint_pass, declare_tool_lint};
773+
774+
declare_tool_lint! {
775+
pub example::EXAMPLE_LINT,
776+
Deny,
777+
"some example lint"
778+
}
779+
declare_lint_pass!(ExampleLint => [EXAMPLE_LINT]);
780+
781+
#[derive(Diagnostic)]
782+
#[diag("this triggered the lint")]
783+
pub(crate) struct Example;
784+
785+
impl LateLintPass<'_> for ExampleLint {
786+
// lint implementation
787+
}
788+
789+
#[unsafe(no_mangle)]
790+
unsafe extern "C" fn register_lints(
791+
sess: &rustc_session::Session,
792+
lint_store: &mut rustc_lint::LintStore,
793+
) {
794+
lint_store.register_lints(&[HELLO_LINT]);
795+
lint_store.register_late_pass(|_| Box::new(HelloLint));
796+
}
797+
```
798+
799+
> **Note**: The `register_lints` signature must match _exactly_,
800+
> as it will be dynamically loaded by the test runner.
801+
744802
## Revisions
745803

746804
Revisions allow a single test file to be used for multiple tests.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "compiletest-lint-driver"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[package.metadata.rust-analyzer]
7+
rustc_private = true
8+
9+
[dependencies]
10+
libloading = "0.9.0"

0 commit comments

Comments
 (0)