Skip to content

Commit c22011c

Browse files
committed
Idiomatic Rust cleanup across printer submodules
- let-else and unwrap_or_else instead of is_none()/unwrap() and manual if-let-else in link_map.rs - Remove unnecessary .clone() on HashMap get_mut/insert keys - Pass offset by value (usize) rather than by reference (&usize) - Remove redundant prelude re-imports (std::str, std::vec::Vec) - Extract opaque_placeholder_ty() for the Ty::to_val(0) sentinel - Tighten hash() visibility from pub to pub(super)
1 parent 50bb7c5 commit c22011c

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

src/printer/items.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ extern crate rustc_smir;
1010
extern crate rustc_span;
1111
extern crate stable_mir;
1212

13-
use std::str;
14-
use std::vec::Vec;
15-
1613
use rustc_middle as middle;
1714
use rustc_middle::ty::{EarlyBinder, FnSig, GenericArgs, Ty, TyCtxt, TypeFoldable};
1815
use rustc_smir::rustc_internal;
@@ -80,7 +77,7 @@ fn get_body_details(body: &stable_mir::mir::Body) -> BodyDetails {
8077
let mut v = Vec::new();
8178
let _ = body.dump(&mut v, "<omitted>");
8279
BodyDetails {
83-
pp: str::from_utf8(&v).unwrap().into(),
80+
pp: std::str::from_utf8(&v).unwrap().into(),
8481
}
8582
}
8683

src/printer/link_map.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(super) fn fn_inst_sym<'tcx>(
3131
) -> Option<FnSymInfo<'tcx>> {
3232
use FnSymType::*;
3333
inst.and_then(|inst| {
34-
let ty = if let Some(ty) = ty { ty } else { inst.ty() };
34+
let ty = ty.unwrap_or_else(|| inst.ty());
3535
let kind = ty.kind();
3636
if kind.fn_def().is_some() {
3737
let internal_inst = rustc_internal::internal(tcx, inst);
@@ -58,17 +58,16 @@ pub(super) fn update_link_map<'tcx>(
5858
fn_sym: Option<FnSymInfo<'tcx>>,
5959
source: ItemSource,
6060
) {
61-
if fn_sym.is_none() {
61+
let Some((ty, kind, name)) = fn_sym else {
6262
return;
63-
}
64-
let (ty, kind, name) = fn_sym.unwrap();
63+
};
6564
let new_val = (source, name.clone());
6665
let key = if super::link_instance_enabled() {
6766
LinkMapKey(ty, Some(kind))
6867
} else {
6968
LinkMapKey(ty, None)
7069
};
71-
if let Some(curr_val) = link_map.get_mut(&key.clone()) {
70+
if let Some(curr_val) = link_map.get_mut(&key) {
7271
if curr_val.1 != new_val.1 {
7372
if !super::link_instance_enabled() {
7473
// When LINK_INST is disabled, prefer Item over ReifyShim.
@@ -103,6 +102,6 @@ pub(super) fn update_link_map<'tcx>(
103102
key.0.kind().fn_def(),
104103
new_val
105104
);
106-
link_map.insert(key.clone(), new_val);
105+
link_map.insert(key, new_val);
107106
}
108107
}

src/printer/mir_visitor.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ use super::schema::{
3131
use super::ty_visitor::TyCollector;
3232
use super::util::{def_id_to_inst, fn_inst_for_ty};
3333

34+
/// Placeholder type used when the precise pointee type of an allocation cannot
35+
/// be recovered from the surrounding context.
36+
fn opaque_placeholder_ty() -> stable_mir::ty::Ty {
37+
stable_mir::ty::Ty::to_val(0)
38+
}
39+
3440
struct InternedValueCollector<'tcx, 'local> {
3541
tcx: TyCtxt<'tcx>,
3642
locals: &'local [LocalDecl],
@@ -57,12 +63,12 @@ fn field_for_offset(l: LayoutShape, offset: usize) -> Option<usize> {
5763
}
5864
}
5965

60-
fn get_prov_ty(ty: stable_mir::ty::Ty, offset: &usize) -> Option<stable_mir::ty::Ty> {
66+
fn get_prov_ty(ty: stable_mir::ty::Ty, offset: usize) -> Option<stable_mir::ty::Ty> {
6167
use stable_mir::ty::RigidTy;
6268
let ty_kind = ty.kind();
6369
// if ty is a pointer, box, or Ref, expect no offset and dereference
6470
if let Some(ty) = ty_kind.builtin_deref(true) {
65-
assert!(*offset == 0);
71+
assert!(offset == 0);
6672
return Some(ty.ty);
6773
}
6874

@@ -87,14 +93,14 @@ fn get_prov_ty(ty: stable_mir::ty::Ty, offset: &usize) -> Option<stable_mir::ty:
8793
}
8894
// For other structs, consult layout to determine field type
8995
RigidTy::Adt(adt_def, args) if ty_kind.is_struct() => {
90-
let field_idx = field_for_offset(layout, *offset).unwrap();
96+
let field_idx = field_for_offset(layout, offset).unwrap();
9197
// NB struct, single variant
9298
let fields = adt_def.variants().pop().map(|v| v.fields()).unwrap();
9399
fields.get(field_idx).map(|f| f.ty_with_args(args))
94100
}
95101
RigidTy::Adt(_adt_def, _args) if ty_kind.is_enum() => {
96102
// we have to figure out which variant we are dealing with (requires the data)
97-
match field_for_offset(layout, *offset) {
103+
match field_for_offset(layout, offset) {
98104
None =>
99105
// FIXME we'd have to figure out which variant we are dealing with (requires the data)
100106
{
@@ -108,7 +114,7 @@ fn get_prov_ty(ty: stable_mir::ty::Ty, offset: &usize) -> Option<stable_mir::ty:
108114
}
109115
}
110116
RigidTy::Tuple(fields) => {
111-
let field_idx = field_for_offset(layout, *offset)?;
117+
let field_idx = field_for_offset(layout, offset)?;
112118
fields.get(field_idx).copied()
113119
}
114120
RigidTy::FnPtr(_) => None,
@@ -122,14 +128,14 @@ fn get_prov_ty(ty: stable_mir::ty::Ty, offset: &usize) -> Option<stable_mir::ty:
122128
};
123129
match ref_ty {
124130
None => None,
125-
Some(ty) => get_prov_ty(ty, &0),
131+
Some(ty) => get_prov_ty(ty, 0),
126132
}
127133
}
128134

129135
fn collect_alloc(
130136
val_collector: &mut InternedValueCollector,
131137
ty: stable_mir::ty::Ty,
132-
offset: &usize,
138+
offset: usize,
133139
val: stable_mir::mir::alloc::AllocId,
134140
) {
135141
let entry = val_collector.visited_allocs.entry(val);
@@ -160,10 +166,10 @@ fn collect_alloc(
160166
.ptrs
161167
.iter()
162168
.for_each(|(prov_offset, prov)| {
163-
collect_alloc(val_collector, p_ty, prov_offset, prov.0);
169+
collect_alloc(val_collector, p_ty, *prov_offset, prov.0);
164170
});
165171
} else {
166-
entry.or_insert((stable_mir::ty::Ty::to_val(0), global_alloc.clone()));
172+
entry.or_insert((opaque_placeholder_ty(), global_alloc.clone()));
167173
}
168174
}
169175
GlobalAlloc::Static(_) | GlobalAlloc::VTable(_, _) => {
@@ -190,7 +196,7 @@ fn collect_alloc(
190196
} else {
191197
// Could not recover a precise pointee type; use an opaque 0-valued Ty
192198
// as a conservative placeholder.
193-
entry.or_insert((stable_mir::ty::Ty::to_val(0), global_alloc.clone()));
199+
entry.or_insert((opaque_placeholder_ty(), global_alloc.clone()));
194200
}
195201
} else {
196202
entry.or_insert((ty, global_alloc.clone()));
@@ -280,7 +286,7 @@ impl MirVisitor for InternedValueCollector<'_, '_> {
280286
.provenance
281287
.ptrs
282288
.iter()
283-
.for_each(|(offset, prov)| collect_alloc(self, constant.ty(), offset, prov.0));
289+
.for_each(|(offset, prov)| collect_alloc(self, constant.ty(), *offset, prov.0));
284290
}
285291
ConstantKind::Ty(ty_const) => {
286292
if let TyConstKind::Value(..) = ty_const.kind() {

src/printer/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_smir::rustc_internal;
1010
use rustc_span::symbol;
1111
use stable_mir::mir::mono::{Instance, MonoItem};
1212

13-
pub fn hash<T: std::hash::Hash>(obj: T) -> u64 {
13+
pub(super) fn hash<T: std::hash::Hash>(obj: T) -> u64 {
1414
use std::hash::Hasher;
1515
let mut hasher = std::hash::DefaultHasher::new();
1616
obj.hash(&mut hasher);

0 commit comments

Comments
 (0)