Skip to content

Commit bbf62cd

Browse files
committed
Add wasm_c_abi future-incompat lint
1 parent e35a56d commit bbf62cd

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
@@ -129,6 +129,7 @@ declare_lint_pass! {
129129
UNUSED_VARIABLES,
130130
USELESS_DEPRECATED,
131131
WARNINGS,
132+
WASM_C_ABI,
132133
WHERE_CLAUSES_OBJECT_SAFETY,
133134
WRITES_THROUGH_IMMUTABLE_POINTER,
134135
// tidy-alphabetical-end
@@ -4656,3 +4657,41 @@ declare_lint! {
46564657
reference: "issue #120192 <https://github.com/rust-lang/rust/issues/120192>",
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 therefore dependencies relying
4688+
/// on 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
@@ -31,8 +31,9 @@ use proc_macro::bridge::client::ProcMacro;
3131
use std::error::Error;
3232
use std::ops::Fn;
3333
use std::path::Path;
34+
use std::str::FromStr;
3435
use std::time::Duration;
35-
use std::{cmp, iter};
36+
use std::{cmp, env, iter};
3637

3738
/// The backend's way to give the crate store access to the metadata in a library.
3839
/// Note that it returns the raw metadata bytes stored in the library file, whether
@@ -993,13 +994,53 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
993994
}
994995
}
995996

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

10021042
self.report_unused_deps(krate);
1043+
self.report_future_incompatible_deps(krate);
10031044

10041045
info!("{:?}", CrateDump(self.cstore));
10051046
}

0 commit comments

Comments
 (0)