Skip to content

Commit

Permalink
Rollup merge of rust-lang#54788 - ljedrz:cleanup_rustc_mir, r=oli-obk
Browse files Browse the repository at this point in the history
A handful of cleanups for rustc/mir

- use the "regular" `into()` instead of `graphviz::IntoCow` in `mod.rs`
- `format!("{}", x)` > `x.to_string()`
- remove one unnecessary `String` allocation
- shorten the logic of one loop
- `assert!(x == y)` > `assert_eq!(x, y)`
- whitespace & formatting fixes

r? @oli-obk
  • Loading branch information
kennytm authored Oct 3, 2018
2 parents a42a4d6 + f0de294 commit 94508ca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<'tcx> Scalar {
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
let i = i.into();
debug_assert_eq!(truncate(i, size), i,
"Unsigned value {} does not fit in {} bits", i, size.bits());
"Unsigned value {} does not fit in {} bits", i, size.bits());
Scalar::Bits { bits: i, size: size.bytes() as u8 }
}

Expand All @@ -181,7 +181,7 @@ impl<'tcx> Scalar {
// `into` performed sign extension, we have to truncate
let truncated = truncate(i as u128, size);
debug_assert_eq!(sign_extend(truncated, size) as i128, i,
"Signed value {} does not fit in {} bits", i, size.bits());
"Signed value {} does not fit in {} bits", i, size.bits());
Scalar::Bits { bits: truncated, size: size.bytes() as u8 }
}

Expand Down
31 changes: 13 additions & 18 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//!
//! [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/mir/index.html

use graphviz::IntoCow;
use hir::def::CtorKind;
use hir::def_id::DefId;
use hir::{self, HirId, InlineAsm};
Expand Down Expand Up @@ -327,22 +326,20 @@ impl<'tcx> Mir<'tcx> {
if idx < stmts.len() {
&stmts[idx].source_info
} else {
assert!(idx == stmts.len());
assert_eq!(idx, stmts.len());
&block.terminator().source_info
}
}

/// Check if `sub` is a sub scope of `sup`
pub fn is_sub_scope(&self, mut sub: SourceScope, sup: SourceScope) -> bool {
loop {
if sub == sup {
return true;
}
while sub != sup {
match self.source_scopes[sub].parent_scope {
None => return false,
Some(p) => sub = p,
}
}
true
}

/// Return the return type, it always return first element from `local_decls` array
Expand Down Expand Up @@ -526,9 +523,7 @@ impl BorrowKind {
pub fn allows_two_phase_borrow(&self) -> bool {
match *self {
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
BorrowKind::Mut {
allow_two_phase_borrow,
} => allow_two_phase_borrow,
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
}
}
}
Expand Down Expand Up @@ -1574,42 +1569,42 @@ impl<'tcx> TerminatorKind<'tcx> {
};
fmt_const_val(&mut s, &c).unwrap();
s.into()
}).chain(iter::once(String::from("otherwise").into()))
}).chain(iter::once("otherwise".into()))
.collect()
}
Call {
destination: Some(_),
cleanup: Some(_),
..
} => vec!["return".into_cow(), "unwind".into_cow()],
} => vec!["return".into(), "unwind".into()],
Call {
destination: Some(_),
cleanup: None,
..
} => vec!["return".into_cow()],
} => vec!["return".into()],
Call {
destination: None,
cleanup: Some(_),
..
} => vec!["unwind".into_cow()],
} => vec!["unwind".into()],
Call {
destination: None,
cleanup: None,
..
} => vec![],
Yield { drop: Some(_), .. } => vec!["resume".into_cow(), "drop".into_cow()],
Yield { drop: None, .. } => vec!["resume".into_cow()],
Yield { drop: Some(_), .. } => vec!["resume".into(), "drop".into()],
Yield { drop: None, .. } => vec!["resume".into()],
DropAndReplace { unwind: None, .. } | Drop { unwind: None, .. } => {
vec!["return".into_cow()]
vec!["return".into()]
}
DropAndReplace {
unwind: Some(_), ..
}
| Drop {
unwind: Some(_), ..
} => vec!["return".into_cow(), "unwind".into_cow()],
} => vec!["return".into(), "unwind".into()],
Assert { cleanup: None, .. } => vec!["".into()],
Assert { .. } => vec!["success".into_cow(), "unwind".into_cow()],
Assert { .. } => vec!["success".into(), "unwind".into()],
FalseEdges {
ref imaginary_targets,
..
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
String::new()
};

let crate_disambiguator = format!("{}", tcx.crate_disambiguator(cnum));
let crate_disambiguator = tcx.crate_disambiguator(cnum).to_string();
// Using a shortened disambiguator of about 40 bits
format!("{}.{}{}",
tcx.crate_name(cnum),
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
assert!(index < adt_def.variants.len());
assert_eq!(adt_def, adt_def1);
PlaceTy::Downcast { adt_def,
substs,
variant_index: index }
substs,
variant_index: index }
}
_ => {
bug!("cannot downcast non-ADT type: `{:?}`", self)
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<'tcx> Place<'tcx> {
}
},
_ => None,
}
}
_ => None,
}
}
Expand Down Expand Up @@ -255,9 +255,9 @@ impl<'tcx> Operand<'tcx> {

impl<'tcx> BinOp {
pub fn ty<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
lhs_ty: Ty<'tcx>,
rhs_ty: Ty<'tcx>)
-> Ty<'tcx> {
lhs_ty: Ty<'tcx>,
rhs_ty: Ty<'tcx>)
-> Ty<'tcx> {
// FIXME: handle SIMD correctly
match self {
&BinOp::Add | &BinOp::Sub | &BinOp::Mul | &BinOp::Div | &BinOp::Rem |
Expand Down

0 comments on commit 94508ca

Please sign in to comment.