Skip to content

Commit

Permalink
Auto merge of #5712 - alexcrichton:better-debug, r=ehuss
Browse files Browse the repository at this point in the history
Improve Debug display for a few types

Try to cut down on the wordiness here and make these implementations a bit more
usable by default.
  • Loading branch information
bors committed Jul 13, 2018
2 parents a5617df + de70d81 commit 88f92e5
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/cargo/core/compiler/context/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
//! (for example, with and without tests), so we actually build a dependency
//! graph of `Unit`s, which capture these properties.
use super::{BuildContext, CompileMode, Kind, Unit};
use std::collections::HashMap;

use CargoResult;
use core::dependency::Kind as DepKind;
use core::profiles::ProfileFor;
use core::{Package, Target};
use std::collections::HashMap;
use CargoResult;
use super::{BuildContext, CompileMode, Kind, Unit};

pub fn build_unit_dependencies<'a, 'cfg>(
roots: &[Unit<'a>],
Expand All @@ -41,6 +42,7 @@ pub fn build_unit_dependencies<'a, 'cfg>(
};
deps_of(unit, bcx, &mut deps, profile_for)?;
}
trace!("ALL UNIT DEPENDENCIES {:#?}", deps);

Ok(())
}
Expand Down
76 changes: 72 additions & 4 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub struct ManifestMetadata {
pub links: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum LibKind {
Lib,
Rlib,
Expand Down Expand Up @@ -125,7 +125,13 @@ impl LibKind {
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
impl fmt::Debug for LibKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.crate_type().fmt(f)
}
}

#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum TargetKind {
Lib(Vec<LibKind>),
Bin,
Expand Down Expand Up @@ -153,9 +159,23 @@ impl ser::Serialize for TargetKind {
}
}

impl fmt::Debug for TargetKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::TargetKind::*;
match *self {
Lib(ref kinds) => kinds.fmt(f),
Bin => "bin".fmt(f),
ExampleBin | ExampleLib(_) => "example".fmt(f),
Test => "test".fmt(f),
CustomBuild => "custom-build".fmt(f),
Bench => "bench".fmt(f),
}
}
}

/// Information about a binary, a library, an example, etc. that is part of the
/// package.
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct Target {
kind: TargetKind,
name: String,
Expand All @@ -173,7 +193,7 @@ pub struct Target {
for_host: bool,
}

#[derive(Clone, PartialEq, Eq, Debug)]
#[derive(Clone, PartialEq, Eq)]
struct NonHashedPathBuf {
path: PathBuf,
}
Expand All @@ -184,6 +204,12 @@ impl Hash for NonHashedPathBuf {
}
}

impl fmt::Debug for NonHashedPathBuf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.path.fmt(f)
}
}

#[derive(Serialize)]
struct SerializedTarget<'a> {
/// Is this a `--bin bin`, `--lib`, `--example ex`?
Expand All @@ -207,6 +233,48 @@ impl ser::Serialize for Target {
}
}

compact_debug! {
impl fmt::Debug for Target {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (default, default_name) = {
let src = self.src_path().to_path_buf();
match &self.kind {
TargetKind::Lib(kinds) => {
(
Target::lib_target(&self.name, kinds.clone(), src.clone()),
format!("lib_target({:?}, {:?}, {:?})",
self.name, kinds, src),
)
}
TargetKind::CustomBuild => {
(
Target::custom_build_target(&self.name, src.clone()),
format!("custom_build_target({:?}, {:?})",
self.name, src),
)
}
_ => (
Target::with_path(src.clone()),
format!("with_path({:?})", src),
),
}
};
[debug_the_fields(
kind
name
src_path
required_features
tested
benched
doc
doctest
harness
for_host
)]
}
}
}

impl Manifest {
pub fn new(
summary: Summary,
Expand Down
11 changes: 10 additions & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use util::errors::{CargoResult, CargoResultExt};
///
/// A package is a `Cargo.toml` file plus all the files that are part of it.
// TODO: Is manifest_path a relic?
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Package {
/// The package's manifest
manifest: Manifest,
Expand Down Expand Up @@ -199,6 +199,15 @@ impl fmt::Display for Package {
}
}

impl fmt::Debug for Package {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Package")
.field("id", self.summary().package_id())
.field("..", &"..")
.finish()
}
}

impl PartialEq for Package {
fn eq(&self, other: &Package) -> bool {
self.package_id() == other.package_id()
Expand Down
29 changes: 28 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {

/// Profile settings used to determine which compiler flags to use for a
/// target.
#[derive(Debug, Clone, Copy, Eq)]
#[derive(Clone, Copy, Eq)]
pub struct Profile {
pub name: &'static str,
pub opt_level: InternedString,
Expand Down Expand Up @@ -403,6 +403,33 @@ impl Default for Profile {
}
}

compact_debug! {
impl fmt::Debug for Profile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (default, default_name) = match self.name {
"dev" => (Profile::default_dev(), "default_dev()"),
"release" => (Profile::default_release(), "default_release()"),
"test" => (Profile::default_test(), "default_test()"),
"bench" => (Profile::default_bench(), "default_bench()"),
"doc" => (Profile::default_doc(), "default_doc()"),
_ => (Profile::default(), "default()"),
};
[debug_the_fields(
name
opt_level
lto
codegen_units
debuginfo
debug_assertions
overflow_checks
rpath
incremental
panic
)]
}
}
}

impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Profile({})", self.name)
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub use util::errors::Internal;

pub const CARGO_ENV: &str = "CARGO";

#[macro_use]
mod macros;

pub mod core;
pub mod ops;
pub mod sources;
Expand Down
49 changes: 49 additions & 0 deletions src/cargo/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::fmt;

macro_rules! compact_debug {
(
impl fmt::Debug for $ty:ident {
fn fmt(&$this:ident, f: &mut fmt::Formatter) -> fmt::Result {
let (default, default_name) = $e:expr;
[debug_the_fields($($field:ident)*)]
}
}
) => (

impl fmt::Debug for $ty {
fn fmt(&$this, f: &mut fmt::Formatter) -> fmt::Result {
// Try printing a pretty version where we collapse as many fields as
// possible, indicating that they're equivalent to a function call
// that's hopefully enough to indicate what each value is without
// actually dumping everything so verbosely.
let mut s = f.debug_struct(stringify!($ty));
let (default, default_name) = $e;
let mut any_default = false;

// Exhaustively match so when fields are added we get a compile
// failure
let $ty { $($field),* } = $this;
$(
if *$field == default.$field {
any_default = true;
} else {
s.field(stringify!($field), $field);
}
)*

if any_default {
s.field("..", &::macros::DisplayAsDebug(default_name));
}
s.finish()
}
}
)
}

pub struct DisplayAsDebug<T>(pub T);

impl<T: fmt::Display> fmt::Debug for DisplayAsDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

0 comments on commit 88f92e5

Please sign in to comment.