Skip to content

Commit fc145e1

Browse files
committed
Auto merge of #71445 - Dylan-DPC:rollup-31givp1, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #71256 (Lint must_use on mem::replace) - #71350 (Error code explanation extra check) - #71369 (allow wasm32 compilation of librustc_data_structures/profiling.rs) - #71400 (proc_macro::is_available()) - #71440 (Implement `Copy` for `AllocErr`) Failed merges: r? @ghost
2 parents db9b05a + bb13aab commit fc145e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+345
-129
lines changed

src/libcore/alloc/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::ptr::{self, NonNull};
1818
/// something wrong when combining the given input arguments with this
1919
/// allocator.
2020
#[unstable(feature = "allocator_api", issue = "32838")]
21-
#[derive(Clone, PartialEq, Eq, Debug)]
21+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2222
pub struct AllocErr;
2323

2424
// (we need this for downstream impl of trait Error)

src/libcore/marker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
709709
/// So this, for example, can only be done on types implementing `Unpin`:
710710
///
711711
/// ```rust
712+
/// # #![allow(unused_must_use)]
712713
/// use std::mem;
713714
/// use std::pin::Pin;
714715
///

src/libcore/mem/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ pub fn take<T: Default>(dest: &mut T) -> T {
808808
/// [`Clone`]: ../../std/clone/trait.Clone.html
809809
#[inline]
810810
#[stable(feature = "rust1", since = "1.0.0")]
811+
#[must_use = "if you don't need the old value, you can just assign the new value directly"]
811812
pub fn replace<T>(dest: &mut T, mut src: T) -> T {
812813
swap(dest, &mut src);
813814
src

src/libproc_macro/bridge/client.rs

+7
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ impl BridgeState<'_> {
290290
}
291291

292292
impl Bridge<'_> {
293+
pub(crate) fn is_available() -> bool {
294+
BridgeState::with(|state| match state {
295+
BridgeState::Connected(_) | BridgeState::InUse => true,
296+
BridgeState::NotConnected => false,
297+
})
298+
}
299+
293300
fn enter<R>(self, f: impl FnOnce() -> R) -> R {
294301
// Hide the default panic output within `proc_macro` expansions.
295302
// NB. the server can't do this because it may use a different libstd.

src/libproc_macro/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ use std::path::PathBuf;
4545
use std::str::FromStr;
4646
use std::{error, fmt, iter, mem};
4747

48+
/// Determines whether proc_macro has been made accessible to the currently
49+
/// running program.
50+
///
51+
/// The proc_macro crate is only intended for use inside the implementation of
52+
/// procedural macros. All the functions in this crate panic if invoked from
53+
/// outside of a procedural macro, such as from a build script or unit test or
54+
/// ordinary Rust binary.
55+
///
56+
/// With consideration for Rust libraries that are designed to support both
57+
/// macro and non-macro use cases, `proc_macro::is_available()` provides a
58+
/// non-panicking way to detect whether the infrastructure required to use the
59+
/// API of proc_macro is presently available. Returns true if invoked from
60+
/// inside of a procedural macro, false if invoked from any other binary.
61+
#[unstable(feature = "proc_macro_is_available", issue = "71436")]
62+
pub fn is_available() -> bool {
63+
bridge::Bridge::is_available()
64+
}
65+
4866
/// The main type provided by this crate, representing an abstract stream of
4967
/// tokens, or, more specifically, a sequence of token trees.
5068
/// The type provide interfaces for iterating over those token trees and, conversely,

src/librustc_data_structures/profiling.rs

+42-31
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,17 @@ use std::time::{Duration, Instant};
9797
use measureme::{EventId, EventIdBuilder, SerializableString, StringId};
9898
use parking_lot::RwLock;
9999

100-
/// MmapSerializatioSink is faster on macOS and Linux
101-
/// but FileSerializationSink is faster on Windows
102-
#[cfg(not(windows))]
103-
type SerializationSink = measureme::MmapSerializationSink;
104-
#[cfg(windows)]
105-
type SerializationSink = measureme::FileSerializationSink;
100+
cfg_if! {
101+
if #[cfg(any(windows, target_os = "wasi"))] {
102+
/// FileSerializationSink is faster on Windows
103+
type SerializationSink = measureme::FileSerializationSink;
104+
} else if #[cfg(target_arch = "wasm32")] {
105+
type SerializationSink = measureme::ByteVecSink;
106+
} else {
107+
/// MmapSerializatioSink is faster on macOS and Linux
108+
type SerializationSink = measureme::MmapSerializationSink;
109+
}
110+
}
106111

107112
type Profiler = measureme::Profiler<SerializationSink>;
108113

@@ -602,31 +607,37 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
602607
}
603608

604609
// Memory reporting
605-
#[cfg(unix)]
606-
fn get_resident() -> Option<usize> {
607-
let field = 1;
608-
let contents = fs::read("/proc/self/statm").ok()?;
609-
let contents = String::from_utf8(contents).ok()?;
610-
let s = contents.split_whitespace().nth(field)?;
611-
let npages = s.parse::<usize>().ok()?;
612-
Some(npages * 4096)
613-
}
614-
615-
#[cfg(windows)]
616-
fn get_resident() -> Option<usize> {
617-
use std::mem::{self, MaybeUninit};
618-
use winapi::shared::minwindef::DWORD;
619-
use winapi::um::processthreadsapi::GetCurrentProcess;
620-
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
621-
622-
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
623-
match unsafe {
624-
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
625-
} {
626-
0 => None,
627-
_ => {
628-
let pmc = unsafe { pmc.assume_init() };
629-
Some(pmc.WorkingSetSize as usize)
610+
cfg_if! {
611+
if #[cfg(windows)] {
612+
fn get_resident() -> Option<usize> {
613+
use std::mem::{self, MaybeUninit};
614+
use winapi::shared::minwindef::DWORD;
615+
use winapi::um::processthreadsapi::GetCurrentProcess;
616+
use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
617+
618+
let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
619+
match unsafe {
620+
GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
621+
} {
622+
0 => None,
623+
_ => {
624+
let pmc = unsafe { pmc.assume_init() };
625+
Some(pmc.WorkingSetSize as usize)
626+
}
627+
}
628+
}
629+
} else if #[cfg(unix)] {
630+
fn get_resident() -> Option<usize> {
631+
let field = 1;
632+
let contents = fs::read("/proc/self/statm").ok()?;
633+
let contents = String::from_utf8(contents).ok()?;
634+
let s = contents.split_whitespace().nth(field)?;
635+
let npages = s.parse::<usize>().ok()?;
636+
Some(npages * 4096)
637+
}
638+
} else {
639+
fn get_resident() -> Option<usize> {
640+
None
630641
}
631642
}
632643
}

src/librustc_error_codes/error_codes/E0060.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ External C functions are allowed to be variadic. However, a variadic function
22
takes a minimum number of arguments. For example, consider C's variadic `printf`
33
function:
44

5-
```
5+
```compile_fail,E0060
66
use std::os::raw::{c_char, c_int};
77
88
extern "C" {
99
fn printf(_: *const c_char, ...) -> c_int;
1010
}
11+
12+
unsafe { printf(); } // error!
1113
```
1214

1315
Using this declaration, it must be called with at least one argument, so

src/librustc_error_codes/error_codes/E0130.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ A pattern was declared as an argument in a foreign function declaration.
22

33
Erroneous code example:
44

5-
```compile_fail
5+
```compile_fail,E0130
66
extern {
77
fn foo((a, b): (u32, u32)); // error: patterns aren't allowed in foreign
88
// function declarations

src/librustc_error_codes/error_codes/E0198.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ A negative implementation was marked as unsafe.
22

33
Erroneous code example:
44

5-
```compile_fail
5+
```compile_fail,E0198
66
struct Foo;
77
88
unsafe impl !Clone for Foo { } // error!
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Inherent associated types were part of [RFC 195] but are not yet implemented.
22
See [the tracking issue][iss8995] for the status of this implementation.
33

4+
Erroneous code example:
5+
6+
```compile_fail,E0202
7+
struct Foo;
8+
9+
impl Foo {
10+
type Bar = isize; // error!
11+
}
12+
```
13+
414
[RFC 195]: https://github.com/rust-lang/rfcs/blob/master/text/0195-associated-items.md
515
[iss8995]: https://github.com/rust-lang/rust/issues/8995

src/librustc_error_codes/error_codes/E0230.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ message for when a particular trait isn't implemented on a type placed in a
33
position that needs that trait. For example, when the following code is
44
compiled:
55

6-
```compile_fail
6+
```compile_fail,E0230
77
#![feature(rustc_attrs)]
88
9-
fn foo<T: Index<u8>>(x: T){}
10-
11-
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
12-
trait Index<Idx> { /* ... */ }
13-
14-
foo(true); // `bool` does not implement `Index<u8>`
9+
#[rustc_on_unimplemented = "error on `{Self}` with params `<{A},{B}>`"] // error
10+
trait BadAnnotation<A> {}
1511
```
1612

1713
There will be an error about `bool` not implementing `Index<u8>`, followed by a

src/librustc_error_codes/error_codes/E0231.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ message for when a particular trait isn't implemented on a type placed in a
33
position that needs that trait. For example, when the following code is
44
compiled:
55

6-
```compile_fail
6+
```compile_fail,E0231
77
#![feature(rustc_attrs)]
88
9-
fn foo<T: Index<u8>>(x: T){}
10-
11-
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
12-
trait Index<Idx> { /* ... */ }
13-
14-
foo(true); // `bool` does not implement `Index<u8>`
9+
#[rustc_on_unimplemented = "error on `{Self}` with params `<{A},{}>`"] // error!
10+
trait BadAnnotation<A> {}
1511
```
1612

1713
there will be an error about `bool` not implementing `Index<u8>`, followed by a

src/librustc_error_codes/error_codes/E0232.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ message for when a particular trait isn't implemented on a type placed in a
33
position that needs that trait. For example, when the following code is
44
compiled:
55

6-
```compile_fail
6+
```compile_fail,E0232
77
#![feature(rustc_attrs)]
88
9-
fn foo<T: Index<u8>>(x: T){}
10-
11-
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
12-
trait Index<Idx> { /* ... */ }
13-
14-
foo(true); // `bool` does not implement `Index<u8>`
9+
#[rustc_on_unimplemented(lorem="")] // error!
10+
trait BadAnnotation {}
1511
```
1612

1713
there will be an error about `bool` not implementing `Index<u8>`, followed by a

src/librustc_error_codes/error_codes/E0281.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You tried to supply a type which doesn't implement some trait in a location
44
which expected that trait. This error typically occurs when working with
55
`Fn`-based types. Erroneous code example:
66

7-
```compile-fail
7+
```compile_fail
88
fn foo<F: Fn(usize)>(x: F) { }
99
1010
fn main() {

src/librustc_error_codes/error_codes/E0364.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ attempted to `pub use` a type or value that was not itself public.
33

44
Erroneous code example:
55

6-
```compile_fail
7-
mod foo {
8-
const X: u32 = 1;
9-
}
10-
11-
pub use foo::X;
6+
```compile_fail,E0364
7+
mod a {
8+
fn foo() {}
129
13-
fn main() {}
10+
mod a {
11+
pub use super::foo; // error!
12+
}
13+
}
1414
```
1515

1616
The solution to this problem is to ensure that the items that you are
1717
re-exporting are themselves marked with `pub`:
1818

1919
```
20-
mod foo {
21-
pub const X: u32 = 1;
22-
}
23-
24-
pub use foo::X;
20+
mod a {
21+
pub fn foo() {} // ok!
2522
26-
fn main() {}
23+
mod a {
24+
pub use super::foo;
25+
}
26+
}
2727
```
2828

2929
See the [Use Declarations][use-declarations] section of the reference for

src/librustc_error_codes/error_codes/E0378.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ or a newtype wrapper around a pointer.
33

44
Erroneous code example:
55

6-
```compile-fail,E0378
6+
```compile_fail,E0378
77
#![feature(dispatch_from_dyn)]
88
use std::ops::DispatchFromDyn;
99

src/librustc_error_codes/error_codes/E0590.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Example of erroneous code:
55

6-
```compile_fail
6+
```compile_fail,E0590
77
while break {}
88
```
99

src/librustc_error_codes/error_codes/E0639.md

+12
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,17 @@ instantiated from outside of the defining crate as it has been marked
33
as `non_exhaustive` and as such more fields/variants may be added in
44
future that could cause adverse side effects for this code.
55

6+
Erroneous code example:
7+
8+
```ignore (it only works cross-crate)
9+
#[non_exhaustive]
10+
pub struct NormalStruct {
11+
pub first_field: u16,
12+
pub second_field: u16,
13+
}
14+
15+
let ns = NormalStruct { first_field: 640, second_field: 480 }; // error!
16+
```
17+
618
It is recommended that you look for a `new` function or equivalent in the
719
crate's documentation.

src/librustc_error_codes/error_codes/E0644.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ A closure or generator was constructed that references its own type.
22

33
Erroneous example:
44

5-
```compile-fail,E0644
5+
```compile_fail,E0644
66
fn fix<F>(f: &F)
77
where F: Fn(&F)
88
{
9-
f(&f);
9+
f(&f);
1010
}
1111
1212
fn main() {
13-
fix(&|y| {
14-
// Here, when `x` is called, the parameter `y` is equal to `x`.
15-
});
13+
fix(&|y| {
14+
// Here, when `x` is called, the parameter `y` is equal to `x`.
15+
});
1616
}
1717
```
1818

src/librustc_error_codes/error_codes/E0658.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ An unstable feature was used.
22

33
Erroneous code example:
44

5-
```compile_fail,E658
5+
```compile_fail,E0658
66
#[repr(u128)] // error: use of unstable library feature 'repr128'
77
enum Foo {
88
Bar(u64),

src/librustc_error_codes/error_codes/E0669.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
Cannot convert inline assembly operand to a single LLVM value.
22

3+
Erroneous code example:
4+
5+
```compile_fail,E0669
6+
#![feature(llvm_asm)]
7+
8+
fn main() {
9+
unsafe {
10+
llvm_asm!("" :: "r"("")); // error!
11+
}
12+
}
13+
```
14+
315
This error usually happens when trying to pass in a value to an input inline
416
assembly operand that is actually a pair of values. In particular, this can
517
happen when trying to pass in a slice, for instance a `&str`. In Rust, these

0 commit comments

Comments
 (0)