Merge pull request #1920 from Mingun/track_caller

Show correct location in error messages by tracking caller of utility `assert_tokens` functions
This commit is contained in:
David Tolnay 2021-01-23 14:54:24 -08:00 committed by GitHub
commit 5fd52100b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

33
serde_test/build.rs Normal file
View File

@ -0,0 +1,33 @@
use std::env;
use std::process::Command;
use std::str;
// The rustc-cfg strings below are *not* public API. Please let us know by
// opening a GitHub issue if your build environment requires some way to enable
// these cfgs other than by executing our build script.
fn main() {
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};
// Check ability to use #[track_caller]:
// https://doc.rust-lang.org/reference/attributes/codegen.html#the-track_caller-attribute
//
// Perhaps sometime it will be possible to replace by built-in `version` attribute:
// https://github.com/rust-lang/rust/issues/64796
if minor >= 46 {
println!("cargo:rustc-cfg=has_track_caller");
}
}
fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
let output = Command::new(rustc).arg("--version").output().ok()?;
let version = str::from_utf8(&output.stdout).ok()?;
let mut pieces = version.split('.');
if pieces.next() != Some("rustc 1") {
return None;
}
pieces.next()?.parse().ok()
}

View File

@ -28,6 +28,7 @@ use std::fmt::Debug;
/// Token::StructEnd, /// Token::StructEnd,
/// ]); /// ]);
/// ``` /// ```
#[cfg_attr(has_track_caller, track_caller)]
pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token]) pub fn assert_tokens<'de, T>(value: &T, tokens: &'de [Token])
where where
T: Serialize + Deserialize<'de> + PartialEq + Debug, T: Serialize + Deserialize<'de> + PartialEq + Debug,
@ -58,6 +59,7 @@ where
/// Token::StructEnd, /// Token::StructEnd,
/// ]); /// ]);
/// ``` /// ```
#[cfg_attr(has_track_caller, track_caller)]
pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token]) pub fn assert_ser_tokens<T>(value: &T, tokens: &[Token])
where where
T: Serialize, T: Serialize,
@ -110,6 +112,7 @@ where
/// assert_ser_tokens_error(&example, expected, error); /// assert_ser_tokens_error(&example, expected, error);
/// } /// }
/// ``` /// ```
#[cfg_attr(has_track_caller, track_caller)]
pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str) pub fn assert_ser_tokens_error<T>(value: &T, tokens: &[Token], error: &str)
where where
T: Serialize, T: Serialize,
@ -147,6 +150,7 @@ where
/// Token::StructEnd, /// Token::StructEnd,
/// ]); /// ]);
/// ``` /// ```
#[cfg_attr(has_track_caller, track_caller)]
pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token]) pub fn assert_de_tokens<'de, T>(value: &T, tokens: &'de [Token])
where where
T: Deserialize<'de> + PartialEq + Debug, T: Deserialize<'de> + PartialEq + Debug,
@ -199,6 +203,7 @@ where
/// "unknown field `x`, expected `a` or `b`", /// "unknown field `x`, expected `a` or `b`",
/// ); /// );
/// ``` /// ```
#[cfg_attr(has_track_caller, track_caller)]
pub fn assert_de_tokens_error<'de, T>(tokens: &'de [Token], error: &str) pub fn assert_de_tokens_error<'de, T>(tokens: &'de [Token], error: &str)
where where
T: Deserialize<'de>, T: Deserialize<'de>,