Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 4 pull requests #128319

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2921,7 +2921,7 @@ impl<'a, K, V> Cursor<'a, K, V> {
/// Returns a reference to the key and value of the next element without
/// moving the cursor.
///
/// If the cursor is at the end of the map then `None` is returned
/// If the cursor is at the end of the map then `None` is returned.
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn peek_next(&self) -> Option<(&'a K, &'a V)> {
self.clone().next()
Expand Down Expand Up @@ -2963,7 +2963,7 @@ impl<'a, K, V, A> CursorMut<'a, K, V, A> {
/// Returns a reference to the key and value of the next element without
/// moving the cursor.
///
/// If the cursor is at the end of the map then `None` is returned
/// If the cursor is at the end of the map then `None` is returned.
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn peek_next(&mut self) -> Option<(&K, &mut V)> {
let (k, v) = self.inner.peek_next()?;
Expand Down Expand Up @@ -3061,7 +3061,7 @@ impl<'a, K, V, A> CursorMutKey<'a, K, V, A> {
/// Returns a reference to the key and value of the next element without
/// moving the cursor.
///
/// If the cursor is at the end of the map then `None` is returned
/// If the cursor is at the end of the map then `None` is returned.
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn peek_next(&mut self) -> Option<(&mut K, &mut V)> {
let current = self.current.as_mut()?;
Expand Down
65 changes: 35 additions & 30 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,55 +210,60 @@ impl<T> Cursor<T>
where
T: AsRef<[u8]>,
{
/// Returns the remaining slice.
/// Splits the underlying slice at the cursor position and returns them.
///
/// # Examples
///
/// ```
/// #![feature(cursor_remaining)]
/// #![feature(cursor_split)]
/// use std::io::Cursor;
///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
///
/// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
/// assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
///
/// buff.set_position(2);
/// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
///
/// buff.set_position(4);
/// assert_eq!(buff.remaining_slice(), &[5]);
/// assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
///
/// buff.set_position(6);
/// assert_eq!(buff.remaining_slice(), &[]);
/// assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
/// ```
#[unstable(feature = "cursor_remaining", issue = "86369")]
pub fn remaining_slice(&self) -> &[u8] {
let len = self.pos.min(self.inner.as_ref().len() as u64);
&self.inner.as_ref()[(len as usize)..]
#[unstable(feature = "cursor_split", issue = "86369")]
pub fn split(&self) -> (&[u8], &[u8]) {
let slice = self.inner.as_ref();
let pos = self.pos.min(slice.len() as u64);
slice.split_at(pos as usize)
}
}

/// Returns `true` if the remaining slice is empty.
impl<T> Cursor<T>
where
T: AsMut<[u8]>,
{
/// Splits the underlying slice at the cursor position and returns them
/// mutably.
///
/// # Examples
///
/// ```
/// #![feature(cursor_remaining)]
/// #![feature(cursor_split)]
/// use std::io::Cursor;
///
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
///
/// buff.set_position(2);
/// assert!(!buff.is_empty());
/// assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
///
/// buff.set_position(5);
/// assert!(buff.is_empty());
/// buff.set_position(2);
/// assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
///
/// buff.set_position(10);
/// assert!(buff.is_empty());
/// buff.set_position(6);
/// assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
/// ```
#[unstable(feature = "cursor_remaining", issue = "86369")]
pub fn is_empty(&self) -> bool {
self.pos >= self.inner.as_ref().len() as u64
#[unstable(feature = "cursor_split", issue = "86369")]
pub fn split_mut(&mut self) -> (&mut [u8], &mut [u8]) {
let slice = self.inner.as_mut();
let pos = self.pos.min(slice.len() as u64);
slice.split_at_mut(pos as usize)
}
}

Expand Down Expand Up @@ -320,15 +325,15 @@ where
T: AsRef<[u8]>,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = Read::read(&mut self.remaining_slice(), buf)?;
let n = Read::read(&mut Cursor::split(self).1, buf)?;
self.pos += n as u64;
Ok(n)
}

fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
let prev_written = cursor.written();

Read::read_buf(&mut self.remaining_slice(), cursor.reborrow())?;
Read::read_buf(&mut Cursor::split(self).1, cursor.reborrow())?;

self.pos += (cursor.written() - prev_written) as u64;

Expand All @@ -352,7 +357,7 @@ where
}

fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
let result = Read::read_exact(&mut self.remaining_slice(), buf);
let result = Read::read_exact(&mut Cursor::split(self).1, buf);

match result {
Ok(_) => self.pos += buf.len() as u64,
Expand All @@ -366,14 +371,14 @@ where
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
let prev_written = cursor.written();

let result = Read::read_buf_exact(&mut self.remaining_slice(), cursor.reborrow());
let result = Read::read_buf_exact(&mut Cursor::split(self).1, cursor.reborrow());
self.pos += (cursor.written() - prev_written) as u64;

result
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
let content = self.remaining_slice();
let content = Cursor::split(self).1;
let len = content.len();
buf.try_reserve(len)?;
buf.extend_from_slice(content);
Expand All @@ -384,7 +389,7 @@ where

fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
let content =
crate::str::from_utf8(self.remaining_slice()).map_err(|_| io::Error::INVALID_UTF8)?;
crate::str::from_utf8(Cursor::split(self).1).map_err(|_| io::Error::INVALID_UTF8)?;
let len = content.len();
buf.try_reserve(len)?;
buf.push_str(content);
Expand All @@ -400,7 +405,7 @@ where
T: AsRef<[u8]>,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Ok(self.remaining_slice())
Ok(Cursor::split(self).1)
}
fn consume(&mut self, amt: usize) {
self.pos += amt as u64;
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,13 @@ fn cursor_read_exact_eof() {

let mut r = slice.clone();
assert!(r.read_exact(&mut [0; 10]).is_err());
assert!(r.is_empty());
assert!(Cursor::split(&r).1.is_empty());

let mut r = slice;
let buf = &mut [0; 10];
let mut buf = BorrowedBuf::from(buf.as_mut_slice());
assert!(r.read_buf_exact(buf.unfilled()).is_err());
assert!(r.is_empty());
assert!(Cursor::split(&r).1.is_empty());
assert_eq!(buf.filled(), b"123456");
}

Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Implementation of compiling the compiler and standard library, in "check"-based modes.

use crate::core::build_steps::compile::{
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo,
add_to_sysroot, run_cargo, rustc_cargo, rustc_cargo_env, std_cargo, std_crates_for_run_make,
};
use crate::core::build_steps::tool::{prepare_tool_cargo, SourceType};
use crate::core::builder::{
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Step for Std {
}

fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Library);
let crates = std_crates_for_run_make(&run);
run.builder.ensure(Std { target: run.target, crates });
}

Expand Down
3 changes: 2 additions & 1 deletion src/bootstrap/src/core/build_steps/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::compile::libstd_stamp;
use super::compile::run_cargo;
use super::compile::rustc_cargo;
use super::compile::std_cargo;
use super::compile::std_crates_for_run_make;
use super::tool::prepare_tool_cargo;
use super::tool::SourceType;

Expand Down Expand Up @@ -120,7 +121,7 @@ impl Step for Std {
}

fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Library);
let crates = std_crates_for_run_make(&run);
run.builder.ensure(Std { target: run.target, crates });
}

Expand Down
28 changes: 23 additions & 5 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ impl Step for Std {
}

fn make_run(run: RunConfig<'_>) {
// If the paths include "library", build the entire standard library.
let has_alias =
run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
let crates = if has_alias { Default::default() } else { run.cargo_crates_in_set() };

let crates = std_crates_for_run_make(&run);
run.builder.ensure(Std {
compiler: run.builder.compiler(run.builder.top_stage, run.build_triple()),
target: run.target,
Expand Down Expand Up @@ -429,6 +425,28 @@ fn copy_self_contained_objects(
target_deps
}

/// Resolves standard library crates for `Std::run_make` for any build kind (like check, build, clippy, etc.).
pub fn std_crates_for_run_make(run: &RunConfig<'_>) -> Vec<String> {
// FIXME: Extend builder tests to cover the `crates` field of `Std` instances.
if cfg!(feature = "bootstrap-self-test") {
return vec![];
}

let has_alias = run.paths.iter().any(|set| set.assert_single_path().path.ends_with("library"));
let target_is_no_std = run.builder.no_std(run.target).unwrap_or(false);

// For no_std targets, do not add any additional crates to the compilation other than what `compile::std_cargo` already adds for no_std targets.
if target_is_no_std {
vec![]
}
// If the paths include "library", build the entire standard library.
else if has_alias {
run.make_run_crates(builder::Alias::Library)
} else {
run.cargo_crates_in_set()
}
}

/// Configure cargo to compile the standard library, adding appropriate env vars
/// and such.
pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, cargo: &mut Cargo) {
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl Step for JsonDocs {
builder.ensure(crate::core::build_steps::doc::Std::new(
builder.top_stage,
host,
builder,
DocumentationFormat::Json,
));

Expand Down
24 changes: 4 additions & 20 deletions src/bootstrap/src/core/build_steps/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,8 @@ pub struct Std {
}

impl Std {
pub(crate) fn new(
stage: u32,
target: TargetSelection,
builder: &Builder<'_>,
format: DocumentationFormat,
) -> Self {
let crates = builder
.in_tree_crates("sysroot", Some(target))
.into_iter()
.map(|krate| krate.name.to_string())
.collect();
Std { stage, target, format, crates }
pub(crate) fn new(stage: u32, target: TargetSelection, format: DocumentationFormat) -> Self {
Std { stage, target, format, crates: vec![] }
}
}

Expand All @@ -588,6 +578,7 @@ impl Step for Std {
}

fn make_run(run: RunConfig<'_>) {
let crates = compile::std_crates_for_run_make(&run);
run.builder.ensure(Std {
stage: run.builder.top_stage,
target: run.target,
Expand All @@ -596,7 +587,7 @@ impl Step for Std {
} else {
DocumentationFormat::Html
},
crates: run.make_run_crates(Alias::Library),
crates,
});
}

Expand Down Expand Up @@ -694,13 +685,6 @@ fn doc_std(
extra_args: &[&str],
requested_crates: &[String],
) {
if builder.no_std(target) == Some(true) {
panic!(
"building std documentation for no_std target {target} is not supported\n\
Set `docs = false` in the config to disable documentation, or pass `--skip library`."
);
}

let compiler = builder.compiler(stage, builder.config.build);

let target_doc_dir_name = if format == DocumentationFormat::Json { "json-doc" } else { "doc" };
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,6 @@ impl Step for RustdocJSStd {
builder.ensure(crate::core::build_steps::doc::Std::new(
builder.top_stage,
self.target,
builder,
DocumentationFormat::Html,
));
let _guard = builder.msg(
Expand Down
4 changes: 0 additions & 4 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,9 @@ macro_rules! std {

macro_rules! doc_std {
($host:ident => $target:ident, stage = $stage:literal) => {{
let config = configure("doc", &["A-A"], &["A-A"]);
let build = Build::new(config);
let builder = Builder::new(&build);
doc::Std::new(
$stage,
TargetSelection::from_user(concat!(stringify!($target), "-", stringify!($target))),
&builder,
DocumentationFormat::Html,
)
}};
Expand Down
7 changes: 7 additions & 0 deletions src/tools/run-make-support/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ pub fn env_var_os(name: &str) -> OsString {
None => panic!("failed to retrieve environment variable {name:?}"),
}
}

/// Check if `NO_DEBUG_ASSERTIONS` is set (usually this may be set in CI jobs).
#[track_caller]
#[must_use]
pub fn no_debug_assertions() -> bool {
std::env::var_os("NO_DEBUG_ASSERTIONS").is_some()
}
1 change: 1 addition & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod run;
pub mod scoped_run;
pub mod string;
pub mod targets;
pub mod symbols;

// Internally we call our fs-related support module as `fs`, but re-export its content as `rfs`
// to tests to avoid colliding with commonly used `use std::fs;`.
Expand Down
Loading
Loading