Skip to content

Commit

Permalink
Auto merge of #94079 - petrochenkov:cstr, r=joshtriplett
Browse files Browse the repository at this point in the history
library: Move `CStr` to libcore, and `CString` to liballoc

Closes #46736

Interesting points:
- Stability:
    - To make `CStr(ing)` from libcore/liballoc unusable without enabling features I had to make these structures unstable, and reexport them from libstd using stable type aliases instead of `pub use` reexports. (Because stability of `use` items is not checked.)
- Relying on target ABI in libcore is ok:
    - #94079 (comment)
- `trait CStrExt` (UPDATE: used only in `cfg(bootstrap)` mode, otherwise lang items are used instead)
    - #94079 (comment)
- `strlen`
    - #94079 (comment)

Otherwise it's just a code move + some minor hackery usual for liballoc in `cfg(test)` mode.
  • Loading branch information
bors committed Apr 15, 2022
2 parents 69a5ae3 + f62c84e commit 1e6fe58
Show file tree
Hide file tree
Showing 28 changed files with 940 additions and 695 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,9 @@ dependencies = [

[[package]]
name = "compiler_builtins"
version = "0.1.70"
version = "0.1.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80873f979f0a344a4ade87c2f70d9ccf5720b83b10c97ec7cd745895d021e85a"
checksum = "163437f05ca8f29d7e9128ea728dedf5eb620e445fbca273641d3a3050305f23"
dependencies = [
"cc",
"rustc-std-workspace-core",
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ language_item_table! {
Range, sym::Range, range_struct, Target::Struct, GenericRequirement::None;
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;

CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
}

pub enum GenericRequirement {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ impl<'a> CrateLoader<'a> {

let crate_metadata = CrateMetadata::new(
self.sess,
&self.cstore,
metadata,
crate_root,
raw_proc_macros,
Expand Down
24 changes: 15 additions & 9 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Decoding metadata from a single crate's metadata

use crate::creader::CrateMetadataRef;
use crate::creader::{CStore, CrateMetadataRef};
use crate::rmeta::table::{FixedSizeEncoding, Table};
use crate::rmeta::*;

Expand Down Expand Up @@ -1737,6 +1737,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
impl CrateMetadata {
crate fn new(
sess: &Session,
cstore: &CStore,
blob: MetadataBlob,
root: CrateRoot<'static>,
raw_proc_macros: Option<&'static [ProcMacro]>,
Expand All @@ -1752,11 +1753,6 @@ impl CrateMetadata {
.decode((&blob, sess))
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
.collect();
let incoherent_impls = root
.incoherent_impls
.decode((&blob, sess))
.map(|incoherent_impls| (incoherent_impls.self_ty, incoherent_impls.impls))
.collect();
let alloc_decoding_state =
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
let dependencies = Lock::new(cnum_map.iter().cloned().collect());
Expand All @@ -1765,11 +1761,11 @@ impl CrateMetadata {
// that does not copy any data. It just does some data verification.
let def_path_hash_map = root.def_path_hash_map.decode(&blob);

CrateMetadata {
let mut cdata = CrateMetadata {
blob,
root,
trait_impls,
incoherent_impls,
incoherent_impls: Default::default(),
raw_proc_macros,
source_map_import_info: OnceCell::new(),
def_path_hash_map,
Expand All @@ -1786,7 +1782,17 @@ impl CrateMetadata {
hygiene_context: Default::default(),
def_key_cache: Default::default(),
def_path_hash_cache: Default::default(),
}
};

// Need `CrateMetadataRef` to decode `DefId`s in simplified types.
cdata.incoherent_impls = cdata
.root
.incoherent_impls
.decode(CrateMetadataRef { cdata: &cdata, cstore })
.map(|incoherent_impls| (incoherent_impls.self_ty, incoherent_impls.impls))
.collect();

cdata
}

crate fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_typeck/src/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
self.assemble_inherent_impl_candidates_for_type(p.def_id());
}
ty::Adt(def, _) => {
self.assemble_inherent_impl_candidates_for_type(def.did());
let def_id = def.did();
self.assemble_inherent_impl_candidates_for_type(def_id);
if Some(def_id) == self.tcx.lang_items().c_str() {
self.assemble_inherent_candidates_for_incoherent_ty(raw_self_ty);
}
}
ty::Foreign(did) => {
self.assemble_inherent_impl_candidates_for_type(did);
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_typeck/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ impl<'tcx> ItemLikeVisitor<'_> for InherentCollect<'tcx> {
let self_ty = self.tcx.type_of(item.def_id);
match *self_ty.kind() {
ty::Adt(def, _) => {
self.check_def_id(item, def.did());
let def_id = def.did();
if !def_id.is_local() && Some(def_id) == self.tcx.lang_items().c_str() {
self.check_primitive_impl(item.def_id, self_ty, items, ty.span)
} else {
self.check_def_id(item, def_id);
}
}
ty::Foreign(did) => {
self.check_def_id(item, did);
Expand Down
Loading

0 comments on commit 1e6fe58

Please sign in to comment.