Skip to content

Commit

Permalink
Auto merge of #67964 - JohnTitor:rollup-pu5kosl, r=JohnTitor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 7, 2020
2 parents aa0769b + 23d9788 commit 4f074de
Show file tree
Hide file tree
Showing 46 changed files with 237 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/doc/book
2 changes: 1 addition & 1 deletion src/doc/edition-guide
2 changes: 1 addition & 1 deletion src/doc/embedded-book
2 changes: 1 addition & 1 deletion src/doc/nomicon
Submodule nomicon updated 1 files
+1 −3 src/phantom-data.md
2 changes: 1 addition & 1 deletion src/doc/reference
2 changes: 1 addition & 1 deletion src/doc/rust-by-example
1 change: 0 additions & 1 deletion src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#![feature(associated_type_bounds)]
#![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_drain_sorted)]
#![feature(vec_remove_item)]

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand Down
5 changes: 2 additions & 3 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ impl<T> Vec<T> {
///
/// ```
/// let mut vec = vec![1, 2, 3, 4];
/// vec.retain(|&x| x%2 == 0);
/// vec.retain(|&x| x % 2 == 0);
/// assert_eq!(vec, [2, 4]);
/// ```
///
Expand Down Expand Up @@ -1696,14 +1696,13 @@ impl<T> Vec<T> {
/// # Examples
///
/// ```
/// # #![feature(vec_remove_item)]
/// let mut vec = vec![1, 2, 3, 1];
///
/// vec.remove_item(&1);
///
/// assert_eq!(vec, vec![2, 3, 1]);
/// ```
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
#[stable(feature = "vec_remove_item", since = "1.42.0")]
pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
where
T: PartialEq<V>,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::dep_graph::DepNode;
use crate::ich::Fingerprint;
use rustc_index::vec::{Idx, IndexVec};
use rustc_index::vec::IndexVec;

rustc_index::newtype_index! {
pub struct SerializedDepNodeIndex { .. }
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#![feature(thread_local)]
#![feature(trace_macros)]
#![feature(trusted_len)]
#![feature(vec_remove_item)]
#![feature(stmt_expr_attributes)]
#![feature(integer_atomics)]
#![feature(test)]
Expand Down
1 change: 0 additions & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_hir::Node;

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_index::vec::Idx;
use rustc_macros::HashStable;
use rustc_span::{Span, DUMMY_SP};

Expand Down
17 changes: 5 additions & 12 deletions src/librustc/ty/print/obsolete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,12 @@ impl DefPathBasedNames<'tcx> {
}

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

pub fn push_def_path(&self, def_id: DefId, output: &mut String) {
Expand Down
25 changes: 18 additions & 7 deletions src/librustc_error_codes/error_codes/E0178.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
In types, the `+` type operator has low precedence, so it is often necessary
to use parentheses.
The `+` type operator was used in an ambiguous context.

For example:
Erroneous code example:

```compile_fail,E0178
trait Foo {}
struct Bar<'a> {
w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
x: &'a Foo + 'a, // error!
y: &'a mut Foo + 'a, // error!
z: fn() -> Foo + 'a, // error!
}
```

In types, the `+` type operator has low precedence, so it is often necessary
to use parentheses:

```
trait Foo {}
struct Bar<'a> {
x: &'a (Foo + 'a), // ok!
y: &'a mut (Foo + 'a), // ok!
z: fn() -> (Foo + 'a), // ok!
}
```

Expand Down
18 changes: 13 additions & 5 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ impl EmitterWriter {

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

let offset: isize = offsets
.iter()
.filter_map(
|(start, v)| if span_start_pos <= *start { None } else { Some(v) },
)
.sum();
let underline_start = (span_start_pos + start) as isize + offset;
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
assert!(underline_start >= 0 && underline_end >= 0);
for p in underline_start..underline_end {
buffer.putc(
row_num,
max_line_num_len + 3 + p as usize,
((max_line_num_len + 3) as isize + p) as usize,
'^',
Style::UnderlinePrimary,
);
Expand All @@ -1565,7 +1572,7 @@ impl EmitterWriter {
for p in underline_start - 1..underline_start + 1 {
buffer.putc(
row_num,
max_line_num_len + 3 + p as usize,
((max_line_num_len + 3) as isize + p) as usize,
'-',
Style::UnderlineSecondary,
);
Expand All @@ -1582,8 +1589,9 @@ impl EmitterWriter {
// length of the code to be substituted
let snippet_len = span_end_pos as isize - span_start_pos as isize;
// For multiple substitutions, use the position *after* the previous
// substitutions have happened.
offset += full_sub_len - snippet_len;
// substitutions have happened, only when further substitutions are
// located strictly after.
offsets.push((span_end_pos, full_sub_len - snippet_len));
}
row_num += 1;
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc_hir/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl fmt::Display for HirId {
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);

use rustc_index::vec::Idx;
rustc_index::newtype_index! {
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
Expand Down
43 changes: 23 additions & 20 deletions src/librustc_index/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ macro_rules! newtype_index {
impl $type {
$v const MAX_AS_U32: u32 = $max;

$v const MAX: $type = $type::from_u32_const($max);
$v const MAX: Self = Self::from_u32_const($max);

#[inline]
$v fn from_usize(value: usize) -> Self {
assert!(value <= ($max as usize));
unsafe {
$type::from_u32_unchecked(value as u32)
Self::from_u32_unchecked(value as u32)
}
}

#[inline]
$v fn from_u32(value: u32) -> Self {
assert!(value <= $max);
unsafe {
$type::from_u32_unchecked(value)
Self::from_u32_unchecked(value)
}
}

Expand All @@ -152,13 +152,13 @@ macro_rules! newtype_index {
];

unsafe {
$type { private: value }
Self { private: value }
}
}

#[inline]
$v const unsafe fn from_u32_unchecked(value: u32) -> Self {
$type { private: value }
Self { private: value }
}

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

fn add(self, other: usize) -> Self {
Self::new(self.index() + other)
Self::from_usize(self.index() + other)
}
}

impl Idx for $type {
impl $crate::vec::Idx for $type {
#[inline]
fn new(value: usize) -> Self {
Self::from(value)
Self::from_usize(value)
}

#[inline]
Expand All @@ -204,39 +204,39 @@ macro_rules! newtype_index {
#[inline]
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
<usize as ::std::iter::Step>::steps_between(
&Idx::index(*start),
&Idx::index(*end),
&Self::index(*start),
&Self::index(*end),
)
}

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

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

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

#[inline]
fn sub_one(&self) -> Self {
Self::new(Idx::index(*self) - 1)
Self::from_usize(Self::index(*self) - 1)
}

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

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

Expand All @@ -257,14 +257,14 @@ macro_rules! newtype_index {
impl From<usize> for $type {
#[inline]
fn from(value: usize) -> Self {
$type::from_usize(value)
Self::from_usize(value)
}
}

impl From<u32> for $type {
#[inline]
fn from(value: u32) -> Self {
$type::from_u32(value)
Self::from_u32(value)
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ macro_rules! newtype_index {
(@decodable $type:ident) => (
impl ::rustc_serialize::Decodable for $type {
fn decode<D: ::rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
d.read_u32().map(Self::from)
d.read_u32().map(Self::from_u32)
}
}
);
Expand Down Expand Up @@ -500,7 +500,7 @@ macro_rules! newtype_index {
const $name:ident = $constant:expr,
$($tokens:tt)*) => (
$(#[doc = $doc])*
pub const $name: $type = $type::from_u32_const($constant);
$v const $name: $type = $type::from_u32_const($constant);
$crate::newtype_index!(
@derives [$($derives,)*]
@attrs [$(#[$attrs])*]
Expand Down Expand Up @@ -839,3 +839,6 @@ impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
I::new(n)
}
}

#[cfg(test)]
mod tests;
51 changes: 51 additions & 0 deletions src/librustc_index/vec/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![allow(dead_code)]
newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });

#[test]
fn index_size_is_optimized() {
use std::mem::size_of;

assert_eq!(size_of::<MyIdx>(), 4);
// Uses 0xFFFF_FFFB
assert_eq!(size_of::<Option<MyIdx>>(), 4);
// Uses 0xFFFF_FFFC
assert_eq!(size_of::<Option<Option<MyIdx>>>(), 4);
// Uses 0xFFFF_FFFD
assert_eq!(size_of::<Option<Option<Option<MyIdx>>>>(), 4);
// Uses 0xFFFF_FFFE
assert_eq!(size_of::<Option<Option<Option<Option<MyIdx>>>>>(), 4);
// Uses 0xFFFF_FFFF
assert_eq!(size_of::<Option<Option<Option<Option<Option<MyIdx>>>>>>(), 4);
// Uses a tag
assert_eq!(size_of::<Option<Option<Option<Option<Option<Option<MyIdx>>>>>>>(), 8);
}

#[test]
fn range_iterator_iterates_forwards() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(
range.collect::<Vec<_>>(),
[MyIdx::from_u32(1), MyIdx::from_u32(2), MyIdx::from_u32(3)]
);
}

#[test]
fn range_iterator_iterates_backwards() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(
range.rev().collect::<Vec<_>>(),
[MyIdx::from_u32(3), MyIdx::from_u32(2), MyIdx::from_u32(1)]
);
}

#[test]
fn range_count_is_correct() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(range.count(), 3);
}

#[test]
fn range_size_hint_is_correct() {
let range = MyIdx::from_u32(1)..MyIdx::from_u32(4);
assert_eq!(range.size_hint(), (3, Some(3)));
}
Loading

0 comments on commit 4f074de

Please sign in to comment.