about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
AgeCommit message (Collapse)AuthorLines
2022-06-08Rename `rustc_serialize::opaque::Encoder` as `MemEncoder`.Nicholas Nethercote-5/+5
This avoids the name clash with `rustc_serialize::Encoder` (a trait), and allows lots qualifiers to be removed and imports to be simplified (e.g. fewer `as` imports).
2022-06-08Use delayed error handling for `Encodable` and `Encoder` infallible.Nicholas Nethercote-7/+6
There are two impls of the `Encoder` trait: `opaque::Encoder` and `opaque::FileEncoder`. The former encodes into memory and is infallible, the latter writes to file and is fallible. Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a bit verbose and has non-trivial cost, which is annoying given how rare failures are (especially in the infallible `opaque::Encoder` case). This commit changes how `Encoder` fallibility is handled. All the `emit_*` methods are now infallible. `opaque::Encoder` requires no great changes for this. `opaque::FileEncoder` now implements a delayed error handling strategy. If a failure occurs, it records this via the `res` field, and all subsequent encoding operations are skipped if `res` indicates an error has occurred. Once encoding is complete, the new `finish` method is called, which returns a `Result`. In other words, there is now a single `Result`-producing method instead of many of them. This has very little effect on how any file errors are reported if `opaque::FileEncoder` has any failures. Much of this commit is boring mechanical changes, removing `Result` return values and `?` or `unwrap` from expressions. The more interesting parts are as follows. - serialize.rs: The `Encoder` trait gains an `Ok` associated type. The `into_inner` method is changed into `finish`, which returns `Result<Vec<u8>, !>`. - opaque.rs: The `FileEncoder` adopts the delayed error handling strategy. Its `Ok` type is a `usize`, returning the number of bytes written, replacing previous uses of `FileEncoder::position`. - Various methods that take an encoder now consume it, rather than being passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-06Split `process_obligation` in two.Nicholas Nethercote-5/+14
Because it really has two halves: - A read-only part that checks if further work is needed. - The further work part, which is much less hot. This makes things a bit clearer and nicer.
2022-06-06Handle stalling within `ObligationForest`.Nicholas Nethercote-84/+62
It is simpler if `ObligationForest` does this itself, rather than the caller having to manage it.
2022-06-06Streamline `active_cache` to `done_cache` transfer.Nicholas Nethercote-8/+5
2022-06-04Fix stacked borrows invalidation in rustc_data_structures sip128Nilstrieb-1/+2
It creates the src pointer first, which is then invalidated by a unique borrow of the destination pointer. Swap the borrows around to fix this. Found with miri.
2022-06-04Adapt rustc_data_structures tests to run in strict miriNilstrieb-1/+10
Some tests took too long and owning_ref is fundamentally flawed, so don't run these tests or run them with a shorter N. This makes miri with `-Zmiri-strict-provenance` usable to find UB.
2022-05-31Tweak insert docsAriel Davis-2/+3
2022-05-30Fix typos in commentTobias Stoeckmann-2/+2
2022-05-27libcore: Add `iter::from_generator` which is like `iter::from_fn`, but for ↵Vadim Petrochenkov-25/+0
coroutines instead of functions
2022-05-07Auto merge of #96094 - Elliot-Roberts:fix_doctests, r=compiler-errorsbors-45/+28
Begin fixing all the broken doctests in `compiler/` Begins to fix #95994. All of them pass now but 24 of them I've marked with `ignore HELP (<explanation>)` (asking for help) as I'm unsure how to get them to work / if we should leave them as they are. There are also a few that I marked `ignore` that could maybe be made to work but seem less important. Each `ignore` has a rough "reason" for ignoring after it parentheses, with - `(pseudo-rust)` meaning "mostly rust-like but contains foreign syntax" - `(illustrative)` a somewhat catchall for either a fragment of rust that doesn't stand on its own (like a lone type), or abbreviated rust with ellipses and undeclared types that would get too cluttered if made compile-worthy. - `(not-rust)` stuff that isn't rust but benefits from the syntax highlighting, like MIR. - `(internal)` uses `rustc_*` code which would be difficult to make work with the testing setup. Those reason notes are a bit inconsistently applied and messy though. If that's important I can go through them again and try a more principled approach. When I run `rg '```ignore \(' .` on the repo, there look to be lots of different conventions other people have used for this sort of thing. I could try unifying them all if that would be helpful. I'm not sure if there was a better existing way to do this but I wrote my own script to help me run all the doctests and wade through the output. If that would be useful to anyone else, I put it here: https://github.com/Elliot-Roberts/rust_doctest_fixing_tool
2022-05-06Auto merge of #95454 - randomicon00:fix95444, r=wesleywiserbors-1/+20
Fixing #95444 by only displaying passes that take more than 5 millise… As discussed in #95444, I have added the code to test and only display prints that are greater than 5 milliseconds. r? `@jyn514`
2022-05-06Auto merge of #94598 - scottmcm:prefix-free-hasher-methods, r=Amanieubors-0/+20
Add a dedicated length-prefixing method to `Hasher` This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future. Fixes #94026 r? rust-lang/libs --- The core of this change is the following two new methods on `Hasher`: ```rust pub trait Hasher { /// Writes a length prefix into this hasher, as part of being prefix-free. /// /// If you're implementing [`Hash`] for a custom collection, call this before /// writing its contents to this `Hasher`. That way /// `(collection![1, 2, 3], collection![4, 5])` and /// `(collection![1, 2], collection![3, 4, 5])` will provide different /// sequences of values to the `Hasher` /// /// The `impl<T> Hash for [T]` includes a call to this method, so if you're /// hashing a slice (or array or vector) via its `Hash::hash` method, /// you should **not** call this yourself. /// /// This method is only for providing domain separation. If you want to /// hash a `usize` that represents part of the *data*, then it's important /// that you pass it to [`Hasher::write_usize`] instead of to this method. /// /// # Examples /// /// ``` /// #![feature(hasher_prefixfree_extras)] /// # // Stubs to make the `impl` below pass the compiler /// # struct MyCollection<T>(Option<T>); /// # impl<T> MyCollection<T> { /// # fn len(&self) -> usize { todo!() } /// # } /// # impl<'a, T> IntoIterator for &'a MyCollection<T> { /// # type Item = T; /// # type IntoIter = std::iter::Empty<T>; /// # fn into_iter(self) -> Self::IntoIter { todo!() } /// # } /// /// use std::hash::{Hash, Hasher}; /// impl<T: Hash> Hash for MyCollection<T> { /// fn hash<H: Hasher>(&self, state: &mut H) { /// state.write_length_prefix(self.len()); /// for elt in self { /// elt.hash(state); /// } /// } /// } /// ``` /// /// # Note to Implementers /// /// If you've decided that your `Hasher` is willing to be susceptible to /// Hash-DoS attacks, then you might consider skipping hashing some or all /// of the `len` provided in the name of increased performance. #[inline] #[unstable(feature = "hasher_prefixfree_extras", issue = "88888888")] fn write_length_prefix(&mut self, len: usize) { self.write_usize(len); } /// Writes a single `str` into this hasher. /// /// If you're implementing [`Hash`], you generally do not need to call this, /// as the `impl Hash for str` does, so you can just use that. /// /// This includes the domain separator for prefix-freedom, so you should /// **not** call `Self::write_length_prefix` before calling this. /// /// # Note to Implementers /// /// The default implementation of this method includes a call to /// [`Self::write_length_prefix`], so if your implementation of `Hasher` /// doesn't care about prefix-freedom and you've thus overridden /// that method to do nothing, there's no need to override this one. /// /// This method is available to be overridden separately from the others /// as `str` being UTF-8 means that it never contains `0xFF` bytes, which /// can be used to provide prefix-freedom cheaper than hashing a length. /// /// For example, if your `Hasher` works byte-by-byte (perhaps by accumulating /// them into a buffer), then you can hash the bytes of the `str` followed /// by a single `0xFF` byte. /// /// If your `Hasher` works in chunks, you can also do this by being careful /// about how you pad partial chunks. If the chunks are padded with `0x00` /// bytes then just hashing an extra `0xFF` byte doesn't necessarily /// provide prefix-freedom, as `"ab"` and `"ab\u{0}"` would likely hash /// the same sequence of chunks. But if you pad with `0xFF` bytes instead, /// ensuring at least one padding byte, then it can often provide /// prefix-freedom cheaper than hashing the length would. #[inline] #[unstable(feature = "hasher_prefixfree_extras", issue = "88888888")] fn write_str(&mut self, s: &str) { self.write_length_prefix(s.len()); self.write(s.as_bytes()); } } ``` With updates to the `Hash` implementations for slices and containers to call `write_length_prefix` instead of `write_usize`. `write_str` defaults to using `write_length_prefix` since, as was pointed out in the issue, the `write_u8(0xFF)` approach is insufficient for hashers that work in chunks, as those would hash `"a\u{0}"` and `"a"` to the same thing. But since `SipHash` works byte-wise (there's an internal buffer to accumulate bytes until a full chunk is available) it overrides `write_str` to continue to use the add-non-UTF-8-byte approach. --- Compatibility: Because the default implementation of `write_length_prefix` calls `write_usize`, the changed hash implementation for slices will do the same thing the old one did on existing `Hasher`s.
2022-05-06Add a dedicated length-prefixing method to `Hasher`Scott McMurray-0/+20
This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future.
2022-05-05Fixing #95444 by only displaying passes that take more than 5 millisecondsPeh-1/+20
95444: Adding passes that include memory increase Fix95444: Change the substraction with the abs_diff() method Fix95444: Change the substraction with abs_diff() method
2022-05-04Stabilize `bool::then_some`Josh Triplett-1/+0
2022-05-02fix most compiler/ doctestsElliot Roberts-45/+28
2022-04-19incr. comp.: Don't export impl_stable_hash_via_hash!() and warn about using it.Michael Woerister-3/+12
2022-04-16Auto merge of #95899 - petrochenkov:modchild2, r=cjgillotbors-0/+25
rustc_metadata: Do not encode unnecessary module children This should remove the syntax context shift and the special case for `ExternCrate` in decoder in https://github.com/rust-lang/rust/pull/95880. This PR also shifts some work from decoding to encoding, which is typically useful for performance (but probably not much in this case). r? `@cjgillot`
2022-04-16Rollup merge of #96058 - euclio:flock-impls, r=nagisaDylan DPC-215/+194
separate flock implementations into separate modules The main benefit of doing this is that rustfmt will now format each of these modules.
2022-04-16Auto merge of #95689 - lqd:self-profiler, r=wesleywiserbors-5/+93
Allow self-profiler to only record potentially costly arguments when argument recording is turned on As discussed [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/247081-t-compiler.2Fperformance/topic/Identifying.20proc-macro.20slowdowns/near/277304909) with `@wesleywiser,` I'd like to record proc-macro expansions in the self-profiler, with some detailed data (per-expansion spans for example, to follow #95473). At the same time, I'd also like to avoid doing expensive things when tracking a generic activity's arguments, if they were not specifically opted into the event filter mask, to allow the self-profiler to be used in hotter contexts. This PR tries to offer: - a way to ensure a closure to record arguments will only be called in that situation, so that potentially costly arguments can still be recorded when needed. With the additional requirement that, if possible, it would offer a way to record non-owned data without adding many `generic_activity_with_arg_{...}`-style methods. This lead to the `generic_activity_with_arg_recorder` single entry-point, and the closure parameter would offer the new methods, able to be executed in a context where costly argument could be created without disturbing the profiled piece of code. - some facilities/patterns allowing to record more rustc specific data in this situation, without making `rustc_data_structures` where the self-profiler is defined, depend on other rustc crates (causing circular dependencies): in particular, spans. They are quite tricky to turn into strings (if the default `Debug` impl output does not match the context one needs them for), and since I'd also like to avoid the allocation there when arg recording is turned off today, that has turned into another flexibility requirement for the API in this PR (separating the span-specific recording into an extension trait). **edit**: I've removed this from the PR so that it's easier to review, and opened https://github.com/rust-lang/rust/pull/95739. - allow for extensibility in the future: other ways to record arguments, or additional data attached to them could be added in the future (e.g. recording the argument's name as well as its data). Some areas where I'd love feedback: - the API and names: the `EventArgRecorder` and its method for example. As well as the verbosity that comes from the increased flexibility. - if I should convert the existing `generic_activity_with_arg{s}` to just forward to `generic_activity_with_arg_recorder` + `recorder.record_arg` (or remove them altogether ? Probably not): I've used the new API in the simple case I could find of allocating for an arg that may not be recorded, and the rest don't seem costly. - [x] whether this API should panic if no arguments were recorded by the user-provided closure (like this PR currently does: it seems like an error to use an API dedicated to record arguments but not call the methods to then do so) or if this should just record a generic activity without arguments ? - whether the `record_arg` function should be `#[inline(always)]`, like the `generic_activity_*` functions ? As mentioned, r? `@wesleywiser` following our recent discussion.
2022-04-16Rollup merge of #95372 - RalfJung:unaligned_references, r=oli-obkDylan DPC-1/+0
make unaligned_references lint deny-by-default This lint has been warn-by-default for a year now (since https://github.com/rust-lang/rust/pull/82525), so I think it is time to crank it up a bit. Code that triggers the lint causes UB (without `unsafe`) when executed, so we really don't want people to write code like this.
2022-04-14make unaligned_references lint deny-by-defaultRalf Jung-1/+0
2022-04-14separate flock implementations into separate modulesAndy Russell-215/+194
2022-04-13rustc_metadata: Do not encode unnecessary module childrenVadim Petrochenkov-0/+25
2022-04-12Remove NodeIdHashingMode.Camille GILLOT-7/+0
2022-04-09Auto merge of #95524 - oli-obk:cached_stable_hash_cleanups, r=nnethercotebors-0/+84
Cached stable hash cleanups r? `@nnethercote` Add a sanity assertion in debug mode to check that the cached hashes are actually the ones we get if we compute the hash each time. Add a new data structure that bundles all the hash-caching work to make it easier to re-use it for different interned data structures
2022-04-08Auto merge of #95519 - oli-obk:tait_ub2, r=compiler-errorsbors-1/+1
Enforce well formedness for type alias impl trait's hidden type fixes #84657 This was not an issue with return-position-impl-trait because the generic bounds of the function are the same as those of the opaque type, and the hidden type must already be well formed within the function. With type-alias-impl-trait the hidden type could be defined in a function that has *more* lifetime bounds than the type alias. This is fine, but the hidden type must still be well formed without those additional bounds.
2022-04-07add `generic_activity_with_arg_recorder` to the self-profilerRémy Rakic-0/+88
This allows profiling costly arguments to be recorded only when `-Zself-profile-events=args` is on: using a closure that takes an `EventArgRecorder` and call its `record_arg` or `record_args` methods.
2022-04-07turn `exec` comment into doc commentRémy Rakic-5/+5
2022-04-07Document and rename the new wrapper typeOli Scherer-10/+17
2022-04-06Fix some fallout around type alias impl trait in associated typesOli Scherer-1/+1
2022-04-03Auto merge of #92686 - saethlin:unsafe-debug-asserts, r=Amanieubors-4/+4
Add debug assertions to some unsafe functions As suggested by https://github.com/rust-lang/rust/issues/51713 ~~Some similar code calls `abort()` instead of `panic!()` but aborting doesn't work in a `const fn`, and the intrinsic for doing dispatch based on whether execution is in a const is unstable.~~ This picked up some invalid uses of `get_unchecked` in the compiler, and fixes them. I can confirm that they do in fact pick up invalid uses of `get_unchecked` in the wild, though the user experience is less-than-awesome: ``` Running unittests (target/x86_64-unknown-linux-gnu/debug/deps/rle_decode_fast-04b7918da2001b50) running 6 tests error: test failed, to rerun pass '--lib' Caused by: process didn't exit successfully: `/home/ben/rle-decode-helper/target/x86_64-unknown-linux-gnu/debug/deps/rle_decode_fast-04b7918da2001b50` (signal: 4, SIGILL: illegal instruction) ``` ~~As best I can tell these changes produce a 6% regression in the runtime of `./x.py test` when `[rust] debug = true` is set.~~ Latest commit (https://github.com/rust-lang/rust/pull/92686/commits/6894d559bdb4365243b3f4bf73f18e4b1bed04d1) brings the additional overhead from this PR down to 0.5%, while also adding a few more assertions. I think this actually covers all the places in `core` that it is reasonable to check for safety requirements at runtime. Thoughts?
2022-03-31Check that the cached stable hash is the right one if debug assertions are ↵Oli Scherer-4/+8
enabled
2022-03-31Move stable hash from TyS into a datastructure that can be shared with other ↵Oli Scherer-0/+73
interned types.
2022-03-30Auto merge of #95466 - Dylan-DPC:rollup-g7ddr8y, r=Dylan-DPCbors-2/+2
Rollup of 5 pull requests Successful merges: - #95294 (Document Linux kernel handoff in std::io::copy and std::fs::copy) - #95443 (Clarify how `src/tools/x` searches for python) - #95452 (fix since field version for termination stabilization) - #95460 (Spellchecking compiler code) - #95461 (Spellchecking some comments) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-03-30Spellchecking some commentsYuri Astrakhan-2/+2
This PR attempts to clean up some minor spelling mistakes in comments
2022-03-29Add debug assertions to some unsafe functionsBen Kimock-4/+4
These debug assertions are all implemented only at runtime using `const_eval_select`, and in the error path they execute `intrinsics::abort` instead of being a normal debug assertion to minimize the impact of these assertions on code size, when enabled. Of all these changes, the bounds checks for unchecked indexing are expected to be most impactful (case in point, they found a problem in rustc).
2022-03-28Revert "Auto merge of #93893 - oli-obk:sad_revert, r=oli-obk"Oli Scherer-1/+15
This reverts commit 6499c5e7fc173a3f55b7a3bd1e6a50e9edef782d, reversing changes made to 78450d2d602b06d9b94349aaf8cece1a4acaf3a8.
2022-03-08add `#[rustc_pass_by_value]` to more typeslcnr-70/+70
2022-03-07Clarify `Layout` interning.Nicholas Nethercote-0/+10
`Layout` is another type that is sometimes interned, sometimes not, and we always use references to refer to it so we can't take any advantage of the uniqueness properties for hashing or equality checks. This commit renames `Layout` as `LayoutS`, and then introduces a new `Layout` that is a newtype around an `Interned<LayoutS>`. It also interns more layouts than before. Previously layouts within layouts (via the `variants` field) were never interned, but now they are. Hence the lifetime on the new `Layout` type. Unlike other interned types, these ones are in `rustc_target` instead of `rustc_middle`. This reflects the existing structure of the code, which does layout-specific stuff in `rustc_target` while `TyAndLayout` is generic over the `Ty`, allowing the type-specific stuff to occur in `rustc_middle`. The commit also adds a `HashStable` impl for `Interned`, which was needed. It hashes the contents, unlike the `Hash` impl which hashes the pointer.
2022-03-07Introduce `ConstAllocation`.Nicholas Nethercote-6/+10
Currently some `Allocation`s are interned, some are not, and it's very hard to tell at a use point which is which. This commit introduces `ConstAllocation` for the known-interned ones, which makes the division much clearer. `ConstAllocation::inner()` is used to get the underlying `Allocation`. In some places it's natural to use an `Allocation`, in some it's natural to use a `ConstAllocation`, and in some places there's no clear choice. I've tried to make things look as nice as possible, while generally favouring `ConstAllocation`, which is the type that embodies more information. This does require quite a few calls to `inner()`. The commit also tweaks how `PartialOrd` works for `Interned`. The previous code was too clever by half, building on `T: Ord` to make the code shorter. That caused problems with deriving `PartialOrd` and `Ord` for `ConstAllocation`, so I changed it to build on `T: PartialOrd`, which is slightly more verbose but much more standard and avoided the problems.
2022-03-06Auto merge of #94579 - tmiasko:target-features, r=nagisabors-0/+90
Always include global target features in function attributes This ensures that information about target features configured with `-C target-feature=...` or detected with `-C target-cpu=native` is retained for subsequent consumers of LLVM bitcode. This is crucial for linker plugin LTO, since this information is not conveyed to the plugin otherwise. <details><summary>Additional test case demonstrating the issue</summary> ```rust extern crate core; #[inline] #[target_feature(enable = "aes")] unsafe fn f(a: u128, b: u128) -> u128 { use core::arch::x86_64::*; use core::mem::transmute; transmute(_mm_aesenc_si128(transmute(a), transmute(b))) } pub fn g(a: u128, b: u128) -> u128 { unsafe { f(a, b) } } fn main() { let mut args = std::env::args(); let _ = args.next().unwrap(); let a: u128 = args.next().unwrap().parse().unwrap(); let b: u128 = args.next().unwrap().parse().unwrap(); println!("{}", g(a, b)); } ``` ```console $ rustc --edition=2021 a.rs -Clinker-plugin-lto -Clink-arg=-fuse-ld=lld -Ctarget-feature=+aes -O ... = note: LLVM ERROR: Cannot select: intrinsic %llvm.x86.aesni.aesenc ``` </details> r? `@nagisa`
2022-03-04Add SmallStrTomasz Miąsko-0/+89
2022-03-04Inline SmallCStr::derefTomasz Miąsko-0/+1
2022-03-04Remove invalid #[cfg(tests)] in index_mapLoïc BRANSTETT-3/+0
2022-03-02Auto merge of #94514 - matthiaskrgr:rollup-pdzn82h, r=matthiaskrgrbors-1/+1
Rollup of 9 pull requests Successful merges: - #94464 (Suggest adding a new lifetime parameter when two elided lifetimes should match up for traits and impls.) - #94476 (7 - Make more use of `let_chains`) - #94478 (Fix panic when handling intra doc links generated from macro) - #94482 (compiler: fix some typos) - #94490 (Update books) - #94496 (tests: accept llvm intrinsic in align-checking test) - #94498 (9 - Make more use of `let_chains`) - #94503 (Provide C FFI types via core::ffi, not just in std) - #94513 (update Miri) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-03-01compiler: fix some typoscuishuang-1/+1
2022-03-01Querify `global_backend_features`Simonas Kazlauskas-0/+12
At the very least this serves to deduplicate the diagnostics that are output about unknown target features provided via CLI.
2022-02-27Auto merge of #94084 - Mark-Simulacrum:drop-sharded, r=cjgillotbors-1/+1
Avoid query cache sharding code in single-threaded mode In non-parallel compilers, this is just adding needless overhead at compilation time (since there is only one shard statically anyway). This amounts to roughly ~10 seconds reduction in bootstrap time, with overall neutral (some wins, some losses) performance results. Parallel compiler performance should be largely unaffected by this PR; sharding is kept there.