Skip to content

Commit 277e618

Browse files
committed
introduce Mutability::ptr_str
1 parent db3a5c5 commit 277e618

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
@@ -671,13 +671,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
671671
p!("(", print(ty), ") is ", write("{pat:?}"))
672672
}
673673
ty::RawPtr(ty, mutbl) => {
674-
p!(write(
675-
"*{} ",
676-
match mutbl {
677-
hir::Mutability::Mut => "mut",
678-
hir::Mutability::Not => "const",
679-
}
680-
));
674+
p!(write("*{} ", mutbl.ptr_str()));
681675
p!(print(ty))
682676
}
683677
ty::Ref(r, ty, mutbl) => {

compiler/rustc_type_ir/src/ty_kind.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,8 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
373373
Array(t, c) => write!(f, "[{:?}; {:?}]", &this.wrap(t), &this.wrap(c)),
374374
Pat(t, p) => write!(f, "pattern_type!({:?} is {:?})", &this.wrap(t), &this.wrap(p)),
375375
Slice(t) => write!(f, "[{:?}]", &this.wrap(t)),
376-
RawPtr(ty, mutbl) => {
377-
match mutbl {
378-
Mutability::Mut => write!(f, "*mut "),
379-
Mutability::Not => write!(f, "*const "),
380-
}?;
381-
write!(f, "{:?}", &this.wrap(ty))
382-
}
383-
Ref(r, t, m) => match m {
384-
Mutability::Mut => write!(f, "&{:?} mut {:?}", &this.wrap(r), &this.wrap(t)),
385-
Mutability::Not => write!(f, "&{:?} {:?}", &this.wrap(r), &this.wrap(t)),
386-
},
376+
RawPtr(ty, mutbl) => write!(f, "*{} {:?}", mutbl.ptr_str(), this.wrap(ty)),
377+
Ref(r, t, m) => write!(f, "&{:?} {}{:?}", this.wrap(r), m.prefix_str(), this.wrap(t)),
387378
FnDef(d, s) => f.debug_tuple("FnDef").field(d).field(&this.wrap(s)).finish(),
388379
FnPtr(s) => write!(f, "{:?}", &this.wrap(s)),
389380
Dynamic(p, r, repr) => match repr {

0 commit comments

Comments
 (0)