Skip to content

Commit 7ac844f

Browse files
committed
Auto merge of #42130 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 6 pull requests - Successful merges: #41892, #42062, #42091, #42093, #42098, #42127 - Failed merges:
2 parents 92ee08a + 382c0eb commit 7ac844f

22 files changed

+129
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `needs_drop`
2+
3+
The tracking issue for this feature is: [#41890]
4+
5+
[#41890]: https://github.com/rust-lang/rust/issues/41890
6+
7+
------------------------

src/libarena/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(core_intrinsics)]
3333
#![feature(dropck_eyepatch)]
3434
#![feature(generic_param_attrs)]
35+
#![feature(needs_drop)]
3536
#![cfg_attr(stage0, feature(staged_api))]
3637
#![cfg_attr(test, feature(test))]
3738

@@ -82,7 +83,7 @@ impl<T> TypedArenaChunk<T> {
8283
unsafe fn destroy(&mut self, len: usize) {
8384
// The branch on needs_drop() is an -O1 performance optimization.
8485
// Without the branch, dropping TypedArena<u8> takes linear time.
85-
if intrinsics::needs_drop::<T>() {
86+
if mem::needs_drop::<T>() {
8687
let mut start = self.start();
8788
// Destroy all allocated objects.
8889
for _ in 0..len {
@@ -350,7 +351,7 @@ impl DroplessArena {
350351
#[inline]
351352
pub fn alloc<T>(&self, object: T) -> &mut T {
352353
unsafe {
353-
assert!(!intrinsics::needs_drop::<T>());
354+
assert!(!mem::needs_drop::<T>());
354355
assert!(mem::size_of::<T>() != 0);
355356

356357
self.align_for::<T>();
@@ -379,9 +380,7 @@ impl DroplessArena {
379380
#[inline]
380381
pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
381382
where T: Copy {
382-
unsafe {
383-
assert!(!intrinsics::needs_drop::<T>());
384-
}
383+
assert!(!mem::needs_drop::<T>());
385384
assert!(mem::size_of::<T>() != 0);
386385
assert!(slice.len() != 0);
387386
self.align_for::<T>();

src/libcore/mem.rs

+52
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,58 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
302302
unsafe { intrinsics::min_align_of_val(val) }
303303
}
304304

305+
/// Returns whether dropping values of type `T` matters.
306+
///
307+
/// This is purely an optimization hint, and may be implemented conservatively.
308+
/// For instance, always returning `true` would be a valid implementation of
309+
/// this function.
310+
///
311+
/// Low level implementations of things like collections, which need to manually
312+
/// drop their data, should use this function to avoid unnecessarily
313+
/// trying to drop all their contents when they are destroyed. This might not
314+
/// make a difference in release builds (where a loop that has no side-effects
315+
/// is easily detected and eliminated), but is often a big win for debug builds.
316+
///
317+
/// Note that `ptr::drop_in_place` already performs this check, so if your workload
318+
/// can be reduced to some small number of drop_in_place calls, using this is
319+
/// unnecessary. In particular note that you can drop_in_place a slice, and that
320+
/// will do a single needs_drop check for all the values.
321+
///
322+
/// Types like Vec therefore just `drop_in_place(&mut self[..])` without using
323+
/// needs_drop explicitly. Types like HashMap, on the other hand, have to drop
324+
/// values one at a time and should use this API.
325+
///
326+
///
327+
/// # Examples
328+
///
329+
/// Here's an example of how a collection might make use of needs_drop:
330+
///
331+
/// ```ignore
332+
/// #![feature(needs_drop)]
333+
/// use std::{mem, ptr};
334+
///
335+
/// pub struct MyCollection<T> { /* ... */ }
336+
///
337+
/// impl<T> Drop for MyCollection<T> {
338+
/// fn drop(&mut self) {
339+
/// unsafe {
340+
/// // drop the data
341+
/// if mem::needs_drop::<T>() {
342+
/// for x in self.iter_mut() {
343+
/// ptr::drop_in_place(x);
344+
/// }
345+
/// }
346+
/// self.free_buffer();
347+
/// }
348+
/// }
349+
/// }
350+
/// ```
351+
#[inline]
352+
#[unstable(feature = "needs_drop", issue = "41890")]
353+
pub fn needs_drop<T>() -> bool {
354+
unsafe { intrinsics::needs_drop::<T>() }
355+
}
356+
305357
/// Creates a value whose bytes are all zero.
306358
///
307359
/// This has the same effect as allocating space with

src/libcore/sync/atomic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ impl<T> AtomicPtr<T> {
918918
}
919919
}
920920

921+
#[cfg(target_has_atomic = "ptr")]
921922
macro_rules! atomic_int {
922923
($stable:meta,
923924
$stable_cxchg:meta,

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
113113
heading: &str, span: Span)
114114
-> (String, Option<Span>) {
115115
let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo);
116-
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize()),
116+
(format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
117117
Some(span))
118118
}
119119

src/librustc_errors/diagnostic_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ impl<'a> Debug for DiagnosticBuilder<'a> {
192192
}
193193
}
194194

195-
/// Destructor bomb - a DiagnosticBuilder must be either emitted or cancelled or
196-
/// we emit a bug.
195+
/// Destructor bomb - a `DiagnosticBuilder` must be either emitted or cancelled
196+
/// or we emit a bug.
197197
impl<'a> Drop for DiagnosticBuilder<'a> {
198198
fn drop(&mut self) {
199199
if !panicking() && !self.cancelled() {

src/librustc_errors/emitter.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,8 @@ impl Write for BufferedWriter {
12961296
}
12971297
fn flush(&mut self) -> io::Result<()> {
12981298
let mut stderr = io::stderr();
1299-
let result = (|| {
1300-
stderr.write_all(&self.buffer)?;
1301-
stderr.flush()
1302-
})();
1299+
let result = stderr.write_all(&self.buffer)
1300+
.and_then(|_| stderr.flush());
13031301
self.buffer.clear();
13041302
result
13051303
}

src/librustc_errors/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl Handler {
383383
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> FatalError {
384384
self.emit(&sp.into(), msg, Fatal);
385385
self.panic_if_treat_err_as_bug();
386-
return FatalError;
386+
FatalError
387387
}
388388
pub fn span_fatal_with_code<S: Into<MultiSpan>>(&self,
389389
sp: S,
@@ -392,7 +392,7 @@ impl Handler {
392392
-> FatalError {
393393
self.emit_with_code(&sp.into(), msg, code, Fatal);
394394
self.panic_if_treat_err_as_bug();
395-
return FatalError;
395+
FatalError
396396
}
397397
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
398398
self.emit(&sp.into(), msg, Error);

src/libstd/collections/hash/table.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use alloc::heap::{allocate, deallocate};
1212

1313
use cmp;
1414
use hash::{BuildHasher, Hash, Hasher};
15-
use intrinsics::needs_drop;
1615
use marker;
17-
use mem::{align_of, size_of};
16+
use mem::{align_of, size_of, needs_drop};
1817
use mem;
1918
use ops::{Deref, DerefMut};
2019
use ptr::{self, Unique, Shared};

src/libstd/env.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
//! This module contains functions to inspect various aspects such as
1414
//! environment variables, process arguments, the current directory, and various
1515
//! other important directories.
16+
//!
17+
//! There are several functions and structs in this module that have a
18+
//! counterpart ending in `os`. Those ending in `os` will return an [`OsString`]
19+
//! and those without will be returning a [`String`].
20+
//!
21+
//! [`OsString`]: ../../std/ffi/struct.OsString.html
22+
//! [`String`]: ../string/struct.String.html
1623
1724
#![stable(feature = "env", since = "1.0.0")]
1825

@@ -74,15 +81,17 @@ pub fn set_current_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
7481

7582
/// An iterator over a snapshot of the environment variables of this process.
7683
///
77-
/// This structure is created through the [`std::env::vars`] function.
84+
/// This structure is created by the [`std::env::vars`] function. See its
85+
/// documentation for more.
7886
///
7987
/// [`std::env::vars`]: fn.vars.html
8088
#[stable(feature = "env", since = "1.0.0")]
8189
pub struct Vars { inner: VarsOs }
8290

8391
/// An iterator over a snapshot of the environment variables of this process.
8492
///
85-
/// This structure is created through the [`std::env::vars_os`] function.
93+
/// This structure is created by the [`std::env::vars_os`] function. See
94+
/// its documentation for more.
8695
///
8796
/// [`std::env::vars_os`]: fn.vars_os.html
8897
#[stable(feature = "env", since = "1.0.0")]
@@ -176,12 +185,10 @@ impl fmt::Debug for VarsOs {
176185

177186
/// Fetches the environment variable `key` from the current process.
178187
///
179-
/// The returned result is [`Ok(s)`] if the environment variable is present and is
180-
/// valid unicode. If the environment variable is not present, or it is not
181-
/// valid unicode, then [`VarError`] will be returned.
188+
/// # Errors
182189
///
183-
/// [`Ok(s)`]: ../result/enum.Result.html#variant.Ok
184-
/// [`VarError`]: enum.VarError.html
190+
/// * Environment variable is not present
191+
/// * Environment variable is not valid unicode
185192
///
186193
/// # Examples
187194
///
@@ -233,7 +240,8 @@ fn _var_os(key: &OsStr) -> Option<OsString> {
233240
})
234241
}
235242

236-
/// Possible errors from the [`env::var`] function.
243+
/// The error type for operations interacting with environment variables.
244+
/// Possibly returned from the [`env::var`] function.
237245
///
238246
/// [`env::var`]: fn.var.html
239247
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -356,10 +364,13 @@ fn _remove_var(k: &OsStr) {
356364
})
357365
}
358366

359-
/// An iterator over `PathBuf` instances for parsing an environment variable
360-
/// according to platform-specific conventions.
367+
/// An iterator that splits an environment variable into paths according to
368+
/// platform-specific conventions.
361369
///
362-
/// This structure is returned from `std::env::split_paths`.
370+
/// This structure is created by the [`std::env::split_paths`] function See its
371+
/// documentation for more.
372+
///
373+
/// [`std::env::split_paths`]: fn.split_paths.html
363374
#[stable(feature = "env", since = "1.0.0")]
364375
pub struct SplitPaths<'a> { inner: os_imp::SplitPaths<'a> }
365376

@@ -402,8 +413,10 @@ impl<'a> fmt::Debug for SplitPaths<'a> {
402413
}
403414
}
404415

405-
/// Error type returned from `std::env::join_paths` when paths fail to be
406-
/// joined.
416+
/// The error type for operations on the `PATH` variable. Possibly returned from
417+
/// the [`env::join_paths`] function.
418+
///
419+
/// [`env::join_paths`]: fn.join_paths.html
407420
#[derive(Debug)]
408421
#[stable(feature = "env", since = "1.0.0")]
409422
pub struct JoinPathsError {
@@ -413,7 +426,7 @@ pub struct JoinPathsError {
413426
/// Joins a collection of [`Path`]s appropriately for the `PATH`
414427
/// environment variable.
415428
///
416-
/// Returns an [`OsString`] on success.
429+
/// # Errors
417430
///
418431
/// Returns an [`Err`][err] (containing an error message) if one of the input
419432
/// [`Path`]s contains an invalid character for constructing the `PATH`
@@ -493,12 +506,16 @@ pub fn home_dir() -> Option<PathBuf> {
493506

494507
/// Returns the path of a temporary directory.
495508
///
496-
/// On Unix, returns the value of the `TMPDIR` environment variable if it is
509+
/// # Unix
510+
///
511+
/// Returns the value of the `TMPDIR` environment variable if it is
497512
/// set, otherwise for non-Android it returns `/tmp`. If Android, since there
498513
/// is no global temporary folder (it is usually allocated per-app), it returns
499514
/// `/data/local/tmp`.
500515
///
501-
/// On Windows, returns the value of, in order, the `TMP`, `TEMP`,
516+
/// # Windows
517+
///
518+
/// Returns the value of, in order, the `TMP`, `TEMP`,
502519
/// `USERPROFILE` environment variable if any are set and not the empty
503520
/// string. Otherwise, `temp_dir` returns the path of the Windows directory.
504521
/// This behavior is identical to that of [`GetTempPath`][msdn], which this

src/libstd/ffi/os_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'a> From<&'a OsStr> for Box<OsStr> {
530530
}
531531

532532
#[stable(feature = "os_string_from_box", since = "1.18.0")]
533-
impl<'a> From<Box<OsStr>> for OsString {
533+
impl From<Box<OsStr>> for OsString {
534534
fn from(boxed: Box<OsStr>) -> OsString {
535535
boxed.into_os_string()
536536
}

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
#![feature(linkage)]
282282
#![feature(macro_reexport)]
283283
#![feature(needs_panic_runtime)]
284+
#![feature(needs_drop)]
284285
#![feature(never_type)]
285286
#![feature(num_bits_bytes)]
286287
#![feature(old_wrapping)]

src/libstd/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ impl<'a> From<&'a Path> for Box<Path> {
13421342
}
13431343

13441344
#[stable(feature = "path_buf_from_box", since = "1.18.0")]
1345-
impl<'a> From<Box<Path>> for PathBuf {
1345+
impl From<Box<Path>> for PathBuf {
13461346
fn from(boxed: Box<Path>) -> PathBuf {
13471347
boxed.into_path_buf()
13481348
}

src/libstd/sys/redox/fast_thread_local.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
#![unstable(feature = "thread_local_internals", issue = "0")]
1313

1414
use cell::{Cell, UnsafeCell};
15-
use intrinsics;
15+
use mem;
1616
use ptr;
1717

18+
1819
pub struct Key<T> {
1920
inner: UnsafeCell<Option<T>>,
2021

@@ -37,7 +38,7 @@ impl<T> Key<T> {
3738

3839
pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
3940
unsafe {
40-
if intrinsics::needs_drop::<T>() && self.dtor_running.get() {
41+
if mem::needs_drop::<T>() && self.dtor_running.get() {
4142
return None
4243
}
4344
self.register_dtor();
@@ -46,7 +47,7 @@ impl<T> Key<T> {
4647
}
4748

4849
unsafe fn register_dtor(&self) {
49-
if !intrinsics::needs_drop::<T>() || self.dtor_registered.get() {
50+
if !mem::needs_drop::<T>() || self.dtor_registered.get() {
5051
return
5152
}
5253

src/libstd/sys/unix/fast_thread_local.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use cell::{Cell, UnsafeCell};
1515
use fmt;
16-
use intrinsics;
16+
use mem;
1717
use ptr;
1818

1919
pub struct Key<T> {
@@ -44,7 +44,7 @@ impl<T> Key<T> {
4444

4545
pub fn get(&'static self) -> Option<&'static UnsafeCell<Option<T>>> {
4646
unsafe {
47-
if intrinsics::needs_drop::<T>() && self.dtor_running.get() {
47+
if mem::needs_drop::<T>() && self.dtor_running.get() {
4848
return None
4949
}
5050
self.register_dtor();
@@ -53,7 +53,7 @@ impl<T> Key<T> {
5353
}
5454

5555
unsafe fn register_dtor(&self) {
56-
if !intrinsics::needs_drop::<T>() || self.dtor_registered.get() {
56+
if !mem::needs_drop::<T>() || self.dtor_registered.get() {
5757
return
5858
}
5959

src/test/compile-fail/issue-27942.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
pub trait Resources<'a> {}
1212

1313
pub trait Buffer<'a, R: Resources<'a>> {
14-
//~^ NOTE the lifetime 'a as defined on the trait at 13:0...
14+
//~^ NOTE the lifetime 'a as defined on the trait at 13:1...
1515
//~| NOTE ...does not necessarily outlive the lifetime 'a as defined on the trait
1616

1717
fn select(&self) -> BufferViewHandle<R>;
@@ -22,7 +22,7 @@ pub trait Buffer<'a, R: Resources<'a>> {
2222
//~| ERROR mismatched types
2323
//~| lifetime mismatch
2424
//~| NOTE expected type `Resources<'_>`
25-
//~| NOTE the anonymous lifetime #1 defined on the method body at 17:4...
25+
//~| NOTE the anonymous lifetime #1 defined on the method body at 17:5...
2626
}
2727

2828
pub struct BufferViewHandle<'a, R: 'a+Resources<'a>>(&'a R);

0 commit comments

Comments
 (0)