Skip to content

Commit 4f074de

Browse files
committed
Auto merge of #67964 - JohnTitor:rollup-pu5kosl, r=JohnTitor
Rollup of 13 pull requests Successful merges: - #67566 (Add an unstable conversion from thread ID to u64) - #67671 (Account for `type X = impl Trait;` in lifetime suggestion) - #67727 (Stabilise vec::remove_item) - #67877 (Omit underscore constants from rustdoc) - #67880 (Handle multiple error fix suggestions carefuly) - #67898 (Improve hygiene of `newtype_index`) - #67908 (rustdoc: HTML escape const values) - #67909 (Fix ICE in const pretty printing and resolve FIXME) - #67929 (Formatting an example for method Vec.retain) - #67934 (Clean up E0178 explanation) - #67936 (fire "non_camel_case_types" for associated types) - #67943 (Missing module std in example.) - #67962 (Update books) Failed merges: r? @ghost
2 parents aa0769b + 23d9788 commit 4f074de

File tree

46 files changed

+237
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+237
-117
lines changed

src/doc/nomicon

src/liballoc/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![feature(associated_type_bounds)]
1212
#![feature(binary_heap_into_iter_sorted)]
1313
#![feature(binary_heap_drain_sorted)]
14-
#![feature(vec_remove_item)]
1514

1615
use std::collections::hash_map::DefaultHasher;
1716
use std::hash::{Hash, Hasher};

src/liballoc/vec.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ impl<T> Vec<T> {
10541054
///
10551055
/// ```
10561056
/// let mut vec = vec![1, 2, 3, 4];
1057-
/// vec.retain(|&x| x%2 == 0);
1057+
/// vec.retain(|&x| x % 2 == 0);
10581058
/// assert_eq!(vec, [2, 4]);
10591059
/// ```
10601060
///
@@ -1696,14 +1696,13 @@ impl<T> Vec<T> {
16961696
/// # Examples
16971697
///
16981698
/// ```
1699-
/// # #![feature(vec_remove_item)]
17001699
/// let mut vec = vec![1, 2, 3, 1];
17011700
///
17021701
/// vec.remove_item(&1);
17031702
///
17041703
/// assert_eq!(vec, vec![2, 3, 1]);
17051704
/// ```
1706-
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
1705+
#[stable(feature = "vec_remove_item", since = "1.42.0")]
17071706
pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
17081707
where
17091708
T: PartialEq<V>,

src/librustc/dep_graph/serialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::dep_graph::DepNode;
44
use crate::ich::Fingerprint;
5-
use rustc_index::vec::{Idx, IndexVec};
5+
use rustc_index::vec::IndexVec;
66

77
rustc_index::newtype_index! {
88
pub struct SerializedDepNodeIndex { .. }

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#![feature(thread_local)]
5151
#![feature(trace_macros)]
5252
#![feature(trusted_len)]
53-
#![feature(vec_remove_item)]
5453
#![feature(stmt_expr_attributes)]
5554
#![feature(integer_atomics)]
5655
#![feature(test)]

src/librustc/middle/region.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_hir::Node;
1414

1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
17-
use rustc_index::vec::Idx;
1817
use rustc_macros::HashStable;
1918
use rustc_span::{Span, DUMMY_SP};
2019

src/librustc/ty/print/obsolete.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,12 @@ impl DefPathBasedNames<'tcx> {
166166
}
167167

168168
// Pushes the the name of the specified const to the provided string.
169-
// If `debug` is true, usually-unprintable consts (such as `Infer`) will be printed,
170-
// as well as the unprintable types of constants (see `push_type_name` for more details).
171-
pub fn push_const_name(&self, c: &Const<'tcx>, output: &mut String, debug: bool) {
172-
if let ty::ConstKind::Value(_) = c.val {
173-
// FIXME(const_generics): we could probably do a better job here.
174-
write!(output, "{:?}", c).unwrap()
175-
} else if debug {
176-
write!(output, "{:?}", c).unwrap()
177-
} else {
178-
bug!("DefPathBasedNames: trying to create const name for unexpected const: {:?}", c,);
179-
}
169+
// If `debug` is true, the unprintable types of constants will be printed with `fmt::Debug`
170+
// (see `push_type_name` for more details).
171+
pub fn push_const_name(&self, ct: &Const<'tcx>, output: &mut String, debug: bool) {
172+
write!(output, "{}", ct).unwrap();
180173
output.push_str(": ");
181-
self.push_type_name(c.ty, output, debug);
174+
self.push_type_name(ct.ty, output, debug);
182175
}
183176

184177
pub fn push_def_path(&self, def_id: DefId, output: &mut String) {

src/librustc_error_codes/error_codes/E0178.md

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
In types, the `+` type operator has low precedence, so it is often necessary
2-
to use parentheses.
1+
The `+` type operator was used in an ambiguous context.
32

4-
For example:
3+
Erroneous code example:
54

65
```compile_fail,E0178
76
trait Foo {}
87
98
struct Bar<'a> {
10-
w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
11-
x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
12-
y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
13-
z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
9+
x: &'a Foo + 'a, // error!
10+
y: &'a mut Foo + 'a, // error!
11+
z: fn() -> Foo + 'a, // error!
12+
}
13+
```
14+
15+
In types, the `+` type operator has low precedence, so it is often necessary
16+
to use parentheses:
17+
18+
```
19+
trait Foo {}
20+
21+
struct Bar<'a> {
22+
x: &'a (Foo + 'a), // ok!
23+
y: &'a mut (Foo + 'a), // ok!
24+
z: fn() -> (Foo + 'a), // ok!
1425
}
1526
```
1627

src/librustc_errors/emitter.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ impl EmitterWriter {
15301530

15311531
// This offset and the ones below need to be signed to account for replacement code
15321532
// that is shorter than the original code.
1533-
let mut offset: isize = 0;
1533+
let mut offsets: Vec<(usize, isize)> = Vec::new();
15341534
// Only show an underline in the suggestions if the suggestion is not the
15351535
// entirety of the code being shown and the displayed code is not multiline.
15361536
if show_underline {
@@ -1550,12 +1550,19 @@ impl EmitterWriter {
15501550
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
15511551
.sum();
15521552

1553+
let offset: isize = offsets
1554+
.iter()
1555+
.filter_map(
1556+
|(start, v)| if span_start_pos <= *start { None } else { Some(v) },
1557+
)
1558+
.sum();
15531559
let underline_start = (span_start_pos + start) as isize + offset;
15541560
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
1561+
assert!(underline_start >= 0 && underline_end >= 0);
15551562
for p in underline_start..underline_end {
15561563
buffer.putc(
15571564
row_num,
1558-
max_line_num_len + 3 + p as usize,
1565+
((max_line_num_len + 3) as isize + p) as usize,
15591566
'^',
15601567
Style::UnderlinePrimary,
15611568
);
@@ -1565,7 +1572,7 @@ impl EmitterWriter {
15651572
for p in underline_start - 1..underline_start + 1 {
15661573
buffer.putc(
15671574
row_num,
1568-
max_line_num_len + 3 + p as usize,
1575+
((max_line_num_len + 3) as isize + p) as usize,
15691576
'-',
15701577
Style::UnderlineSecondary,
15711578
);
@@ -1582,8 +1589,9 @@ impl EmitterWriter {
15821589
// length of the code to be substituted
15831590
let snippet_len = span_end_pos as isize - span_start_pos as isize;
15841591
// For multiple substitutions, use the position *after* the previous
1585-
// substitutions have happened.
1586-
offset += full_sub_len - snippet_len;
1592+
// substitutions have happened, only when further substitutions are
1593+
// located strictly after.
1594+
offsets.push((span_end_pos, full_sub_len - snippet_len));
15871595
}
15881596
row_num += 1;
15891597
}

src/librustc_hir/hir_id.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl fmt::Display for HirId {
5656
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
5757
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
5858

59-
use rustc_index::vec::Idx;
6059
rustc_index::newtype_index! {
6160
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
6261
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no

src/librustc_index/vec.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ macro_rules! newtype_index {
120120
impl $type {
121121
$v const MAX_AS_U32: u32 = $max;
122122

123-
$v const MAX: $type = $type::from_u32_const($max);
123+
$v const MAX: Self = Self::from_u32_const($max);
124124

125125
#[inline]
126126
$v fn from_usize(value: usize) -> Self {
127127
assert!(value <= ($max as usize));
128128
unsafe {
129-
$type::from_u32_unchecked(value as u32)
129+
Self::from_u32_unchecked(value as u32)
130130
}
131131
}
132132

133133
#[inline]
134134
$v fn from_u32(value: u32) -> Self {
135135
assert!(value <= $max);
136136
unsafe {
137-
$type::from_u32_unchecked(value)
137+
Self::from_u32_unchecked(value)
138138
}
139139
}
140140

@@ -152,13 +152,13 @@ macro_rules! newtype_index {
152152
];
153153

154154
unsafe {
155-
$type { private: value }
155+
Self { private: value }
156156
}
157157
}
158158

159159
#[inline]
160160
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
161-
$type { private: value }
161+
Self { private: value }
162162
}
163163

164164
/// Extracts the value of this index as an integer.
@@ -184,14 +184,14 @@ macro_rules! newtype_index {
184184
type Output = Self;
185185

186186
fn add(self, other: usize) -> Self {
187-
Self::new(self.index() + other)
187+
Self::from_usize(self.index() + other)
188188
}
189189
}
190190

191-
impl Idx for $type {
191+
impl $crate::vec::Idx for $type {
192192
#[inline]
193193
fn new(value: usize) -> Self {
194-
Self::from(value)
194+
Self::from_usize(value)
195195
}
196196

197197
#[inline]
@@ -204,39 +204,39 @@ macro_rules! newtype_index {
204204
#[inline]
205205
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
206206
<usize as ::std::iter::Step>::steps_between(
207-
&Idx::index(*start),
208-
&Idx::index(*end),
207+
&Self::index(*start),
208+
&Self::index(*end),
209209
)
210210
}
211211

212212
#[inline]
213213
fn replace_one(&mut self) -> Self {
214-
::std::mem::replace(self, Self::new(1))
214+
::std::mem::replace(self, Self::from_u32(1))
215215
}
216216

217217
#[inline]
218218
fn replace_zero(&mut self) -> Self {
219-
::std::mem::replace(self, Self::new(0))
219+
::std::mem::replace(self, Self::from_u32(0))
220220
}
221221

222222
#[inline]
223223
fn add_one(&self) -> Self {
224-
Self::new(Idx::index(*self) + 1)
224+
Self::from_usize(Self::index(*self) + 1)
225225
}
226226

227227
#[inline]
228228
fn sub_one(&self) -> Self {
229-
Self::new(Idx::index(*self) - 1)
229+
Self::from_usize(Self::index(*self) - 1)
230230
}
231231

232232
#[inline]
233233
fn add_usize(&self, u: usize) -> Option<Self> {
234-
Idx::index(*self).checked_add(u).map(Self::new)
234+
Self::index(*self).checked_add(u).map(Self::from_usize)
235235
}
236236

237237
#[inline]
238238
fn sub_usize(&self, u: usize) -> Option<Self> {
239-
Idx::index(*self).checked_sub(u).map(Self::new)
239+
Self::index(*self).checked_sub(u).map(Self::from_usize)
240240
}
241241
}
242242

@@ -257,14 +257,14 @@ macro_rules! newtype_index {
257257
impl From<usize> for $type {
258258
#[inline]
259259
fn from(value: usize) -> Self {
260-
$type::from_usize(value)
260+
Self::from_usize(value)
261261
}
262262
}
263263

264264
impl From<u32> for $type {
265265
#[inline]
266266
fn from(value: u32) -> Self {
267-
$type::from_u32(value)
267+
Self::from_u32(value)
268268
}
269269
}
270270

@@ -409,7 +409,7 @@ macro_rules! newtype_index {
409409
(@decodable $type:ident) => (
410410
impl ::rustc_serialize::Decodable for $type {
411411
fn decode<D: ::rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
412-
d.read_u32().map(Self::from)
412+
d.read_u32().map(Self::from_u32)
413413
}
414414
}
415415
);
@@ -500,7 +500,7 @@ macro_rules! newtype_index {
500500
const $name:ident = $constant:expr,
501501
$($tokens:tt)*) => (
502502
$(#[doc = $doc])*
503-
pub const $name: $type = $type::from_u32_const($constant);
503+
$v const $name: $type = $type::from_u32_const($constant);
504504
$crate::newtype_index!(
505505
@derives [$($derives,)*]
506506
@attrs [$(#[$attrs])*]
@@ -839,3 +839,6 @@ impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
839839
I::new(n)
840840
}
841841
}
842+
843+
#[cfg(test)]
844+
mod tests;

src/librustc_index/vec/tests.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![allow(dead_code)]
2+
newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });
3+
4+
#[test]
5+
fn index_size_is_optimized() {
6+
use std::mem::size_of;
7+
8+
assert_eq!(size_of::<MyIdx>(), 4);
9+
// Uses 0xFFFF_FFFB
10+
assert_eq!(size_of::<Option<MyIdx>>(), 4);
11+
// Uses 0xFFFF_FFFC
12+
assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
13+
// Uses 0xFFFF_FFFD
14+
assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
15+
// Uses 0xFFFF_FFFE
16+
assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
17+
// Uses 0xFFFF_FFFF
18+
assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
19+
// Uses a tag
20+
assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
21+
}
22+
23+
#[test]
24+
fn range_iterator_iterates_forwards() {
25+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
26+
assert_eq!(
27+
range.collect::<Vec<_>>(),
28+
[MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)]
29+
);
30+
}
31+
32+
#[test]
33+
fn range_iterator_iterates_backwards() {
34+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
35+
assert_eq!(
36+
range.rev().collect::<Vec<_>>(),
37+
[MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)]
38+
);
39+
}
40+
41+
#[test]
42+
fn range_count_is_correct() {
43+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
44+
assert_eq!(range.count(), 3);
45+
}
46+
47+
#[test]
48+
fn range_size_hint_is_correct() {
49+
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
50+
assert_eq!(range.size_hint(), (3, Some(3)));
51+
}

0 commit comments

Comments
 (0)