Skip to content

Commit b74aa52

Browse files
committed
Disable rustc-serialize derives for future compilers
The built-in derives are being [removed], but crater showed problems with crates depending on `num v0.1`, where this feature is enabled by default. With this PR, we detect the missing built-ins and disable the derives, adding a build-script warning about it. Cargo won't show such warnings by default from non-path dependencies, unless the build fails. [removed]: rust-lang/rust#134272
1 parent e27c204 commit b74aa52

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

.travis.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ rust:
55
- nightly
66
matrix:
77
include:
8-
- rust: 1.8.0
8+
- rust: 1.19.0
99
before_script:
10-
# rand 0.3.22 started depending on rand 0.4, which requires rustc 1.15
11-
# manually hacking the lockfile due to the limitations of cargo#2773
1210
- cargo generate-lockfile
13-
- sed -i -e 's/"rand 0.[34].[0-9]\+/"rand 0.3.20/' Cargo.lock
14-
- sed -i -e '/^name = "rand"/,/^$/s/version = "0.3.[0-9]\+"/version = "0.3.20"/' Cargo.lock
11+
- cargo update -p num-integer --precise 0.1.45
12+
- cargo update -p num-traits --precise 0.2.15
13+
- cargo update -p libc --precise 0.2.163
1514
sudo: false
1615
script:
1716
- cargo build --verbose

Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ categories = [ "algorithms", "data-structures", "science" ]
88
license = "MIT/Apache-2.0"
99
name = "num-rational"
1010
repository = "https://github.com/rust-num/num-rational"
11-
version = "0.1.42"
11+
version = "0.1.43"
1212
readme = "README.md"
13+
build = "build.rs"
1314

1415
[dependencies]
1516

1617
[dependencies.num-bigint]
1718
optional = true
18-
version = "0.1.42"
19+
version = "0.1.45"
1920

2021
[dependencies.num-integer]
2122
version = "0.1.36"
@@ -37,3 +38,6 @@ version = ">= 0.7.0, < 0.9.0"
3738
[features]
3839
default = ["bigint", "rustc-serialize"]
3940
bigint = ["num-bigint"]
41+
42+
[build-dependencies]
43+
autocfg = "1.4.0"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![crate](https://img.shields.io/crates/v/num-rational.svg)](https://crates.io/crates/num-rational)
44
[![documentation](https://docs.rs/num-rational/badge.svg)](https://docs.rs/num-rational)
5-
![minimum rustc 1.8](https://img.shields.io/badge/rustc-1.8+-red.svg)
5+
![minimum rustc 1.19](https://img.shields.io/badge/rustc-1.19+-red.svg)
66
[![Travis status](https://travis-ci.org/rust-num/num-rational.svg?branch=master)](https://travis-ci.org/rust-num/num-rational)
77

88
Generic `Rational` numbers for Rust.
@@ -28,4 +28,4 @@ Release notes are available in [RELEASES.md](RELEASES.md).
2828

2929
## Compatibility
3030

31-
The `num-rational` crate is tested for rustc 1.8 and greater.
31+
The `num-rational` crate is tested for rustc 1.19 and greater.

build.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate autocfg;
2+
3+
fn main() {
4+
autocfg::rerun_path("build.rs");
5+
autocfg::emit_possibility(HAS_DERIVE);
6+
if std::env::var_os("CARGO_FEATURE_RUSTC_SERIALIZE").is_some() {
7+
let ac = autocfg::new();
8+
9+
// These built-in derives are being removed! (rust-lang/rust#134272)
10+
//
11+
// It's hard to directly probe for `derive(RustcDecodable, RustcEncodable)`, because that
12+
// depends on the external `rustc-serialize` dependency. They're in `prelude::v1` where we
13+
// can probe by path, but ironically only on relatively new versions, so we're also using
14+
// *inaccessible* `rust_2024` as a proxy for older versions.
15+
if ac.probe_raw(PRELUDE_DERIVE).is_ok() || !ac.probe_path(RUST_2024) {
16+
autocfg::emit(HAS_DERIVE);
17+
} else {
18+
println!("cargo:warning=rustc-serialize is not supported by the current compiler");
19+
}
20+
}
21+
}
22+
23+
const HAS_DERIVE: &str = "has_derive_rustc_serialize";
24+
25+
const PRELUDE_DERIVE: &str = "
26+
#[allow(soft_unstable, deprecated)]
27+
pub use std::prelude::v1::{RustcDecodable, RustcEncodable};
28+
";
29+
30+
const RUST_2024: &str = "std::prelude::rust_2024";

ci/rustup.sh

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#!/bin/sh
22
# Use rustup to locally run the same suite of tests as .travis.yml.
3-
# (You should first install/update 1.8.0, stable, beta, and nightly.)
3+
# (You should first install/update 1.19.0, stable, beta, and nightly.)
44

55
set -ex
66

77
export TRAVIS_RUST_VERSION
8-
for TRAVIS_RUST_VERSION in 1.8.0 stable beta nightly; do
8+
for TRAVIS_RUST_VERSION in 1.19.0 stable beta nightly; do
99
run="rustup run $TRAVIS_RUST_VERSION"
10-
if [ "$TRAVIS_RUST_VERSION" = 1.8.0 ]; then
11-
# rand 0.3.22 started depending on rand 0.4, which requires rustc 1.15
12-
# manually hacking the lockfile due to the limitations of cargo#2773
13-
$run cargo generate-lockfile
14-
$run sed -i -e 's/"rand 0.[34].[0-9]\+/"rand 0.3.20/' Cargo.lock
15-
$run sed -i -e '/^name = "rand"/,/^$/s/version = "0.3.[0-9]\+"/version = "0.3.20"/' Cargo.lock
10+
$run cargo generate-lockfile
11+
if [ "$TRAVIS_RUST_VERSION" = 1.19.0 ]; then
12+
$run cargo update -p num-integer --precise 0.1.45
13+
$run cargo update -p num-traits --precise 0.2.15
14+
$run cargo update -p libc --precise 0.2.163
1615
fi
1716
$run cargo build --verbose
1817
$run $PWD/ci/test_full.sh

src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
//!
1313
//! ## Compatibility
1414
//!
15-
//! The `num-rational` crate is tested for rustc 1.8 and greater.
15+
//! The `num-rational` crate is tested for rustc 1.19 and greater.
1616
1717
#![doc(html_root_url = "https://docs.rs/num-rational/0.1")]
18+
#![cfg_attr(has_derive_rustc_serialize, warn(soft_unstable))] // un-deny
1819

1920
#[cfg(feature = "rustc-serialize")]
2021
extern crate rustc_serialize;
@@ -41,7 +42,7 @@ use traits::{FromPrimitive, Float, PrimInt, Num, Signed, Zero, One, Bounded, Num
4142

4243
/// Represents the ratio between 2 numbers.
4344
#[derive(Copy, Clone, Debug)]
44-
#[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
45+
#[cfg_attr(has_derive_rustc_serialize, derive(RustcEncodable, RustcDecodable))]
4546
#[allow(missing_docs)]
4647
pub struct Ratio<T> {
4748
numer: T,

0 commit comments

Comments
 (0)