Skip to content

Commit

Permalink
A few cleanups for fmt_macros, graphviz, apfloat, target, serialize a…
Browse files Browse the repository at this point in the history
…nd term
  • Loading branch information
ljedrz committed Aug 11, 2018
1 parent f6d43ed commit 535bd13
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 153 deletions.
12 changes: 5 additions & 7 deletions src/libfmt_macros/lib.rs
Expand Up @@ -411,7 +411,7 @@ impl<'a> Parser<'a> {

// fill character
if let Some(&(_, c)) = self.cur.peek() {
match self.cur.clone().skip(1).next() {
match self.cur.clone().nth(1) {
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
spec.fill = Some(c);
self.cur.next();
Expand Down Expand Up @@ -504,13 +504,11 @@ impl<'a> Parser<'a> {
if word.is_empty() {
self.cur = tmp;
CountImplied
} else if self.consume('$') {
CountIsName(word)
} else {
if self.consume('$') {
CountIsName(word)
} else {
self.cur = tmp;
CountImplied
}
self.cur = tmp;
CountImplied
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/libgraphviz/lib.rs
Expand Up @@ -420,7 +420,8 @@ impl<'a> Id<'a> {
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) {
return Err(());
}
return Ok(Id { name: name });

Ok(Id { name })
}

pub fn as_slice(&'a self) -> &'a str {
Expand Down Expand Up @@ -533,10 +534,10 @@ impl<'a> LabelText<'a> {
/// Renders text as string suitable for a label in a .dot file.
/// This includes quotes or suitable delimiters.
pub fn to_dot_string(&self) -> String {
match self {
&LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
&EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
&HtmlStr(ref s) => format!("<{}>", s),
match *self {
LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
HtmlStr(ref s) => format!("<{}>", s),
}
}

Expand Down
91 changes: 44 additions & 47 deletions src/librustc_apfloat/ieee.rs
Expand Up @@ -536,23 +536,21 @@ impl<S: Semantics> fmt::Display for IeeeFloat<S> {
// Check whether we should use scientific notation.
let scientific = if width == 0 {
true
} else if exp >= 0 {
// 765e3 --> 765000
// ^^^
// But we shouldn't make the number look more precise than it is.
exp as usize > width || digits + exp as usize > precision
} else {
if exp >= 0 {
// 765e3 --> 765000
// ^^^
// But we shouldn't make the number look more precise than it is.
exp as usize > width || digits + exp as usize > precision
// Power of the most significant digit.
let msd = exp + (digits - 1) as ExpInt;
if msd >= 0 {
// 765e-2 == 7.65
false
} else {
// Power of the most significant digit.
let msd = exp + (digits - 1) as ExpInt;
if msd >= 0 {
// 765e-2 == 7.65
false
} else {
// 765e-5 == 0.00765
// ^ ^^
-msd as usize > width
}
// 765e-5 == 0.00765
// ^ ^^
-msd as usize > width
}
};

Expand Down Expand Up @@ -702,7 +700,7 @@ impl<S: Semantics> Float for IeeeFloat<S> {
// exponent = 1..10
// significand = 1..1
IeeeFloat {
sig: [!0 & ((1 << S::PRECISION) - 1)],
sig: [(1 << S::PRECISION) - 1],
exp: S::MAX_EXP,
category: Category::Normal,
sign: false,
Expand Down Expand Up @@ -1507,10 +1505,11 @@ impl<S: Semantics, T: Semantics> FloatConvert<IeeeFloat<T>> for IeeeFloat<S> {
}

// If this is a truncation, perform the shift.
let mut loss = Loss::ExactlyZero;
if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
loss = sig::shift_right(&mut r.sig, &mut 0, -shift as usize);
}
let loss = if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
sig::shift_right(&mut r.sig, &mut 0, -shift as usize)
} else {
Loss::ExactlyZero
};

// If this is an extension, perform the shift.
if shift > 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
Expand Down Expand Up @@ -1738,27 +1737,25 @@ impl<S: Semantics> IeeeFloat<S> {
bit_pos -= 4;
if bit_pos >= 0 {
r.sig[0] |= (hex_value as Limb) << bit_pos;
} else {
// If zero or one-half (the hexadecimal digit 8) are followed
// by non-zero, they're a little more than zero or one-half.
if let Some(ref mut loss) = loss {
if hex_value != 0 {
if *loss == Loss::ExactlyZero {
*loss = Loss::LessThanHalf;
}
if *loss == Loss::ExactlyHalf {
*loss = Loss::MoreThanHalf;
}
// If zero or one-half (the hexadecimal digit 8) are followed
// by non-zero, they're a little more than zero or one-half.
} else if let Some(ref mut loss) = loss {
if hex_value != 0 {
if *loss == Loss::ExactlyZero {
*loss = Loss::LessThanHalf;
}
if *loss == Loss::ExactlyHalf {
*loss = Loss::MoreThanHalf;
}
} else {
loss = Some(match hex_value {
0 => Loss::ExactlyZero,
1..=7 => Loss::LessThanHalf,
8 => Loss::ExactlyHalf,
9..=15 => Loss::MoreThanHalf,
_ => unreachable!(),
});
}
} else {
loss = Some(match hex_value {
0 => Loss::ExactlyZero,
1..=7 => Loss::LessThanHalf,
8 => Loss::ExactlyHalf,
9..=15 => Loss::MoreThanHalf,
_ => unreachable!(),
});
}
} else if c == 'p' || c == 'P' {
if !any_digits {
Expand Down Expand Up @@ -2309,9 +2306,9 @@ mod sig {

/// One, not zero, based LSB. That is, returns 0 for a zeroed significand.
pub(super) fn olsb(limbs: &[Limb]) -> usize {
for i in 0..limbs.len() {
if limbs[i] != 0 {
return i * LIMB_BITS + limbs[i].trailing_zeros() as usize + 1;
for (i, &limb) in limbs.iter().enumerate() {
if limb != 0 {
return i * LIMB_BITS + limb.trailing_zeros() as usize + 1;
}
}

Expand All @@ -2320,9 +2317,9 @@ mod sig {

/// One, not zero, based MSB. That is, returns 0 for a zeroed significand.
pub(super) fn omsb(limbs: &[Limb]) -> usize {
for i in (0..limbs.len()).rev() {
if limbs[i] != 0 {
return (i + 1) * LIMB_BITS - limbs[i].leading_zeros() as usize;
for (i, &limb) in limbs.iter().enumerate().rev() {
if limb != 0 {
return (i + 1) * LIMB_BITS - limb.leading_zeros() as usize;
}
}

Expand Down Expand Up @@ -2378,7 +2375,7 @@ mod sig {
limb = dst[i - jump];
if shift > 0 {
limb <<= shift;
if i >= jump + 1 {
if i > jump {
limb |= dst[i - jump - 1] >> (LIMB_BITS - shift);
}
}
Expand Down Expand Up @@ -2448,7 +2445,7 @@ mod sig {
let n = dst_limbs * LIMB_BITS - shift;
if n < src_bits {
let mask = (1 << (src_bits - n)) - 1;
dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << n % LIMB_BITS;
dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << (n % LIMB_BITS);
} else if n > src_bits && src_bits % LIMB_BITS > 0 {
dst[dst_limbs - 1] &= (1 << (src_bits % LIMB_BITS)) - 1;
}
Expand Down
12 changes: 4 additions & 8 deletions src/librustc_target/spec/mod.rs
Expand Up @@ -764,14 +764,10 @@ impl Target {
// the JSON parser is not updated to match the structs.

let get_req_field = |name: &str| {
match obj.find(name)
.map(|s| s.as_string())
.and_then(|os| os.map(|s| s.to_string())) {
Some(val) => Ok(val),
None => {
return Err(format!("Field {} in target specification is required", name))
}
}
obj.find(name)
.map(|s| s.as_string())
.and_then(|os| os.map(|s| s.to_string()))
.ok_or_else(|| format!("Field {} in target specification is required", name))
};

let get_opt_field = |name: &str, default: &str| {
Expand Down
2 changes: 1 addition & 1 deletion src/libserialize/hex.rs
Expand Up @@ -22,7 +22,7 @@ pub trait ToHex {
fn to_hex(&self) -> String;
}

const CHARS: &'static [u8] = b"0123456789abcdef";
const CHARS: &[u8] = b"0123456789abcdef";

impl ToHex for [u8] {
/// Turn a vector of `u8` bytes into a hexadecimal string.
Expand Down
58 changes: 28 additions & 30 deletions src/libserialize/json.rs
Expand Up @@ -438,7 +438,7 @@ fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
}

fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
const BUF: &'static str = " ";
const BUF: &str = " ";

while n >= BUF.len() {
wr.write_str(BUF)?;
Expand Down Expand Up @@ -799,21 +799,21 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
escape_str(self.writer, name)
} else {
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
write!(self.writer, "{{\n")?;
writeln!(self.writer, "{{")?;
self.curr_indent += self.indent;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "\"variant\": ")?;
escape_str(self.writer, name)?;
write!(self.writer, ",\n")?;
writeln!(self.writer, ",")?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "\"fields\": [\n")?;
writeln!(self.writer, "\"fields\": [")?;
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
write!(self.writer, "\n")?;
writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
self.curr_indent -= self.indent;
write!(self.writer, "]\n")?;
writeln!(self.writer, "]")?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
Ok(())
Expand All @@ -825,7 +825,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx != 0 {
write!(self.writer, ",\n")?;
writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
f(self)
Expand Down Expand Up @@ -864,7 +864,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
write!(self.writer, "\n")?;
writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
}
Expand All @@ -876,9 +876,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
write!(self.writer, "\n")?;
writeln!(self.writer)?;
} else {
write!(self.writer, ",\n")?;
writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
escape_str(self.writer, name)?;
Expand Down Expand Up @@ -940,7 +940,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
write!(self.writer, "\n")?;
writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "]")?;
}
Expand All @@ -952,9 +952,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
write!(self.writer, "\n")?;
writeln!(self.writer)?;
} else {
write!(self.writer, ",\n")?;
writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
f(self)
Expand All @@ -971,7 +971,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
self.curr_indent += self.indent;
f(self)?;
self.curr_indent -= self.indent;
write!(self.writer, "\n")?;
writeln!(self.writer)?;
spaces(self.writer, self.curr_indent)?;
write!(self.writer, "}}")?;
}
Expand All @@ -983,9 +983,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
{
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
if idx == 0 {
write!(self.writer, "\n")?;
writeln!(self.writer)?;
} else {
write!(self.writer, ",\n")?;
writeln!(self.writer, ",")?;
}
spaces(self.writer, self.curr_indent)?;
self.is_emitting_map_key = true;
Expand Down Expand Up @@ -1387,10 +1387,10 @@ impl Stack {

// Used by Parser to test whether the top-most element is an index.
fn last_is_index(&self) -> bool {
if self.is_empty() { return false; }
return match *self.stack.last().unwrap() {
InternalIndex(_) => true,
_ => false,
if let Some(InternalIndex(_)) = self.stack.last() {
true
} else {
false
}
}

Expand Down Expand Up @@ -1530,19 +1530,17 @@ impl<T: Iterator<Item=char>> Parser<T> {
}

F64Value(res)
} else {
if neg {
let res = (res as i64).wrapping_neg();
} else if neg {
let res = (res as i64).wrapping_neg();

// Make sure we didn't underflow.
if res > 0 {
Error(SyntaxError(InvalidNumber, self.line, self.col))
} else {
I64Value(res)
}
// Make sure we didn't underflow.
if res > 0 {
Error(SyntaxError(InvalidNumber, self.line, self.col))
} else {
U64Value(res)
I64Value(res)
}
} else {
U64Value(res)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libserialize/leb128.rs
Expand Up @@ -103,8 +103,8 @@ pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
loop {
let mut byte = (value as u8) & 0x7f;
value >>= 7;
let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
((value == -1) && ((byte & 0x40) != 0))));
let more = !(((value == 0) && ((byte & 0x40) == 0)) ||
((value == -1) && ((byte & 0x40) != 0)));

if more {
byte |= 0x80; // Mark this byte to show that more bytes will follow.
Expand Down

0 comments on commit 535bd13

Please sign in to comment.