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

Implement RFC 2011 (nicer assert messages) #48973

Closed
wants to merge 7 commits into from
Closed
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
84 changes: 84 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,87 @@ pub use coresimd::simd;
#[unstable(feature = "stdsimd", issue = "48556")]
#[cfg(not(stage0))]
pub use coresimd::arch;

// FIXME: move to appropriate location
Copy link
Member

Choose a reason for hiding this comment

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

Please move this to libcore/asserting.rs (by analogy with runtime support for panicking in libcore/panicking.rs).

#[doc(hidden)]
#[allow(missing_docs)]
#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub mod assert_helper {
use fmt::{self, Debug, Formatter};

#[unstable(feature = "generic_assert_internals", issue = "44838")]
#[allow(missing_debug_implementations)]
pub enum Captured<T> {
Value(T),
NotCopy,
Unevaluated,
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub struct DebugFallback<T> {
value: Captured<T>,
alt: &'static str,
}

impl<T> DebugFallback<T> {
#[inline]
#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub fn new(value: Captured<T>, alt: &'static str) -> Self {
DebugFallback { value, alt }
}
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
impl<T> Debug for DebugFallback<T> {
default fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(match self.value {
Captured::Value(_) | Captured::NotCopy => self.alt,
Captured::Unevaluated => "(unevaluated)",
})
}
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
impl<T: Debug> Debug for DebugFallback<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.value {
Captured::Value(ref value) => Debug::fmt(value, f),
Captured::NotCopy => f.write_str(self.alt),
Captured::Unevaluated => f.write_str("(unevaluated)"),
}
}
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub trait TryCapture: Sized {
fn try_capture(&self, to: &mut Captured<Self>) -> &Self;
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
impl<T> TryCapture for T {
#[inline]
default fn try_capture(&self, to: &mut Captured<Self>) -> &Self {
*to = Captured::NotCopy;
self
}
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
impl<T: Copy> TryCapture for T {
#[inline]
fn try_capture(&self, to: &mut Captured<Self>) -> &Self {
*to = Captured::Value(*self);
self
}
}

#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub struct Unevaluated;

#[unstable(feature = "generic_assert_internals", issue = "44838")]
impl Debug for Unevaluated {
default fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("(unevaluated)")
}
}
}
4 changes: 4 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
#![feature(doc_cfg)]
#![feature(doc_masked)]
#![feature(doc_spotlight)]
#![feature(generic_assert_internals)]
#![cfg_attr(test, feature(update_panic_count))]
#![cfg_attr(windows, feature(used))]
#![cfg_attr(stage0, feature(never_type))]
Expand Down Expand Up @@ -536,3 +537,6 @@ pub use stdsimd::arch;
// the rustdoc documentation for primitive types. Using `include!`
// because rustdoc only looks for these modules at the crate level.
include!("primitive_docs.rs");

#[unstable(feature = "generic_assert_internals", issue = "44838")]
pub use core::assert_helper;
16 changes: 16 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,22 @@ pub enum BindingMode {
ByValue(Mutability),
}

impl BindingMode {
pub fn is_by_ref(&self) -> bool {
match *self {
BindingMode::ByRef(..) => true,
_ => false,
}
}

pub fn is_by_value(&self) -> bool {
match *self {
BindingMode::ByValue(..) => true,
_ => false,
}
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum RangeEnd {
Included(RangeSyntax),
Expand Down
Loading