multiversx_sc_meta_lib/tools/
rustc_version_warning.rs

1use rustc_version::Channel;
2use semver::Version;
3
4const MX_STABLE_VERSION: Version = Version::new(0, 50, 0);
5const MX_BULK_MEMORY_FIX_VERSION: Version = Version::new(0, 58, 0);
6const RUSTC_BULK_MEMORY_VERSION: Version = Version::new(1, 87, 0);
7
8/// Writes a warning if there is an incompatiblilty between the framework version and the Rustc version.
9pub fn rustc_version_warning(
10    contract_name: &str,
11    framework_version: Version,
12    rustc_version: &super::RustcVersion,
13) -> Option<String> {
14    let rustc_semver = &rustc_version.version_meta.semver;
15
16    if framework_version < MX_STABLE_VERSION
17        && rustc_version.version_meta.channel != Channel::Nightly
18    {
19        return Some(format!("
20WARNING! Contract {contract_name} is using multiversx-sc v{framework_version} with Rust {rustc_semver}.
21Compiling contracts using multiversx-sc before {MX_STABLE_VERSION} requires nightly Rust!
22Recommended Rust versions: nightly-2023-12-11 or nightly-2024-05-22."));
23    }
24
25    if framework_version < MX_BULK_MEMORY_FIX_VERSION && *rustc_semver >= RUSTC_BULK_MEMORY_VERSION
26    {
27        return Some(format!("
28WARNING! Contract {contract_name} is using multiversx-sc v{framework_version} with Rust {rustc_semver}.
29Compiling contracts using multiversx-sc before {MX_BULK_MEMORY_FIX_VERSION} with Rust v{RUSTC_BULK_MEMORY_VERSION} or newer is not supported!
30Either upgrade the framework (to {MX_BULK_MEMORY_FIX_VERSION} or newer), or downgrade the compiler to v1.86!"));
31    }
32
33    // This condition will rarely be hit.
34    //
35    // This is because in order to reach this point, the local meta crate needs to be built,
36    // which will fail if the rustc version is below minimum.
37    //
38    // It can be reached if rustc-version was configured explicitly in sc-config.toml.
39    if let Some(minimum_version) = minimum_rustc_version(&framework_version) {
40        if *rustc_semver < minimum_version {
41            return Some(format!("
42WARNING! Contract {contract_name} is using multiversx-sc v{framework_version} with Rust {rustc_semver}.
43This is below the minimum rustc version for this release, which is v{minimum_version}."));
44        }
45    }
46
47    None
48}
49
50fn minimum_rustc_version(framework_version: &Version) -> Option<Version> {
51    if *framework_version < Version::new(0, 50, 0) {
52        return None;
53    }
54
55    if *framework_version < Version::new(0, 57, 0) {
56        return Some(Version::new(1, 78, 0));
57    }
58
59    Some(Version::new(1, 83, 0))
60}