Skip to content

Commit a279826

Browse files
committed
Auto merge of #26267 - eefriedman:split-tyarray, r=eddyb
Arrays and slices are closely related, but not that closely; making the separation more explicit is generally more clear.
2 parents a9f1e29 + 33b7386 commit a279826

34 files changed

+138
-150
lines changed

src/librustc/metadata/tyencode.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
113113
ty::TyArray(t, sz) => {
114114
mywrite!(w, "V");
115115
enc_ty(w, cx, t);
116-
mywrite!(w, "/");
117-
match sz {
118-
Some(n) => mywrite!(w, "{}|", n),
119-
None => mywrite!(w, "|"),
120-
}
116+
mywrite!(w, "/{}|", sz);
117+
}
118+
ty::TySlice(t) => {
119+
mywrite!(w, "V");
120+
enc_ty(w, cx, t);
121+
mywrite!(w, "/|");
121122
}
122123
ty::TyStr => {
123124
mywrite!(w, "v");

src/librustc/middle/check_const.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
423423
self.visit_expr(&**element);
424424
// The count is checked elsewhere (typeck).
425425
let count = match node_ty.sty {
426-
ty::TyArray(_, Some(n)) => n,
426+
ty::TyArray(_, n) => n,
427427
_ => unreachable!()
428428
};
429429
// [element; 0] is always zero-sized.
@@ -851,10 +851,14 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'tcx> {
851851
}
852852
let mutbl = bk.to_mutbl_lossy();
853853
if mutbl == ast::MutMutable && self.mode == Mode::StaticMut {
854-
// Mutable slices are the only `&mut` allowed in globals,
855-
// but only in `static mut`, nowhere else.
854+
// Mutable slices are the only `&mut` allowed in
855+
// globals, but only in `static mut`, nowhere else.
856+
// FIXME: This exception is really weird... there isn't
857+
// any fundamental reason to restrict this based on
858+
// type of the expression. `&mut [1]` has exactly the
859+
// same representation as &mut 1.
856860
match cmt.ty.sty {
857-
ty::TyArray(_, _) => break,
861+
ty::TyArray(_, _) | ty::TySlice(_) => break,
858862
_ => {}
859863
}
860864
}

src/librustc/middle/check_match.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,14 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
537537

538538
ty::TyRef(_, ty::mt { ty, mutbl }) => {
539539
match ty.sty {
540-
ty::TyArray(_, Some(n)) => match ctor {
540+
ty::TyArray(_, n) => match ctor {
541541
&Single => {
542542
assert_eq!(pats_len, n);
543543
ast::PatVec(pats.collect(), None, vec!())
544544
},
545545
_ => unreachable!()
546546
},
547-
ty::TyArray(_, None) => match ctor {
547+
ty::TySlice(_) => match ctor {
548548
&Slice(n) => {
549549
assert_eq!(pats_len, n);
550550
ast::PatVec(pats.collect(), None, vec!())
@@ -560,7 +560,7 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
560560
}
561561
}
562562

563-
ty::TyArray(_, Some(len)) => {
563+
ty::TyArray(_, len) => {
564564
assert_eq!(pats_len, len);
565565
ast::PatVec(pats.collect(), None, vec![])
566566
}
@@ -601,7 +601,7 @@ fn all_constructors(cx: &MatchCheckCtxt, left_ty: Ty,
601601
[true, false].iter().map(|b| ConstantValue(const_bool(*b))).collect(),
602602

603603
ty::TyRef(_, ty::mt { ty, .. }) => match ty.sty {
604-
ty::TyArray(_, None) =>
604+
ty::TySlice(_) =>
605605
range_inclusive(0, max_slice_length).map(|length| Slice(length)).collect(),
606606
_ => vec!(Single)
607607
},
@@ -779,7 +779,7 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
779779
vec!(ConstantRange(eval_const_expr(cx.tcx, &**lo), eval_const_expr(cx.tcx, &**hi))),
780780
ast::PatVec(ref before, ref slice, ref after) =>
781781
match left_ty.sty {
782-
ty::TyArray(_, Some(_)) => vec!(Single),
782+
ty::TyArray(_, _) => vec!(Single),
783783
_ => if slice.is_some() {
784784
range_inclusive(before.len() + after.len(), max_slice_length)
785785
.map(|length| Slice(length))
@@ -807,7 +807,7 @@ pub fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> usi
807807
ty::TyTuple(ref fs) => fs.len(),
808808
ty::TyBox(_) => 1,
809809
ty::TyRef(_, ty::mt { ty, .. }) => match ty.sty {
810-
ty::TyArray(_, None) => match *ctor {
810+
ty::TySlice(_) => match *ctor {
811811
Slice(length) => length,
812812
ConstantValue(_) => 0,
813813
_ => unreachable!()
@@ -822,7 +822,7 @@ pub fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> usi
822822
}
823823
}
824824
ty::TyStruct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
825-
ty::TyArray(_, Some(n)) => n,
825+
ty::TyArray(_, n) => n,
826826
_ => 0
827827
}
828828
}

src/librustc/middle/fast_reject.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn simplify_type(tcx: &ty::ctxt,
5555
ty::TyFloat(float_type) => Some(FloatSimplifiedType(float_type)),
5656
ty::TyEnum(def_id, _) => Some(EnumSimplifiedType(def_id)),
5757
ty::TyStr => Some(StrSimplifiedType),
58-
ty::TyArray(..) => Some(VecSimplifiedType),
58+
ty::TyArray(..) | ty::TySlice(_) => Some(VecSimplifiedType),
5959
ty::TyRawPtr(_) => Some(PtrSimplifiedType),
6060
ty::TyTrait(ref trait_info) => {
6161
Some(TraitSimplifiedType(trait_info.principal_def_id()))

src/librustc/middle/implicator.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl<'a, 'tcx> Implicator<'a, 'tcx> {
117117
}
118118

119119
ty::TyArray(t, _) |
120+
ty::TySlice(t) |
120121
ty::TyRawPtr(ty::mt { ty: t, .. }) |
121122
ty::TyBox(t) => {
122123
self.accumulate_from_ty(t)

src/librustc/middle/infer/freshen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
159159
ty::TyStr |
160160
ty::TyError |
161161
ty::TyArray(..) |
162+
ty::TySlice(..) |
162163
ty::TyRawPtr(..) |
163164
ty::TyRef(..) |
164165
ty::TyBareFn(..) |

src/librustc/middle/mem_categorization.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn deref_kind(t: Ty, context: DerefKindContext) -> McResult<deref_kind> {
230230
Ok(deref_interior(InteriorField(PositionalField(0))))
231231
}
232232

233-
ty::TyArray(_, _) | ty::TyStr => {
233+
ty::TyArray(_, _) | ty::TySlice(_) | ty::TyStr => {
234234
// no deref of indexed content without supplying InteriorOffsetKind
235235
if let Some(context) = context {
236236
Ok(deref_interior(InteriorElement(context, element_kind(t))))
@@ -843,7 +843,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
843843
// Only promote `[T; 0]` before an RFC for rvalue promotions
844844
// is accepted.
845845
let qualif = match expr_ty.sty {
846-
ty::TyArray(_, Some(0)) => qualif,
846+
ty::TyArray(_, 0) => qualif,
847847
_ => check_const::ConstQualif::NOT_CONST
848848
};
849849

@@ -1130,7 +1130,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
11301130
-> (ast::Mutability, ty::Region) {
11311131
match slice_ty.sty {
11321132
ty::TyRef(r, ref mt) => match mt.ty.sty {
1133-
ty::TyArray(_, None) => (mt.mutbl, *r),
1133+
ty::TySlice(_) => (mt.mutbl, *r),
11341134
_ => vec_slice_info(tcx, pat, mt.ty),
11351135
},
11361136

@@ -1669,10 +1669,10 @@ fn element_kind(t: Ty) -> ElementKind {
16691669
match t.sty {
16701670
ty::TyRef(_, ty::mt{ty, ..}) |
16711671
ty::TyBox(ty) => match ty.sty {
1672-
ty::TyArray(_, None) => VecElement,
1672+
ty::TySlice(_) => VecElement,
16731673
_ => OtherElement
16741674
},
1675-
ty::TyArray(..) => VecElement,
1675+
ty::TyArray(..) | ty::TySlice(_) => VecElement,
16761676
_ => OtherElement
16771677
}
16781678
}

src/librustc/middle/traits/coherence.rs

+1
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ fn ty_is_local_constructor<'tcx>(tcx: &ty::ctxt<'tcx>,
306306
ty::TyStr(..) |
307307
ty::TyBareFn(..) |
308308
ty::TyArray(..) |
309+
ty::TySlice(..) |
309310
ty::TyRawPtr(..) |
310311
ty::TyRef(..) |
311312
ty::TyTuple(..) |

src/librustc/middle/traits/select.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14291429
}
14301430

14311431
// [T; n] -> [T].
1432-
(&ty::TyArray(_, Some(_)), &ty::TyArray(_, None)) => true,
1432+
(&ty::TyArray(_, _), &ty::TySlice(_)) => true,
14331433

14341434
// Struct<T> -> Struct<U>.
14351435
(&ty::TyStruct(def_id_a, _), &ty::TyStruct(def_id_b, _)) => {
@@ -1662,35 +1662,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16621662
}
16631663
}
16641664

1665-
ty::TyArray(element_ty, ref len) => {
1666-
// [T; n] and [T]
1665+
ty::TyArray(element_ty, _) => {
1666+
// [T; n]
16671667
match bound {
1668-
ty::BoundCopy => {
1669-
match *len {
1670-
// [T; n] is copy iff T is copy
1671-
Some(_) => ok_if(vec![element_ty]),
1672-
1673-
// [T] is unsized and hence affine
1674-
None => Err(Unimplemented),
1675-
}
1676-
}
1677-
1678-
ty::BoundSized => {
1679-
if len.is_some() {
1680-
ok_if(Vec::new())
1681-
} else {
1682-
Err(Unimplemented)
1683-
}
1684-
}
1685-
1668+
ty::BoundCopy => ok_if(vec![element_ty]),
1669+
ty::BoundSized => ok_if(Vec::new()),
16861670
ty::BoundSync | ty::BoundSend => {
16871671
self.tcx().sess.bug("Send/Sync shouldn't occur in builtin_bounds()");
16881672
}
16891673
}
16901674
}
16911675

1692-
ty::TyStr => {
1693-
// Equivalent to [u8]
1676+
ty::TyStr | ty::TySlice(_) => {
16941677
match bound {
16951678
ty::BoundSync | ty::BoundSend => {
16961679
self.tcx().sess.bug("Send/Sync shouldn't occur in builtin_bounds()");
@@ -1855,7 +1838,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
18551838
Some(vec![element_ty])
18561839
},
18571840

1858-
ty::TyArray(element_ty, _) => {
1841+
ty::TyArray(element_ty, _) | ty::TySlice(element_ty) => {
18591842
Some(vec![element_ty])
18601843
}
18611844

@@ -2510,7 +2493,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
25102493
}
25112494

25122495
// [T; n] -> [T].
2513-
(&ty::TyArray(a, Some(_)), &ty::TyArray(b, None)) => {
2496+
(&ty::TyArray(a, _), &ty::TySlice(b)) => {
25142497
let origin = infer::Misc(obligation.cause.span);
25152498
if self.infcx.sub_types(false, origin, a, b).is_err() {
25162499
return Err(Unimplemented);

0 commit comments

Comments
 (0)