Skip to content

Commit 535b6d3

Browse files
committed
Auto merge of #38214 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 9 pull requests - Successful merges: #38085, #38123, #38151, #38153, #38158, #38163, #38186, #38189, #38208 - Failed merges:
2 parents 2093084 + ef45ec0 commit 535b6d3

File tree

12 files changed

+193
-59
lines changed

12 files changed

+193
-59
lines changed

src/doc/book/ffi.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -662,26 +662,31 @@ attribute turns off Rust's name mangling, so that it is easier to link to.
662662

663663
It’s important to be mindful of `panic!`s when working with FFI. A `panic!`
664664
across an FFI boundary is undefined behavior. If you’re writing code that may
665-
panic, you should run it in another thread, so that the panic doesn’t bubble up
666-
to C:
665+
panic, you should run it in a closure with [`catch_unwind()`]:
667666

668667
```rust
669-
use std::thread;
668+
use std::panic::catch_unwind;
670669

671670
#[no_mangle]
672671
pub extern fn oh_no() -> i32 {
673-
let h = thread::spawn(|| {
672+
let result = catch_unwind(|| {
674673
panic!("Oops!");
675674
});
676-
677-
match h.join() {
678-
Ok(_) => 1,
679-
Err(_) => 0,
675+
match result {
676+
Ok(_) => 0,
677+
Err(_) => 1,
680678
}
681679
}
682-
# fn main() {}
680+
681+
fn main() {}
683682
```
684683

684+
Please note that [`catch_unwind()`] will only catch unwinding panics, not
685+
those who abort the process. See the documentation of [`catch_unwind()`]
686+
for more information.
687+
688+
[`catch_unwind()`]: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
689+
685690
# Representing opaque structs
686691

687692
Sometimes, a C library wants to provide a pointer to something, but not let you

src/doc/reference.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ syntax named by _designator_. Valid designators are:
603603
* `ty`: a [type](#types)
604604
* `ident`: an [identifier](#identifiers)
605605
* `path`: a [path](#paths)
606-
* `tt`: either side of the `=>` in macro rules
606+
* `tt`: a token tree (a single [token](#tokens) or a sequence of token trees surrounded
607+
by matching `()`, `[]`, or `{}`)
607608
* `meta`: the contents of an [attribute](#attributes)
608609

609610
In the transcriber, the

src/liballoc/rc.rs

+45-43
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@
1212

1313
//! Single-threaded reference-counting pointers.
1414
//!
15-
//! The type [`Rc<T>`][rc] provides shared ownership of a value of type `T`,
16-
//! allocated in the heap. Invoking [`clone`][clone] on `Rc` produces a new
17-
//! pointer to the same value in the heap. When the last `Rc` pointer to a
15+
//! The type [`Rc<T>`][`Rc`] provides shared ownership of a value of type `T`,
16+
//! allocated in the heap. Invoking [`clone()`][clone] on [`Rc`] produces a new
17+
//! pointer to the same value in the heap. When the last [`Rc`] pointer to a
1818
//! given value is destroyed, the pointed-to value is also destroyed.
1919
//!
2020
//! Shared references in Rust disallow mutation by default, and `Rc` is no
21-
//! exception. If you need to mutate through an `Rc`, use [`Cell`][cell] or
22-
//! [`RefCell`][refcell].
21+
//! exception. If you need to mutate through an [`Rc`], use [`Cell`] or
22+
//! [`RefCell`].
2323
//!
24-
//! `Rc` uses non-atomic reference counting. This means that overhead is very
25-
//! low, but an `Rc` cannot be sent between threads, and consequently `Rc`
24+
//! [`Rc`] uses non-atomic reference counting. This means that overhead is very
25+
//! low, but an [`Rc`] cannot be sent between threads, and consequently [`Rc`]
2626
//! does not implement [`Send`][send]. As a result, the Rust compiler
27-
//! will check *at compile time* that you are not sending `Rc`s between
27+
//! will check *at compile time* that you are not sending [`Rc`]s between
2828
//! threads. If you need multi-threaded, atomic reference counting, use
2929
//! [`sync::Arc`][arc].
3030
//!
31-
//! The [`downgrade`][downgrade] method can be used to create a non-owning
32-
//! [`Weak`][weak] pointer. A `Weak` pointer can be [`upgrade`][upgrade]d
33-
//! to an `Rc`, but this will return [`None`][option] if the value has
31+
//! The [`downgrade()`][downgrade] method can be used to create a non-owning
32+
//! [`Weak`] pointer. A [`Weak`] pointer can be [`upgrade`][upgrade]d
33+
//! to an [`Rc`], but this will return [`None`] if the value has
3434
//! already been dropped.
3535
//!
36-
//! A cycle between `Rc` pointers will never be deallocated. For this reason,
37-
//! `Weak` is used to break cycles. For example, a tree could have strong
38-
//! `Rc` pointers from parent nodes to children, and `Weak` pointers from
36+
//! A cycle between [`Rc`] pointers will never be deallocated. For this reason,
37+
//! [`Weak`] is used to break cycles. For example, a tree could have strong
38+
//! [`Rc`] pointers from parent nodes to children, and [`Weak`] pointers from
3939
//! children back to their parents.
4040
//!
41-
//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`][deref] trait),
42-
//! so you can call `T`'s methods on a value of type `Rc<T>`. To avoid name
43-
//! clashes with `T`'s methods, the methods of `Rc<T>` itself are [associated
41+
//! `Rc<T>` automatically dereferences to `T` (via the [`Deref`] trait),
42+
//! so you can call `T`'s methods on a value of type [`Rc<T>`][`Rc`]. To avoid name
43+
//! clashes with `T`'s methods, the methods of [`Rc<T>`][`Rc`] itself are [associated
4444
//! functions][assoc], called using function-like syntax:
4545
//!
4646
//! ```
@@ -50,28 +50,15 @@
5050
//! Rc::downgrade(&my_rc);
5151
//! ```
5252
//!
53-
//! `Weak<T>` does not auto-dereference to `T`, because the value may have
53+
//! [`Weak<T>`][`Weak`] does not auto-dereference to `T`, because the value may have
5454
//! already been destroyed.
5555
//!
56-
//! [rc]: struct.Rc.html
57-
//! [weak]: struct.Weak.html
58-
//! [clone]: ../../std/clone/trait.Clone.html#tymethod.clone
59-
//! [cell]: ../../std/cell/struct.Cell.html
60-
//! [refcell]: ../../std/cell/struct.RefCell.html
61-
//! [send]: ../../std/marker/trait.Send.html
62-
//! [arc]: ../../std/sync/struct.Arc.html
63-
//! [deref]: ../../std/ops/trait.Deref.html
64-
//! [downgrade]: struct.Rc.html#method.downgrade
65-
//! [upgrade]: struct.Weak.html#method.upgrade
66-
//! [option]: ../../std/option/enum.Option.html
67-
//! [assoc]: ../../book/method-syntax.html#associated-functions
68-
//!
6956
//! # Examples
7057
//!
7158
//! Consider a scenario where a set of `Gadget`s are owned by a given `Owner`.
7259
//! We want to have our `Gadget`s point to their `Owner`. We can't do this with
7360
//! unique ownership, because more than one gadget may belong to the same
74-
//! `Owner`. `Rc` allows us to share an `Owner` between multiple `Gadget`s,
61+
//! `Owner`. [`Rc`] allows us to share an `Owner` between multiple `Gadget`s,
7562
//! and have the `Owner` remain allocated as long as any `Gadget` points at it.
7663
//!
7764
//! ```
@@ -127,20 +114,20 @@
127114
//! ```
128115
//!
129116
//! If our requirements change, and we also need to be able to traverse from
130-
//! `Owner` to `Gadget`, we will run into problems. An `Rc` pointer from `Owner`
117+
//! `Owner` to `Gadget`, we will run into problems. An [`Rc`] pointer from `Owner`
131118
//! to `Gadget` introduces a cycle between the values. This means that their
132119
//! reference counts can never reach 0, and the values will remain allocated
133-
//! forever: a memory leak. In order to get around this, we can use `Weak`
120+
//! forever: a memory leak. In order to get around this, we can use [`Weak`]
134121
//! pointers.
135122
//!
136123
//! Rust actually makes it somewhat difficult to produce this loop in the first
137124
//! place. In order to end up with two values that point at each other, one of
138-
//! them needs to be mutable. This is difficult because `Rc` enforces
125+
//! them needs to be mutable. This is difficult because [`Rc`] enforces
139126
//! memory safety by only giving out shared references to the value it wraps,
140127
//! and these don't allow direct mutation. We need to wrap the part of the
141-
//! value we wish to mutate in a [`RefCell`][refcell], which provides *interior
128+
//! value we wish to mutate in a [`RefCell`], which provides *interior
142129
//! mutability*: a method to achieve mutability through a shared reference.
143-
//! `RefCell` enforces Rust's borrowing rules at runtime.
130+
//! [`RefCell`] enforces Rust's borrowing rules at runtime.
144131
//!
145132
//! ```
146133
//! use std::rc::Rc;
@@ -214,6 +201,19 @@
214201
//! // Gadget Man, so he gets destroyed as well.
215202
//! }
216203
//! ```
204+
//!
205+
//! [`Rc`]: struct.Rc.html
206+
//! [`Weak`]: struct.Weak.html
207+
//! [clone]: ../../std/clone/trait.Clone.html#tymethod.clone
208+
//! [`Cell`]: ../../std/cell/struct.Cell.html
209+
//! [`RefCell`]: ../../std/cell/struct.RefCell.html
210+
//! [send]: ../../std/marker/trait.Send.html
211+
//! [arc]: ../../std/sync/struct.Arc.html
212+
//! [`Deref`]: ../../std/ops/trait.Deref.html
213+
//! [downgrade]: struct.Rc.html#method.downgrade
214+
//! [upgrade]: struct.Weak.html#method.upgrade
215+
//! [`None`]: ../../std/option/enum.Option.html#variant.None
216+
//! [assoc]: ../../book/method-syntax.html#associated-functions
217217
218218
#![stable(feature = "rust1", since = "1.0.0")]
219219

@@ -251,9 +251,11 @@ struct RcBox<T: ?Sized> {
251251
/// See the [module-level documentation](./index.html) for more details.
252252
///
253253
/// The inherent methods of `Rc` are all associated functions, which means
254-
/// that you have to call them as e.g. `Rc::get_mut(&value)` instead of
255-
/// `value.get_mut()`. This avoids conflicts with methods of the inner
254+
/// that you have to call them as e.g. [`Rc::get_mut(&value)`][get_mut] instead of
255+
/// `value.get_mut()`. This avoids conflicts with methods of the inner
256256
/// type `T`.
257+
///
258+
/// [get_mut]: #method.get_mut
257259
#[stable(feature = "rust1", since = "1.0.0")]
258260
pub struct Rc<T: ?Sized> {
259261
ptr: Shared<RcBox<T>>,
@@ -337,10 +339,10 @@ impl<T> Rc<T> {
337339
}
338340

339341
/// Checks whether [`Rc::try_unwrap`][try_unwrap] would return
340-
/// [`Ok`][result].
342+
/// [`Ok`].
341343
///
342344
/// [try_unwrap]: struct.Rc.html#method.try_unwrap
343-
/// [result]: ../../std/result/enum.Result.html
345+
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
344346
///
345347
/// # Examples
346348
///
@@ -543,14 +545,14 @@ impl<T: ?Sized> Rc<T> {
543545
/// Returns a mutable reference to the inner value, if there are
544546
/// no other `Rc` or [`Weak`][weak] pointers to the same value.
545547
///
546-
/// Returns [`None`][option] otherwise, because it is not safe to
548+
/// Returns [`None`] otherwise, because it is not safe to
547549
/// mutate a shared value.
548550
///
549551
/// See also [`make_mut`][make_mut], which will [`clone`][clone]
550552
/// the inner value when it's shared.
551553
///
552554
/// [weak]: struct.Weak.html
553-
/// [option]: ../../std/option/enum.Option.html
555+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
554556
/// [make_mut]: struct.Rc.html#method.make_mut
555557
/// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone
556558
///

src/librustc_resolve/check_unused.rs

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ impl<'a, 'b> Visitor for UnusedImportCheckVisitor<'a, 'b> {
103103
}
104104

105105
ViewPathList(_, ref list) => {
106+
if list.len() == 0 {
107+
self.unused_imports
108+
.entry(item.id)
109+
.or_insert_with(NodeMap)
110+
.insert(item.id, item.span);
111+
}
106112
for i in list {
107113
self.check_import(item.id, i.node.id, i.span);
108114
}

src/libstd/collections/hash/map.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,10 @@ impl DefaultHasher {
21172117

21182118
#[stable(feature = "hashmap_default_hasher", since = "1.13.0")]
21192119
impl Default for DefaultHasher {
2120+
/// Creates a new `DefaultHasher` using [`DefaultHasher::new`]. See
2121+
/// [`DefaultHasher::new`] documentation for more information.
2122+
///
2123+
/// [`DefaultHasher::new`]: #method.new
21202124
fn default() -> DefaultHasher {
21212125
DefaultHasher::new()
21222126
}

src/libstd/panicking.rs

+104-2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ pub fn take_hook() -> Box<Fn(&PanicInfo) + 'static + Sync + Send> {
159159
}
160160

161161
/// A struct providing information about a panic.
162+
///
163+
/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook()`]
164+
/// function.
165+
///
166+
/// [`set_hook()`]: ../../std/panic/fn.set_hook.html
167+
///
168+
/// # Examples
169+
///
170+
/// ```should_panic
171+
/// use std::panic;
172+
///
173+
/// panic::set_hook(Box::new(|panic_info| {
174+
/// println!("panic occured: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
175+
/// }));
176+
///
177+
/// panic!("Normal panic");
178+
/// ```
162179
#[stable(feature = "panic_hooks", since = "1.10.0")]
163180
pub struct PanicInfo<'a> {
164181
payload: &'a (Any + Send),
@@ -168,7 +185,21 @@ pub struct PanicInfo<'a> {
168185
impl<'a> PanicInfo<'a> {
169186
/// Returns the payload associated with the panic.
170187
///
171-
/// This will commonly, but not always, be a `&'static str` or `String`.
188+
/// This will commonly, but not always, be a `&'static str` or [`String`].
189+
///
190+
/// [`String`]: ../../std/string/struct.String.html
191+
///
192+
/// # Examples
193+
///
194+
/// ```should_panic
195+
/// use std::panic;
196+
///
197+
/// panic::set_hook(Box::new(|panic_info| {
198+
/// println!("panic occured: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
199+
/// }));
200+
///
201+
/// panic!("Normal panic");
202+
/// ```
172203
#[stable(feature = "panic_hooks", since = "1.10.0")]
173204
pub fn payload(&self) -> &(Any + Send) {
174205
self.payload
@@ -177,15 +208,54 @@ impl<'a> PanicInfo<'a> {
177208
/// Returns information about the location from which the panic originated,
178209
/// if available.
179210
///
180-
/// This method will currently always return `Some`, but this may change
211+
/// This method will currently always return [`Some`], but this may change
181212
/// in future versions.
213+
///
214+
/// [`Some`]: ../../std/option/enum.Option.html#variant.Some
215+
///
216+
/// # Examples
217+
///
218+
/// ```should_panic
219+
/// use std::panic;
220+
///
221+
/// panic::set_hook(Box::new(|panic_info| {
222+
/// if let Some(location) = panic_info.location() {
223+
/// println!("panic occured in file '{}' at line {}", location.file(), location.line());
224+
/// } else {
225+
/// println!("panic occured but can't get location information...");
226+
/// }
227+
/// }));
228+
///
229+
/// panic!("Normal panic");
230+
/// ```
182231
#[stable(feature = "panic_hooks", since = "1.10.0")]
183232
pub fn location(&self) -> Option<&Location> {
184233
Some(&self.location)
185234
}
186235
}
187236

188237
/// A struct containing information about the location of a panic.
238+
///
239+
/// This structure is created by the [`location()`] method of [`PanicInfo`].
240+
///
241+
/// [`location()`]: ../../std/panic/struct.PanicInfo.html#method.location
242+
/// [`PanicInfo`]: ../../std/panic/struct.PanicInfo.html
243+
///
244+
/// # Examples
245+
///
246+
/// ```should_panic
247+
/// use std::panic;
248+
///
249+
/// panic::set_hook(Box::new(|panic_info| {
250+
/// if let Some(location) = panic_info.location() {
251+
/// println!("panic occured in file '{}' at line {}", location.file(), location.line());
252+
/// } else {
253+
/// println!("panic occured but can't get location information...");
254+
/// }
255+
/// }));
256+
///
257+
/// panic!("Normal panic");
258+
/// ```
189259
#[stable(feature = "panic_hooks", since = "1.10.0")]
190260
pub struct Location<'a> {
191261
file: &'a str,
@@ -194,12 +264,44 @@ pub struct Location<'a> {
194264

195265
impl<'a> Location<'a> {
196266
/// Returns the name of the source file from which the panic originated.
267+
///
268+
/// # Examples
269+
///
270+
/// ```should_panic
271+
/// use std::panic;
272+
///
273+
/// panic::set_hook(Box::new(|panic_info| {
274+
/// if let Some(location) = panic_info.location() {
275+
/// println!("panic occured in file '{}'", location.file());
276+
/// } else {
277+
/// println!("panic occured but can't get location information...");
278+
/// }
279+
/// }));
280+
///
281+
/// panic!("Normal panic");
282+
/// ```
197283
#[stable(feature = "panic_hooks", since = "1.10.0")]
198284
pub fn file(&self) -> &str {
199285
self.file
200286
}
201287

202288
/// Returns the line number from which the panic originated.
289+
///
290+
/// # Examples
291+
///
292+
/// ```should_panic
293+
/// use std::panic;
294+
///
295+
/// panic::set_hook(Box::new(|panic_info| {
296+
/// if let Some(location) = panic_info.location() {
297+
/// println!("panic occured at line {}", location.line());
298+
/// } else {
299+
/// println!("panic occured but can't get location information...");
300+
/// }
301+
/// }));
302+
///
303+
/// panic!("Normal panic");
304+
/// ```
203305
#[stable(feature = "panic_hooks", since = "1.10.0")]
204306
pub fn line(&self) -> u32 {
205307
self.line

0 commit comments

Comments
 (0)