Skip to content

Commit 137c171

Browse files
committed
Move str to libcore.
1 parent a35355c commit 137c171

File tree

96 files changed

+314
-278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+314
-278
lines changed

src/libcore/str/mod.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ pub mod pattern;
2626
#[allow(missing_docs)]
2727
pub mod lossy;
2828

29-
// HACK(eddyb) private and not named `str` to avoid changing name resolution.
30-
#[cfg(not(bootstrap))]
31-
#[lang = "str"]
32-
struct Str([u8]);
29+
mod sealed {
30+
// HACK(eddyb) sealed away and not named `str` to avoid changing name resolution.
31+
// Only `pub` to avoid the privacy checker producing errors.
32+
#[cfg(not(bootstrap))]
33+
#[lang = "str"]
34+
#[stable(feature = "rust1", since = "1.0.0")]
35+
pub struct Str([u8]);
36+
37+
#[unstable(feature = "structural_match", issue = "31434")]
38+
impl crate::marker::StructuralPartialEq for str {}
39+
40+
#[unstable(feature = "structural_match", issue = "31434")]
41+
impl crate::marker::StructuralEq for str {}
42+
}
3343

3444
/// Parse a value from a string
3545
///

src/librustc_codegen_llvm/debuginfo/metadata.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,9 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp
573573

574574
let ptr_metadata = |ty: Ty<'tcx>| match ty.kind {
575575
ty::Slice(typ) => Ok(vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span)),
576-
ty::Str => Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span)),
576+
ty::Adt(def, _) if def.is_str() => {
577+
Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span))
578+
}
577579
ty::Dynamic(..) => Ok(MetadataCreationResult::new(
578580
trait_pointer_metadata(cx, ty, Some(t), unique_type_id),
579581
false,
@@ -601,7 +603,9 @@ pub fn type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>, usage_site_span: Sp
601603
ty::Array(typ, _) | ty::Slice(typ) => {
602604
fixed_vec_metadata(cx, unique_type_id, t, typ, usage_site_span)
603605
}
604-
ty::Str => fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span),
606+
ty::Adt(def, _) if def.is_str() => {
607+
fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span)
608+
}
605609
ty::Dynamic(..) => {
606610
MetadataCreationResult::new(trait_pointer_metadata(cx, t, None, unique_type_id), false)
607611
}

src/librustc_codegen_llvm/type_of.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ fn uncached_llvm_type<'a, 'tcx>(
5858
// FIXME(eddyb) producing readable type names for trait objects can result
5959
// in problematically distinct types due to HRTB and subtyping (see #47638).
6060
// ty::Dynamic(..) |
61-
ty::Foreign(..) |
62-
ty::Str => {
61+
ty::Foreign(..) => {
6362
let mut name = String::with_capacity(32);
6463
let printer = DefPathBasedNames::new(cx.tcx, true, true);
6564
printer.push_type_name(layout.ty, &mut name, false);

src/librustc_codegen_ssa/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn push_debuginfo_type_name<'tcx>(
3636
match t.kind {
3737
ty::Bool => output.push_str("bool"),
3838
ty::Char => output.push_str("char"),
39-
ty::Str => output.push_str("str"),
39+
ty::Adt(def, _) if def.is_str() => output.push_str("str"),
4040
ty::Never => output.push_str("!"),
4141
ty::Int(int_ty) => output.push_str(int_ty.name_str()),
4242
ty::Uint(uint_ty) => output.push_str(uint_ty.name_str()),

src/librustc_codegen_ssa/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2525
let vtable = info.unwrap();
2626
(meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable))
2727
}
28-
ty::Slice(_) | ty::Str => {
28+
ty::Slice(_) => {
2929
let unit = layout.field(bx, 0);
3030
// The info in this case is the length of the str, so the size is that
3131
// times the unit size.

src/librustc_codegen_ssa/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
125125
return simple();
126126
}
127127
_ if !field.is_unsized() => return simple(),
128-
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
128+
ty::Slice(..) | ty::Foreign(..) => return simple(),
129129
ty::Adt(def, _) => {
130130
if def.repr.packed() {
131131
// FIXME(eddyb) generalize the adjustment when we

src/librustc_codegen_ssa/traits/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> {
8686
let tail = self.tcx().struct_tail_erasing_lifetimes(ty, param_env);
8787
match tail.kind {
8888
ty::Foreign(..) => false,
89-
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
89+
ty::Slice(..) | ty::Dynamic(..) => true,
9090
_ => bug!("unexpected unsized tail: {:?}", tail),
9191
}
9292
}

src/librustc_infer/infer/canonical/canonicalizer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
403403
| ty::Uint(..)
404404
| ty::Float(..)
405405
| ty::Adt(..)
406-
| ty::Str
407406
| ty::Error
408407
| ty::Array(..)
409408
| ty::Slice(..)

src/librustc_infer/infer/freshen.rs

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
194194
| ty::Uint(..)
195195
| ty::Float(..)
196196
| ty::Adt(..)
197-
| ty::Str
198197
| ty::Error
199198
| ty::Array(..)
200199
| ty::Slice(..)

src/librustc_lint/types.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
617617
}
618618

619619
match ty.kind {
620+
ty::Adt(def, _) if def.is_str() => FfiUnsafe {
621+
ty,
622+
reason: "string slices have no C equivalent",
623+
help: Some("consider using `*const u8` and a length instead"),
624+
},
625+
620626
ty::Adt(def, substs) => {
621627
if def.is_phantom_data() {
622628
return FfiPhantom(ty);
@@ -822,12 +828,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
822828
FfiUnsafe { ty, reason: "trait objects have no C equivalent", help: None }
823829
}
824830

825-
ty::Str => FfiUnsafe {
826-
ty,
827-
reason: "string slices have no C equivalent",
828-
help: Some("consider using `*const u8` and a length instead"),
829-
},
830-
831831
ty::Tuple(..) => FfiUnsafe {
832832
ty,
833833
reason: "tuples have unspecified layout",

src/librustc_middle/traits/query.rs

-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
219219
| ty::GeneratorWitness(..)
220220
| ty::RawPtr(_)
221221
| ty::Ref(..)
222-
| ty::Str
223222
| ty::Foreign(..)
224223
| ty::Error => true,
225224

src/librustc_middle/ty/context.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ macro_rules! sty_debug_print {
18381838
for &Interned(t) in types {
18391839
let variant = match t.kind {
18401840
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
1841-
ty::Float(..) | ty::Str | ty::Never => continue,
1841+
ty::Float(..) | ty::Never => continue,
18421842
ty::Error => /* unimportant */ continue,
18431843
$(ty::$variant(..) => &mut $variant,)*
18441844
};
@@ -2165,7 +2165,9 @@ impl<'tcx> TyCtxt<'tcx> {
21652165

21662166
#[inline]
21672167
pub fn mk_str(self) -> Ty<'tcx> {
2168-
self.mk_ty(Str)
2168+
let def_id = self.require_lang_item(lang_items::StrItem, None);
2169+
let adt_def = self.adt_def(def_id);
2170+
self.mk_ty(Adt(adt_def, InternalSubsts::empty()))
21692171
}
21702172

21712173
#[inline]

src/librustc_middle/ty/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ impl<'tcx> TyS<'tcx> {
1010
match self.kind {
1111
Bool
1212
| Char
13-
| Str
1413
| Int(_)
1514
| Uint(_)
1615
| Float(_)
1716
| Infer(InferTy::IntVar(_))
1817
| Infer(InferTy::FloatVar(_))
1918
| Infer(InferTy::FreshIntTy(_))
2019
| Infer(InferTy::FreshFloatTy(_)) => true,
20+
Adt(def, _) if def.is_str() => true,
2121
_ => false,
2222
}
2323
}
@@ -28,14 +28,14 @@ impl<'tcx> TyS<'tcx> {
2828
match self.kind {
2929
Bool
3030
| Char
31-
| Str
3231
| Int(_)
3332
| Uint(_)
3433
| Float(_)
3534
| Infer(InferTy::IntVar(_))
3635
| Infer(InferTy::FloatVar(_))
3736
| Infer(InferTy::FreshIntTy(_))
3837
| Infer(InferTy::FreshFloatTy(_)) => true,
38+
Adt(def, _) if def.is_str() => true,
3939
Ref(_, x, _) | Array(x, _) | Slice(x) => x.peel_refs().is_simple_ty(),
4040
Tuple(tys) if tys.is_empty() => true,
4141
_ => false,

src/librustc_middle/ty/error.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,10 @@ impl<'tcx> TypeError<'tcx> {
216216
impl<'tcx> ty::TyS<'tcx> {
217217
pub fn sort_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
218218
match self.kind {
219-
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Str | ty::Never => {
219+
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
220220
format!("`{}`", self).into()
221221
}
222+
ty::Adt(def, _) if def.is_str() => "`str`".into(),
222223
ty::Tuple(ref tys) if tys.is_empty() => format!("`{}`", self).into(),
223224

224225
ty::Adt(def, _) => format!("{} `{}`", def.descr(), tcx.def_path_str(def.did)).into(),
@@ -288,8 +289,8 @@ impl<'tcx> ty::TyS<'tcx> {
288289
| ty::Int(_)
289290
| ty::Uint(_)
290291
| ty::Float(_)
291-
| ty::Str
292292
| ty::Never => "type".into(),
293+
ty::Adt(def, _) if def.is_str() => "type".into(),
293294
ty::Tuple(ref tys) if tys.is_empty() => "unit type".into(),
294295
ty::Adt(def, _) => def.descr().into(),
295296
ty::Foreign(_) => "extern type".into(),

src/librustc_middle/ty/fast_reject.rs

-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ where
2828
UintSimplifiedType(ast::UintTy),
2929
FloatSimplifiedType(ast::FloatTy),
3030
AdtSimplifiedType(D),
31-
StrSimplifiedType,
3231
ArraySimplifiedType,
3332
PtrSimplifiedType,
3433
NeverSimplifiedType,
@@ -67,7 +66,6 @@ pub fn simplify_type(
6766
ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)),
6867
ty::Float(float_type) => Some(FloatSimplifiedType(float_type)),
6968
ty::Adt(def, _) => Some(AdtSimplifiedType(def.did)),
70-
ty::Str => Some(StrSimplifiedType),
7169
ty::Array(..) | ty::Slice(_) => Some(ArraySimplifiedType),
7270
ty::RawPtr(_) => Some(PtrSimplifiedType),
7371
ty::Dynamic(ref trait_info, ..) => match trait_info.principal_def_id() {
@@ -122,7 +120,6 @@ impl<D: Copy + Debug + Ord + Eq> SimplifiedTypeGen<D> {
122120
UintSimplifiedType(t) => UintSimplifiedType(t),
123121
FloatSimplifiedType(t) => FloatSimplifiedType(t),
124122
AdtSimplifiedType(d) => AdtSimplifiedType(map(d)),
125-
StrSimplifiedType => StrSimplifiedType,
126123
ArraySimplifiedType => ArraySimplifiedType,
127124
PtrSimplifiedType => PtrSimplifiedType,
128125
NeverSimplifiedType => NeverSimplifiedType,
@@ -149,7 +146,6 @@ where
149146
match *self {
150147
BoolSimplifiedType
151148
| CharSimplifiedType
152-
| StrSimplifiedType
153149
| ArraySimplifiedType
154150
| PtrSimplifiedType
155151
| NeverSimplifiedType

src/librustc_middle/ty/flags.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ impl FlagComputation {
6767
| &ty::Float(_)
6868
| &ty::Uint(_)
6969
| &ty::Never
70-
| &ty::Str
7170
| &ty::Foreign(..) => {}
7271

7372
// You might think that we could just return Error for

src/librustc_middle/ty/layout.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
542542
ty::Foreign(..) => {
543543
return Ok(tcx.intern_layout(Layout::scalar(self, data_ptr)));
544544
}
545-
ty::Slice(_) | ty::Str => scalar_unit(Int(dl.ptr_sized_integer(), false)),
545+
ty::Slice(_) => scalar_unit(Int(dl.ptr_sized_integer(), false)),
546546
ty::Dynamic(..) => {
547547
let mut vtable = scalar_unit(Pointer);
548548
vtable.valid_range = 1..=*vtable.valid_range.end();
@@ -597,14 +597,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
597597
size: Size::ZERO,
598598
})
599599
}
600-
ty::Str => tcx.intern_layout(Layout {
601-
variants: Variants::Single { index: VariantIdx::new(0) },
602-
fields: FieldsShape::Array { stride: Size::from_bytes(1), count: 0 },
603-
abi: Abi::Aggregate { sized: false },
604-
largest_niche: None,
605-
align: dl.i8_align,
606-
size: Size::ZERO,
607-
}),
608600

609601
// Odd unit types.
610602
ty::FnDef(..) => univariant(&[], &ReprOptions::default(), StructKind::AlwaysSized)?,
@@ -2069,7 +2061,7 @@ where
20692061
}
20702062

20712063
match tcx.struct_tail_erasing_lifetimes(pointee, cx.param_env()).kind {
2072-
ty::Slice(_) | ty::Str => tcx.types.usize,
2064+
ty::Slice(_) => tcx.types.usize,
20732065
ty::Dynamic(_, _) => {
20742066
tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_array(tcx.types.usize, 3))
20752067
/* FIXME: use actual fn pointers
@@ -2092,7 +2084,6 @@ where
20922084

20932085
// Arrays and slices.
20942086
ty::Array(element, _) | ty::Slice(element) => element,
2095-
ty::Str => tcx.types.u8,
20962087

20972088
// Tuples, generators and closures.
20982089
ty::Closure(_, ref substs) => substs.as_closure().upvar_tys().nth(i).unwrap(),

src/librustc_middle/ty/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,8 @@ bitflags! {
18641864
/// Indicates whether the variant list of this ADT is `#[non_exhaustive]`.
18651865
/// (i.e., this flag is never set unless this ADT is an enum).
18661866
const IS_VARIANT_LIST_NON_EXHAUSTIVE = 1 << 10;
1867+
/// Indicates whether the type is `str`.
1868+
const IS_STR = 1 << 11;
18671869
}
18681870
}
18691871

@@ -2254,6 +2256,9 @@ impl<'tcx> AdtDef {
22542256
if Some(did) == tcx.lang_items().rc() {
22552257
flags |= AdtFlags::IS_RC;
22562258
}
2259+
if Some(did) == tcx.lang_items().str_type() {
2260+
flags |= AdtFlags::IS_STR;
2261+
}
22572262

22582263
AdtDef { did, variants, flags, repr }
22592264
}
@@ -2354,6 +2359,12 @@ impl<'tcx> AdtDef {
23542359
self.flags.contains(AdtFlags::IS_MANUALLY_DROP)
23552360
}
23562361

2362+
/// Returns `true` if this is `str`.
2363+
#[inline]
2364+
pub fn is_str(&self) -> bool {
2365+
self.flags.contains(AdtFlags::IS_STR)
2366+
}
2367+
23572368
/// Returns `true` if this type has a destructor.
23582369
pub fn has_dtor(&self, tcx: TyCtxt<'tcx>) -> bool {
23592370
self.destructor(tcx).is_some()

src/librustc_middle/ty/outlives.rs

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ fn compute_components(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, out: &mut SmallVec<[Compo
138138
ty::Adt(..) | // OutlivesNominalType
139139
ty::Opaque(..) | // OutlivesNominalType (ish)
140140
ty::Foreign(..) | // OutlivesNominalType
141-
ty::Str | // OutlivesScalar (ish)
142141
ty::Array(..) | // ...
143142
ty::Slice(..) | // ...
144143
ty::RawPtr(..) | // ...

src/librustc_middle/ty/print/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
290290
| ty::Char
291291
| ty::Int(_)
292292
| ty::Uint(_)
293-
| ty::Str
294293
| ty::FnPtr(_)
295294
| ty::Projection(_)
296295
| ty::Placeholder(..)

src/librustc_middle/ty/print/obsolete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl DefPathBasedNames<'tcx> {
3636
match t.kind {
3737
ty::Bool => output.push_str("bool"),
3838
ty::Char => output.push_str("char"),
39-
ty::Str => output.push_str("str"),
39+
ty::Adt(def, _) if def.is_str() => output.push_str("str"),
4040
ty::Never => output.push_str("!"),
4141
ty::Int(ty) => output.push_str(ty.name_str()),
4242
ty::Uint(ty) => output.push_str(ty.name_str()),

0 commit comments

Comments
 (0)