Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasm_c_abi future-incompat lint #117918

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ declare_lint_pass! {
UNUSED_VARIABLES,
USELESS_DEPRECATED,
WARNINGS,
WASM_C_ABI,
WHERE_CLAUSES_OBJECT_SAFETY,
WRITES_THROUGH_IMMUTABLE_POINTER,
// tidy-alphabetical-end
Expand Down Expand Up @@ -4564,3 +4565,41 @@ declare_lint! {
reference: "issue #120192 <https://github.com/rust-lang/rust/issues/120192>",
};
}

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.
///
/// ### Example
///
/// ```rust,ignore (needs extern crate)
/// #![deny(wasm_c_abi)]
/// ```
///
/// This will produce:
///
/// ```text
/// error: the following packages contain code that will be rejected by a future version of Rust: wasm-bindgen v0.2.87
/// |
/// note: the lint level is defined here
/// --> src/lib.rs:1:9
/// |
/// 1 | #![deny(wasm_c_abi)]
/// | ^^^^^^^^^^
/// ```
///
/// ### Explanation
///
/// Rust has historically emitted non-spec-compliant C ABI. This has caused
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm uncertain: "rust" as in "rustc" or as in "wasm-bindgen"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rustc emitted the non-spec-compliant C ABI and wasm-bindgen depended on this until recently.

/// incompatibilities between other compilers and Wasm targets. In a future
/// version of Rust this will be fixed and therefore dependencies relying
/// on the non-spec-compliant C ABI will stop functioning.
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
pub WASM_C_ABI,
Warn,
"detects dependencies that are incompatible with the Wasm C ABI",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
reference: "issue #71871 <https://github.com/rust-lang/rust/issues/71871>",
};
crate_level_only
}
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ metadata_unsupported_abi =
metadata_unsupported_abi_i686 =
ABI not supported by `#[link(kind = "raw-dylib")]` on i686

metadata_wasm_c_abi =
older versions of the `wasm-bindgen` crate will be incompatible with future versions of Rust; please update to `wasm-bindgen` v0.2.88
metadata_wasm_import_form =
wasm import module must be of the form `wasm_import_module = "string"`

Expand Down
42 changes: 41 additions & 1 deletion compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/// The backend's way to give the crate store access to the metadata in a library.
/// Note that it returns the raw metadata bytes stored in the library file, whether
Expand Down Expand Up @@ -985,13 +986,52 @@ 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" {
let major = env::var("CARGO_PKG_VERSION_MAJOR")
.ok()
.and_then(|major| u64::from_str(&major).ok());
let minor = env::var("CARGO_PKG_VERSION_MINOR")
.ok()
.and_then(|minor| u64::from_str(&minor).ok());
let patch = env::var("CARGO_PKG_VERSION_PATCH")
.ok()
.and_then(|patch| u64::from_str(&patch).ok());

match (major, minor, patch) {
// v1 or bigger is valid.
(Some(1..), _, _) => return,
// v0.3 or bigger is valid.
(Some(0), Some(3..), _) => return,
// v0.2.88 or bigger is valid.
(Some(0), Some(2), Some(88..)) => return,
// Not using Cargo.
(None, None, None) => return,
_ => (),
}

// Make a point span rather than covering the whole file
let span = krate.spans.inner_span.shrink_to_lo();

self.sess.psess.buffer_lint(
lint::builtin::WASM_C_ABI,
span,
ast::CRATE_NODE_ID,
crate::fluent_generated::metadata_wasm_c_abi,
);
}
}

pub fn postprocess(&mut self, krate: &ast::Crate) {
self.inject_forced_externs();
self.inject_profiler_runtime(krate);
self.inject_allocator_crate(krate);
self.inject_panic_runtime(krate);

self.report_unused_deps(krate);
self.report_future_incompatible_deps(krate);

info!("{:?}", CrateDump(self.cstore));
}
Expand Down
Loading