Skip to content

Commit 17841cc

Browse files
committed
Auto merge of #50345 - kennytm:rollup, r=kennytm
Rollup of 7 pull requests Successful merges: - #50233 (Make `Vec::new` a `const fn`) - #50312 (Add more links in panic docs) - #50316 (Fix some broken links in docs.) - #50325 (Add a few more tests for proc macro feature gating) - #50327 (Display correct unused field suggestion for nested struct patterns) - #50330 (check that #[used] is used only on statics) - #50344 (Update Cargo to 2018-04-28 122fd5be5201913d42e219e132d6569493583bca) Failed merges:
2 parents 4745092 + 6166f20 commit 17841cc

File tree

18 files changed

+277
-57
lines changed

18 files changed

+277
-57
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#![feature(pointer_methods)]
125125
#![feature(inclusive_range_fields)]
126126
#![cfg_attr(stage0, feature(generic_param_attrs))]
127+
#![feature(rustc_const_unstable)]
127128

128129
#![cfg_attr(not(test), feature(fn_traits, i128))]
129130
#![cfg_attr(test, feature(test))]

src/liballoc/raw_vec.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ pub struct RawVec<T, A: Alloc = Global> {
5656
impl<T, A: Alloc> RawVec<T, A> {
5757
/// Like `new` but parameterized over the choice of allocator for
5858
/// the returned RawVec.
59-
pub fn new_in(a: A) -> Self {
59+
pub const fn new_in(a: A) -> Self {
6060
// !0 is usize::MAX. This branch should be stripped at compile time.
61-
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
61+
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`
62+
//let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
6263

6364
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
6465
RawVec {
6566
ptr: Unique::empty(),
66-
cap,
67+
// FIXME(mark-i-m): use `cap` when ifs are allowed in const
68+
cap: [0, !0][(mem::size_of::<T>() == 0) as usize],
6769
a,
6870
}
6971
}
@@ -120,7 +122,7 @@ impl<T> RawVec<T, Global> {
120122
/// RawVec with capacity 0. If T has 0 size, then it makes a
121123
/// RawVec with capacity `usize::MAX`. Useful for implementing
122124
/// delayed allocation.
123-
pub fn new() -> Self {
125+
pub const fn new() -> Self {
124126
Self::new_in(Global)
125127
}
126128

src/liballoc/vec.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ impl<T> Vec<T> {
322322
/// ```
323323
#[inline]
324324
#[stable(feature = "rust1", since = "1.0.0")]
325-
pub fn new() -> Vec<T> {
325+
#[rustc_const_unstable(feature = "const_vec_new")]
326+
pub const fn new() -> Vec<T> {
326327
Vec {
327328
buf: RawVec::new(),
328329
len: 0,

src/libcore/iter/iterator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,8 @@ pub trait Iterator {
10941094
/// `flatten()` a three-dimensional array the result will be
10951095
/// two-dimensional and not one-dimensional. To get a one-dimensional
10961096
/// structure, you have to `flatten()` again.
1097+
///
1098+
/// [`flat_map()`]: #method.flat_map
10971099
#[inline]
10981100
#[unstable(feature = "iterator_flatten", issue = "48213")]
10991101
fn flatten(self) -> Flatten<Self>

src/libcore/marker.rs

+2
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ unsafe impl<'a, T: ?Sized> Freeze for &'a mut T {}
602602
/// `Pin` pointer.
603603
///
604604
/// This trait is automatically implemented for almost every type.
605+
///
606+
/// [`Pin`]: ../mem/struct.Pin.html
605607
#[unstable(feature = "pin", issue = "49150")]
606608
pub unsafe auto trait Unpin {}
607609

src/libcore/ptr.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2552,10 +2552,9 @@ impl<T: Sized> Unique<T> {
25522552
/// This is useful for initializing types which lazily allocate, like
25532553
/// `Vec::new` does.
25542554
// FIXME: rename to dangling() to match NonNull?
2555-
pub fn empty() -> Self {
2555+
pub const fn empty() -> Self {
25562556
unsafe {
2557-
let ptr = mem::align_of::<T>() as *mut T;
2558-
Unique::new_unchecked(ptr)
2557+
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
25592558
}
25602559
}
25612560
}

src/librustc/hir/check_attr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum Target {
3131
Expression,
3232
Statement,
3333
Closure,
34+
Static,
3435
Other,
3536
}
3637

@@ -43,6 +44,7 @@ impl Target {
4344
hir::ItemEnum(..) => Target::Enum,
4445
hir::ItemConst(..) => Target::Const,
4546
hir::ItemForeignMod(..) => Target::ForeignMod,
47+
hir::ItemStatic(..) => Target::Static,
4648
_ => Target::Other,
4749
}
4850
}
@@ -102,6 +104,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
102104
}
103105

104106
self.check_repr(item, target);
107+
self.check_used(item, target);
105108
}
106109

107110
/// Check if an `#[inline]` is applied to a function or a closure.
@@ -305,6 +308,15 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
305308
}
306309
}
307310
}
311+
312+
fn check_used(&self, item: &hir::Item, target: Target) {
313+
for attr in &item.attrs {
314+
if attr.name().map(|name| name == "used").unwrap_or(false) && target != Target::Static {
315+
self.tcx.sess
316+
.span_err(attr.span, "attribute must be applied to a `static` variable");
317+
}
318+
}
319+
}
308320
}
309321

310322
impl<'a, 'tcx> Visitor<'tcx> for CheckAttrVisitor<'a, 'tcx> {

src/librustc/middle/liveness.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ use ty::{self, TyCtxt};
111111
use lint;
112112
use util::nodemap::{NodeMap, NodeSet};
113113

114+
use std::collections::VecDeque;
114115
use std::{fmt, usize};
115116
use std::io::prelude::*;
116117
use std::io;
@@ -412,18 +413,43 @@ fn visit_local<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, local: &'tcx hir::Local) {
412413
}
413414

414415
fn visit_arm<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, arm: &'tcx hir::Arm) {
415-
for pat in &arm.pats {
416-
// for struct patterns, take note of which fields used shorthand (`x` rather than `x: x`)
416+
for mut pat in &arm.pats {
417+
// For struct patterns, take note of which fields used shorthand
418+
// (`x` rather than `x: x`).
417419
//
418-
// FIXME: according to the rust-lang-nursery/rustc-guide book, `NodeId`s are to be phased
419-
// out in favor of `HirId`s; however, we need to match the signature of `each_binding`,
420-
// which uses `NodeIds`.
420+
// FIXME: according to the rust-lang-nursery/rustc-guide book, `NodeId`s are to be
421+
// phased out in favor of `HirId`s; however, we need to match the signature of
422+
// `each_binding`, which uses `NodeIds`.
421423
let mut shorthand_field_ids = NodeSet();
422-
if let hir::PatKind::Struct(_, ref fields, _) = pat.node {
423-
for field in fields {
424-
if field.node.is_shorthand {
425-
shorthand_field_ids.insert(field.node.pat.id);
424+
let mut pats = VecDeque::new();
425+
pats.push_back(pat);
426+
while let Some(pat) = pats.pop_front() {
427+
use hir::PatKind::*;
428+
match pat.node {
429+
Binding(_, _, _, ref inner_pat) => {
430+
pats.extend(inner_pat.iter());
426431
}
432+
Struct(_, ref fields, _) => {
433+
for field in fields {
434+
if field.node.is_shorthand {
435+
shorthand_field_ids.insert(field.node.pat.id);
436+
}
437+
}
438+
}
439+
Ref(ref inner_pat, _) |
440+
Box(ref inner_pat) => {
441+
pats.push_back(inner_pat);
442+
}
443+
TupleStruct(_, ref inner_pats, _) |
444+
Tuple(ref inner_pats, _) => {
445+
pats.extend(inner_pats.iter());
446+
}
447+
Slice(ref pre_pats, ref inner_pat, ref post_pats) => {
448+
pats.extend(pre_pats.iter());
449+
pats.extend(inner_pat.iter());
450+
pats.extend(post_pats.iter());
451+
}
452+
_ => {}
427453
}
428454
}
429455

src/libstd/collections/hash/table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl TaggedHashUintPtr {
7979
///
8080
/// Essential invariants of this structure:
8181
///
82-
/// - if t.hashes[i] == EMPTY_BUCKET, then `Bucket::at_index(&t, i).raw`
82+
/// - if `t.hashes[i] == EMPTY_BUCKET`, then `Bucket::at_index(&t, i).raw`
8383
/// points to 'undefined' contents. Don't read from it. This invariant is
8484
/// enforced outside this module with the `EmptyBucket`, `FullBucket`,
8585
/// and `SafeHash` types.

src/libstd/ffi/c_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ impl CStr {
11181118
///
11191119
/// [`Cow`]: ../borrow/enum.Cow.html
11201120
/// [`Borrowed`]: ../borrow/enum.Cow.html#variant.Borrowed
1121+
/// [`Owned`]: ../borrow/enum.Cow.html#variant.Owned
11211122
/// [`str`]: ../primitive.str.html
11221123
/// [`String`]: ../string/struct.String.html
11231124
///

src/libstd/panic.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ pub use core::panic::{PanicInfo, Location};
3131
/// A marker trait which represents "panic safe" types in Rust.
3232
///
3333
/// This trait is implemented by default for many types and behaves similarly in
34-
/// terms of inference of implementation to the `Send` and `Sync` traits. The
35-
/// purpose of this trait is to encode what types are safe to cross a `catch_unwind`
34+
/// terms of inference of implementation to the [`Send`] and [`Sync`] traits. The
35+
/// purpose of this trait is to encode what types are safe to cross a [`catch_unwind`]
3636
/// boundary with no fear of unwind safety.
3737
///
38+
/// [`Send`]: ../marker/trait.Send.html
39+
/// [`Sync`]: ../marker/trait.Sync.html
40+
/// [`catch_unwind`]: ./fn.catch_unwind.html
41+
///
3842
/// ## What is unwind safety?
3943
///
4044
/// In Rust a function can "return" early if it either panics or calls a
@@ -95,12 +99,13 @@ pub use core::panic::{PanicInfo, Location};
9599
///
96100
/// ## When should `UnwindSafe` be used?
97101
///
98-
/// Is not intended that most types or functions need to worry about this trait.
99-
/// It is only used as a bound on the `catch_unwind` function and as mentioned above,
100-
/// the lack of `unsafe` means it is mostly an advisory. The `AssertUnwindSafe`
101-
/// wrapper struct in this module can be used to force this trait to be
102-
/// implemented for any closed over variables passed to the `catch_unwind` function
103-
/// (more on this below).
102+
/// It is not intended that most types or functions need to worry about this trait.
103+
/// It is only used as a bound on the `catch_unwind` function and as mentioned
104+
/// above, the lack of `unsafe` means it is mostly an advisory. The
105+
/// [`AssertUnwindSafe`] wrapper struct can be used to force this trait to be
106+
/// implemented for any closed over variables passed to `catch_unwind`.
107+
///
108+
/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
104109
#[stable(feature = "catch_unwind", since = "1.9.0")]
105110
#[rustc_on_unimplemented = "the type {Self} may not be safely transferred \
106111
across an unwind boundary"]
@@ -109,11 +114,14 @@ pub auto trait UnwindSafe {}
109114
/// A marker trait representing types where a shared reference is considered
110115
/// unwind safe.
111116
///
112-
/// This trait is namely not implemented by `UnsafeCell`, the root of all
117+
/// This trait is namely not implemented by [`UnsafeCell`], the root of all
113118
/// interior mutability.
114119
///
115120
/// This is a "helper marker trait" used to provide impl blocks for the
116-
/// `UnwindSafe` trait, for more information see that documentation.
121+
/// [`UnwindSafe`] trait, for more information see that documentation.
122+
///
123+
/// [`UnsafeCell`]: ../cell/struct.UnsafeCell.html
124+
/// [`UnwindSafe`]: ./trait.UnwindSafe.html
117125
#[stable(feature = "catch_unwind", since = "1.9.0")]
118126
#[rustc_on_unimplemented = "the type {Self} may contain interior mutability \
119127
and a reference may not be safely transferrable \
@@ -122,14 +130,15 @@ pub auto trait RefUnwindSafe {}
122130

123131
/// A simple wrapper around a type to assert that it is unwind safe.
124132
///
125-
/// When using `catch_unwind` it may be the case that some of the closed over
133+
/// When using [`catch_unwind`] it may be the case that some of the closed over
126134
/// variables are not unwind safe. For example if `&mut T` is captured the
127135
/// compiler will generate a warning indicating that it is not unwind safe. It
128136
/// may not be the case, however, that this is actually a problem due to the
129-
/// specific usage of `catch_unwind` if unwind safety is specifically taken into
137+
/// specific usage of [`catch_unwind`] if unwind safety is specifically taken into
130138
/// account. This wrapper struct is useful for a quick and lightweight
131139
/// annotation that a variable is indeed unwind safe.
132140
///
141+
/// [`catch_unwind`]: ./fn.catch_unwind.html
133142
/// # Examples
134143
///
135144
/// One way to use `AssertUnwindSafe` is to assert that the entire closure
@@ -318,18 +327,22 @@ impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
318327
/// panic and allowing a graceful handling of the error.
319328
///
320329
/// It is **not** recommended to use this function for a general try/catch
321-
/// mechanism. The `Result` type is more appropriate to use for functions that
330+
/// mechanism. The [`Result`] type is more appropriate to use for functions that
322331
/// can fail on a regular basis. Additionally, this function is not guaranteed
323332
/// to catch all panics, see the "Notes" section below.
324333
///
325-
/// The closure provided is required to adhere to the `UnwindSafe` trait to ensure
334+
/// [`Result`]: ../result/enum.Result.html
335+
///
336+
/// The closure provided is required to adhere to the [`UnwindSafe`] trait to ensure
326337
/// that all captured variables are safe to cross this boundary. The purpose of
327338
/// this bound is to encode the concept of [exception safety][rfc] in the type
328339
/// system. Most usage of this function should not need to worry about this
329340
/// bound as programs are naturally unwind safe without `unsafe` code. If it
330-
/// becomes a problem the associated `AssertUnwindSafe` wrapper type in this
331-
/// module can be used to quickly assert that the usage here is indeed unwind
332-
/// safe.
341+
/// becomes a problem the [`AssertUnwindSafe`] wrapper struct can be used to quickly
342+
/// assert that the usage here is indeed unwind safe.
343+
///
344+
/// [`AssertUnwindSafe`]: ./struct.AssertUnwindSafe.html
345+
/// [`UnwindSafe`]: ./trait.UnwindSafe.html
333346
///
334347
/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
335348
///
@@ -364,9 +377,11 @@ pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
364377

365378
/// Triggers a panic without invoking the panic hook.
366379
///
367-
/// This is designed to be used in conjunction with `catch_unwind` to, for
380+
/// This is designed to be used in conjunction with [`catch_unwind`] to, for
368381
/// example, carry a panic across a layer of C code.
369382
///
383+
/// [`catch_unwind`]: ./fn.catch_unwind.html
384+
///
370385
/// # Notes
371386
///
372387
/// Note that panics in Rust are not always implemented via unwinding, but they

src/libstd/panicking.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ static mut HOOK: Hook = Hook::Default;
7676
/// is invoked. As such, the hook will run with both the aborting and unwinding
7777
/// runtimes. The default hook prints a message to standard error and generates
7878
/// a backtrace if requested, but this behavior can be customized with the
79-
/// `set_hook` and `take_hook` functions.
79+
/// `set_hook` and [`take_hook`] functions.
80+
///
81+
/// [`take_hook`]: ./fn.take_hook.html
8082
///
8183
/// The hook is provided with a `PanicInfo` struct which contains information
8284
/// about the origin of the panic, including the payload passed to `panic!` and
@@ -121,6 +123,10 @@ pub fn set_hook(hook: Box<Fn(&PanicInfo) + 'static + Sync + Send>) {
121123

122124
/// Unregisters the current panic hook, returning it.
123125
///
126+
/// *See also the function [`set_hook`].*
127+
///
128+
/// [`set_hook`]: ./fn.set_hook.html
129+
///
124130
/// If no custom hook is registered, the default hook will be returned.
125131
///
126132
/// # Panics

0 commit comments

Comments
 (0)