Skip to content

Commit 325b50f

Browse files
committed
Add wasm_c_abi future-incompat lint
1 parent 8043f62 commit 325b50f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+39
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ declare_lint_pass! {
131131
UNUSED_VARIABLES,
132132
USELESS_DEPRECATED,
133133
WARNINGS,
134+
WASM_C_ABI,
134135
WHERE_CLAUSES_OBJECT_SAFETY,
135136
WRITES_THROUGH_IMMUTABLE_POINTER,
136137
// tidy-alphabetical-end
@@ -4656,3 +4657,41 @@ declare_lint! {
46564657
reference: "issue #X <https://github.com/rust-lang/rust/issues/X>",
46574658
};
46584659
}
4660+
4661+
declare_lint! {
4662+
/// The `wasm_c_abi` lint detects crate dependencies that are incompatible
4663+
/// with future versions of Rust that will emit spec-compliant C ABI.
4664+
///
4665+
/// ### Example
4666+
///
4667+
/// ```rust,ignore (needs extern crate)
4668+
/// #![deny(wasm_c_abi)]
4669+
/// ```
4670+
///
4671+
/// This will produce:
4672+
///
4673+
/// ```text
4674+
/// error: the following packages contain code that will be rejected by a future version of Rust: wasm-bindgen v0.2.87
4675+
/// |
4676+
/// note: the lint level is defined here
4677+
/// --> src/lib.rs:1:9
4678+
/// |
4679+
/// 1 | #![deny(wasm_c_abi)]
4680+
/// | ^^^^^^^^^^
4681+
/// ```
4682+
///
4683+
/// ### Explanation
4684+
///
4685+
/// Rust has historically emitted non-spec-compliant C ABI. This has caused
4686+
/// incompatibilities between other compilers and Wasm targets. In a future
4687+
/// version of Rust this will be fixed and therefor dependencies relying on
4688+
/// the non-spec-compliant C ABI will stop functioning.
4689+
pub WASM_C_ABI,
4690+
Warn,
4691+
"detects dependencies that are incompatible with the Wasm C ABI",
4692+
@future_incompatible = FutureIncompatibleInfo {
4693+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
4694+
reference: "issue #71871 <https://github.com/rust-lang/rust/issues/71871>",
4695+
};
4696+
crate_level_only
4697+
}

compiler/rustc_metadata/src/creader.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ use proc_macro::bridge::client::ProcMacro;
3030
use std::error::Error;
3131
use std::ops::Fn;
3232
use std::path::Path;
33+
use std::str::FromStr;
3334
use std::time::Duration;
34-
use std::{cmp, iter};
35+
use std::{cmp, env, iter};
3536

3637
/// The backend's way to give the crate store access to the metadata in a library.
3738
/// Note that it returns the raw metadata bytes stored in the library file, whether
@@ -989,13 +990,53 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
989990
}
990991
}
991992

993+
fn report_future_incompatible_deps(&self, krate: &ast::Crate) {
994+
let name = self.tcx.crate_name(LOCAL_CRATE);
995+
996+
if name.as_str() == "wasm_bindgen" {
997+
if !env::var("CARGO_PKG_VERSION_MAJOR")
998+
.ok()
999+
.and_then(|major| u64::from_str(&major).ok())
1000+
.is_some_and(|major| major == 0)
1001+
{
1002+
return;
1003+
}
1004+
if !env::var("CARGO_PKG_VERSION_MINOR")
1005+
.ok()
1006+
.and_then(|minor| u64::from_str(&minor).ok())
1007+
.is_some_and(|minor| minor <= 2)
1008+
{
1009+
return;
1010+
}
1011+
if !env::var("CARGO_PKG_VERSION_PATCH")
1012+
.ok()
1013+
.and_then(|patch| u64::from_str(&patch).ok())
1014+
.is_some_and(|minor| minor <= 87)
1015+
{
1016+
return;
1017+
}
1018+
1019+
// Make a point span rather than covering the whole file
1020+
let span = krate.spans.inner_span.shrink_to_lo();
1021+
1022+
self.sess.parse_sess.buffer_lint(
1023+
lint::builtin::WASM_C_ABI,
1024+
span,
1025+
ast::CRATE_NODE_ID,
1026+
"older versions of the `wasm-bindgen` crate will be incompatible with future versions of Rust; \
1027+
please update to `wasm-bindgen` v0.2.88".to_string(),
1028+
);
1029+
}
1030+
}
1031+
9921032
pub fn postprocess(&mut self, krate: &ast::Crate) {
9931033
self.inject_forced_externs();
9941034
self.inject_profiler_runtime(krate);
9951035
self.inject_allocator_crate(krate);
9961036
self.inject_panic_runtime(krate);
9971037

9981038
self.report_unused_deps(krate);
1039+
self.report_future_incompatible_deps(krate);
9991040

10001041
info!("{:?}", CrateDump(self.cstore));
10011042
}

0 commit comments

Comments
 (0)