Skip to content

Commit 1c228c6

Browse files
committed
introduce Mutability::ptr_str
1 parent 89a5176 commit 1c228c6

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

compiler/rustc_ast_ir/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ impl Mutability {
5151
}
5252
}
5353

54+
/// Returns `"const"` or `"mut"` depending on the mutability.
55+
pub fn ptr_str(self) -> &'static str {
56+
match self {
57+
Mutability::Not => "const",
58+
Mutability::Mut => "mut",
59+
}
60+
}
61+
5462
/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
5563
pub fn mutably_str(self) -> &'static str {
5664
match self {

compiler/rustc_middle/src/mir/pretty.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -978,12 +978,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
978978
CopyForDeref(ref place) => write!(fmt, "deref_copy {place:#?}"),
979979

980980
AddressOf(mutability, ref place) => {
981-
let kind_str = match mutability {
982-
Mutability::Mut => "mut",
983-
Mutability::Not => "const",
984-
};
985-
986-
write!(fmt, "&raw {kind_str} {place:?}")
981+
write!(fmt, "&raw {mut_str} {place:?}", mut_str = mutability.ptr_str())
987982
}
988983

989984
Aggregate(ref kind, ref places) => {

compiler/rustc_middle/src/ty/print/pretty.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
668668
ty::Uint(t) => p!(write("{}", t.name_str())),
669669
ty::Float(t) => p!(write("{}", t.name_str())),
670670
ty::RawPtr(ty, mutbl) => {
671-
p!(write(
672-
"*{} ",
673-
match mutbl {
674-
hir::Mutability::Mut => "mut",
675-
hir::Mutability::Not => "const",
676-
}
677-
));
671+
p!(write("*{} ", mutbl.ptr_str()));
678672
p!(print(ty))
679673
}
680674
ty::Ref(r, ty, mutbl) => {

compiler/rustc_type_ir/src/ty_kind.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,8 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
363363
Str => write!(f, "str"),
364364
Array(t, c) => write!(f, "[{:?}; {:?}]", &this.wrap(t), &this.wrap(c)),
365365
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
366-
RawPtr(ty, mutbl) => {
367-
match mutbl {
368-
Mutability::Mut => write!(f, "*mut "),
369-
Mutability::Not => write!(f, "*const "),
370-
}?;
371-
write!(f, "{:?}", &this.wrap(ty))
372-
}
373-
Ref(r, t, m) => match m {
374-
Mutability::Mut => write!(f, "&{:?} mut {:?}", &this.wrap(r), &this.wrap(t)),
375-
Mutability::Not => write!(f, "&{:?} {:?}", &this.wrap(r), &this.wrap(t)),
376-
},
366+
RawPtr(ty, mutbl) => write!(f, "*{} {:?}", mutbl.ptr_str(), this.wrap(ty)),
367+
Ref(r, t, m) => write!(f, "&{:?} {}{:?}", this.wrap(r), m.prefix_str(), this.wrap(t)),
377368
FnDef(d, s) => f.debug_tuple("FnDef").field(d).field(&this.wrap(s)).finish(),
378369
FnPtr(s) => write!(f, "{:?}", &this.wrap(s)),
379370
Dynamic(p, r, repr) => match repr {

0 commit comments

Comments
 (0)