From a043acd0b2cb9cb510cdeb040c0ce881af376142 Mon Sep 17 00:00:00 2001 From: Frank Steffahn Date: Fri, 7 Jan 2022 00:39:48 +0100 Subject: [PATCH 01/26] change PhantomData type for BuildHasherDefault --- library/core/src/hash/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/hash/mod.rs b/library/core/src/hash/mod.rs index 3ff84cc9672eb..53de8b42c059f 100644 --- a/library/core/src/hash/mod.rs +++ b/library/core/src/hash/mod.rs @@ -602,7 +602,7 @@ pub trait BuildHasher { /// [`HashSet`]: ../../std/collections/struct.HashSet.html /// [zero-sized]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts #[stable(since = "1.7.0", feature = "build_hasher")] -pub struct BuildHasherDefault(marker::PhantomData); +pub struct BuildHasherDefault(marker::PhantomData H>); #[stable(since = "1.9.0", feature = "core_impl_debug")] impl fmt::Debug for BuildHasherDefault { From 731bbae816e49cb404f402e8a5d392059e417ee5 Mon Sep 17 00:00:00 2001 From: Frank Steffahn Date: Fri, 7 Jan 2022 01:52:42 +0100 Subject: [PATCH 02/26] Also change PhantomData parameter of iter::Empty, and future::Pending --- library/core/src/future/pending.rs | 5 +---- library/core/src/iter/sources/empty.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/library/core/src/future/pending.rs b/library/core/src/future/pending.rs index 560dd25ecff42..2877e66eca885 100644 --- a/library/core/src/future/pending.rs +++ b/library/core/src/future/pending.rs @@ -12,7 +12,7 @@ use crate::task::{Context, Poll}; #[stable(feature = "future_readiness_fns", since = "1.48.0")] #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Pending { - _data: marker::PhantomData, + _data: marker::PhantomData T>, } /// Creates a future which never resolves, representing a computation that never @@ -43,9 +43,6 @@ impl Future for Pending { } } -#[stable(feature = "future_readiness_fns", since = "1.48.0")] -impl Unpin for Pending {} - #[stable(feature = "future_readiness_fns", since = "1.48.0")] impl Debug for Pending { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/library/core/src/iter/sources/empty.rs b/library/core/src/iter/sources/empty.rs index 7abe01d17c90b..98734c527f2b9 100644 --- a/library/core/src/iter/sources/empty.rs +++ b/library/core/src/iter/sources/empty.rs @@ -22,17 +22,17 @@ pub const fn empty() -> Empty { Empty(marker::PhantomData) } +// Newtype for use in `PhantomData` to avoid +// > error: const-stable function cannot use `#[feature(const_fn_fn_ptr_basics)]` +// in `const fn empty()` above. +struct FnReturning(fn() -> T); + /// An iterator that yields nothing. /// /// This `struct` is created by the [`empty()`] function. See its documentation for more. #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iter_empty", since = "1.2.0")] -pub struct Empty(marker::PhantomData); - -#[stable(feature = "iter_empty_send_sync", since = "1.42.0")] -unsafe impl Send for Empty {} -#[stable(feature = "iter_empty_send_sync", since = "1.42.0")] -unsafe impl Sync for Empty {} +pub struct Empty(marker::PhantomData>); #[stable(feature = "core_impl_debug", since = "1.9.0")] impl fmt::Debug for Empty { From 6a1f9e6fc7272599de76fec37e31db4978780649 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 26 Dec 2021 22:12:41 +0800 Subject: [PATCH 03/26] mangling_v0: Update tests for the rust-demangler tool --- src/tools/rust-demangler/tests/lib.rs | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/tools/rust-demangler/tests/lib.rs b/src/tools/rust-demangler/tests/lib.rs index 5a67b42322535..85019df7867dd 100644 --- a/src/tools/rust-demangler/tests/lib.rs +++ b/src/tools/rust-demangler/tests/lib.rs @@ -29,14 +29,14 @@ cc[4d6468d6c9fd4bb3]::spawn::{closure#0}::{closure#0} as core[846817f741e54dfd]::iter::iterator::Iterator>::rposition::::{closure#0} alloc[f15a878b47eb696b]::alloc::box_free::> INtC8arrayvec8ArrayVechKj7b_E -> -> -> -> -> -> -> -> +> +> +> +> +> +> +> +> >::foo::FOO foo[0] foo[0] @@ -51,14 +51,14 @@ cc::spawn::{closure#0}::{closure#0} as core::iter::iterator::Iterator>::rposition::::{closure#0} alloc::alloc::box_free::> INtC8arrayvec8ArrayVechKj7b_E -> -> -> -> -> -> -> -> +> +> +> +> +> +> +> +> >::foo::FOO foo[0] foo[0] From 1b8daf8c58267a5d9fae2021a311dfbbda11b02f Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 27 Dec 2021 20:05:22 +0800 Subject: [PATCH 04/26] mangling_v0: Add a test for mangling of foreign types --- src/test/ui/symbol-names/foreign-types.rs | 19 ++++++++++++++++++ src/test/ui/symbol-names/foreign-types.stderr | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/ui/symbol-names/foreign-types.rs create mode 100644 src/test/ui/symbol-names/foreign-types.stderr diff --git a/src/test/ui/symbol-names/foreign-types.rs b/src/test/ui/symbol-names/foreign-types.rs new file mode 100644 index 0000000000000..33c5e6adfef29 --- /dev/null +++ b/src/test/ui/symbol-names/foreign-types.rs @@ -0,0 +1,19 @@ +// build-fail +// compile-flags: -Z symbol-mangling-version=v0 + +#![feature(extern_types)] +#![feature(rustc_attrs)] + +extern "C" { + type ForeignType; +} + +struct Check(T); + +#[rustc_symbol_name] +//~^ ERROR symbol-name(_RMCs +//~| ERROR demangling(>) +impl Check {} + +fn main() {} diff --git a/src/test/ui/symbol-names/foreign-types.stderr b/src/test/ui/symbol-names/foreign-types.stderr new file mode 100644 index 0000000000000..c6c59a6c82ea7 --- /dev/null +++ b/src/test/ui/symbol-names/foreign-types.stderr @@ -0,0 +1,20 @@ +error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB_5CheckNvNtB_011ForeignTypeE) + --> $DIR/foreign-types.rs:13:1 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling(>) + --> $DIR/foreign-types.rs:13:1 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: demangling-alt(>) + --> $DIR/foreign-types.rs:13:1 + | +LL | #[rustc_symbol_name] + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + From 333a5cc3691a96aab461ebcbc6064ee79356f1ae Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 27 Dec 2021 20:09:20 +0800 Subject: [PATCH 05/26] mangling_v0: Skip extern blocks during mangling --- compiler/rustc_symbol_mangling/src/v0.rs | 6 +++--- src/test/ui/symbol-names/foreign-types.rs | 2 +- src/test/ui/symbol-names/foreign-types.stderr | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index c2519adcbe416..fbb9e28d182f8 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -771,9 +771,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> { disambiguated_data: &DisambiguatedDefPathData, ) -> Result { let ns = match disambiguated_data.data { - // FIXME: It shouldn't be necessary to add anything for extern block segments, - // but we add 't' for backward compatibility. - DefPathData::ForeignMod => 't', + // Extern block segments can be skipped, names from extern blocks + // are effectively living in their parent modules. + DefPathData::ForeignMod => return print_prefix(self), // Uppercase categories are more stable than lowercase ones. DefPathData::TypeNs(_) => 't', diff --git a/src/test/ui/symbol-names/foreign-types.rs b/src/test/ui/symbol-names/foreign-types.rs index 33c5e6adfef29..8f5b07769caff 100644 --- a/src/test/ui/symbol-names/foreign-types.rs +++ b/src/test/ui/symbol-names/foreign-types.rs @@ -1,5 +1,5 @@ // build-fail -// compile-flags: -Z symbol-mangling-version=v0 +// compile-flags: -C symbol-mangling-version=v0 #![feature(extern_types)] #![feature(rustc_attrs)] diff --git a/src/test/ui/symbol-names/foreign-types.stderr b/src/test/ui/symbol-names/foreign-types.stderr index c6c59a6c82ea7..fcffdd2a8ec15 100644 --- a/src/test/ui/symbol-names/foreign-types.stderr +++ b/src/test/ui/symbol-names/foreign-types.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB_5CheckNvNtB_011ForeignTypeE) +error: symbol-name(_RMCsCRATE_HASH_13foreign_typesINtB_5CheckNvB_11ForeignTypeE) --> $DIR/foreign-types.rs:13:1 | LL | #[rustc_symbol_name] From 3c0fb7f36d872184b67884838b967d0b57dbf3a2 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 11 Jan 2022 17:00:07 -0800 Subject: [PATCH 06/26] Add manifest docs fallback. --- src/tools/build-manifest/src/main.rs | 95 +++++++++++++++--------- src/tools/build-manifest/src/versions.rs | 8 +- 2 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index c1579ae9ac54a..d3bce6b3e8df5 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -155,17 +155,19 @@ static TARGETS: &[&str] = &[ "x86_64-unknown-hermit", ]; -static DOCS_TARGETS: &[&str] = &[ - "aarch64-unknown-linux-gnu", - "i686-apple-darwin", - "i686-pc-windows-gnu", - "i686-pc-windows-msvc", - "i686-unknown-linux-gnu", - "x86_64-apple-darwin", - "x86_64-pc-windows-gnu", - "x86_64-pc-windows-msvc", - "x86_64-unknown-linux-gnu", - "x86_64-unknown-linux-musl", +/// This allows the manifest to contain rust-docs for hosts that don't build +/// docs. +/// +/// Tuples of `(host_partial, host_instead)`. If the host does not have the +/// rust-docs component available, then if the host name contains +/// `host_partial`, it will use the docs from `host_instead` instead. +/// +/// The order here matters, more specific entries should be first. +static DOCS_FALLBACK: &[(&str, &str)] = &[ + ("-apple-", "x86_64-apple-darwin"), + ("aarch64", "aarch64-unknown-linux-gnu"), + ("arm-", "aarch64-unknown-linux-gnu"), + ("", "x86_64-unknown-linux-gnu"), ]; static MSI_INSTALLERS: &[&str] = &[ @@ -301,23 +303,27 @@ impl Builder { } fn add_packages_to(&mut self, manifest: &mut Manifest) { - let mut package = |name, targets| self.package(name, &mut manifest.pkg, targets); - package("rustc", HOSTS); - package("rustc-dev", HOSTS); - package("reproducible-artifacts", HOSTS); - package("rustc-docs", HOSTS); - package("cargo", HOSTS); - package("rust-mingw", MINGW); - package("rust-std", TARGETS); - package("rust-docs", DOCS_TARGETS); - package("rust-src", &["*"]); - package("rls-preview", HOSTS); - package("rust-analyzer-preview", HOSTS); - package("clippy-preview", HOSTS); - package("miri-preview", HOSTS); - package("rustfmt-preview", HOSTS); - package("rust-analysis", TARGETS); - package("llvm-tools-preview", TARGETS); + macro_rules! package { + ($name:expr, $targets:expr) => { + self.package($name, &mut manifest.pkg, $targets, &[]) + }; + } + package!("rustc", HOSTS); + package!("rustc-dev", HOSTS); + package!("reproducible-artifacts", HOSTS); + package!("rustc-docs", HOSTS); + package!("cargo", HOSTS); + package!("rust-mingw", MINGW); + package!("rust-std", TARGETS); + self.package("rust-docs", &mut manifest.pkg, HOSTS, DOCS_FALLBACK); + package!("rust-src", &["*"]); + package!("rls-preview", HOSTS); + package!("rust-analyzer-preview", HOSTS); + package!("clippy-preview", HOSTS); + package!("miri-preview", HOSTS); + package!("rustfmt-preview", HOSTS); + package!("rust-analysis", TARGETS); + package!("llvm-tools-preview", TARGETS); } fn add_artifacts_to(&mut self, manifest: &mut Manifest) { @@ -500,7 +506,13 @@ impl Builder { .extend(pkgs.iter().map(|s| (*s).to_owned())); } - fn package(&mut self, pkgname: &str, dst: &mut BTreeMap, targets: &[&str]) { + fn package( + &mut self, + pkgname: &str, + dst: &mut BTreeMap, + targets: &[&str], + fallback: &[(&str, &str)], + ) { let version_info = self .versions .version(&PkgType::from_component(pkgname)) @@ -512,16 +524,29 @@ impl Builder { is_present = false; // Pretend the component is entirely missing. } + macro_rules! tarball_name { + ($target_name:expr) => { + self.versions.tarball_name(&PkgType::from_component(pkgname), $target_name).unwrap() + }; + } + let mut target_from_compressed_tar = |target_name| { + let target = Target::from_compressed_tar(self, &tarball_name!(target_name)); + if target.available { + return target; + } + for (substr, fallback_target) in fallback { + if target_name.contains(substr) { + return Target::from_compressed_tar(self, &tarball_name!(fallback_target)); + } + } + Target::unavailable() + }; + let targets = targets .iter() .map(|name| { let target = if is_present { - let filename = self - .versions - .tarball_name(&PkgType::from_component(pkgname), name) - .unwrap(); - - Target::from_compressed_tar(self, &filename) + target_from_compressed_tar(name) } else { // If the component is not present for this build add it anyway but mark it as // unavailable -- this way rustup won't allow upgrades without --force diff --git a/src/tools/build-manifest/src/versions.rs b/src/tools/build-manifest/src/versions.rs index 11575139adcf6..95c2297de264b 100644 --- a/src/tools/build-manifest/src/versions.rs +++ b/src/tools/build-manifest/src/versions.rs @@ -169,7 +169,7 @@ impl Versions { } pub(crate) fn archive_name( - &mut self, + &self, package: &PkgType, target: &str, extension: &str, @@ -189,11 +189,7 @@ impl Versions { } } - pub(crate) fn tarball_name( - &mut self, - package: &PkgType, - target: &str, - ) -> Result { + pub(crate) fn tarball_name(&self, package: &PkgType, target: &str) -> Result { self.archive_name(package, target, "tar.gz") } From 52fdd2dd86af4b7dfa6b3aad63e5b5148b59c444 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 11 Jan 2022 17:01:00 -0800 Subject: [PATCH 07/26] Remove VERSION from manifest readme docs. The version was removed in https://github.com/rust-lang/rust/pull/77145/ --- src/tools/build-manifest/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tools/build-manifest/README.md b/src/tools/build-manifest/README.md index b77c5a907c118..44c96f31d0ba8 100644 --- a/src/tools/build-manifest/README.md +++ b/src/tools/build-manifest/README.md @@ -20,8 +20,7 @@ Then, you can generate the manifest and all the packages from `path/to/dist` to `path/to/output` with: ``` -$ cargo +nightly run path/to/dist path/to/output 1970-01-01 http://example.com \ - CHANNEL VERSION +$ cargo +nightly run path/to/dist path/to/output 1970-01-01 http://example.com CHANNEL ``` Remember to replace `CHANNEL` with the channel you produced dist artifacts of From 6a1099c2026dc224bdf53a17665cef563f10755a Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 17 Jan 2022 20:18:56 -0800 Subject: [PATCH 08/26] Disable docs on aarch64-apple-darwin. This builder is the slowest in the fleet. This should cut a considerable amount of time. The manifest should now include the docs from x86_64-apple-darwin. Although those docs are slightly different, it should be close enough. When aarch64-apple-darwin heads towards tier 1, we can revisit whether or not to re-enable the docs. --- .github/workflows/ci.yml | 2 +- src/bootstrap/dist.rs | 3 +-- src/ci/github-actions/ci.yml | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67deb3d977942..fe5dedb6ba4b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -320,7 +320,7 @@ jobs: - name: dist-aarch64-apple env: SCRIPT: "./x.py dist --stage 2" - RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --host=aarch64-apple-darwin --target=aarch64-apple-darwin --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false" + RUST_CONFIGURE_ARGS: "--build=x86_64-apple-darwin --host=aarch64-apple-darwin --target=aarch64-apple-darwin --enable-full-tools --enable-sanitizers --enable-profiler --disable-docs --set rust.jemalloc --set llvm.ninja=false" RUSTC_RETRY_LINKER_ON_SEGFAULT: 1 USE_XCODE_CLANG: 1 MACOSX_DEPLOYMENT_TARGET: 11.0 diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 7d9b3da48ecb0..a46a4e63714c2 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -1483,11 +1483,10 @@ impl Step for Extended { }; prepare("rustc"); prepare("cargo"); - prepare("rust-docs"); prepare("rust-std"); prepare("rust-analysis"); prepare("clippy"); - for tool in &["rust-demangler", "rls", "rust-analyzer", "miri"] { + for tool in &["rust-docs", "rust-demangler", "rls", "rust-analyzer", "miri"] { if built_tools.contains(tool) { prepare(tool); } diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index ac5d5822bfbd5..a70cdc4b519e6 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -496,6 +496,7 @@ jobs: --enable-full-tools --enable-sanitizers --enable-profiler + --disable-docs --set rust.jemalloc --set llvm.ninja=false RUSTC_RETRY_LINKER_ON_SEGFAULT: 1 From fdb95f54e81532f5a620e79b8930f89e0e9aee80 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:02:49 -0800 Subject: [PATCH 09/26] Inline Printer's advance_right method --- compiler/rustc_ast_pretty/src/pp.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 25437f8b53a94..785125ccafcc5 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -290,7 +290,8 @@ impl Printer { self.right = self.left; self.buf.truncate(1); } else { - self.advance_right(); + self.right += 1; + self.buf.advance_right(); } self.scan_push(BufEntry { token: Token::Begin(b), size: -self.right_total }); } @@ -299,7 +300,8 @@ impl Printer { if self.scan_stack.is_empty() { self.print_end(); } else { - self.advance_right(); + self.right += 1; + self.buf.advance_right(); self.scan_push(BufEntry { token: Token::End, size: -1 }); } } @@ -311,7 +313,8 @@ impl Printer { self.right = self.left; self.buf.truncate(1); } else { - self.advance_right(); + self.right += 1; + self.buf.advance_right(); } self.check_stack(0); self.scan_push(BufEntry { token: Token::Break(b), size: -self.right_total }); @@ -322,7 +325,8 @@ impl Printer { if self.scan_stack.is_empty() { self.print_string(s); } else { - self.advance_right(); + self.right += 1; + self.buf.advance_right(); let len = s.len() as isize; self.buf[self.right] = BufEntry { token: Token::String(s), size: len }; self.right_total += len; @@ -360,11 +364,6 @@ impl Printer { self.scan_stack.pop_back().unwrap() } - fn advance_right(&mut self) { - self.right += 1; - self.buf.advance_right(); - } - fn advance_left(&mut self) { let mut left_size = self.buf[self.left].size; From e219b2b5f90bf48bf56533c16745f2ce85216a2c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:04:12 -0800 Subject: [PATCH 10/26] Inline Printer's scan_push method --- compiler/rustc_ast_pretty/src/pp.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 785125ccafcc5..4f5140349c97b 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -293,7 +293,8 @@ impl Printer { self.right += 1; self.buf.advance_right(); } - self.scan_push(BufEntry { token: Token::Begin(b), size: -self.right_total }); + self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total }; + self.scan_stack.push_front(self.right); } fn scan_end(&mut self) { @@ -302,7 +303,8 @@ impl Printer { } else { self.right += 1; self.buf.advance_right(); - self.scan_push(BufEntry { token: Token::End, size: -1 }); + self.buf[self.right] = BufEntry { token: Token::End, size: -1 }; + self.scan_stack.push_front(self.right); } } @@ -317,7 +319,8 @@ impl Printer { self.buf.advance_right(); } self.check_stack(0); - self.scan_push(BufEntry { token: Token::Break(b), size: -self.right_total }); + self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total }; + self.scan_stack.push_front(self.right); self.right_total += b.blank_space; } @@ -347,11 +350,6 @@ impl Printer { } } - fn scan_push(&mut self, entry: BufEntry) { - self.buf[self.right] = entry; - self.scan_stack.push_front(self.right); - } - fn scan_pop(&mut self) -> usize { self.scan_stack.pop_front().unwrap() } From 50d722a6917eb150362ff05e4881e63b3e7adc62 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:07:12 -0800 Subject: [PATCH 11/26] Simplify ring buffer pushes --- compiler/rustc_ast_pretty/src/pp.rs | 11 ++++------- compiler/rustc_ast_pretty/src/pp/ring.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 4f5140349c97b..43a3c792d4ac9 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -288,12 +288,11 @@ impl Printer { self.left_total = 1; self.right_total = 1; self.right = self.left; - self.buf.truncate(1); + self.buf.clear(); } else { self.right += 1; - self.buf.advance_right(); } - self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total }; + self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); self.scan_stack.push_front(self.right); } @@ -302,8 +301,7 @@ impl Printer { self.print_end(); } else { self.right += 1; - self.buf.advance_right(); - self.buf[self.right] = BufEntry { token: Token::End, size: -1 }; + self.buf.push(BufEntry { token: Token::End, size: -1 }); self.scan_stack.push_front(self.right); } } @@ -329,9 +327,8 @@ impl Printer { self.print_string(s); } else { self.right += 1; - self.buf.advance_right(); let len = s.len() as isize; - self.buf[self.right] = BufEntry { token: Token::String(s), size: len }; + self.buf.push(BufEntry { token: Token::String(s), size: len }); self.right_total += len; self.check_stream(); } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 7e4e353ef1f8c..94bb10382f8af 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -22,6 +22,10 @@ impl RingBuffer { RingBuffer { data: VecDeque::new(), offset: 0 } } + pub fn push(&mut self, value: T) { + self.data.push_back(value); + } + pub fn advance_right(&mut self) where T: Default, @@ -34,6 +38,10 @@ impl RingBuffer { self.offset += 1; } + pub fn clear(&mut self) { + self.data.clear(); + } + pub fn truncate(&mut self, len: usize) { self.data.truncate(len); } From e20d5abdfb39f38ac9f4919b053029ffb6560641 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:15:02 -0800 Subject: [PATCH 12/26] Inline Printer's scan_pop method --- compiler/rustc_ast_pretty/src/pp.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 43a3c792d4ac9..96d446e363804 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -347,10 +347,6 @@ impl Printer { } } - fn scan_pop(&mut self) -> usize { - self.scan_stack.pop_front().unwrap() - } - fn scan_top(&self) -> usize { *self.scan_stack.front().unwrap() } @@ -396,19 +392,19 @@ impl Printer { match self.buf[x].token { Token::Begin(_) => { if k > 0 { - self.scan_pop(); + self.scan_stack.pop_front().unwrap(); self.buf[x].size += self.right_total; self.check_stack(k - 1); } } Token::End => { // paper says + not =, but that makes no sense. - self.scan_pop(); + self.scan_stack.pop_front().unwrap(); self.buf[x].size = 1; self.check_stack(k + 1); } _ => { - self.scan_pop(); + self.scan_stack.pop_front().unwrap(); self.buf[x].size += self.right_total; if k > 0 { self.check_stack(k); From 2a14275500b5c9cddfab83bb606368a9a4192ea4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:15:21 -0800 Subject: [PATCH 13/26] Inline Printer's scan_top method --- compiler/rustc_ast_pretty/src/pp.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 96d446e363804..4aeb4e4807b38 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -347,10 +347,6 @@ impl Printer { } } - fn scan_top(&self) -> usize { - *self.scan_stack.front().unwrap() - } - fn scan_pop_bottom(&mut self) -> usize { self.scan_stack.pop_back().unwrap() } @@ -388,7 +384,7 @@ impl Printer { fn check_stack(&mut self, k: usize) { if !self.scan_stack.is_empty() { - let x = self.scan_top(); + let x = *self.scan_stack.front().unwrap(); match self.buf[x].token { Token::Begin(_) => { if k > 0 { From ae28ec5a9c09b0ea163ccdff26b5650d4d0ea3c0 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:15:38 -0800 Subject: [PATCH 14/26] Inline Printer's scan_pop_bottom method --- compiler/rustc_ast_pretty/src/pp.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 4aeb4e4807b38..e5542eda5961b 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -337,7 +337,7 @@ impl Printer { fn check_stream(&mut self) { if self.right_total - self.left_total > self.space { if Some(&self.left) == self.scan_stack.back() { - let scanned = self.scan_pop_bottom(); + let scanned = self.scan_stack.pop_back().unwrap(); self.buf[scanned].size = SIZE_INFINITY; } self.advance_left(); @@ -347,10 +347,6 @@ impl Printer { } } - fn scan_pop_bottom(&mut self) -> usize { - self.scan_stack.pop_back().unwrap() - } - fn advance_left(&mut self) { let mut left_size = self.buf[self.left].size; From 80139a0f02ef04dc759f47ad59ece2138117c41c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:18:04 -0800 Subject: [PATCH 15/26] Ensure Printer buf is always indexed using self.left or self.right --- compiler/rustc_ast_pretty/src/pp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index e5542eda5961b..8d58eaa82a701 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -336,9 +336,9 @@ impl Printer { fn check_stream(&mut self) { if self.right_total - self.left_total > self.space { - if Some(&self.left) == self.scan_stack.back() { - let scanned = self.scan_stack.pop_back().unwrap(); - self.buf[scanned].size = SIZE_INFINITY; + if self.scan_stack.back() == Some(&self.left) { + self.scan_stack.pop_back().unwrap(); + self.buf[self.left].size = SIZE_INFINITY; } self.advance_left(); if self.left != self.right { From 947a09a4a88f97aa0ab59a5d21afd8ca3d429e60 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:18:47 -0800 Subject: [PATCH 16/26] Replace `if` + `unwrap` with `if let` in check_stack --- compiler/rustc_ast_pretty/src/pp.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 8d58eaa82a701..b2870ac2734fa 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -379,8 +379,7 @@ impl Printer { } fn check_stack(&mut self, k: usize) { - if !self.scan_stack.is_empty() { - let x = *self.scan_stack.front().unwrap(); + if let Some(&x) = self.scan_stack.front() { match self.buf[x].token { Token::Begin(_) => { if k > 0 { From 0490e43422b3061ba3afef9b064a9360871d1455 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:19:18 -0800 Subject: [PATCH 17/26] Implement check_stream nonrecursively --- compiler/rustc_ast_pretty/src/pp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index b2870ac2734fa..d32cc4694d3e5 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -335,14 +335,14 @@ impl Printer { } fn check_stream(&mut self) { - if self.right_total - self.left_total > self.space { + while self.right_total - self.left_total > self.space { if self.scan_stack.back() == Some(&self.left) { self.scan_stack.pop_back().unwrap(); self.buf[self.left].size = SIZE_INFINITY; } self.advance_left(); - if self.left != self.right { - self.check_stream(); + if self.left == self.right { + break; } } } From a37d272892172dfe598d9df182fa908d74359c6e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:20:33 -0800 Subject: [PATCH 18/26] Implement check_stack nonrecursively --- compiler/rustc_ast_pretty/src/pp.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index d32cc4694d3e5..dca0515a33033 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -378,27 +378,28 @@ impl Printer { } } - fn check_stack(&mut self, k: usize) { - if let Some(&x) = self.scan_stack.front() { + fn check_stack(&mut self, mut k: usize) { + while let Some(&x) = self.scan_stack.front() { match self.buf[x].token { Token::Begin(_) => { - if k > 0 { - self.scan_stack.pop_front().unwrap(); - self.buf[x].size += self.right_total; - self.check_stack(k - 1); + if k == 0 { + break; } + self.scan_stack.pop_front().unwrap(); + self.buf[x].size += self.right_total; + k -= 1; } Token::End => { // paper says + not =, but that makes no sense. self.scan_stack.pop_front().unwrap(); self.buf[x].size = 1; - self.check_stack(k + 1); + k += 1; } _ => { self.scan_stack.pop_front().unwrap(); self.buf[x].size += self.right_total; - if k > 0 { - self.check_stack(k); + if k == 0 { + break; } } } From 377c9dbabfe6574ff0161f0fd8e750bb57181530 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:21:18 -0800 Subject: [PATCH 19/26] Index a single time in check_stack --- compiler/rustc_ast_pretty/src/pp.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index dca0515a33033..d9832d560a841 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -380,24 +380,25 @@ impl Printer { fn check_stack(&mut self, mut k: usize) { while let Some(&x) = self.scan_stack.front() { - match self.buf[x].token { + let mut entry = &mut self.buf[x]; + match entry.token { Token::Begin(_) => { if k == 0 { break; } self.scan_stack.pop_front().unwrap(); - self.buf[x].size += self.right_total; + entry.size += self.right_total; k -= 1; } Token::End => { // paper says + not =, but that makes no sense. self.scan_stack.pop_front().unwrap(); - self.buf[x].size = 1; + entry.size = 1; k += 1; } _ => { self.scan_stack.pop_front().unwrap(); - self.buf[x].size += self.right_total; + entry.size += self.right_total; if k == 0 { break; } From fe5c4eab2dbc646f60f748b4b348acf5202ebf59 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:23:22 -0800 Subject: [PATCH 20/26] Eliminate a check_stack call on an empty scan stack --- compiler/rustc_ast_pretty/src/pp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index d9832d560a841..21e685fff4a91 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -315,8 +315,8 @@ impl Printer { } else { self.right += 1; self.buf.advance_right(); + self.check_stack(0); } - self.check_stack(0); self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total }; self.scan_stack.push_front(self.right); self.right_total += b.blank_space; From 6e8b06015e7f0cfa05208c2d83e0a9140f62ab7a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:24:17 -0800 Subject: [PATCH 21/26] Simplify the buffer push done by scan_break --- compiler/rustc_ast_pretty/src/pp.rs | 7 +++---- compiler/rustc_ast_pretty/src/pp/ring.rs | 4 ---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 21e685fff4a91..b9b2f77431ef4 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -311,13 +311,12 @@ impl Printer { self.left_total = 1; self.right_total = 1; self.right = self.left; - self.buf.truncate(1); + self.buf.clear(); } else { - self.right += 1; - self.buf.advance_right(); self.check_stack(0); + self.right += 1; } - self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total }; + self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); self.scan_stack.push_front(self.right); self.right_total += b.blank_space; } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 94bb10382f8af..62900ddfa2d46 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -41,10 +41,6 @@ impl RingBuffer { pub fn clear(&mut self) { self.data.clear(); } - - pub fn truncate(&mut self, len: usize) { - self.data.truncate(len); - } } impl Index for RingBuffer { From cc66a7ff20d0c8626661fe6f53daab265537425f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 19:59:36 -0800 Subject: [PATCH 22/26] Eliminate eof token state --- compiler/rustc_ast_pretty/src/pp.rs | 51 +++++++++---------- compiler/rustc_ast_pretty/src/pp/ring.rs | 15 +++--- compiler/rustc_ast_pretty/src/pprust/state.rs | 20 +++++--- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index b9b2f77431ef4..0c9d4ef0e950d 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -167,14 +167,9 @@ pub enum Token { Break(BreakToken), Begin(BeginToken), End, - Eof, } impl Token { - crate fn is_eof(&self) -> bool { - matches!(self, Token::Eof) - } - pub fn is_hardbreak_tok(&self) -> bool { matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY })) } @@ -187,7 +182,6 @@ impl fmt::Display for Token { Token::Break(_) => f.write_str("BREAK"), Token::Begin(_) => f.write_str("BEGIN"), Token::End => f.write_str("END"), - Token::Eof => f.write_str("EOF"), } } } @@ -233,6 +227,9 @@ pub struct Printer { print_stack: Vec, /// Buffered indentation to avoid writing trailing whitespace pending_indentation: isize, + /// The token most recently popped from the left boundary of the + /// ring-buffer for printing + last_printed: Option, } #[derive(Clone)] @@ -241,39 +238,36 @@ struct BufEntry { size: isize, } -impl Default for BufEntry { - fn default() -> Self { - BufEntry { token: Token::Eof, size: 0 } - } -} - impl Printer { pub fn new() -> Self { let linewidth = 78; - let mut buf = RingBuffer::new(); - buf.advance_right(); Printer { out: String::new(), margin: linewidth as isize, space: linewidth as isize, left: 0, right: 0, - buf, + buf: RingBuffer::new(), left_total: 0, right_total: 0, scan_stack: VecDeque::new(), print_stack: Vec::new(), pending_indentation: 0, + last_printed: None, } } - pub fn last_token(&self) -> Token { - self.buf[self.right].token.clone() + pub fn last_token(&self) -> Option<&Token> { + self.last_token_still_buffered().or_else(|| self.last_printed.as_ref()) + } + + pub fn last_token_still_buffered(&self) -> Option<&Token> { + self.buf.last().map(|last| &last.token) } /// Be very careful with this! - pub fn replace_last_token(&mut self, t: Token) { - self.buf[self.right].token = t; + pub fn replace_last_token_still_buffered(&mut self, t: Token) { + self.buf.last_mut().unwrap().token = t; } fn scan_eof(&mut self) { @@ -323,7 +317,7 @@ impl Printer { fn scan_string(&mut self, s: Cow<'static, str>) { if self.scan_stack.is_empty() { - self.print_string(s); + self.print_string(&s); } else { self.right += 1; let len = s.len() as isize; @@ -459,7 +453,7 @@ impl Printer { } } - fn print_string(&mut self, s: Cow<'static, str>) { + fn print_string(&mut self, s: &str) { let len = s.len() as isize; // assert!(len <= space); self.space -= len; @@ -473,21 +467,21 @@ impl Printer { self.out.reserve(self.pending_indentation as usize); self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize)); self.pending_indentation = 0; - self.out.push_str(&s); + self.out.push_str(s); } fn print(&mut self, token: Token, l: isize) { - match token { - Token::Begin(b) => self.print_begin(b, l), + match &token { + Token::Begin(b) => self.print_begin(*b, l), Token::End => self.print_end(), - Token::Break(b) => self.print_break(b, l), + Token::Break(b) => self.print_break(*b, l), Token::String(s) => { let len = s.len() as isize; assert_eq!(len, l); self.print_string(s); } - Token::Eof => panic!(), // Eof should never get here. } + self.last_printed = Some(token); } // Convenience functions to talk to the printer. @@ -542,7 +536,10 @@ impl Printer { } pub fn is_beginning_of_line(&self) -> bool { - self.last_token().is_eof() || self.last_token().is_hardbreak_tok() + match self.last_token() { + Some(last_token) => last_token.is_hardbreak_tok(), + None => true, + } } pub fn hardbreak_tok_offset(off: isize) -> Token { diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 62900ddfa2d46..86b87614c1986 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -26,13 +26,6 @@ impl RingBuffer { self.data.push_back(value); } - pub fn advance_right(&mut self) - where - T: Default, - { - self.data.push_back(T::default()); - } - pub fn advance_left(&mut self) { self.data.pop_front().unwrap(); self.offset += 1; @@ -41,6 +34,14 @@ impl RingBuffer { pub fn clear(&mut self) { self.data.clear(); } + + pub fn last(&self) -> Option<&T> { + self.data.back() + } + + pub fn last_mut(&mut self) -> Option<&mut T> { + self.data.back_mut() + } } impl Index for RingBuffer { diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 17941058ed6f0..044f6b228dc0b 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -329,9 +329,9 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere CommentStyle::BlankLine => { // We need to do at least one, possibly two hardbreaks. let twice = match self.last_token() { - pp::Token::String(s) => ";" == s, - pp::Token::Begin(_) => true, - pp::Token::End => true, + Some(pp::Token::String(s)) => ";" == s, + Some(pp::Token::Begin(_)) => true, + Some(pp::Token::End) => true, _ => false, }; if twice { @@ -687,11 +687,15 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { if !self.is_beginning_of_line() { self.break_offset(n, off) - } else if off != 0 && self.last_token().is_hardbreak_tok() { - // We do something pretty sketchy here: tuck the nonzero - // offset-adjustment we were going to deposit along with the - // break into the previous hardbreak. - self.replace_last_token(pp::Printer::hardbreak_tok_offset(off)); + } else if off != 0 { + if let Some(last_token) = self.last_token_still_buffered() { + if last_token.is_hardbreak_tok() { + // We do something pretty sketchy here: tuck the nonzero + // offset-adjustment we were going to deposit along with the + // break into the previous hardbreak. + self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off)); + } + } } } From 4d3faae5cd34cc3a34a7c01fb2f22c68e092e0eb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 18 Jan 2022 20:19:10 -0800 Subject: [PATCH 23/26] Eliminate left and right cursors in favor of ring buffer --- compiler/rustc_ast_pretty/src/pp.rs | 43 ++++++++---------------- compiler/rustc_ast_pretty/src/pp/ring.rs | 20 ++++++++++- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 0c9d4ef0e950d..bdd70148d85a0 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -206,10 +206,6 @@ pub struct Printer { margin: isize, /// Number of spaces left on line space: isize, - /// Index of left side of input stream - left: usize, - /// Index of right side of input stream - right: usize, /// Ring-buffer of tokens and calculated sizes buf: RingBuffer, /// Running size of stream "...left" @@ -245,8 +241,6 @@ impl Printer { out: String::new(), margin: linewidth as isize, space: linewidth as isize, - left: 0, - right: 0, buf: RingBuffer::new(), left_total: 0, right_total: 0, @@ -281,22 +275,18 @@ impl Printer { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; - self.right = self.left; self.buf.clear(); - } else { - self.right += 1; } - self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); + self.scan_stack.push_front(right); } fn scan_end(&mut self) { if self.scan_stack.is_empty() { self.print_end(); } else { - self.right += 1; - self.buf.push(BufEntry { token: Token::End, size: -1 }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::End, size: -1 }); + self.scan_stack.push_front(right); } } @@ -304,14 +294,12 @@ impl Printer { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; - self.right = self.left; self.buf.clear(); } else { self.check_stack(0); - self.right += 1; } - self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); - self.scan_stack.push_front(self.right); + let right = self.buf.push(BufEntry { token: Token::Break(b), size: -self.right_total }); + self.scan_stack.push_front(right); self.right_total += b.blank_space; } @@ -319,7 +307,6 @@ impl Printer { if self.scan_stack.is_empty() { self.print_string(&s); } else { - self.right += 1; let len = s.len() as isize; self.buf.push(BufEntry { token: Token::String(s), size: len }); self.right_total += len; @@ -329,22 +316,22 @@ impl Printer { fn check_stream(&mut self) { while self.right_total - self.left_total > self.space { - if self.scan_stack.back() == Some(&self.left) { + if *self.scan_stack.back().unwrap() == self.buf.index_of_first() { self.scan_stack.pop_back().unwrap(); - self.buf[self.left].size = SIZE_INFINITY; + self.buf.first_mut().unwrap().size = SIZE_INFINITY; } self.advance_left(); - if self.left == self.right { + if self.buf.is_empty() { break; } } } fn advance_left(&mut self) { - let mut left_size = self.buf[self.left].size; + let mut left_size = self.buf.first().unwrap().size; while left_size >= 0 { - let left = self.buf[self.left].token.clone(); + let left = self.buf.first().unwrap().token.clone(); let len = match left { Token::Break(b) => b.blank_space, @@ -360,14 +347,12 @@ impl Printer { self.left_total += len; - if self.left == self.right { + self.buf.advance_left(); + if self.buf.is_empty() { break; } - self.buf.advance_left(); - self.left += 1; - - left_size = self.buf[self.left].size; + left_size = self.buf.first().unwrap().size; } } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 86b87614c1986..d20142eb591fe 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -22,8 +22,14 @@ impl RingBuffer { RingBuffer { data: VecDeque::new(), offset: 0 } } - pub fn push(&mut self, value: T) { + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + pub fn push(&mut self, value: T) -> usize { + let index = self.offset + self.data.len(); self.data.push_back(value); + index } pub fn advance_left(&mut self) { @@ -35,6 +41,18 @@ impl RingBuffer { self.data.clear(); } + pub fn index_of_first(&self) -> usize { + self.offset + } + + pub fn first(&self) -> Option<&T> { + self.data.front() + } + + pub fn first_mut(&mut self) -> Option<&mut T> { + self.data.front_mut() + } + pub fn last(&self) -> Option<&T> { self.data.back() } From 48f5dcad104dd4dd58b8852cb6147abb4179b3b1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 17 Jan 2022 17:36:39 +0100 Subject: [PATCH 24/26] Move back templates into html folder --- Cargo.lock | 2 ++ src/librustdoc/Cargo.toml | 2 +- src/librustdoc/askama.toml | 2 ++ src/librustdoc/{ => html}/templates/STYLE.md | 0 src/librustdoc/{ => html}/templates/page.html | 0 src/librustdoc/{ => html}/templates/print_item.html | 0 6 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 src/librustdoc/askama.toml rename src/librustdoc/{ => html}/templates/STYLE.md (100%) rename src/librustdoc/{ => html}/templates/page.html (100%) rename src/librustdoc/{ => html}/templates/print_item.html (100%) diff --git a/Cargo.lock b/Cargo.lock index 73ffd3e044eb3..a7496e61b8beb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,7 +152,9 @@ dependencies = [ "nom", "proc-macro2", "quote", + "serde", "syn", + "toml", ] [[package]] diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index ccb844cd10b25..67d167e86df7f 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -8,7 +8,7 @@ path = "lib.rs" [dependencies] arrayvec = { version = "0.7", default-features = false } -askama = { version = "0.11", default-features = false } +askama = { version = "0.11", default-features = false, features = ["config"] } atty = "0.2" pulldown-cmark = { version = "0.9", default-features = false } minifier = "0.0.41" diff --git a/src/librustdoc/askama.toml b/src/librustdoc/askama.toml new file mode 100644 index 0000000000000..0c984f637ba29 --- /dev/null +++ b/src/librustdoc/askama.toml @@ -0,0 +1,2 @@ +[general] +dirs = ["html/templates"] diff --git a/src/librustdoc/templates/STYLE.md b/src/librustdoc/html/templates/STYLE.md similarity index 100% rename from src/librustdoc/templates/STYLE.md rename to src/librustdoc/html/templates/STYLE.md diff --git a/src/librustdoc/templates/page.html b/src/librustdoc/html/templates/page.html similarity index 100% rename from src/librustdoc/templates/page.html rename to src/librustdoc/html/templates/page.html diff --git a/src/librustdoc/templates/print_item.html b/src/librustdoc/html/templates/print_item.html similarity index 100% rename from src/librustdoc/templates/print_item.html rename to src/librustdoc/html/templates/print_item.html From 4bd571c4ffc3ce14a3584bd11e3f1f4883b2bff6 Mon Sep 17 00:00:00 2001 From: lcnr Date: Wed, 19 Jan 2022 10:33:23 +0100 Subject: [PATCH 25/26] remove `is_noop` --- compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs | 2 +- compiler/rustc_middle/src/ty/subst.rs | 4 ---- compiler/rustc_trait_selection/src/traits/specialize/mod.rs | 2 +- compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs | 4 ++-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index f4703b22ecbcf..55c9b4d9ba12d 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -90,7 +90,7 @@ macro call_intrinsic_match { match $intrinsic { $( sym::$name => { - assert!($substs.is_noop()); + assert!($substs.is_empty()); if let [$(ref $arg),*] = *$args { let ($($arg,)*) = ( $(codegen_operand($fx, $arg),)* diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index ab33fbcca15a3..63e9b58584c5f 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -275,10 +275,6 @@ impl<'a, 'tcx> InternalSubsts<'tcx> { } } - pub fn is_noop(&self) -> bool { - self.is_empty() - } - #[inline] pub fn types(&'a self) -> impl DoubleEndedIterator> + 'a { self.iter() diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index cd2e0f18e0cc0..195a4a4a653e1 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -484,7 +484,7 @@ crate fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option FnCtxt<'a, 'tcx> { // `foo.bar::(...)` -- the `Self` type here will be the // type of `foo` (possibly adjusted), but we don't want to // include that. We want just the `[_, u32]` part. - if !method.substs.is_noop() { + if !method.substs.is_empty() { let method_generics = self.tcx.generics_of(method.def_id); if !method_generics.params.is_empty() { let user_type_annotation = self.infcx.probe(|_| { @@ -211,7 +211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } pub fn write_substs(&self, node_id: hir::HirId, substs: SubstsRef<'tcx>) { - if !substs.is_noop() { + if !substs.is_empty() { debug!("write_substs({:?}, {:?}) in fcx {}", node_id, substs, self.tag()); self.typeck_results.borrow_mut().node_substs_mut().insert(node_id, substs); From c8e6889e08fd3a707134a3089840a01e615c1551 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 19 Jan 2022 09:41:04 -0800 Subject: [PATCH 26/26] Add assert that fallback targets must be available. --- src/tools/build-manifest/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index d3bce6b3e8df5..6b56d6bc4adf0 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -536,7 +536,10 @@ impl Builder { } for (substr, fallback_target) in fallback { if target_name.contains(substr) { - return Target::from_compressed_tar(self, &tarball_name!(fallback_target)); + let t = Target::from_compressed_tar(self, &tarball_name!(fallback_target)); + // Fallbacks must always be available. + assert!(t.available); + return t; } } Target::unavailable()