Skip to content

Commit 690332a

Browse files
committed
Auto merge of #131345 - Zalathar:rollup-scdxuou, r=Zalathar
Rollup of 3 pull requests Successful merges: - #128399 (liballoc: introduce String, Vec const-slicing) - #131308 (enable f16 and f128 on windows-gnullvm targets) - #131325 (coverage: Multiple small tweaks to counter creation) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8841a3d + 99e1244 commit 690332a

File tree

11 files changed

+195
-163
lines changed

11 files changed

+195
-163
lines changed

Diff for: compiler/rustc_mir_transform/src/coverage/counters.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_data_structures::captures::Captures;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::graph::DirectedGraph;
66
use rustc_index::IndexVec;
7+
use rustc_index::bit_set::BitSet;
78
use rustc_middle::bug;
89
use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId, Op};
910
use tracing::{debug, debug_span, instrument};
@@ -13,13 +14,13 @@ use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, TraverseCoverage
1314
/// The coverage counter or counter expression associated with a particular
1415
/// BCB node or BCB edge.
1516
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
16-
pub(super) enum BcbCounter {
17+
enum BcbCounter {
1718
Counter { id: CounterId },
1819
Expression { id: ExpressionId },
1920
}
2021

2122
impl BcbCounter {
22-
pub(super) fn as_term(&self) -> CovTerm {
23+
fn as_term(&self) -> CovTerm {
2324
match *self {
2425
BcbCounter::Counter { id, .. } => CovTerm::Counter(id),
2526
BcbCounter::Expression { id, .. } => CovTerm::Expression(id),
@@ -78,21 +79,22 @@ impl CoverageCounters {
7879
/// counters or counter expressions for nodes and edges as required.
7980
pub(super) fn make_bcb_counters(
8081
basic_coverage_blocks: &CoverageGraph,
81-
bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool,
82+
bcb_needs_counter: &BitSet<BasicCoverageBlock>,
8283
) -> Self {
83-
let num_bcbs = basic_coverage_blocks.num_nodes();
84+
let mut counters = MakeBcbCounters::new(basic_coverage_blocks, bcb_needs_counter);
85+
counters.make_bcb_counters();
8486

85-
let mut this = Self {
87+
counters.coverage_counters
88+
}
89+
90+
fn with_num_bcbs(num_bcbs: usize) -> Self {
91+
Self {
8692
counter_increment_sites: IndexVec::new(),
8793
bcb_counters: IndexVec::from_elem_n(None, num_bcbs),
8894
bcb_edge_counters: FxHashMap::default(),
8995
expressions: IndexVec::new(),
9096
expressions_memo: FxHashMap::default(),
91-
};
92-
93-
MakeBcbCounters::new(&mut this, basic_coverage_blocks).make_bcb_counters(bcb_needs_counter);
94-
95-
this
97+
}
9698
}
9799

98100
/// Shared helper used by [`Self::make_phys_node_counter`] and
@@ -218,8 +220,8 @@ impl CoverageCounters {
218220
}
219221
}
220222

221-
pub(super) fn bcb_counter(&self, bcb: BasicCoverageBlock) -> Option<BcbCounter> {
222-
self.bcb_counters[bcb]
223+
pub(super) fn term_for_bcb(&self, bcb: BasicCoverageBlock) -> Option<CovTerm> {
224+
self.bcb_counters[bcb].map(|counter| counter.as_term())
223225
}
224226

225227
/// Returns an iterator over all the nodes/edges in the coverage graph that
@@ -265,19 +267,25 @@ impl CoverageCounters {
265267

266268
/// Helper struct that allows counter creation to inspect the BCB graph.
267269
struct MakeBcbCounters<'a> {
268-
coverage_counters: &'a mut CoverageCounters,
270+
coverage_counters: CoverageCounters,
269271
basic_coverage_blocks: &'a CoverageGraph,
272+
bcb_needs_counter: &'a BitSet<BasicCoverageBlock>,
270273
}
271274

272275
impl<'a> MakeBcbCounters<'a> {
273276
fn new(
274-
coverage_counters: &'a mut CoverageCounters,
275277
basic_coverage_blocks: &'a CoverageGraph,
278+
bcb_needs_counter: &'a BitSet<BasicCoverageBlock>,
276279
) -> Self {
277-
Self { coverage_counters, basic_coverage_blocks }
280+
assert_eq!(basic_coverage_blocks.num_nodes(), bcb_needs_counter.domain_size());
281+
Self {
282+
coverage_counters: CoverageCounters::with_num_bcbs(basic_coverage_blocks.num_nodes()),
283+
basic_coverage_blocks,
284+
bcb_needs_counter,
285+
}
278286
}
279287

280-
fn make_bcb_counters(&mut self, bcb_needs_counter: impl Fn(BasicCoverageBlock) -> bool) {
288+
fn make_bcb_counters(&mut self) {
281289
debug!("make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock");
282290

283291
// Traverse the coverage graph, ensuring that every node that needs a
@@ -290,7 +298,7 @@ impl<'a> MakeBcbCounters<'a> {
290298
let mut traversal = TraverseCoverageGraphWithLoops::new(self.basic_coverage_blocks);
291299
while let Some(bcb) = traversal.next() {
292300
let _span = debug_span!("traversal", ?bcb).entered();
293-
if bcb_needs_counter(bcb) {
301+
if self.bcb_needs_counter.contains(bcb) {
294302
self.make_node_counter_and_out_edge_counters(&traversal, bcb);
295303
}
296304
}

Diff for: compiler/rustc_mir_transform/src/coverage/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
9494
return;
9595
}
9696

97-
let bcb_has_counter_mappings = |bcb| bcbs_with_counter_mappings.contains(bcb);
9897
let coverage_counters =
99-
CoverageCounters::make_bcb_counters(&basic_coverage_blocks, bcb_has_counter_mappings);
98+
CoverageCounters::make_bcb_counters(&basic_coverage_blocks, &bcbs_with_counter_mappings);
10099

101100
let mappings = create_mappings(tcx, &hir_info, &extracted_mappings, &coverage_counters);
102101
if mappings.is_empty() {
@@ -153,12 +152,8 @@ fn create_mappings<'tcx>(
153152
&source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
154153
);
155154

156-
let term_for_bcb = |bcb| {
157-
coverage_counters
158-
.bcb_counter(bcb)
159-
.expect("all BCBs with spans were given counters")
160-
.as_term()
161-
};
155+
let term_for_bcb =
156+
|bcb| coverage_counters.term_for_bcb(bcb).expect("all BCBs with spans were given counters");
162157
let region_for_span = |span: Span| make_source_region(source_map, file_name, span, body_span);
163158

164159
// Fully destructure the mappings struct to make sure we don't miss any kinds.

Diff for: library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
#![feature(const_option)]
116116
#![feature(const_pin)]
117117
#![feature(const_size_of_val)]
118+
#![feature(const_vec_string_slice)]
118119
#![feature(core_intrinsics)]
119120
#![feature(deprecated_suggestion)]
120121
#![feature(deref_pure_trait)]

Diff for: library/alloc/src/raw_vec.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<T, A: Allocator> RawVec<T, A> {
280280
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
281281
/// be careful.
282282
#[inline]
283-
pub fn ptr(&self) -> *mut T {
283+
pub const fn ptr(&self) -> *mut T {
284284
self.inner.ptr()
285285
}
286286

@@ -293,7 +293,7 @@ impl<T, A: Allocator> RawVec<T, A> {
293293
///
294294
/// This will always be `usize::MAX` if `T` is zero-sized.
295295
#[inline]
296-
pub fn capacity(&self) -> usize {
296+
pub const fn capacity(&self) -> usize {
297297
self.inner.capacity(size_of::<T>())
298298
}
299299

@@ -488,17 +488,17 @@ impl<A: Allocator> RawVecInner<A> {
488488
}
489489

490490
#[inline]
491-
fn ptr<T>(&self) -> *mut T {
491+
const fn ptr<T>(&self) -> *mut T {
492492
self.non_null::<T>().as_ptr()
493493
}
494494

495495
#[inline]
496-
fn non_null<T>(&self) -> NonNull<T> {
497-
self.ptr.cast().into()
496+
const fn non_null<T>(&self) -> NonNull<T> {
497+
self.ptr.cast().as_non_null_ptr()
498498
}
499499

500500
#[inline]
501-
fn capacity(&self, elem_size: usize) -> usize {
501+
const fn capacity(&self, elem_size: usize) -> usize {
502502
if elem_size == 0 { usize::MAX } else { self.cap.0 }
503503
}
504504

Diff for: library/alloc/src/string.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,8 @@ impl String {
10591059
#[inline]
10601060
#[must_use = "`self` will be dropped if the result is not used"]
10611061
#[stable(feature = "rust1", since = "1.0.0")]
1062-
pub fn into_bytes(self) -> Vec<u8> {
1062+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1063+
pub const fn into_bytes(self) -> Vec<u8> {
10631064
self.vec
10641065
}
10651066

@@ -1076,8 +1077,11 @@ impl String {
10761077
#[must_use]
10771078
#[stable(feature = "string_as_str", since = "1.7.0")]
10781079
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")]
1079-
pub fn as_str(&self) -> &str {
1080-
self
1080+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1081+
pub const fn as_str(&self) -> &str {
1082+
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
1083+
// at construction.
1084+
unsafe { str::from_utf8_unchecked(self.vec.as_slice()) }
10811085
}
10821086

10831087
/// Converts a `String` into a mutable string slice.
@@ -1096,8 +1100,11 @@ impl String {
10961100
#[must_use]
10971101
#[stable(feature = "string_as_str", since = "1.7.0")]
10981102
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")]
1099-
pub fn as_mut_str(&mut self) -> &mut str {
1100-
self
1103+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1104+
pub const fn as_mut_str(&mut self) -> &mut str {
1105+
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error
1106+
// at construction.
1107+
unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }
11011108
}
11021109

11031110
/// Appends a given string slice onto the end of this `String`.
@@ -1168,7 +1175,8 @@ impl String {
11681175
#[inline]
11691176
#[must_use]
11701177
#[stable(feature = "rust1", since = "1.0.0")]
1171-
pub fn capacity(&self) -> usize {
1178+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1179+
pub const fn capacity(&self) -> usize {
11721180
self.vec.capacity()
11731181
}
11741182

@@ -1431,8 +1439,9 @@ impl String {
14311439
#[inline]
14321440
#[must_use]
14331441
#[stable(feature = "rust1", since = "1.0.0")]
1434-
pub fn as_bytes(&self) -> &[u8] {
1435-
&self.vec
1442+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1443+
pub const fn as_bytes(&self) -> &[u8] {
1444+
self.vec.as_slice()
14361445
}
14371446

14381447
/// Shortens this `String` to the specified length.
@@ -1784,7 +1793,8 @@ impl String {
17841793
/// ```
17851794
#[inline]
17861795
#[stable(feature = "rust1", since = "1.0.0")]
1787-
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1796+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1797+
pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
17881798
&mut self.vec
17891799
}
17901800

@@ -1805,8 +1815,9 @@ impl String {
18051815
#[inline]
18061816
#[must_use]
18071817
#[stable(feature = "rust1", since = "1.0.0")]
1818+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
18081819
#[rustc_confusables("length", "size")]
1809-
pub fn len(&self) -> usize {
1820+
pub const fn len(&self) -> usize {
18101821
self.vec.len()
18111822
}
18121823

@@ -1824,7 +1835,8 @@ impl String {
18241835
#[inline]
18251836
#[must_use]
18261837
#[stable(feature = "rust1", since = "1.0.0")]
1827-
pub fn is_empty(&self) -> bool {
1838+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1839+
pub const fn is_empty(&self) -> bool {
18281840
self.len() == 0
18291841
}
18301842

@@ -2589,7 +2601,7 @@ impl ops::Deref for String {
25892601

25902602
#[inline]
25912603
fn deref(&self) -> &str {
2592-
unsafe { str::from_utf8_unchecked(&self.vec) }
2604+
self.as_str()
25932605
}
25942606
}
25952607

@@ -2600,7 +2612,7 @@ unsafe impl ops::DerefPure for String {}
26002612
impl ops::DerefMut for String {
26012613
#[inline]
26022614
fn deref_mut(&mut self) -> &mut str {
2603-
unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
2615+
self.as_mut_str()
26042616
}
26052617
}
26062618

0 commit comments

Comments
 (0)