Skip to content

Commit

Permalink
Bumped release to 0.2.13. assertion macros now use core::panic (#14)
Browse files Browse the repository at this point in the history
* Bumped release to 0.2.13. assertion macros now use `core::panic`

`core::panic` changed to allow passing a non-literal `&'static str` argument after
writing the const_format assertion macros.

Updated changelog

* Fixed bug caused by use of respan macro

* Delete .travis.yml
  • Loading branch information
rodrimati1992 authored Jan 13, 2021
1 parent 9d0fc38 commit 2946cfe
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 142 deletions.
62 changes: 0 additions & 62 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ This is the changelog,summarising changes in each version(some minor changes may

# 0.2

### 0.2.13

Fixed the assertion macros not to use `std::panic`, using `core::panic` instead, since `core::panic` changed to allow passing a non-literal `&'static str` argument.

### 0.2.11

Fixed the documentation in case that the https://github.com/rust-lang/rust/pull/80243
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,8 @@ This implicitly uses the `syn` crate, so clean compiles take a bit longer than w

- "assert": implies the "fmt" feature,
enables the assertion macros.<br>
This is a separate cargo feature because:
- It uses nightly Rust features that are less stable than the "fmt" feature does.<br>
- It requires the `std` crate, because `core::panic` requires a string literal argument.
This is a separate cargo feature because
it uses nightly Rust features that are less stable than the "fmt" feature does.

- "constant_time_as_str": implies the "fmt" feature.
An optimization that requires a few additional nightly features,
Expand All @@ -283,9 +282,6 @@ This requires a nightly Rust compiler.

`const_format` is `#![no_std]`, it can be used anywhere Rust can be used.

Caveat: The opt-in "assert" feature uses the `std::panic` macro to panic,
as of 2020-09-06 `core::panic` requires the argument to be a literal.

# Minimum Supported Rust Version

`const_format` requires Rust 1.46.0, because it uses looping an branching in const contexts.
Expand Down
2 changes: 1 addition & 1 deletion const_format/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "const_format"
version = "0.2.12"
version = "0.2.13"
authors = ["rodrimati1992 <[email protected]>"]
edition = "2018"
license = "Zlib"
Expand Down
9 changes: 2 additions & 7 deletions const_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@
//!
//! - "assert": implies the "fmt" feature,
//! enables the assertion macros.<br>
//! This is a separate cargo feature because:
//! - It uses nightly Rust features that are less stable than the "fmt" feature does.<br>
//! - It requires the `std` crate, because `core::panic` requires a string literal argument.
//! This is a separate cargo feature because
//! it uses nightly Rust features that are less stable than the "fmt" feature does.<br>
//!
//! - "constant_time_as_str": implies the "fmt" feature.
//! An optimization that requires a few additional nightly features,
Expand Down Expand Up @@ -339,10 +338,6 @@

include! {"const_debug_derive.rs"}

// Only used for panicking. Once panicking works without std, I'll remove this.
#[cfg(feature = "assert")]
extern crate std;

#[macro_use]
mod macros;

Expand Down
2 changes: 0 additions & 2 deletions const_format/src/macros/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ macro_rules! with_shared_docs {(
///
/// This macro requires the "assert" feature to be exported,
/// because it uses some nightly Rust features.<br>
/// It uses `std::panic` for panicking due to an unforseen limitation
/// in `core::panic`, which doesn't allow passing non-literal strings at compile-time.
///
$(#[$before_syntax])*
/// # Syntax
Expand Down
86 changes: 38 additions & 48 deletions const_format/src/macros/fmt_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,64 +322,54 @@ macro_rules! __concatc_expr {

#[doc(hidden)]
#[macro_export]
macro_rules! __concatc_inner{
($debug_fmt_fn:ident, $cond:expr, $($span:tt)*)=>{{
$crate::pmr::respan_to!{($($span)*) {
const fn len_nhpmwyd3nj() -> usize {
if $cond {
let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
let fmt = strlen.make_formatter(__cf_osRcTFl4A::FormattingFlags::NEW);
match $debug_fmt_fn(fmt) {
__cf_osRcTFl4A::pmr::Ok(()) => strlen.len(),
__cf_osRcTFl4A::pmr::Err(_) => 0,
}
} else {
0
macro_rules! __concatc_inner {
($debug_fmt_fn:ident, $cond:expr, $($span:tt)*) => {{
const fn len_nhpmwyd3nj() -> usize {
if $cond {
let mut strlen = __cf_osRcTFl4A::pmr::ComputeStrLength::new();
let fmt = strlen.make_formatter(__cf_osRcTFl4A::FormattingFlags::NEW);
match $debug_fmt_fn(fmt) {
__cf_osRcTFl4A::pmr::Ok(()) => strlen.len(),
__cf_osRcTFl4A::pmr::Err(_) => 0,
}
} else {
0
}
}

const LEN_NHPMWYD3NJA: usize = len_nhpmwyd3nj();
const LEN_NHPMWYD3NJA: usize = len_nhpmwyd3nj();

const fn str_writer_nhpmwyd3nja(
)-> __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]> {
let mut writer = __cf_osRcTFl4A::pmr::StrWriter::new([0; LEN_NHPMWYD3NJA]);
let error = if $cond {
$debug_fmt_fn(
__cf_osRcTFl4A::pmr::Formatter::from_sw(
&mut writer,
__cf_osRcTFl4A::FormattingFlags::NEW,
)
)
} else {
__cf_osRcTFl4A::pmr::Ok(())
};
const fn str_writer_nhpmwyd3nja(
) -> __cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]> {
let mut writer = __cf_osRcTFl4A::pmr::StrWriter::new([0; LEN_NHPMWYD3NJA]);
let error = if $cond {
$debug_fmt_fn(__cf_osRcTFl4A::pmr::Formatter::from_sw(
&mut writer,
__cf_osRcTFl4A::FormattingFlags::NEW,
))
} else {
__cf_osRcTFl4A::pmr::Ok(())
};

__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter{
error: __cf_osRcTFl4A::msg::ErrorTuple::new(error, &writer),
writer,
}
__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter {
error: __cf_osRcTFl4A::msg::ErrorTuple::new(error, &writer),
writer,
}
}

const STR_WRITER_NHPMWYD3NJA:
&__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<[u8; LEN_NHPMWYD3NJA]>=
&str_writer_nhpmwyd3nja();
const STR_WRITER_NHPMWYD3NJA: &__cf_osRcTFl4A::msg::ErrorTupleAndStrWriter<
[u8; LEN_NHPMWYD3NJA],
> = &str_writer_nhpmwyd3nja();

const _: __cf_osRcTFl4A::msg::Ok =
<
<
__cf_osRcTFl4A::msg::ErrorPicker<
[(); STR_WRITER_NHPMWYD3NJA.error.error_variant],
[(); STR_WRITER_NHPMWYD3NJA.error.capacity]
>
as __cf_osRcTFl4A::msg::ErrorAsType
>::Type
>::NEW;
const _: __cf_osRcTFl4A::msg::Ok = <<__cf_osRcTFl4A::msg::ErrorPicker<
[(); STR_WRITER_NHPMWYD3NJA.error.error_variant],
[(); STR_WRITER_NHPMWYD3NJA.error.capacity],
> as __cf_osRcTFl4A::msg::ErrorAsType>::Type>::NEW;

const STR_NHPMWYD3NJA: &str =
__cf_osRcTFl4A::strwriter_as_str!(&STR_WRITER_NHPMWYD3NJA.writer);
const STR_NHPMWYD3NJA: &str =
__cf_osRcTFl4A::strwriter_as_str!(&STR_WRITER_NHPMWYD3NJA.writer);

STR_NHPMWYD3NJA
}}
STR_NHPMWYD3NJA
}};
}

Expand Down
27 changes: 11 additions & 16 deletions const_format/src/panicking.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
pub trait Message {
const COND: bool;
const MSG: &'static str;
const PANIC: usize = <Self as PanicIf>::PANIC;
}

pub trait PanicIf: Message {
const PANIC: usize;
}

macro_rules! panic_ {
($($span:tt)*)=> {crate::pmr::respan_to!{($($span)*)
pub trait Message {
const COND: bool;
const MSG: &'static str;
const PANIC: usize = <Self as PanicIf>::PANIC;
}

pub trait PanicIf: Message {
const PANIC: usize;
}

impl<T> crate::panicking::PanicIf for T
where
T: ?Sized + crate::panicking::Message,
{
const PANIC: usize = {
use ::std::panic as do_not_show_this_code_rustc;
use ::core::panic as do_not_show_this_code_rustc;

let secret = T::MSG;
if T::COND {
Expand Down Expand Up @@ -48,10 +48,5 @@ macro_rules! assert_with_str {
}

const _: usize = <__PanicWithStr as $crate::panicking::Message>::PANIC;

// Errors about infinite loops show up above the panic.
if $cond {
loop {}
}
}};
}
4 changes: 4 additions & 0 deletions print_errors/src/using_assertc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ assertc_eq!((), (), "{}");
assertc_eq!((), (), "{}", foo = "", 100u8);

assertc_eq!(0u8, 1u8, "{}", 0);

assertc_eq!(0u8, 1u8, "{}", 0u8);

assertc!(2 + 2 == 5, "{}", 0u8);

0 comments on commit 2946cfe

Please sign in to comment.