From 8b3004af5374be5614e5d6c86a791c194968bc9d Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Tue, 3 Oct 2023 13:58:33 -0400 Subject: [PATCH 1/7] Librarify `marker_rustc_driver` --- marker_rustc_driver/Cargo.toml | 3 + .../src/bin/marker_rustc_driver.rs | 68 +++++++++++++++++++ marker_rustc_driver/src/{main.rs => lib.rs} | 45 ++---------- marker_rustc_driver/src/lint_pass.rs | 2 +- 4 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 marker_rustc_driver/src/bin/marker_rustc_driver.rs rename marker_rustc_driver/src/{main.rs => lib.rs} (89%) diff --git a/marker_rustc_driver/Cargo.toml b/marker_rustc_driver/Cargo.toml index 5c5deab1..999c1039 100644 --- a/marker_rustc_driver/Cargo.toml +++ b/marker_rustc_driver/Cargo.toml @@ -9,6 +9,9 @@ license = { workspace = true } repository = { workspace = true } version = { workspace = true } +[lib] +doctest = false + [dependencies] marker_adapter = { workspace = true } marker_api = { workspace = true, features = ["driver-api"] } diff --git a/marker_rustc_driver/src/bin/marker_rustc_driver.rs b/marker_rustc_driver/src/bin/marker_rustc_driver.rs new file mode 100644 index 00000000..d8ed8fe2 --- /dev/null +++ b/marker_rustc_driver/src/bin/marker_rustc_driver.rs @@ -0,0 +1,68 @@ +#![doc = include_str!("../../README.md")] +#![feature(rustc_private)] +#![feature(lint_reasons)] +#![feature(non_exhaustive_omitted_patterns_lint)] +#![warn(clippy::pedantic)] +#![warn(non_exhaustive_omitted_patterns)] +#![allow(clippy::missing_panics_doc)] +#![allow(clippy::module_name_repetitions)] +#![allow(clippy::needless_lifetimes, reason = "lifetimes will be required to fix ICEs")] +#![allow(clippy::needless_collect, reason = "false positives for `alloc_slice`")] +#![allow(clippy::too_many_lines, reason = "long functions are unavoidable for matches")] + +extern crate rustc_ast; +extern crate rustc_data_structures; +extern crate rustc_driver; +extern crate rustc_errors; +extern crate rustc_hash; +extern crate rustc_hir; +extern crate rustc_hir_analysis; +extern crate rustc_interface; +extern crate rustc_lint; +extern crate rustc_lint_defs; +extern crate rustc_middle; +extern crate rustc_session; +extern crate rustc_span; +extern crate rustc_target; + +use std::env; + +use rustc_session::config::ErrorOutputType; +use rustc_session::EarlyErrorHandler; + +use marker_rustc_driver::{try_main, MainError}; + +const BUG_REPORT_URL: &str = "https://github.com/rust-marker/marker/issues/new?template=panic.yml"; + +fn main() { + let handler = EarlyErrorHandler::new(ErrorOutputType::default()); + rustc_driver::init_rustc_env_logger(&handler); + + // FIXME(xFrednet): The ICE hook would ideally distinguish where the error + // happens. Panics from lint crates should probably not terminate Marker + // completely, but instead warn the user and continue linting with the other + // lint crate. It would also be cool if the ICE hook printed the node that + // caused the panic in the lint crate. rust-marker/marker#10 + + rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| { + handler.note_without_error(format!("{}", rustc_tools_util::get_version_info!())); + handler.note_without_error("Achievement Unlocked: [Free Ice Cream]"); + }); + + std::process::exit(rustc_driver::catch_with_exit_code(|| { + try_main(env::args()).map_err(|err| { + let err = match err { + MainError::Custom(err) => err, + MainError::Rustc(err) => return err, + }; + + // Emit the error to stderr + err.print(); + + // This is a bit of a hack, but this way we can emit our own errors + // without having to change the rustc driver. + #[expect(deprecated)] + rustc_span::ErrorGuaranteed::unchecked_claim_error_was_emitted() + }) + })) +} diff --git a/marker_rustc_driver/src/main.rs b/marker_rustc_driver/src/lib.rs similarity index 89% rename from marker_rustc_driver/src/main.rs rename to marker_rustc_driver/src/lib.rs index 3f09b52d..1ccbea74 100644 --- a/marker_rustc_driver/src/main.rs +++ b/marker_rustc_driver/src/lib.rs @@ -3,7 +3,6 @@ #![feature(let_chains)] #![feature(lint_reasons)] #![feature(iter_collect_into)] -#![feature(lazy_cell)] #![feature(non_exhaustive_omitted_patterns_lint)] #![feature(once_cell_try)] #![warn(rustc::internal)] @@ -33,7 +32,7 @@ extern crate rustc_target; pub mod context; pub mod conversion; -mod lint_pass; +pub mod lint_pass; use std::env; use std::ops::Deref; @@ -42,8 +41,6 @@ use std::process::Command; use camino::{Utf8Path, Utf8PathBuf}; use marker_adapter::{LintCrateInfo, LINT_CRATES_ENV}; use marker_error::Context; -use rustc_session::config::ErrorOutputType; -use rustc_session::EarlyErrorHandler; use crate::conversion::rustc::RustcConverter; @@ -201,47 +198,13 @@ interfacing with the driver directly and use `cargo marker` instead. " ); } -const BUG_REPORT_URL: &str = "https://github.com/rust-marker/marker/issues/new?template=panic.yml"; - -fn main() { - let handler = EarlyErrorHandler::new(ErrorOutputType::default()); - rustc_driver::init_rustc_env_logger(&handler); - - // FIXME(xFrednet): The ICE hook would ideally distinguish where the error - // happens. Panics from lint crates should probably not terminate Marker - // completely, but instead warn the user and continue linting with the other - // lint crate. It would also be cool if the ICE hook printed the node that - // caused the panic in the lint crate. rust-marker/marker#10 - - rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| { - handler.note_without_error(format!("{}", rustc_tools_util::get_version_info!())); - handler.note_without_error("Achievement Unlocked: [Free Ice Cream]"); - }); - - std::process::exit(rustc_driver::catch_with_exit_code(|| { - try_main().map_err(|err| { - let err = match err { - MainError::Custom(err) => err, - MainError::Rustc(err) => return err, - }; - - // Emit the error to stderr - err.print(); - - // This is a bit of a hack, but this way we can emit our own errors - // without having to change the rustc driver. - #[expect(deprecated)] - rustc_span::ErrorGuaranteed::unchecked_claim_error_was_emitted() - }) - })) -} -fn try_main() -> Result<(), MainError> { +pub fn try_main(args: impl Iterator) -> Result<(), MainError> { // Note: This driver has two different kinds of "arguments". // 1. Normal arguments, passed directly to the binary. (Collected below) // 2. Arguments provided to `cargo-marker` which are forwarded to this process as environment // values. These are usually driver-independent and handled by the adapter. - let mut orig_args: Vec = env::args().collect(); + let mut orig_args: Vec = args.collect(); let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true); let has_sys_root_arg = sys_root_arg.is_some(); @@ -386,7 +349,7 @@ fn find_sys_root(sys_root_arg: Option<&str>) -> String { .expect("need to specify SYSROOT env var during marker compilation, or use rustup or multirust") } -enum MainError { +pub enum MainError { Custom(marker_error::Error), Rustc(rustc_span::ErrorGuaranteed), } diff --git a/marker_rustc_driver/src/lint_pass.rs b/marker_rustc_driver/src/lint_pass.rs index 34360258..5b8fe869 100644 --- a/marker_rustc_driver/src/lint_pass.rs +++ b/marker_rustc_driver/src/lint_pass.rs @@ -46,7 +46,7 @@ impl<'tcx> rustc_lint::LateLintPass<'tcx> for RustcLintPass { } } -fn process_crate(rustc_cx: &rustc_lint::LateContext<'_>, adapter: &Adapter) { +pub fn process_crate(rustc_cx: &rustc_lint::LateContext<'_>, adapter: &Adapter) { let storage = Storage::default(); process_crate_lifetime(rustc_cx, &storage, adapter); } From e668b3f723abd64cd6db04006b77ea26f850442e Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Wed, 4 Oct 2023 18:08:42 -0400 Subject: [PATCH 2/7] Allow Clippy lints --- marker_rustc_driver/src/lib.rs | 1 + marker_rustc_driver/src/lint_pass.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/marker_rustc_driver/src/lib.rs b/marker_rustc_driver/src/lib.rs index 1ccbea74..2bda8f9c 100644 --- a/marker_rustc_driver/src/lib.rs +++ b/marker_rustc_driver/src/lib.rs @@ -199,6 +199,7 @@ interfacing with the driver directly and use `cargo marker` instead. ); } +#[allow(clippy::missing_errors_doc)] pub fn try_main(args: impl Iterator) -> Result<(), MainError> { // Note: This driver has two different kinds of "arguments". // 1. Normal arguments, passed directly to the binary. (Collected below) diff --git a/marker_rustc_driver/src/lint_pass.rs b/marker_rustc_driver/src/lint_pass.rs index 5b8fe869..9e1fa242 100644 --- a/marker_rustc_driver/src/lint_pass.rs +++ b/marker_rustc_driver/src/lint_pass.rs @@ -24,6 +24,7 @@ thread_local! { pub struct RustcLintPass; impl RustcLintPass { + #[allow(clippy::missing_errors_doc)] pub fn init_adapter(lint_crates: &[LintCrateInfo]) -> Result<(), marker_adapter::Error> { ADAPTER.with(move |cell| { cell.get_or_try_init(|| Adapter::new(lint_crates))?; @@ -31,6 +32,7 @@ impl RustcLintPass { }) } + #[must_use] pub fn marker_lints() -> Vec<&'static Lint> { ADAPTER.with(|adapter| adapter.get().unwrap().marker_lints()) } From dd5a9ad8e7c00ab004bb4cb14b523a52c6351457 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Wed, 4 Oct 2023 18:19:14 -0400 Subject: [PATCH 3/7] Allow private intra-doc links --- marker_rustc_driver/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/marker_rustc_driver/src/lib.rs b/marker_rustc_driver/src/lib.rs index 2bda8f9c..08aea828 100644 --- a/marker_rustc_driver/src/lib.rs +++ b/marker_rustc_driver/src/lib.rs @@ -14,6 +14,7 @@ #![allow(clippy::needless_lifetimes, reason = "lifetimes will be required to fix ICEs")] #![allow(clippy::needless_collect, reason = "false positives for `alloc_slice`")] #![allow(clippy::too_many_lines, reason = "long functions are unavoidable for matches")] +#![allow(rustdoc::private_intra_doc_links)] extern crate rustc_ast; extern crate rustc_data_structures; From 4beeb35e86f5786ab16aa4bb53ff0b5d3a34cd20 Mon Sep 17 00:00:00 2001 From: Samuel Moelius <35515885+smoelius@users.noreply.github.com> Date: Sun, 15 Oct 2023 18:13:29 -0400 Subject: [PATCH 4/7] Update marker_rustc_driver/src/bin/marker_rustc_driver.rs Co-authored-by: Fridtjof Stoldt --- .../src/bin/marker_rustc_driver.rs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/marker_rustc_driver/src/bin/marker_rustc_driver.rs b/marker_rustc_driver/src/bin/marker_rustc_driver.rs index d8ed8fe2..e2535dcd 100644 --- a/marker_rustc_driver/src/bin/marker_rustc_driver.rs +++ b/marker_rustc_driver/src/bin/marker_rustc_driver.rs @@ -1,29 +1,12 @@ #![doc = include_str!("../../README.md")] #![feature(rustc_private)] #![feature(lint_reasons)] -#![feature(non_exhaustive_omitted_patterns_lint)] #![warn(clippy::pedantic)] -#![warn(non_exhaustive_omitted_patterns)] #![allow(clippy::missing_panics_doc)] -#![allow(clippy::module_name_repetitions)] -#![allow(clippy::needless_lifetimes, reason = "lifetimes will be required to fix ICEs")] -#![allow(clippy::needless_collect, reason = "false positives for `alloc_slice`")] -#![allow(clippy::too_many_lines, reason = "long functions are unavoidable for matches")] -extern crate rustc_ast; -extern crate rustc_data_structures; extern crate rustc_driver; -extern crate rustc_errors; -extern crate rustc_hash; -extern crate rustc_hir; -extern crate rustc_hir_analysis; -extern crate rustc_interface; -extern crate rustc_lint; -extern crate rustc_lint_defs; -extern crate rustc_middle; extern crate rustc_session; extern crate rustc_span; -extern crate rustc_target; use std::env; From 8ab924030b95c3e9c528864ebdb4e3c105553cd3 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Sun, 15 Oct 2023 18:33:12 -0400 Subject: [PATCH 5/7] Address review comments --- .../src/{bin/marker_rustc_driver.rs => main.rs} | 3 +++ 1 file changed, 3 insertions(+) rename marker_rustc_driver/src/{bin/marker_rustc_driver.rs => main.rs} (93%) diff --git a/marker_rustc_driver/src/bin/marker_rustc_driver.rs b/marker_rustc_driver/src/main.rs similarity index 93% rename from marker_rustc_driver/src/bin/marker_rustc_driver.rs rename to marker_rustc_driver/src/main.rs index e2535dcd..a8525d08 100644 --- a/marker_rustc_driver/src/bin/marker_rustc_driver.rs +++ b/marker_rustc_driver/src/main.rs @@ -1,3 +1,6 @@ +//! This is the source file for the `rustc_marker_driver` binary. However, the bulk of its logic is +//! in `../lib.rs`. + #![doc = include_str!("../../README.md")] #![feature(rustc_private)] #![feature(lint_reasons)] From 8e064d3497ca6b483bfad724731375b0535908d2 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Sun, 15 Oct 2023 18:56:10 -0400 Subject: [PATCH 6/7] Correct path to README.md --- marker_rustc_driver/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marker_rustc_driver/src/main.rs b/marker_rustc_driver/src/main.rs index a8525d08..6ed70476 100644 --- a/marker_rustc_driver/src/main.rs +++ b/marker_rustc_driver/src/main.rs @@ -1,7 +1,7 @@ //! This is the source file for the `rustc_marker_driver` binary. However, the bulk of its logic is //! in `../lib.rs`. -#![doc = include_str!("../../README.md")] +#![doc = include_str!("../README.md")] #![feature(rustc_private)] #![feature(lint_reasons)] #![warn(clippy::pedantic)] From 54767efa7ea20274bdadecef36e9bbece9b3d5b0 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Mon, 16 Oct 2023 03:40:16 -0400 Subject: [PATCH 7/7] Fix doc comment typo --- marker_rustc_driver/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marker_rustc_driver/src/main.rs b/marker_rustc_driver/src/main.rs index 6ed70476..ce262839 100644 --- a/marker_rustc_driver/src/main.rs +++ b/marker_rustc_driver/src/main.rs @@ -1,4 +1,4 @@ -//! This is the source file for the `rustc_marker_driver` binary. However, the bulk of its logic is +//! This is the source file for the `marker_rustc_driver` binary. However, the bulk of its logic is //! in `../lib.rs`. #![doc = include_str!("../README.md")]