Skip to content

Commit 2c34f38

Browse files
Move lock into CodeStats
Prevent accidental too-long borrows by ensuring only encapsulated locking.
1 parent 86c2832 commit 2c34f38

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

src/librustc/session/code_stats.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_target::abi::{Align, Size};
22
use rustc_data_structures::fx::{FxHashSet};
33
use std::cmp::{self, Ordering};
4+
use rustc_data_structures::sync::Lock;
45

56
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
67
pub struct VariantInfo {
@@ -44,13 +45,13 @@ pub struct TypeSizeInfo {
4445
pub variants: Vec<VariantInfo>,
4546
}
4647

47-
#[derive(PartialEq, Eq, Debug, Default)]
48+
#[derive(Default)]
4849
pub struct CodeStats {
49-
type_sizes: FxHashSet<TypeSizeInfo>,
50+
type_sizes: Lock<FxHashSet<TypeSizeInfo>>,
5051
}
5152

5253
impl CodeStats {
53-
pub fn record_type_size<S: ToString>(&mut self,
54+
pub fn record_type_size<S: ToString>(&self,
5455
kind: DataTypeKind,
5556
type_desc: S,
5657
align: Align,
@@ -73,11 +74,12 @@ impl CodeStats {
7374
opt_discr_size: opt_discr_size.map(|s| s.bytes()),
7475
variants,
7576
};
76-
self.type_sizes.insert(info);
77+
self.type_sizes.borrow_mut().insert(info);
7778
}
7879

7980
pub fn print_type_sizes(&self) {
80-
let mut sorted: Vec<_> = self.type_sizes.iter().collect();
81+
let type_sizes = self.type_sizes.borrow();
82+
let mut sorted: Vec<_> = type_sizes.iter().collect();
8183

8284
// Primary sort: large-to-small.
8385
// Secondary sort: description (dictionary order)

src/librustc/session/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub struct Session {
124124
pub perf_stats: PerfStats,
125125

126126
/// Data about code being compiled, gathered during compilation.
127-
pub code_stats: Lock<CodeStats>,
127+
pub code_stats: CodeStats,
128128

129129
/// If `-zfuel=crate=n` is specified, `Some(crate)`.
130130
optimization_fuel_crate: Option<String>,

src/librustc/ty/layout.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1614,13 +1614,13 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
16141614
// (delay format until we actually need it)
16151615
let record = |kind, packed, opt_discr_size, variants| {
16161616
let type_desc = format!("{:?}", layout.ty);
1617-
self.tcx.sess.code_stats.borrow_mut().record_type_size(kind,
1618-
type_desc,
1619-
layout.align.abi,
1620-
layout.size,
1621-
packed,
1622-
opt_discr_size,
1623-
variants);
1617+
self.tcx.sess.code_stats.record_type_size(kind,
1618+
type_desc,
1619+
layout.align.abi,
1620+
layout.size,
1621+
packed,
1622+
opt_discr_size,
1623+
variants);
16241624
};
16251625

16261626
let adt_def = match layout.ty.kind {

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub fn run_compiler(
394394
mem::drop(compiler.global_ctxt()?.take());
395395

396396
if sess.opts.debugging_opts.print_type_sizes {
397-
sess.code_stats.borrow().print_type_sizes();
397+
sess.code_stats.print_type_sizes();
398398
}
399399

400400
compiler.link()?;

0 commit comments

Comments
 (0)