dylint_driver 6.0.0

Dylint driver library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#![feature(rustc_private)]
#![cfg_attr(dylint_lib = "general", allow(crate_wide_allow))]
#![allow(clippy::collapsible_if)]
#![deny(clippy::expect_used)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]
#![deny(unused_extern_crates)]

extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate rustc_lint;
extern crate rustc_session;
extern crate rustc_span;

use anyhow::{Result, bail, ensure};
use dylint_internal::{env, parse_path_filename, rustup::is_rustc};
use std::{
    collections::BTreeSet,
    ffi::{CString, OsStr},
    path::{Path, PathBuf},
};

pub const DYLINT_VERSION: &str = "0.1.0";

type DylintVersionFunc = unsafe fn() -> *mut std::os::raw::c_char;

type RegisterLintsFunc =
    unsafe fn(sess: &rustc_session::Session, store: &mut rustc_lint::LintStore);

/// A library that has been loaded but whose lints have not necessarily been registered.
struct LoadedLibrary {
    path: PathBuf,
    lib: libloading::Library,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
struct Lint {
    name: &'static str,
    level: rustc_lint::Level,
    desc: &'static str,
}

impl From<&rustc_lint::Lint> for Lint {
    fn from(lint: &rustc_lint::Lint) -> Self {
        Self {
            name: lint.name,
            level: lint.default_level,
            desc: lint.desc,
        }
    }
}

impl LoadedLibrary {
    fn register_lints(
        &self,
        sess: &rustc_session::Session,
        lint_store: &mut rustc_lint::LintStore,
    ) {
        (|| unsafe {
            if let Ok(func) = self.lib.get::<DylintVersionFunc>(b"dylint_version") {
                let dylint_version = CString::from_raw(func()).into_string()?;
                ensure!(
                    dylint_version == DYLINT_VERSION,
                    "`{}` has dylint version `{}`, but `{}` was expected",
                    self.path.to_string_lossy(),
                    dylint_version,
                    DYLINT_VERSION
                );
            } else {
                bail!(
                    "could not find `dylint_version` in `{}`",
                    self.path.to_string_lossy()
                );
            }
            if let Ok(func) = self.lib.get::<RegisterLintsFunc>(b"register_lints") {
                func(sess, lint_store);
            } else {
                bail!(
                    "could not find `register_lints` in `{}`",
                    self.path.to_string_lossy()
                );
            }
            Ok(())
        })()
        .unwrap_or_else(|err| {
            session_err(sess, &err);
        });
    }
}

#[rustversion::before(2023-12-18)]
fn session_err(sess: &rustc_session::Session, err: &impl ToString) -> rustc_span::ErrorGuaranteed {
    sess.diagnostic().err(err.to_string())
}

#[rustversion::since(2023-12-18)]
fn session_err(sess: &rustc_session::Session, err: &impl ToString) -> rustc_span::ErrorGuaranteed {
    sess.dcx().err(err.to_string())
}

struct Callbacks {
    loaded_libs: Vec<LoadedLibrary>,
}

// smoelius: Use of thread local storage was added to Clippy by:
// https://github.com/rust-lang/rust-clippy/commit/3c06e0b1ce003912f8fe0536d3a7fe22558e38cf
// This results in a segfault after the Clippy library has been unloaded; see the following issue
// for an explanation as to why: https://github.com/nagisa/rust_libloading/issues/5
// The workaround I've chosen is:
// https://github.com/nagisa/rust_libloading/issues/5#issuecomment-244195096
impl Callbacks {
    // smoelius: Load the libraries when `Callbacks` is created and not later (e.g., in `config`)
    // to ensure that the libraries live long enough.
    fn new(paths: Vec<PathBuf>) -> Self {
        let mut loaded_libs = Vec::new();
        for path in paths {
            unsafe {
                // smoelius: `libloading` does not define `RTLD_NODELETE`.
                #[cfg(unix)]
                let result = libloading::os::unix::Library::open(
                    Some(&path),
                    libloading::os::unix::RTLD_LAZY
                        | libloading::os::unix::RTLD_LOCAL
                        | libc::RTLD_NODELETE,
                )
                .map(Into::into);

                #[cfg(not(unix))]
                let result = libloading::Library::new(&path);

                let lib = result.unwrap_or_else(|err| {
                    // smoelius: rust-lang/rust#111633 changed the type of `early_error`'s `msg`
                    // argument from `&str` to `impl Into<DiagnosticMessage>`.
                    // smoelius: And rust-lang/rust#111748 made it that `msg` is borrowed for
                    // `'static`. Since the program is about to exit, it's probably fine to leak the
                    // string.
                    let msg = format!(
                        "could not load library `{}`: {}",
                        path.to_string_lossy(),
                        err
                    );
                    early_error(msg);
                });

                loaded_libs.push(LoadedLibrary { path, lib });
            }
        }
        Self { loaded_libs }
    }
}

#[rustversion::before(2023-06-28)]
fn early_error(msg: String) -> ! {
    rustc_session::early_error(
        rustc_session::config::ErrorOutputType::default(),
        Box::leak(msg.into_boxed_str()) as &str,
    )
}

#[rustversion::since(2023-06-28)]
extern crate rustc_errors;

#[rustversion::all(since(2023-06-28), before(2023-12-18))]
fn early_error(msg: impl Into<rustc_errors::DiagnosticMessage>) -> ! {
    let handler =
        rustc_session::EarlyErrorHandler::new(rustc_session::config::ErrorOutputType::default());
    handler.early_error(msg)
}

#[rustversion::all(since(2023-12-18), before(2023-12-23))]
fn early_error(msg: impl Into<rustc_errors::DiagnosticMessage>) -> ! {
    let handler =
        rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default());
    handler.early_error(msg)
}

#[rustversion::all(since(2023-12-23), before(2024-03-05))]
fn early_error(msg: impl Into<rustc_errors::DiagnosticMessage>) -> ! {
    let handler =
        rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default());
    handler.early_fatal(msg)
}

#[rustversion::since(2024-03-05)]
fn early_error(msg: impl Into<rustc_errors::DiagMessage>) -> ! {
    let handler =
        rustc_session::EarlyDiagCtxt::new(rustc_session::config::ErrorOutputType::default());
    handler.early_fatal(msg)
}

trait EnvDepInfo {
    fn env_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<
        rustc_data_structures::fx::FxIndexSet<(rustc_span::Symbol, Option<rustc_span::Symbol>)>,
    >;
}

impl EnvDepInfo for rustc_session::Session {
    #[rustversion::before(2024-03-05)]
    fn env_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<
        rustc_data_structures::fx::FxIndexSet<(rustc_span::Symbol, Option<rustc_span::Symbol>)>,
    > {
        &self.parse_sess.env_depinfo
    }

    #[rustversion::all(since(2024-03-05), before(2026-03-18))]
    fn env_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<
        rustc_data_structures::fx::FxIndexSet<(rustc_span::Symbol, Option<rustc_span::Symbol>)>,
    > {
        &self.psess.env_depinfo
    }

    #[rustversion::since(2026-03-18)]
    fn env_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<
        rustc_data_structures::fx::FxIndexSet<(rustc_span::Symbol, Option<rustc_span::Symbol>)>,
    > {
        &self.env_depinfo
    }
}

trait FileDepInfo {
    fn file_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<rustc_data_structures::fx::FxIndexSet<rustc_span::Symbol>>;
}

impl FileDepInfo for rustc_session::Session {
    #[rustversion::before(2024-03-05)]
    fn file_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<rustc_data_structures::fx::FxIndexSet<rustc_span::Symbol>>
    {
        &self.parse_sess.file_depinfo
    }

    #[rustversion::all(since(2024-03-05), before(2026-03-18))]
    fn file_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<rustc_data_structures::fx::FxIndexSet<rustc_span::Symbol>>
    {
        &self.psess.file_depinfo
    }

    #[rustversion::since(2026-03-18)]
    fn file_depinfo(
        &self,
    ) -> &rustc_data_structures::sync::Lock<rustc_data_structures::fx::FxIndexSet<rustc_span::Symbol>>
    {
        &self.file_depinfo
    }
}

#[rustversion::before(2022-07-14)]
fn zero_mir_opt_level(config: &mut rustc_interface::Config) {
    trait Zeroable {
        fn zero(&mut self);
    }

    impl Zeroable for usize {
        fn zero(&mut self) {
            *self = 0;
        }
    }

    impl Zeroable for Option<usize> {
        fn zero(&mut self) {
            *self = Some(0);
        }
    }

    // smoelius: `Zeroable` is a hack to make the next line compile for different Rust versions:
    // https://github.com/rust-lang/rust-clippy/commit/0941fc0bb5d655cdd0816f862af8cfe70556dad6
    config.opts.debugging_opts.mir_opt_level.zero();
}

// smoelius: Relevant PR and merge commit:
// - https://github.com/rust-lang/rust/pull/98975
// - https://github.com/rust-lang/rust/commit/0ed9c64c3e63acac9bd77abce62501696c390450
#[rustversion::since(2022-07-14)]
fn zero_mir_opt_level(config: &mut rustc_interface::Config) {
    config.opts.unstable_opts.mir_opt_level = Some(0);
}

impl rustc_driver::Callbacks for Callbacks {
    fn config(&mut self, config: &mut rustc_interface::Config) {
        let previous = config.register_lints.take();
        let loaded_libs = self.loaded_libs.split_off(0);
        config.register_lints = Some(Box::new(move |sess, lint_store| {
            if let Some(previous) = &previous {
                previous(sess, lint_store);
            }

            let dylint_libs = env::var(env::DYLINT_LIBS).ok();
            let dylint_metadata = env::var(env::DYLINT_METADATA).ok();
            let dylint_no_deps = env::var(env::DYLINT_NO_DEPS).ok();
            let dylint_no_deps_enabled = dylint_no_deps.as_ref().is_some_and(|value| value != "0");
            let cargo_primary_package_is_set = env::var(env::CARGO_PRIMARY_PACKAGE).is_ok();

            sess.env_depinfo().lock().insert((
                rustc_span::Symbol::intern(env::DYLINT_LIBS),
                dylint_libs.as_deref().map(rustc_span::Symbol::intern),
            ));
            sess.env_depinfo().lock().insert((
                rustc_span::Symbol::intern(env::DYLINT_METADATA),
                dylint_metadata.as_deref().map(rustc_span::Symbol::intern),
            ));
            sess.env_depinfo().lock().insert((
                rustc_span::Symbol::intern(env::DYLINT_NO_DEPS),
                dylint_no_deps.as_deref().map(rustc_span::Symbol::intern),
            ));

            if dylint_no_deps_enabled && !cargo_primary_package_is_set {
                return;
            }

            let mut before = BTreeSet::<Lint>::new();
            if list_enabled() {
                lint_store.get_lints().iter().for_each(|&lint| {
                    before.insert(lint.into());
                });
            }
            for loaded_lib in &loaded_libs {
                if let Some(path) = loaded_lib.path.to_str() {
                    sess.file_depinfo()
                        .lock()
                        .insert(rustc_span::Symbol::intern(path));
                }
                loaded_lib.register_lints(sess, lint_store);
            }
            if list_enabled() {
                let mut after = BTreeSet::<Lint>::new();
                lint_store.get_lints().iter().for_each(|&lint| {
                    after.insert(lint.into());
                });
                list_lints(&before, &after);
                std::process::exit(0);
            }
        }));

        register_extra_symbols(config);

        // smoelius: Choose to be compatible with Clippy:
        // https://github.com/rust-lang/rust-clippy/commit/7bae5bd828e98af9d245b77118c075a7f1a036b9
        zero_mir_opt_level(config);
    }
}

#[must_use]
fn list_enabled() -> bool {
    env::var(env::DYLINT_LIST).is_ok_and(|value| value != "0")
}

fn list_lints(before: &BTreeSet<Lint>, after: &BTreeSet<Lint>) {
    let difference: Vec<Lint> = after.difference(before).cloned().collect();

    let name_width = difference
        .iter()
        .map(|lint| lint.name.len())
        .max()
        .unwrap_or_default();

    let level_width = difference
        .iter()
        .map(|lint| lint.level.as_str().len())
        .max()
        .unwrap_or_default();

    for Lint { name, level, desc } in difference {
        println!(
            "    {:<name_width$}    {:<level_width$}    {}",
            name.to_lowercase(),
            level.as_str(),
            desc,
            name_width = name_width,
            level_width = level_width
        );
    }
}

#[rustversion::before(2025-05-14)]
fn register_extra_symbols(_config: &mut rustc_interface::Config) {}

#[rustversion::since(2025-05-14)]
include!(concat!(env!("OUT_DIR"), "/extra_symbols.rs"));

#[rustversion::since(2025-05-14)]
fn register_extra_symbols(config: &mut rustc_interface::Config) {
    config.extra_symbols = EXTRA_SYMBOLS.into();
}

pub fn dylint_driver<T: AsRef<OsStr>>(args: &[T]) -> Result<()> {
    if args.len() <= 1 || args.iter().any(|arg| arg.as_ref() == "-V") {
        println!("{} {}", env!("RUSTUP_TOOLCHAIN"), env!("CARGO_PKG_VERSION"));
        return Ok(());
    }

    run(&args[1..])
}

pub fn run<T: AsRef<OsStr>>(args: &[T]) -> Result<()> {
    let sysroot = sysroot().ok();
    let rustflags = rustflags();
    let paths = paths();

    let rustc_args = rustc_args(args, sysroot.as_deref(), &rustflags, &paths)?;

    let mut callbacks = Callbacks::new(paths);

    // smoelius: I am not sure that this should be here. `RUST_LOG=debug cargo test` fails because
    // of the log messages.
    log::debug!("{rustc_args:?}");

    run_compiler(&rustc_args, &mut callbacks)
}

fn sysroot() -> Result<PathBuf> {
    let rustup_home = env::var(env::RUSTUP_HOME)?;
    let rustup_toolchain = env::var(env::RUSTUP_TOOLCHAIN)?;
    Ok(PathBuf::from(rustup_home)
        .join("toolchains")
        .join(rustup_toolchain))
}

fn rustflags() -> Vec<String> {
    env::var(env::DYLINT_RUSTFLAGS).map_or_else(
        |_| Vec::new(),
        |rustflags| rustflags.split_whitespace().map(String::from).collect(),
    )
}

fn paths() -> Vec<PathBuf> {
    (|| -> Result<_> {
        let dylint_libs = env::var(env::DYLINT_LIBS)?;
        serde_json::from_str(&dylint_libs).map_err(Into::into)
    })()
    .unwrap_or_default()
}

fn rustc_args<T: AsRef<OsStr>, U: AsRef<str>, V: AsRef<Path>>(
    args: &[T],
    sysroot: Option<&Path>,
    rustflags: &[U],
    paths: &[V],
) -> Result<Vec<String>> {
    let mut args = args.iter().peekable();
    let mut rustc_args = Vec::new();

    let first_arg = args.peek();
    // smoelius: `Option::is_none_or` is too recent for some toolchains we test with.
    #[allow(clippy::unnecessary_map_or)]
    if first_arg.map_or(true, |arg| !is_rustc(arg)) {
        rustc_args.push("rustc".to_owned());
    }
    if let Some(arg) = first_arg {
        if is_rustc(arg) {
            rustc_args.push(arg.as_ref().to_string_lossy().to_string());
            let _ = args.next();
        }
    }
    if let Some(sysroot) = sysroot {
        rustc_args.extend([
            "--sysroot".to_owned(),
            sysroot.to_string_lossy().to_string(),
        ]);
    }
    for path in paths {
        if let Some((name, _)) = parse_path_filename(path.as_ref()) {
            rustc_args.push(format!(r#"--cfg=dylint_lib="{name}""#));
        } else {
            bail!("could not parse `{}`", path.as_ref().to_string_lossy());
        }
    }
    rustc_args.extend(args.map(|s| s.as_ref().to_string_lossy().to_string()));
    rustc_args.extend(
        rustflags
            .iter()
            .map(|rustflag| rustflag.as_ref().to_owned()),
    );

    Ok(rustc_args)
}

#[rustversion::before(2024-12-09)]
fn run_compiler(
    at_args: &[String],
    callbacks: &mut (dyn rustc_driver::Callbacks + Send),
) -> Result<()> {
    rustc_driver::RunCompiler::new(at_args, callbacks)
        .run()
        .map_err(|_| std::process::exit(1))
}

#[rustversion::all(since(2024-12-09), before(2025-01-24))]
#[allow(clippy::unnecessary_wraps)]
fn run_compiler(
    at_args: &[String],
    callbacks: &mut (dyn rustc_driver::Callbacks + Send),
) -> Result<()> {
    rustc_driver::RunCompiler::new(at_args, callbacks).run();
    Ok(())
}

#[rustversion::since(2025-01-24)]
fn run_compiler(
    at_args: &[String],
    callbacks: &mut (dyn rustc_driver::Callbacks + Send),
) -> Result<()> {
    rustc_driver::run_compiler(at_args, callbacks);
    Ok(())
}

#[expect(clippy::unwrap_used)]
#[cfg(test)]
mod test {
    use super::*;
    use rustc_version::{Channel, version_meta};

    #[test]
    fn channel_is_nightly() {
        assert!(matches!(version_meta().unwrap().channel, Channel::Nightly));
    }

    #[test]
    fn no_rustc() {
        assert_eq!(
            rustc_args(
                &["--crate-name", "name"],
                None,
                &[] as &[&str],
                &[] as &[&Path]
            )
            .unwrap(),
            vec!["rustc", "--crate-name", "name"]
        );
    }

    #[test]
    fn plain_rustc() {
        assert_eq!(
            rustc_args(
                &["rustc", "--crate-name", "name"],
                None,
                &[] as &[&str],
                &[] as &[&Path]
            )
            .unwrap(),
            vec!["rustc", "--crate-name", "name"]
        );
    }

    #[test]
    fn qualified_rustc() {
        assert_eq!(
            rustc_args(
                &["/bin/rustc", "--crate-name", "name"],
                None,
                &[] as &[&str],
                &[] as &[&Path]
            )
            .unwrap(),
            vec!["/bin/rustc", "--crate-name", "name"]
        );
    }
}