Skip to content

Commit 127a83d

Browse files
authored
Auto merge of #37937 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 7 pull requests - Successful merges: #37442, #37760, #37836, #37851, #37859, #37913, #37925 - Failed merges:
2 parents 9fba8df + d2c600a commit 127a83d

File tree

11 files changed

+415
-21
lines changed

11 files changed

+415
-21
lines changed

src/doc/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ the language.
1717

1818
[**The Rust Reference**][ref]. While Rust does not have a
1919
specification, the reference tries to describe its working in
20-
detail. It tends to be out of date.
20+
detail. It is accurate, but not necessarily complete.
2121

2222
[**Standard Library API Reference**][api]. Documentation for the
2323
standard library.

src/librustc/ty/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use syntax::symbol::{Symbol, InternedString};
4848
use syntax_pos::{DUMMY_SP, Span};
4949

5050
use rustc_const_math::ConstInt;
51+
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
5152

5253
use hir;
5354
use hir::itemlikevisit::ItemLikeVisitor;
@@ -1887,7 +1888,7 @@ impl<'tcx> TyS<'tcx> {
18871888
/// Iterator that walks the immediate children of `self`. Hence
18881889
/// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
18891890
/// (but not `i32`, like `walk`).
1890-
pub fn walk_shallow(&'tcx self) -> IntoIter<Ty<'tcx>> {
1891+
pub fn walk_shallow(&'tcx self) -> AccIntoIter<walk::TypeWalkerArray<'tcx>> {
18911892
walk::walk_shallow(self)
18921893
}
18931894

src/librustc/ty/walk.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,22 @@
1212
//! WARNING: this does not keep track of the region depth.
1313
1414
use ty::{self, Ty};
15-
use std::iter::Iterator;
16-
use std::vec::IntoIter;
15+
use rustc_data_structures::small_vec::SmallVec;
16+
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
17+
18+
// The TypeWalker's stack is hot enough that it's worth going to some effort to
19+
// avoid heap allocations.
20+
pub type TypeWalkerArray<'tcx> = [Ty<'tcx>; 8];
21+
pub type TypeWalkerStack<'tcx> = SmallVec<TypeWalkerArray<'tcx>>;
1722

1823
pub struct TypeWalker<'tcx> {
19-
stack: Vec<Ty<'tcx>>,
24+
stack: TypeWalkerStack<'tcx>,
2025
last_subtree: usize,
2126
}
2227

2328
impl<'tcx> TypeWalker<'tcx> {
2429
pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> {
25-
TypeWalker { stack: vec![ty], last_subtree: 1, }
30+
TypeWalker { stack: SmallVec::one(ty), last_subtree: 1, }
2631
}
2732

2833
/// Skips the subtree of types corresponding to the last type
@@ -61,8 +66,8 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
6166
}
6267
}
6368

64-
pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> IntoIter<Ty<'tcx>> {
65-
let mut stack = vec![];
69+
pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> AccIntoIter<TypeWalkerArray<'tcx>> {
70+
let mut stack = SmallVec::new();
6671
push_subtypes(&mut stack, ty);
6772
stack.into_iter()
6873
}
@@ -73,7 +78,7 @@ pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> IntoIter<Ty<'tcx>> {
7378
// known to be significant to any code, but it seems like the
7479
// natural order one would expect (basically, the order of the
7580
// types as they are written).
76-
fn push_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, parent_ty: Ty<'tcx>) {
81+
fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
7782
match parent_ty.sty {
7883
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
7984
ty::TyStr | ty::TyInfer(_) | ty::TyParam(_) | ty::TyNever | ty::TyError => {
@@ -112,7 +117,7 @@ fn push_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, parent_ty: Ty<'tcx>) {
112117
}
113118
}
114119

115-
fn push_sig_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, sig: &ty::PolyFnSig<'tcx>) {
120+
fn push_sig_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, sig: &ty::PolyFnSig<'tcx>) {
116121
stack.push(sig.0.output);
117122
stack.extend(sig.0.inputs.iter().cloned().rev());
118123
}

src/librustc_data_structures/small_vec.rs

+12
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ impl<A: Array> SmallVec<A> {
130130
self.set_len(len + 1);
131131
}
132132
}
133+
134+
pub fn truncate(&mut self, len: usize) {
135+
unsafe {
136+
while len < self.len() {
137+
// Decrement len before the drop_in_place(), so a panic on Drop
138+
// doesn't re-drop the just-failed value.
139+
let newlen = self.len() - 1;
140+
self.set_len(newlen);
141+
::std::ptr::drop_in_place(self.get_unchecked_mut(newlen));
142+
}
143+
}
144+
}
133145
}
134146

135147
impl<A: Array> Deref for SmallVec<A> {

src/librustc_typeck/check/cast.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum CastError {
102102
/// Cast of thin to fat raw ptr (eg. `*const () as *const [u8]`)
103103
SizedUnsizedCast,
104104
IllegalCast,
105+
NeedDeref,
105106
NeedViaPtr,
106107
NeedViaThinPtr,
107108
NeedViaInt,
@@ -138,6 +139,25 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
138139

139140
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) {
140141
match e {
142+
CastError::NeedDeref => {
143+
let cast_ty = fcx.ty_to_string(self.cast_ty);
144+
let mut err = fcx.type_error_struct(self.cast_span,
145+
|actual| {
146+
format!("casting `{}` as `{}` is invalid",
147+
actual,
148+
cast_ty)
149+
},
150+
self.expr_ty);
151+
err.span_label(self.expr.span,
152+
&format!("cannot cast `{}` as `{}`",
153+
fcx.ty_to_string(self.expr_ty),
154+
cast_ty));
155+
if let Ok(snippet) = fcx.sess().codemap().span_to_snippet(self.expr.span) {
156+
err.span_label(self.expr.span,
157+
&format!("did you mean `*{}`?", snippet));
158+
}
159+
err.emit();
160+
}
141161
CastError::NeedViaThinPtr |
142162
CastError::NeedViaPtr => {
143163
let mut err = fcx.type_error_struct(self.span,
@@ -390,8 +410,28 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
390410
(Ptr(m_e), Ptr(m_c)) => self.check_ptr_ptr_cast(fcx, m_e, m_c), // ptr-ptr-cast
391411
(Ptr(m_expr), Int(_)) => self.check_ptr_addr_cast(fcx, m_expr), // ptr-addr-cast
392412
(FnPtr, Int(_)) => Ok(CastKind::FnPtrAddrCast),
393-
(RPtr(_), Int(_)) |
394-
(RPtr(_), Float) => Err(CastError::NeedViaPtr),
413+
(RPtr(p), Int(_)) |
414+
(RPtr(p), Float) => {
415+
match p.ty.sty {
416+
ty::TypeVariants::TyInt(_) |
417+
ty::TypeVariants::TyUint(_) |
418+
ty::TypeVariants::TyFloat(_) => {
419+
Err(CastError::NeedDeref)
420+
}
421+
ty::TypeVariants::TyInfer(t) => {
422+
match t {
423+
ty::InferTy::IntVar(_) |
424+
ty::InferTy::FloatVar(_) |
425+
ty::InferTy::FreshIntTy(_) |
426+
ty::InferTy::FreshFloatTy(_) => {
427+
Err(CastError::NeedDeref)
428+
}
429+
_ => Err(CastError::NeedViaPtr),
430+
}
431+
}
432+
_ => Err(CastError::NeedViaPtr),
433+
}
434+
}
395435
// * -> ptr
396436
(Int(_), Ptr(mt)) => self.check_addr_ptr_cast(fcx, mt), // addr-ptr-cast
397437
(FnPtr, Ptr(mt)) => self.check_fptr_ptr_cast(fcx, mt),

src/libstd/env.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -546,17 +546,23 @@ pub fn current_exe() -> io::Result<PathBuf> {
546546
os_imp::current_exe()
547547
}
548548

549-
/// An iterator over the arguments of a process, yielding a `String` value
549+
/// An iterator over the arguments of a process, yielding a [`String`] value
550550
/// for each argument.
551551
///
552-
/// This structure is created through the `std::env::args` method.
552+
/// This structure is created through the [`std::env::args`] method.
553+
///
554+
/// [`String`]: ../string/struct.String.html
555+
/// [`std::env::args`]: ./fn.args.html
553556
#[stable(feature = "env", since = "1.0.0")]
554557
pub struct Args { inner: ArgsOs }
555558

556-
/// An iterator over the arguments of a process, yielding an `OsString` value
559+
/// An iterator over the arguments of a process, yielding an [`OsString`] value
557560
/// for each argument.
558561
///
559-
/// This structure is created through the `std::env::args_os` method.
562+
/// This structure is created through the [`std::env::args_os`] method.
563+
///
564+
/// [`OsString`]: ../ffi/struct.OsString.html
565+
/// [`std::env::args_os`]: ./fn.args_os.html
560566
#[stable(feature = "env", since = "1.0.0")]
561567
pub struct ArgsOs { inner: sys::args::Args }
562568

@@ -571,7 +577,7 @@ pub struct ArgsOs { inner: sys::args::Args }
571577
///
572578
/// The returned iterator will panic during iteration if any argument to the
573579
/// process is not valid unicode. If this is not desired,
574-
/// use the `args_os` function instead.
580+
/// use the [`args_os`] function instead.
575581
///
576582
/// # Examples
577583
///
@@ -583,6 +589,8 @@ pub struct ArgsOs { inner: sys::args::Args }
583589
/// println!("{}", argument);
584590
/// }
585591
/// ```
592+
///
593+
/// [`args_os`]: ./fn.args_os.html
586594
#[stable(feature = "env", since = "1.0.0")]
587595
pub fn args() -> Args {
588596
Args { inner: args_os() }

src/libstd/net/addr.rs

+46
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ impl SocketAddr {
194194

195195
impl SocketAddrV4 {
196196
/// Creates a new socket address from the (ip, port) pair.
197+
///
198+
/// # Examples
199+
///
200+
/// ```
201+
/// use std::net::{SocketAddrV4, Ipv4Addr};
202+
///
203+
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
204+
/// ```
197205
#[stable(feature = "rust1", since = "1.0.0")]
198206
pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
199207
SocketAddrV4 {
@@ -207,6 +215,15 @@ impl SocketAddrV4 {
207215
}
208216

209217
/// Returns the IP address associated with this socket address.
218+
///
219+
/// # Examples
220+
///
221+
/// ```
222+
/// use std::net::{SocketAddrV4, Ipv4Addr};
223+
///
224+
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
225+
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
226+
/// ```
210227
#[stable(feature = "rust1", since = "1.0.0")]
211228
pub fn ip(&self) -> &Ipv4Addr {
212229
unsafe {
@@ -215,18 +232,47 @@ impl SocketAddrV4 {
215232
}
216233

217234
/// Change the IP address associated with this socket address.
235+
///
236+
/// # Examples
237+
///
238+
/// ```
239+
/// use std::net::{SocketAddrV4, Ipv4Addr};
240+
///
241+
/// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
242+
/// socket.set_ip(Ipv4Addr::new(192, 168, 0, 1));
243+
/// assert_eq!(socket.ip(), &Ipv4Addr::new(192, 168, 0, 1));
244+
/// ```
218245
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
219246
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
220247
self.inner.sin_addr = *new_ip.as_inner()
221248
}
222249

223250
/// Returns the port number associated with this socket address.
251+
///
252+
/// # Examples
253+
///
254+
/// ```
255+
/// use std::net::{SocketAddrV4, Ipv4Addr};
256+
///
257+
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
258+
/// assert_eq!(socket.port(), 8080);
259+
/// ```
224260
#[stable(feature = "rust1", since = "1.0.0")]
225261
pub fn port(&self) -> u16 {
226262
ntoh(self.inner.sin_port)
227263
}
228264

229265
/// Change the port number associated with this socket address.
266+
///
267+
/// # Examples
268+
///
269+
/// ```
270+
/// use std::net::{SocketAddrV4, Ipv4Addr};
271+
///
272+
/// let mut socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
273+
/// socket.set_port(4242);
274+
/// assert_eq!(socket.port(), 4242);
275+
/// ```
230276
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
231277
pub fn set_port(&mut self, new_port: u16) {
232278
self.inner.sin_port = hton(new_port);

src/test/compile-fail/cast-rfc0401.rs

+5
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,9 @@ fn main()
115115
let _ = cf as *const Bar;
116116
//~^ ERROR casting
117117
//~^^ NOTE vtable kinds
118+
119+
vec![0.0].iter().map(|s| s as f32).collect::<Vec<f32>>();
120+
//~^ ERROR casting `&{float}` as `f32` is invalid
121+
//~| NOTE cannot cast `&{float}` as `f32`
122+
//~| NOTE did you mean `*s`?
118123
}

0 commit comments

Comments
 (0)