Skip to content

Commit c0ff3ca

Browse files
committed
Refactor ty_str to use a ~(str) representation.
Similar to my recent changes to ~[T]/&[T], these changes remove the vstore abstraction and represent str types as ~(str) and &(str). The Option<uint> in ty_str is the length of the string, None if the string is dynamically sized.
1 parent 7a19a82 commit c0ff3ca

29 files changed

+319
-509
lines changed

src/librustc/metadata/tydecode.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,6 @@ pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx:
138138
parse_substs(&mut st, conv)
139139
}
140140

141-
fn parse_vstore(st: &mut PState, conv: conv_did) -> ty::Vstore {
142-
assert_eq!(next(st), '/');
143-
144-
let c = peek(st);
145-
if '0' <= c && c <= '9' {
146-
let n = parse_uint(st);
147-
assert_eq!(next(st), '|');
148-
return ty::VstoreFixed(n);
149-
}
150-
151-
match next(st) {
152-
'~' => ty::VstoreUniq,
153-
'&' => ty::VstoreSlice(parse_region(st, conv)),
154-
c => st.tcx.sess.bug(format!("parse_vstore(): bad input '{}'", c))
155-
}
156-
}
157-
158141
fn parse_size(st: &mut PState) -> Option<uint> {
159142
assert_eq!(next(st), '/');
160143

@@ -361,8 +344,8 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
361344
return ty::mk_vec(st.tcx, mt, sz);
362345
}
363346
'v' => {
364-
let v = parse_vstore(st, |x,y| conv(x,y));
365-
return ty::mk_str(st.tcx, v);
347+
let sz = parse_size(st);
348+
return ty::mk_str(st.tcx, sz);
366349
}
367350
'T' => {
368351
assert_eq!(next(st), '[');

src/librustc/metadata/tyencode.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,6 @@ fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
177177
}
178178
}
179179

180-
pub fn enc_vstore(w: &mut MemWriter, cx: &ctxt,
181-
v: ty::Vstore,
182-
enc_mut: |&mut MemWriter|) {
183-
mywrite!(w, "/");
184-
match v {
185-
ty::VstoreFixed(u) => mywrite!(w, "{}|", u),
186-
ty::VstoreUniq => mywrite!(w, "~"),
187-
ty::VstoreSlice(r) => {
188-
mywrite!(w, "&");
189-
enc_region(w, cx, r);
190-
enc_mut(w);
191-
}
192-
}
193-
}
194-
195180
pub fn enc_trait_ref(w: &mut MemWriter, cx: &ctxt, s: &ty::TraitRef) {
196181
mywrite!(w, "{}|", (cx.ds)(s.def_id));
197182
enc_substs(w, cx, &s.substs);
@@ -275,9 +260,13 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
275260
None => mywrite!(w, "|"),
276261
}
277262
}
278-
ty::ty_str(v) => {
263+
ty::ty_str(sz) => {
279264
mywrite!(w, "v");
280-
enc_vstore(w, cx, v, |_| {});
265+
mywrite!(w, "/");
266+
match sz {
267+
Some(n) => mywrite!(w, "{}|", n),
268+
None => mywrite!(w, "|"),
269+
}
281270
}
282271
ty::ty_closure(ref f) => {
283272
mywrite!(w, "f");

src/librustc/middle/check_match.rs

+1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ fn missing_ctor(cx: &MatchCheckCtxt,
405405
ty::ty_struct(..) => check_matrix_for_wild(cx, m),
406406
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
407407
ty::ty_vec(_, None) => ctor_for_slice(m),
408+
ty::ty_str(None) => Some(single),
408409
_ => check_matrix_for_wild(cx, m),
409410
},
410411
ty::ty_enum(eid, _) => {

src/librustc/middle/effect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ impl<'a> EffectCheckVisitor<'a> {
6868
debug!("effect: checking index with base type {}",
6969
ppaux::ty_to_str(self.tcx, base_type));
7070
match ty::get(base_type).sty {
71+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
72+
ty::ty_str(None) => {
73+
self.tcx.sess.span_err(e.span,
74+
"modification of string types is not allowed");
75+
}
76+
_ => {}
77+
},
7178
ty::ty_str(..) => {
7279
self.tcx.sess.span_err(e.span,
7380
"modification of string types is not allowed");

src/librustc/middle/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
911911
ty::ty_box(_) => {
912912
n_box += 1;
913913
}
914-
ty::ty_uniq(_) | ty::ty_str(ty::VstoreUniq) |
914+
ty::ty_uniq(_) |
915915
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
916916
ty::ty_closure(~ty::ClosureTy { store: ty::UniqTraitStore, .. }) => {
917917
n_uniq += 1;

src/librustc/middle/mem_categorization.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
174174
match ty::get(t).sty {
175175
ty::ty_uniq(_) |
176176
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
177-
ty::ty_str(ty::VstoreUniq) |
178177
ty::ty_closure(~ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
179178
Some(deref_ptr(OwnedPtr))
180179
}
@@ -188,7 +187,6 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
188187
Some(deref_ptr(BorrowedPtr(kind, r)))
189188
}
190189

191-
ty::ty_str(ty::VstoreSlice(r)) |
192190
ty::ty_closure(~ty::ClosureTy {store: ty::RegionTraitStore(r, _), ..}) => {
193191
Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
194192
}
@@ -207,7 +205,7 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
207205
}
208206

209207
ty::ty_vec(_, Some(_)) |
210-
ty::ty_str(ty::VstoreFixed(_)) => {
208+
ty::ty_str(Some(_)) => {
211209
Some(deref_interior(InteriorElement(element_kind(t))))
212210
}
213211

@@ -1306,6 +1304,7 @@ fn element_kind(t: ty::t) -> ElementKind {
13061304
ty::ty_rptr(_, ty::mt{ty:ty, ..}) |
13071305
ty::ty_uniq(ty) => match ty::get(ty).sty {
13081306
ty::ty_vec(_, None) => VecElement,
1307+
ty::ty_str(None) => StrElement,
13091308
_ => OtherElement
13101309
},
13111310
ty::ty_vec(..) => VecElement,

src/librustc/middle/trans/_match.rs

+39-28
Original file line numberDiff line numberDiff line change
@@ -1293,40 +1293,51 @@ fn compare_values<'a>(
12931293
rhs: ValueRef,
12941294
rhs_t: ty::t)
12951295
-> Result<'a> {
1296+
fn compare_str<'a>(cx: &'a Block<'a>,
1297+
lhs: ValueRef,
1298+
rhs: ValueRef,
1299+
rhs_t: ty::t)
1300+
-> Result<'a> {
1301+
let did = langcall(cx, None,
1302+
format!("comparison of `{}`", cx.ty_to_str(rhs_t)),
1303+
StrEqFnLangItem);
1304+
let result = callee::trans_lang_call(cx, did, [lhs, rhs], None);
1305+
Result {
1306+
bcx: result.bcx,
1307+
val: bool_to_i1(result.bcx, result.val)
1308+
}
1309+
}
1310+
12961311
let _icx = push_ctxt("compare_values");
12971312
if ty::type_is_scalar(rhs_t) {
1298-
let rs = compare_scalar_types(cx, lhs, rhs, rhs_t, ast::BiEq);
1299-
return rslt(rs.bcx, rs.val);
1313+
let rs = compare_scalar_types(cx, lhs, rhs, rhs_t, ast::BiEq);
1314+
return rslt(rs.bcx, rs.val);
13001315
}
13011316

13021317
match ty::get(rhs_t).sty {
1303-
ty::ty_str(ty::VstoreUniq) => {
1304-
let scratch_lhs = alloca(cx, val_ty(lhs), "__lhs");
1305-
Store(cx, lhs, scratch_lhs);
1306-
let scratch_rhs = alloca(cx, val_ty(rhs), "__rhs");
1307-
Store(cx, rhs, scratch_rhs);
1308-
let did = langcall(cx, None,
1309-
format!("comparison of `{}`", cx.ty_to_str(rhs_t)),
1310-
UniqStrEqFnLangItem);
1311-
let result = callee::trans_lang_call(cx, did, [scratch_lhs, scratch_rhs], None);
1312-
Result {
1313-
bcx: result.bcx,
1314-
val: bool_to_i1(result.bcx, result.val)
1315-
}
1316-
}
1317-
ty::ty_str(_) => {
1318-
let did = langcall(cx, None,
1319-
format!("comparison of `{}`", cx.ty_to_str(rhs_t)),
1320-
StrEqFnLangItem);
1321-
let result = callee::trans_lang_call(cx, did, [lhs, rhs], None);
1322-
Result {
1323-
bcx: result.bcx,
1324-
val: bool_to_i1(result.bcx, result.val)
1318+
ty::ty_uniq(t) => match ty::get(t).sty {
1319+
ty::ty_str(None) => {
1320+
let scratch_lhs = alloca(cx, val_ty(lhs), "__lhs");
1321+
Store(cx, lhs, scratch_lhs);
1322+
let scratch_rhs = alloca(cx, val_ty(rhs), "__rhs");
1323+
Store(cx, rhs, scratch_rhs);
1324+
let did = langcall(cx, None,
1325+
format!("comparison of `{}`", cx.ty_to_str(rhs_t)),
1326+
UniqStrEqFnLangItem);
1327+
let result = callee::trans_lang_call(cx, did, [scratch_lhs, scratch_rhs], None);
1328+
Result {
1329+
bcx: result.bcx,
1330+
val: bool_to_i1(result.bcx, result.val)
1331+
}
13251332
}
1326-
}
1327-
_ => {
1328-
cx.sess().bug("only scalars and strings supported in compare_values");
1329-
}
1333+
_ => cx.sess().bug("only scalars and strings supported in compare_values"),
1334+
},
1335+
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
1336+
ty::ty_str(None) => compare_str(cx, lhs, rhs, rhs_t),
1337+
_ => cx.sess().bug("only scalars and strings supported in compare_values"),
1338+
},
1339+
ty::ty_str(Some(_)) => compare_str(cx, lhs, rhs, rhs_t),
1340+
_ => cx.sess().bug("only scalars and strings supported in compare_values"),
13301341
}
13311342
}
13321343

src/librustc/middle/trans/adt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,10 @@ impl Case {
270270
self.tys.iter().position(|&ty| {
271271
match ty::get(ty).sty {
272272
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
273-
ty::ty_vec(_, None) => false,
273+
ty::ty_vec(_, None) | ty::ty_str(None)=> false,
274274
_ => true,
275275
},
276276
ty::ty_uniq(..) | ty::ty_box(..) |
277-
ty::ty_str(ty::VstoreUniq) |
278277
ty::ty_bare_fn(..) => true,
279278
// Is that everything? Would closures or slices qualify?
280279
_ => false

src/librustc/middle/trans/base.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv,
188188
// `~` pointer return values never alias because ownership is transferred
189189
// FIXME #6750 ~Trait cannot be directly marked as
190190
// noalias because the actual object pointer is nested.
191-
ty::ty_uniq(..) | // ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
192-
ty::ty_str(ty::VstoreUniq) => {
191+
ty::ty_uniq(..) // | ty::ty_trait(_, _, ty::UniqTraitStore, _, _)
192+
=> {
193193
unsafe {
194194
llvm::LLVMAddReturnAttribute(llfn, lib::llvm::NoAliasAttribute as c_uint);
195195
}
@@ -261,7 +261,6 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
261261
// FIXME #6750 ~Trait cannot be directly marked as
262262
// noalias because the actual object pointer is nested.
263263
ty::ty_uniq(..) | // ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
264-
ty::ty_str(ty::VstoreUniq) |
265264
ty::ty_closure(~ty::ClosureTy {store: ty::UniqTraitStore, ..}) => {
266265
unsafe {
267266
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
@@ -665,7 +664,7 @@ pub fn iter_structural_ty<'r,
665664
}
666665
})
667666
}
668-
ty::ty_str(ty::VstoreFixed(n)) => {
667+
ty::ty_str(Some(n)) => {
669668
let unit_ty = ty::sequence_element_type(cx.tcx(), t);
670669
let (base, len) = tvec::get_fixed_base_and_byte_len(cx, av, unit_ty, n);
671670
cx = tvec::iter_vec_raw(cx, base, unit_ty, len, f);

src/librustc/middle/trans/callee.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,10 @@ pub fn trans_call_inner<'a>(
659659
match ty::get(ret_ty).sty {
660660
// `~` pointer return values never alias because ownership
661661
// is transferred
662-
ty::ty_uniq(..) => {
663-
attrs.push((0, NoAliasAttribute));
664-
}
662+
ty::ty_uniq(ty) => match ty::get(ty).sty {
663+
ty::ty_str(None) => {}
664+
_ => attrs.push((0, NoAliasAttribute)),
665+
},
665666
_ => {}
666667
}
667668

src/librustc/middle/trans/consts.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn const_deref(cx: &CrateContext, v: ValueRef, t: ty::t, explicit: bool)
141141
let dv = match ty::get(t).sty {
142142
ty::ty_ptr(mt) | ty::ty_rptr(_, mt) => {
143143
match ty::get(mt.ty).sty {
144-
ty::ty_vec(_, None) => cx.sess().bug("unexpected slice"),
144+
ty::ty_vec(_, None) | ty::ty_str(None) => cx.sess().bug("unexpected slice"),
145145
_ => const_deref_ptr(cx, v),
146146
}
147147
}
@@ -432,13 +432,9 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
432432
"index is not an integer-constant expression")
433433
};
434434
let (arr, len) = match ty::get(bt).sty {
435-
ty::ty_str(ty::VstoreSlice(..)) => {
436-
let e1 = const_get_elt(cx, bv, [0]);
437-
(const_deref_ptr(cx, e1), const_get_elt(cx, bv, [1]))
438-
},
439435
ty::ty_vec(_, Some(u)) => (bv, C_uint(cx, u)),
440436
ty::ty_rptr(_, mt) => match ty::get(mt.ty).sty {
441-
ty::ty_vec(_, None) => {
437+
ty::ty_vec(_, None) | ty::ty_str(None) => {
442438
let e1 = const_get_elt(cx, bv, [0]);
443439
(const_deref_ptr(cx, e1), const_get_elt(cx, bv, [1]))
444440
},
@@ -451,7 +447,17 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
451447

452448
let len = llvm::LLVMConstIntGetZExtValue(len) as u64;
453449
let len = match ty::get(bt).sty {
454-
ty::ty_str(..) => {assert!(len > 0); len - 1},
450+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) => match ty::get(ty).sty {
451+
ty::ty_str(None) => {
452+
assert!(len > 0);
453+
len - 1
454+
}
455+
_ => len
456+
},
457+
ty::ty_str(Some(_)) => {
458+
assert!(len > 0);
459+
len - 1
460+
},
455461
_ => len
456462
};
457463
if iv >= len {

0 commit comments

Comments
 (0)