Skip to content

Commit c955f48

Browse files
committed
Auto merge of rust-lang#140356 - tgross35:rollup-cvqvrg4, r=tgross35
Rollup of 8 pull requests Successful merges: - rust-lang#137439 (Stabilise `std::ffi::c_str`) - rust-lang#139031 (Use char::is_whitespace directly in str::trim*) - rust-lang#139090 (fix docs for `Peekable::next_if{_eq}`) - rust-lang#140220 (Fix detection of main function if there are expressions around it) - rust-lang#140297 (Update example to use CStr::to_string_lossy) - rust-lang#140330 (Clarified bootstrap optimization "true" argument) - rust-lang#140339 (session: Cleanup `CanonicalizedPath::new`) - rust-lang#140348 (Update lint-docs to default to Rust 2024) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 496145b + c718a63 commit c955f48

File tree

24 files changed

+112
-72
lines changed

24 files changed

+112
-72
lines changed

bootstrap.example.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@
500500
# building without optimizations takes much longer than optimizing. Further, some platforms
501501
# fail to build without this optimization (c.f. #65352).
502502
# The valid options are:
503-
# true - Enable optimizations.
503+
# true - Enable optimizations (same as 3).
504504
# false - Disable optimizations.
505505
# 0 - Disable optimizations.
506506
# 1 - Basic optimizations.

compiler/rustc_interface/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(rustc::bad_opt_access)]
2-
use std::collections::{BTreeMap, BTreeSet};
2+
use std::collections::BTreeMap;
33
use std::num::NonZero;
4-
use std::path::{Path, PathBuf};
4+
use std::path::PathBuf;
55
use std::sync::atomic::AtomicBool;
66

77
use rustc_abi::Align;
@@ -89,8 +89,8 @@ where
8989
S: Into<String>,
9090
I: IntoIterator<Item = S>,
9191
{
92-
let locations: BTreeSet<CanonicalizedPath> =
93-
locations.into_iter().map(|s| CanonicalizedPath::new(Path::new(&s.into()))).collect();
92+
let locations =
93+
locations.into_iter().map(|s| CanonicalizedPath::new(PathBuf::from(s.into()))).collect();
9494

9595
ExternEntry {
9696
location: ExternLocation::ExactPaths(locations),

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ declare_lint! {
948948
///
949949
/// ### Example
950950
///
951-
/// ```rust,compile_fail
951+
/// ```rust,compile_fail,edition2021
952952
/// #[no_mangle]
953953
/// const FOO: i32 = 5;
954954
/// ```

compiler/rustc_lint/src/impl_trait_overcaptures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare_lint! {
4141
///
4242
/// ### Example
4343
///
44-
/// ```rust,compile_fail
44+
/// ```rust,compile_fail,edition2021
4545
/// # #![deny(impl_trait_overcaptures)]
4646
/// # use std::fmt::Display;
4747
/// let mut x = vec![];

compiler/rustc_lint_defs/src/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ declare_lint! {
14241424
///
14251425
/// ### Example
14261426
///
1427-
/// ```rust,compile_fail
1427+
/// ```rust,compile_fail,edition2021
14281428
/// macro_rules! foo {
14291429
/// () => {};
14301430
/// ($name) => { };
@@ -4128,7 +4128,7 @@ declare_lint! {
41284128
///
41294129
/// ### Example
41304130
///
4131-
/// ```rust,compile_fail
4131+
/// ```rust,compile_fail,edition2021
41324132
/// #![deny(dependency_on_unit_never_type_fallback)]
41334133
/// fn main() {
41344134
/// if true {

compiler/rustc_session/src/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2323,14 +2323,13 @@ pub fn parse_externs(
23232323
let ExternOpt { crate_name: name, path, options } =
23242324
split_extern_opt(early_dcx, unstable_opts, &arg).unwrap_or_else(|e| e.emit());
23252325

2326-
let path = path.map(|p| CanonicalizedPath::new(p.as_path()));
2327-
23282326
let entry = externs.entry(name.to_owned());
23292327

23302328
use std::collections::btree_map::Entry;
23312329

23322330
let entry = if let Some(path) = path {
23332331
// --extern prelude_name=some_file.rlib
2332+
let path = CanonicalizedPath::new(path);
23342333
match entry {
23352334
Entry::Vacant(vacant) => {
23362335
let files = BTreeSet::from_iter(iter::once(path));

compiler/rustc_session/src/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::PathBuf;
22
use std::sync::OnceLock;
33

44
use rustc_data_structures::profiling::VerboseTimingGuard;
@@ -104,8 +104,8 @@ pub struct CanonicalizedPath {
104104
}
105105

106106
impl CanonicalizedPath {
107-
pub fn new(path: &Path) -> Self {
108-
Self { original: path.to_owned(), canonicalized: try_canonicalize(path).ok() }
107+
pub fn new(path: PathBuf) -> Self {
108+
Self { canonicalized: try_canonicalize(&path).ok(), original: path }
109109
}
110110

111111
pub fn canonicalized(&self) -> &PathBuf {

library/alloc/src/ffi/c_str.rs

+2
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ impl From<Vec<NonZero<u8>>> for CString {
818818
}
819819
}
820820

821+
#[stable(feature = "c_string_from_str", since = "1.85.0")]
821822
impl FromStr for CString {
822823
type Err = NulError;
823824

@@ -830,6 +831,7 @@ impl FromStr for CString {
830831
}
831832
}
832833

834+
#[stable(feature = "c_string_from_str", since = "1.85.0")]
833835
impl TryFrom<CString> for String {
834836
type Error = IntoStringError;
835837

library/alloc/src/ffi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ pub use self::c_str::CString;
8787
#[stable(feature = "alloc_c_string", since = "1.64.0")]
8888
pub use self::c_str::{FromVecWithNulError, IntoStringError, NulError};
8989

90-
#[unstable(feature = "c_str_module", issue = "112134")]
90+
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
9191
pub mod c_str;

library/core/src/ffi/c_str.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ use crate::{fmt, ops, slice, str};
7979
///
8080
/// fn my_string_safe() -> String {
8181
/// let cstr = unsafe { CStr::from_ptr(my_string()) };
82-
/// // Get copy-on-write Cow<'_, str>, then guarantee a freshly-owned String allocation
83-
/// String::from_utf8_lossy(cstr.to_bytes()).to_string()
82+
/// // Get a copy-on-write Cow<'_, str>, then extract the
83+
/// // allocated String (or allocate a fresh one if needed).
84+
/// cstr.to_string_lossy().into_owned()
8485
/// }
8586
///
8687
/// println!("string: {}", my_string_safe());

library/core/src/ffi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use self::c_str::FromBytesUntilNulError;
2020
pub use self::c_str::FromBytesWithNulError;
2121
use crate::fmt;
2222

23-
#[unstable(feature = "c_str_module", issue = "112134")]
23+
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
2424
pub mod c_str;
2525

2626
#[unstable(

library/core/src/iter/adapters/peekable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<I: Iterator> Peekable<I> {
271271
/// assert_eq!(iter.next_if(|&x| x == 0), Some(0));
272272
/// // The next item returned is now 1, so `next_if` will return `None`.
273273
/// assert_eq!(iter.next_if(|&x| x == 0), None);
274-
/// // `next_if` saves the value of the next item if it was not equal to `expected`.
274+
/// // `next_if` retains the next item if the predicate evaluates to `false` for it.
275275
/// assert_eq!(iter.next(), Some(1));
276276
/// ```
277277
///
@@ -304,9 +304,9 @@ impl<I: Iterator> Peekable<I> {
304304
/// let mut iter = (0..5).peekable();
305305
/// // The first item of the iterator is 0; consume it.
306306
/// assert_eq!(iter.next_if_eq(&0), Some(0));
307-
/// // The next item returned is now 1, so `next_if` will return `None`.
307+
/// // The next item returned is now 1, so `next_if_eq` will return `None`.
308308
/// assert_eq!(iter.next_if_eq(&0), None);
309-
/// // `next_if_eq` saves the value of the next item if it was not equal to `expected`.
309+
/// // `next_if_eq` retains the next item if it was not equal to `expected`.
310310
/// assert_eq!(iter.next(), Some(1));
311311
/// ```
312312
#[stable(feature = "peekable_next_if", since = "1.51.0")]

library/core/src/str/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,7 @@ impl str {
21152115
#[stable(feature = "rust1", since = "1.0.0")]
21162116
#[rustc_diagnostic_item = "str_trim"]
21172117
pub fn trim(&self) -> &str {
2118-
self.trim_matches(|c: char| c.is_whitespace())
2118+
self.trim_matches(char::is_whitespace)
21192119
}
21202120

21212121
/// Returns a string slice with leading whitespace removed.
@@ -2154,7 +2154,7 @@ impl str {
21542154
#[stable(feature = "trim_direction", since = "1.30.0")]
21552155
#[rustc_diagnostic_item = "str_trim_start"]
21562156
pub fn trim_start(&self) -> &str {
2157-
self.trim_start_matches(|c: char| c.is_whitespace())
2157+
self.trim_start_matches(char::is_whitespace)
21582158
}
21592159

21602160
/// Returns a string slice with trailing whitespace removed.
@@ -2193,7 +2193,7 @@ impl str {
21932193
#[stable(feature = "trim_direction", since = "1.30.0")]
21942194
#[rustc_diagnostic_item = "str_trim_end"]
21952195
pub fn trim_end(&self) -> &str {
2196-
self.trim_end_matches(|c: char| c.is_whitespace())
2196+
self.trim_end_matches(char::is_whitespace)
21972197
}
21982198

21992199
/// Returns a string slice with leading whitespace removed.

library/std/src/ffi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
162162
#![stable(feature = "rust1", since = "1.0.0")]
163163

164-
#[unstable(feature = "c_str_module", issue = "112134")]
164+
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
165165
pub mod c_str;
166166

167167
#[stable(feature = "core_c_void", since = "1.30.0")]

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@
329329
#![feature(array_chunks)]
330330
#![feature(bstr)]
331331
#![feature(bstr_internals)]
332-
#![feature(c_str_module)]
333332
#![feature(char_internals)]
334333
#![feature(clone_to_uninit)]
335334
#![feature(core_intrinsics)]

src/librustdoc/doctest/make.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -407,17 +407,27 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
407407
push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi);
408408
}
409409
}
410+
let mut has_non_items = false;
410411
for stmt in &body.stmts {
411412
let mut is_extern_crate = false;
412413
match stmt.kind {
413414
StmtKind::Item(ref item) => {
414415
is_extern_crate = check_item(&item, &mut info, crate_name);
415416
}
416-
StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => {
417-
reset_error_count(&psess);
418-
return Err(());
417+
StmtKind::Expr(ref expr) => {
418+
if matches!(expr.kind, ast::ExprKind::Err(_)) {
419+
reset_error_count(&psess);
420+
return Err(());
421+
}
422+
has_non_items = true;
419423
}
420-
StmtKind::MacCall(ref mac_call) if !info.has_main_fn => {
424+
// We assume that the macro calls will expand to item(s) even though they could
425+
// expand to statements and expressions. And the simple fact that we're trying
426+
// to retrieve a `main` function inside it is a terrible idea.
427+
StmtKind::MacCall(ref mac_call) => {
428+
if info.has_main_fn {
429+
continue;
430+
}
421431
let mut iter = mac_call.mac.args.tokens.iter();
422432

423433
while let Some(token) = iter.next() {
@@ -437,7 +447,9 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
437447
}
438448
}
439449
}
440-
_ => {}
450+
_ => {
451+
has_non_items = true;
452+
}
441453
}
442454

443455
// Weirdly enough, the `Stmt` span doesn't include its attributes, so we need to
@@ -462,6 +474,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
462474
push_to_s(&mut info.crates, source, span, &mut prev_span_hi);
463475
}
464476
}
477+
if has_non_items {
478+
// FIXME: if `info.has_main_fn` is `true`, emit a warning here to mention that
479+
// this code will not be called.
480+
info.has_main_fn = false;
481+
}
465482
Ok(info)
466483
}
467484
Err(e) => {

src/tools/lint-docs/src/lib.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,15 @@ impl<'a> LintExtractor<'a> {
444444
fs::write(&tempfile, source)
445445
.map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?;
446446
let mut cmd = Command::new(self.rustc_path);
447-
if options.contains(&"edition2024") {
448-
cmd.arg("--edition=2024");
449-
cmd.arg("-Zunstable-options");
450-
} else if options.contains(&"edition2021") {
451-
cmd.arg("--edition=2021");
452-
} else if options.contains(&"edition2018") {
453-
cmd.arg("--edition=2018");
454-
} else if options.contains(&"edition2015") {
455-
cmd.arg("--edition=2015");
456-
} else if options.contains(&"edition") {
457-
panic!("lint-docs: unknown edition");
458-
} else {
447+
let edition = options
448+
.iter()
449+
.filter_map(|opt| opt.strip_prefix("edition"))
450+
.next()
459451
// defaults to latest edition
460-
cmd.arg("--edition=2021");
461-
}
452+
.unwrap_or("2024");
453+
cmd.arg(format!("--edition={edition}"));
454+
// Just in case this is an unstable edition.
455+
cmd.arg("-Zunstable-options");
462456
cmd.arg("--error-format=json");
463457
cmd.arg("--target").arg(self.rustc_target);
464458
if let Some(target_linker) = self.rustc_linker {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use std::string::String;

tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
//@ compile-flags:--test
55
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
66
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
7-
//@ failure-status: 101
7+
//@ check-pass
88

99
/// <https://github.com/rust-lang/rust/issues/91014>
1010
///
1111
/// ```rust
12-
/// struct S {}; // unexpected semicolon after struct def
12+
/// struct S {};
1313
///
1414
/// fn main() {
1515
/// assert_eq!(0, 1);
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11

22
running 1 test
3-
test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... FAILED
3+
test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... ok
44

5-
failures:
6-
7-
---- $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) stdout ----
8-
error: expected item, found `;`
9-
--> $DIR/failed-doctest-extra-semicolon-on-item.rs:12:12
10-
|
11-
LL | struct S {}; // unexpected semicolon after struct def
12-
| ^
13-
|
14-
= help: braced struct declarations are not followed by a semicolon
15-
help: remove this semicolon
16-
|
17-
LL - struct S {}; // unexpected semicolon after struct def
18-
LL + struct S {} // unexpected semicolon after struct def
19-
|
20-
21-
error: aborting due to 1 previous error
22-
23-
Couldn't compile the test.
24-
25-
failures:
26-
$DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11)
27-
28-
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
296

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test checks a corner case where the macro calls used to be skipped,
2+
// making them considered as statement, and therefore some cases where
3+
// `include!` macro was then put into a function body, making the doctest
4+
// compilation fail.
5+
6+
//@ compile-flags:--test
7+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
8+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
9+
//@ check-pass
10+
11+
//! ```
12+
//! include!("./auxiliary/macro-after-main.rs");
13+
//!
14+
//! fn main() {}
15+
//! eprintln!();
16+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/macro-after-main.rs - (line 11) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test ensures that if there is an expression alongside a `main`
2+
// function, it will not consider the entire code to be part of the `main`
3+
// function and will generate its own function to wrap everything.
4+
//
5+
// This is a regression test for:
6+
// * <https://github.com/rust-lang/rust/issues/140162>
7+
// * <https://github.com/rust-lang/rust/issues/139651>
8+
//@ compile-flags:--test
9+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
10+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
11+
//@ check-pass
12+
13+
#![crate_name = "foo"]
14+
15+
//! ```
16+
//! # if cfg!(miri) { return; }
17+
//! use std::ops::Deref;
18+
//!
19+
//! fn main() {
20+
//! println!("Hi!");
21+
//! }
22+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/test-main-alongside-exprs.rs - (line 15) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

0 commit comments

Comments
 (0)