Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b68df85

Browse files
committedNov 14, 2023
Add wasm_c_abi future-incompat lint
1 parent 49b27f4 commit b68df85

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
@@ -3470,6 +3470,7 @@ declare_lint_pass! {
34703470
UNUSED_VARIABLES,
34713471
USELESS_DEPRECATED,
34723472
WARNINGS,
3473+
WASM_C_ABI,
34733474
WHERE_CLAUSES_OBJECT_SAFETY,
34743475
// tidy-alphabetical-end
34753476
]
@@ -4619,3 +4620,41 @@ declare_lint! {
46194620
reference: "issue #115010 <https://github.com/rust-lang/rust/issues/115010>",
46204621
};
46214622
}
4623+
4624+
declare_lint! {
4625+
/// The `wasm_c_abi` lint detects crate dependencies that are incompatible
4626+
/// with future versions of Rust that will emit spec-compliant C ABI.
4627+
///
4628+
/// ### Example
4629+
///
4630+
/// ```rust,ignore (needs extern crate)
4631+
/// #![deny(wasm_c_abi)]
4632+
/// ```
4633+
///
4634+
/// This will produce:
4635+
///
4636+
/// ```text
4637+
/// error: the following packages contain code that will be rejected by a future version of Rust: wasm-bindgen v0.2.87
4638+
/// |
4639+
/// note: the lint level is defined here
4640+
/// --> src/lib.rs:1:9
4641+
/// |
4642+
/// 1 | #![deny(wasm_c_abi)]
4643+
/// | ^^^^^^^^^^
4644+
/// ```
4645+
///
4646+
/// ### Explanation
4647+
///
4648+
/// Rust has historically emitted non-spec-compliant C ABI. This has caused
4649+
/// incompatibilities between other compilers and Wasm targets. In a future
4650+
/// version of Rust this will be fixed and therefor dependencies relying on
4651+
/// the non-spec-compliant C ABI will stop functioning.
4652+
pub WASM_C_ABI,
4653+
Warn,
4654+
"detects dependencies that are incompatible with the Wasm C ABI",
4655+
@future_incompatible = FutureIncompatibleInfo {
4656+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
4657+
reference: "issue #71871 <https://github.com/rust-lang/rust/issues/71871>",
4658+
};
4659+
crate_level_only
4660+
}

‎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
pub struct CStore {
3839
metadata_loader: Box<MetadataLoaderDyn>,
@@ -979,13 +980,53 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
979980
}
980981
}
981982

983+
fn report_future_incompatible_deps(&self, krate: &ast::Crate) {
984+
let name = self.tcx.crate_name(LOCAL_CRATE);
985+
986+
if name.as_str() == "wasm_bindgen" {
987+
if !env::var("CARGO_PKG_VERSION_MAJOR")
988+
.ok()
989+
.and_then(|major| u64::from_str(&major).ok())
990+
.is_some_and(|major| major == 0)
991+
{
992+
return;
993+
}
994+
if !env::var("CARGO_PKG_VERSION_MINOR")
995+
.ok()
996+
.and_then(|minor| u64::from_str(&minor).ok())
997+
.is_some_and(|minor| minor <= 2)
998+
{
999+
return;
1000+
}
1001+
if !env::var("CARGO_PKG_VERSION_PATCH")
1002+
.ok()
1003+
.and_then(|patch| u64::from_str(&patch).ok())
1004+
.is_some_and(|minor| minor <= 87)
1005+
{
1006+
return;
1007+
}
1008+
1009+
// Make a point span rather than covering the whole file
1010+
let span = krate.spans.inner_span.shrink_to_lo();
1011+
1012+
self.sess.parse_sess.buffer_lint(
1013+
lint::builtin::WASM_C_ABI,
1014+
span,
1015+
ast::CRATE_NODE_ID,
1016+
"older versions of the `wasm-bindgen` crate will be incompatible with future versions of Rust; \
1017+
please update to `wasm-bindgen` v0.2.88".to_string(),
1018+
);
1019+
}
1020+
}
1021+
9821022
pub fn postprocess(&mut self, krate: &ast::Crate) {
9831023
self.inject_forced_externs();
9841024
self.inject_profiler_runtime(krate);
9851025
self.inject_allocator_crate(krate);
9861026
self.inject_panic_runtime(krate);
9871027

9881028
self.report_unused_deps(krate);
1029+
self.report_future_incompatible_deps(krate);
9891030

9901031
info!("{:?}", CrateDump(&self.cstore));
9911032
}

0 commit comments

Comments
 (0)
Failed to load comments.