Skip to content

Commit 2569e59

Browse files
committed
make BinOp and UnOp 'displayable'
1 parent 9e75cbe commit 2569e59

File tree

1 file changed

+33
-0
lines changed
  • compiler/rustc_middle/src/mir

1 file changed

+33
-0
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,6 +2328,30 @@ impl BinOp {
23282328
use self::BinOp::*;
23292329
matches!(self, Add | Sub | Mul | Shl | Shr)
23302330
}
2331+
2332+
pub fn try_as_string(self) -> Option<String> {
2333+
use self::BinOp::*;
2334+
2335+
match self {
2336+
Add => Some("+".to_string()),
2337+
Sub => Some("-".to_string()),
2338+
Mul => Some("*".to_string()),
2339+
Div => Some("/".to_string()),
2340+
Rem => Some("%".to_string()),
2341+
BitXor => Some("^".to_string()),
2342+
BitAnd => Some("&".to_string()),
2343+
BitOr => Some("|".to_string()),
2344+
Shl => Some("<<".to_string()),
2345+
Shr => Some(">>".to_string()),
2346+
Eq => Some("=".to_string()),
2347+
Lt => Some("<".to_string()),
2348+
Le => Some("<=".to_string()),
2349+
Ne => Some("!=".to_string()),
2350+
Ge => Some(">=".to_string()),
2351+
Gt => Some(">".to_string()),
2352+
Offset => None,
2353+
}
2354+
}
23312355
}
23322356

23332357
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
@@ -2348,6 +2372,15 @@ pub enum UnOp {
23482372
Neg,
23492373
}
23502374

2375+
impl fmt::Display for UnOp {
2376+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2377+
match self {
2378+
UnOp::Not => write!(f, "!"),
2379+
UnOp::Neg => write!(f, "-"),
2380+
}
2381+
}
2382+
}
2383+
23512384
impl<'tcx> Debug for Rvalue<'tcx> {
23522385
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
23532386
use self::Rvalue::*;

0 commit comments

Comments
 (0)