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

Better rustfmt #51

Merged
merged 5 commits into from
Dec 27, 2019
Merged
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
113 changes: 57 additions & 56 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
max_width = 90 # changed
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = true # changed
normalize_doc_attributes = false
# license_template_path = "LICENSE_TEMPLATE" # changed
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
# `rustfmt` internal settings
#
# emit_mode = "Files"
# make_backup = false

binop_separator = "Front"
blank_lines_lower_bound = 0
blank_lines_upper_bound = 1
brace_style = "SameLineWhere"
color = "Auto"
combine_control_expr = false # better all around in our opinions
comment_width = 80 # changed
condense_wildcard_suffixes = true # changed
control_brace_style = "AlwaysSameLine"
disable_all_formatting = false
edition = "2018" # changed
empty_item_single_line = true
struct_lit_single_line = true
enum_discrim_align_threshold = 0
error_on_line_overflow = true # be more strict
error_on_unformatted = true # be more strict
Comment on lines +19 to +20
Copy link
Owner

Choose a reason for hiding this comment

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

Error if Rustfmt is unable to get all lines within max_width, except for comments and string literals. If this happens, then it is a bug in Rustfmt.

Reeeeeally not sure if I want our CI to fail because of a bug in rustfmt ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a bug I encountered (rust-lang/rustfmt#3805), but it was easily fixable by changing the signature width.

fn_args_layout = "Tall"
fn_single_line = false
where_single_line = false
force_explicit_abi = true
force_multiline_blocks = true # changed
format_code_in_doc_comments = true # changed
format_macro_bodies = true # changed
format_macro_matchers = true # changed
Copy link
Owner

Choose a reason for hiding this comment

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

This caused problems in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have unchanged them all.

format_strings = true # changed
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is not a good default to have. Why would you want that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't had any bad experiences with those settings, except for changing the force_multiline_blocks = true setting which was actually better with its default value. I can't remember my reasoning from a few months ago.

hard_tabs = false
hide_parse_errors = false
ignore = []
imports_indent = "Block"
imports_layout = "Vertical" # changed
imports_layout = "Vertical" # makes editing quicker
inline_attribute_width = 0 # unchanged, for ease of editing
Copy link
Owner

Choose a reason for hiding this comment

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

This option is unstable and since we only set it to its default pls remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

# license_template_path = "LICENSE_TEMPLATE" # changed
match_arm_blocks = true
match_block_trailing_comma = false
max_width = 90 # changed
merge_derives = true
merge_imports = true # changed
newline_style = "Unix" # changed, prevents `\r\n` being accidentally introduced
normalize_comments = true # no /* */ comments should be in master
normalize_doc_attributes = true # changed
overflow_delimited_expr = false
remove_nested_parens = true
reorder_impl_items = true # changed
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
report_fixme = "Always" # changed
report_todo = "Always" # changed
# required_version = "CARGO_PKG_VERSION"
skip_children = false
space_after_colon = true
space_before_colon = false
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = false # changed
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = true # changed
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = false # changed
struct_lit_single_line = true
tab_spaces = 4
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2018" # changed
version = "One"
merge_derives = true
use_try_shorthand = true # changed
use_field_init_shorthand = true # changed
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
trailing_semicolon = false # changed
type_punctuation_density = "Wide"
unstable_features = true # changed
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Always"
report_fixme = "Always"
ignore = []

# Below are `rustfmt` internal settings
#
# emit_mode = "Files"
# make_backup = false
use_field_init_shorthand = true # changed
use_small_heuristics = "Default"
use_try_shorthand = true # changed
version = "Two" # changed
where_single_line = false # unchanged, for ease of editing
wrap_comments = true # fits comments to the line
522 changes: 308 additions & 214 deletions src/apint/arithmetic.rs

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions src/apint/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ impl ApInt {
///
/// # Note
///
/// - If the bit at the given position was `0` it will be `1`
/// after this operation and vice versa.
/// - If the bit at the given position was `0` it will be `1` after this
/// operation and vice versa.
///
/// # Errors
///
Expand Down Expand Up @@ -260,9 +260,10 @@ impl ApInt {
///
/// # Note
///
/// - If the sign bit was `0` it will be `1` after this operation and vice versa.
/// - Depending on the interpretation of the `ApInt` this
/// operation changes its signedness.
/// - If the sign bit was `0` it will be `1` after this operation and vice
/// versa.
/// - Depending on the interpretation of the `ApInt` this operation changes
/// its signedness.
pub fn flip_sign_bit(&mut self) {
let sign_bit_pos = self.width().sign_bit_pos();
self.flip_bit_at(sign_bit_pos).expect(
Expand All @@ -282,7 +283,8 @@ impl ApInt {
.sum::<usize>()
}

/// Returns the number of zeros in the binary representation of this `ApInt`.
/// Returns the number of zeros in the binary representation of this
/// `ApInt`.
pub fn count_zeros(&self) -> usize {
let zeros = self
.as_digit_slice()
Expand All @@ -295,7 +297,8 @@ impl ApInt {
zeros - (digit::BITS - self.width().excess_bits().unwrap_or(digit::BITS))
}

/// Returns the number of leading zeros in the binary representation of this `ApInt`.
/// Returns the number of leading zeros in the binary representation of this
/// `ApInt`.
pub fn leading_zeros(&self) -> usize {
let mut zeros = 0;
for d in self.as_digit_slice().iter().rev() {
Expand All @@ -308,7 +311,8 @@ impl ApInt {
zeros - (digit::BITS - self.width().excess_bits().unwrap_or(digit::BITS))
}

/// Returns the number of trailing zeros in the binary representation of this `ApInt`.
/// Returns the number of trailing zeros in the binary representation of
/// this `ApInt`.
pub fn trailing_zeros(&self) -> usize {
let mut zeros = 0;
for d in self.as_digit_slice() {
Expand Down
90 changes: 30 additions & 60 deletions src/apint/casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ impl ApInt {
return Error::truncation_bitwidth_too_large(target_width, actual_width)
.with_annotation(format!(
"Cannot truncate `ApInt` with a width of {:?}
to an `ApInt` with a width of {:?} bits. \
Do you mean to extend the instance instead?",
to an `ApInt` with a width of {:?} bits. Do you mean to extend \
the instance instead?",
actual_width.to_usize(),
target_width.to_usize()
))
Expand All @@ -174,8 +174,8 @@ impl ApInt {
// The same applies to all bit widths that require the same
// amount of digits for their representation.
let excess_width = target_width.excess_bits().expect(
"We already filtered cases where `excess_bits` may return `None` \
by requiring that `self.width() > target_width`.",
"We already filtered cases where `excess_bits` may return `None` by \
requiring that `self.width() > target_width`.",
);
self.most_significant_digit_mut()
.truncate_to(excess_width)
Expand Down Expand Up @@ -255,8 +255,8 @@ impl ApInt {
if target_width < actual_width {
return Error::extension_bitwidth_too_small(target_width, actual_width)
.with_annotation(format!(
"Cannot zero-extend bit-width of {:?} to {:?} bits. \
Do you mean to truncate the instance instead?",
"Cannot zero-extend bit-width of {:?} to {:?} bits. Do you mean to \
truncate the instance instead?",
actual_width, target_width
))
.into()
Expand Down Expand Up @@ -341,8 +341,8 @@ impl ApInt {
if target_width < actual_width {
return Error::extension_bitwidth_too_small(target_width, actual_width)
.with_annotation(format!(
"Cannot sign-extend bit-width of {:?} to {:?} bits. \
Do you mean to truncate the instance instead?",
"Cannot sign-extend bit-width of {:?} to {:?} bits. Do you mean to \
truncate the instance instead?",
actual_width, target_width
))
.into()
Expand Down Expand Up @@ -385,7 +385,8 @@ impl ApInt {
assert!(target_req_digits > actual_req_digits);
let additional_digits = target_req_digits - actual_req_digits;

// Fill most-significant-digit of `self` with `1` starting from its most-significant bit.
// Fill most-significant-digit of `self` with `1` starting from its
// most-significant bit.
if let Some(excess_width) = actual_width.excess_width() {
self.most_significant_digit_mut()
.sign_extend_from(excess_width)?;
Expand Down Expand Up @@ -442,11 +443,9 @@ impl ApInt {
///
/// This operation will forward to
///
/// - [`truncate`](struct.ApInt.html#method.truncate)
/// if `target_width` is less than or equal to the width of
/// the given `ApInt`
/// - [`zero_extend`](struct.ApInt.html#method.zero_extend)
/// otherwise
/// - [`truncate`](struct.ApInt.html#method.truncate) if `target_width` is
/// less than or equal to the width of the given `ApInt`
/// - [`zero_extend`](struct.ApInt.html#method.zero_extend) otherwise
pub fn zero_resize<W>(&mut self, target_width: W)
where
W: Into<BitWidth>,
Expand All @@ -456,13 +455,13 @@ impl ApInt {

if target_width <= actual_width {
self.truncate(target_width).expect(
"It was asserted that `target_width` is \
a valid truncation `BitWidth` in this context.",
"It was asserted that `target_width` is a valid truncation `BitWidth` \
in this context.",
)
} else {
self.zero_extend(target_width).expect(
"It was asserted that `target_width` is \
a valid zero-extension `BitWidth` in this context.",
"It was asserted that `target_width` is a valid zero-extension \
`BitWidth` in this context.",
)
}
}
Expand All @@ -473,11 +472,9 @@ impl ApInt {
///
/// This operation will forward to
///
/// - [`truncate`](struct.ApInt.html#method.truncate)
/// if `target_width` is less than or equal to the width of
/// the given `ApInt`
/// - [`sign_extend`](struct.ApInt.html#method.sign_extend)
/// otherwise
/// - [`truncate`](struct.ApInt.html#method.truncate) if `target_width` is
/// less than or equal to the width of the given `ApInt`
/// - [`sign_extend`](struct.ApInt.html#method.sign_extend) otherwise
pub fn sign_resize<W>(&mut self, target_width: W)
where
W: Into<BitWidth>,
Expand All @@ -487,13 +484,13 @@ impl ApInt {

if target_width <= actual_width {
self.truncate(target_width).expect(
"It was asserted that `target_width` is \
a valid truncation `BitWidth` in this context.",
"It was asserted that `target_width` is a valid truncation `BitWidth` \
in this context.",
)
} else {
self.sign_extend(target_width).expect(
"It was asserted that `target_width` is \
a valid sign-extension `BitWidth` in this context.",
"It was asserted that `target_width` is a valid sign-extension \
`BitWidth` in this context.",
)
}
}
Expand Down Expand Up @@ -543,35 +540,6 @@ mod tests {
ApInt::from_u128(0x0000_0000_FFFF_FFFF_FFFF_FFFF_0000_0000),
ApInt::from_u128(0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF),
ApInt::all_set(BitWidth::w128()),
/* ApInt::zero(width_256),
* ApInt::one(width_256),
* ApInt::repeat_digit(width_256, Digit(1)),
* ApInt::repeat_digit(width_256, Digit(42)),
* ApInt::repeat_digit(width_256, Digit(1337)),
* ApInt::repeat_digit(width_256, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_256, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_256, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_256), */

/* ApInt::zero(width_500),
* ApInt::one(width_500),
* ApInt::repeat_digit(width_500, Digit(1)),
* ApInt::repeat_digit(width_500, Digit(42)),
* ApInt::repeat_digit(width_500, Digit(1337)),
* ApInt::repeat_digit(width_500, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_500, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_500, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_500), */

/* ApInt::zero(width_512),
* ApInt::one(width_512),
* ApInt::repeat_digit(width_512, Digit(1)),
* ApInt::repeat_digit(width_512, Digit(42)),
* ApInt::repeat_digit(width_512, Digit(1337)),
* ApInt::repeat_digit(width_512, 0xFFFF_FFFF_0000_0000),
* ApInt::repeat_digit(width_512, 0x0000_FFFF_FFFF_0000),
* ApInt::repeat_digit(width_512, 0x0000_0000_FFFF_FFFF),
* ApInt::all_set(width_512), */
]
.into_iter()
}
Expand All @@ -581,14 +549,16 @@ mod tests {

/// Test Clone impl of `ApInt`.
///
/// Invariants between the origin `ApInt` `o` and for the cloned `c` are:
/// Invariants between the origin `ApInt` `o` and for the cloned `c`
/// are:
///
/// - `o` and `c` have same bit widths
/// - If `o` is heap-allocated then `c` is, too and vice versa for stack.
/// - If `o` is heap-allocated then `c` is, too and vice versa for
/// stack.
/// - `o` and `c` have an equal amount of digits and the values of their
/// digits is equal and in the same order.
/// - Memory addresses of `c` and `o` won't overlap. (No aliasing!)
/// This is enforced by safe Rust.
/// - Memory addresses of `c` and `o` won't overlap. (No aliasing!) This
/// is enforced by safe Rust.
#[test]
fn clone() {
for apint in test_apints() {
Expand Down
Loading