1
Fork 0

Auto merge of #109971 - WaffleLapkin:yeet_ownership, r=Nilstrieb

Yeet `owning_ref`

Based on the discussions from https://github.com/rust-lang/rust/pull/109948

This replaces `owning_ref` with a far simpler & safer abstraction.

Fixes #109974
This commit is contained in:
bors 2023-04-08 01:08:26 +00:00
commit 0f0dc29264
13 changed files with 227 additions and 2012 deletions

View file

@ -22,8 +22,6 @@ extern crate proc_macro;
extern crate rustc_macros;
#[macro_use]
extern crate rustc_middle;
#[macro_use]
extern crate rustc_data_structures;
#[macro_use]
extern crate tracing;

View file

@ -218,7 +218,7 @@ use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::owned_slice::slice_owned;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_errors::{DiagnosticArgValue, FatalError, IntoDiagnosticArg};
@ -236,6 +236,7 @@ use rustc_target::spec::{Target, TargetTriple};
use snap::read::FrameDecoder;
use std::borrow::Cow;
use std::io::{Read, Result as IoResult, Write};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::{cmp, fmt};
@ -814,15 +815,14 @@ fn get_metadata_section<'p>(
// Assume the decompressed data will be at least the size of the compressed data, so we
// don't have to grow the buffer as much.
let mut inflated = Vec::with_capacity(compressed_bytes.len());
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
Err(_) => {
return Err(MetadataError::LoadFailure(format!(
"failed to decompress metadata: {}",
filename.display()
)));
}
}
FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated).map_err(|_| {
MetadataError::LoadFailure(format!(
"failed to decompress metadata: {}",
filename.display()
))
})?;
slice_owned(inflated, Deref::deref)
}
CrateFlavor::Rmeta => {
// mmap the file, because only a small fraction of it is read.
@ -840,7 +840,7 @@ fn get_metadata_section<'p>(
))
})?;
rustc_erase_owner!(OwningRef::new(mmap).map_owner_box())
slice_owned(mmap, Deref::deref)
}
};
let blob = MetadataBlob::new(raw_bytes);

View file

@ -51,12 +51,6 @@ mod cstore_impl;
#[derive(Clone)]
pub(crate) struct MetadataBlob(Lrc<MetadataRef>);
// This is needed so we can create an OwningRef into the blob.
// The data behind a `MetadataBlob` has a stable address because it is
// contained within an Rc/Arc.
unsafe impl rustc_data_structures::owning_ref::StableAddress for MetadataBlob {}
// This is needed so we can create an OwningRef into the blob.
impl std::ops::Deref for MetadataBlob {
type Target = [u8];

View file

@ -1,14 +1,14 @@
use crate::rmeta::DecodeContext;
use crate::rmeta::EncodeContext;
use crate::rmeta::MetadataBlob;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::owned_slice::slice_owned;
use rustc_data_structures::owned_slice::OwnedSlice;
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
use rustc_middle::parameterized_over_tcx;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::def_id::{DefIndex, DefPathHash};
pub(crate) enum DefPathHashMapRef<'tcx> {
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwningRef<MetadataBlob, [u8]>>),
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice>),
BorrowedFromTcx(&'tcx DefPathHashMap),
}
@ -50,11 +50,11 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static>
let len = d.read_usize();
let pos = d.position();
let o = OwningRef::new(d.blob().clone()).map(|x| &x[pos..pos + len]);
let o = slice_owned(d.blob().clone(), |blob| &blob[pos..pos + len]);
// Although we already have the data we need via the OwningRef, we still need
// to advance the DecodeContext's position so it's in a valid state after
// the method. We use read_raw_bytes() for that.
// Although we already have the data we need via the `OwnedSlice`, we still need
// to advance the `DecodeContext`'s position so it's in a valid state after
// the method. We use `read_raw_bytes()` for that.
let _ = d.read_raw_bytes(len);
let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {