From ffccdbf16d1c2e22937e8fc9160fc6e69b3b014d Mon Sep 17 00:00:00 2001 From: daxpedda Date: Tue, 14 Nov 2023 23:04:22 +0100 Subject: [PATCH] Add `wasm_c_abi` `future-incompat` lint --- compiler/rustc_lint_defs/src/builtin.rs | 19 +++++++++++ compiler/rustc_metadata/src/creader.rs | 43 ++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index bef9f469cc657..2621fd7ba0925 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -4619,3 +4619,22 @@ declare_lint! { reference: "issue #115010 ", }; } + +declare_lint! { + /// The `wasm_c_abi` lint detects crate dependencies that are incompatible + /// with future versions of Rust that will emit spec-compliant C ABI. + /// + /// ### Explanation + /// + /// Rust has historically emitted non-spec-compliant C ABI. This has caused + /// incompatibilities between other compilers and Wasm targets. In a future + /// version of Rust this will be fixed and therefor dependencies relying on + /// the non-spec-compliant C ABI will stop functioning. + pub WASM_C_ABI, + Warn, + "detects dependencies that are incompatible with the Wasm C ABI", + @future_incompatible = FutureIncompatibleInfo { + reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps, + reference: "issue #71871 ", + }; +} diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 14bbe65d5f1fc..ff37e4236f5d7 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -31,8 +31,9 @@ use proc_macro::bridge::client::ProcMacro; use std::error::Error; use std::ops::Fn; use std::path::Path; +use std::str::FromStr; use std::time::Duration; -use std::{cmp, iter}; +use std::{cmp, env, iter}; pub struct CStore { metadata_loader: Box, @@ -979,6 +980,45 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { } } + fn report_future_incompatible_deps(&self, krate: &ast::Crate) { + let name = self.tcx.crate_name(LOCAL_CRATE); + + if name.as_str() == "wasm_bindgen" { + if !env::var("CARGO_PKG_VERSION_MAJOR") + .ok() + .and_then(|major| u64::from_str(&major).ok()) + .is_some_and(|major| major == 0) + { + return; + } + if !env::var("CARGO_PKG_VERSION_MINOR") + .ok() + .and_then(|minor| u64::from_str(&minor).ok()) + .is_some_and(|minor| minor <= 2) + { + return; + } + if !env::var("CARGO_PKG_VERSION_PATCH") + .ok() + .and_then(|patch| u64::from_str(&patch).ok()) + .is_some_and(|minor| minor <= 87) + { + return; + } + + // Make a point span rather than covering the whole file + let span = krate.spans.inner_span.shrink_to_lo(); + + self.sess.parse_sess.buffer_lint( + lint::builtin::WASM_C_ABI, + span, + ast::CRATE_NODE_ID, + "older versions of the `wasm-bindgen` crate will be incompatible with future versions of Rust; \ + please update to `wasm-bindgen` v0.2.88".to_string(), + ); + } + } + pub fn postprocess(&mut self, krate: &ast::Crate) { self.inject_forced_externs(); self.inject_profiler_runtime(krate); @@ -986,6 +1026,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { self.inject_panic_runtime(krate); self.report_unused_deps(krate); + self.report_future_incompatible_deps(krate); info!("{:?}", CrateDump(&self.cstore)); }