Skip to content

Commit 0c5fdc8

Browse files
committed
Rename last to last_opt, last_unsafe to last
As per discussion on IRC. I am about to file an RFC for further discussion about the more general issue of whether to enforce invariants through types, typestate, or dynamic checks, but for now, removing the misleading name "last_unsafe".
1 parent 8047c0c commit 0c5fdc8

File tree

13 files changed

+27
-28
lines changed

13 files changed

+27
-28
lines changed

src/libcore/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn splitext(p: path) -> (str, str) {
170170
if vec::len(parts) > 1u {
171171
let base = str::connect(vec::init(parts), ".");
172172
// We just checked that parts is non-empty, so this is safe
173-
let ext = "." + vec::last_unsafe(parts);
173+
let ext = "." + vec::last(parts);
174174

175175
fn is_dotfile(base: str) -> bool {
176176
str::is_empty(base)

src/libcore/vec.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,27 +187,24 @@ fn init<T: copy>(v: [const T]) -> [T] {
187187
/*
188188
Function: last
189189
190-
Returns the last element of a vector
191-
192-
Returns:
190+
Returns the last element of a `v`, failing if the vector is empty.
193191
194-
An option containing the last element of `v` if `v` is not empty, or
195-
none if `v` is empty.
196192
*/
197-
pure fn last<T: copy>(v: [const T]) -> option<T> {
198-
if len(v) == 0u { ret none; }
199-
ret some(v[len(v) - 1u]);
193+
pure fn last<T: copy>(v: [const T]) -> T {
194+
if len(v) == 0u { fail "last_unsafe: empty vector" }
195+
v[len(v) - 1u]
200196
}
201197

202198
/*
203-
Function: last_unsafe
199+
Function: last_opt
204200
205-
Returns the last element of a `v`, failing if the vector is empty.
201+
Returns some(x) where `x` is the last element of a vector `v`,
202+
or none if the vector is empty.
206203
207204
*/
208-
pure fn last_unsafe<T: copy>(v: [const T]) -> T {
209-
if len(v) == 0u { fail "last_unsafe: empty vector" }
210-
v[len(v) - 1u]
205+
pure fn last_opt<T: copy>(v: [const T]) -> option<T> {
206+
if len(v) == 0u { ret none; }
207+
some(v[len(v) - 1u])
211208
}
212209

213210
/*
@@ -1270,11 +1267,11 @@ mod tests {
12701267

12711268
#[test]
12721269
fn test_last() {
1273-
let n = last([]);
1270+
let n = last_opt([]);
12741271
assert (n == none);
1275-
n = last([1, 2, 3]);
1272+
n = last_opt([1, 2, 3]);
12761273
assert (n == some(3));
1277-
n = last([1, 2, 3, 4, 5]);
1274+
n = last_opt([1, 2, 3, 4, 5]);
12781275
assert (n == some(5));
12791276
}
12801277

src/libstd/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn splitext(p: path) -> (str, str) {
271271
if vec::len(parts) > 1u {
272272
let base = str::connect(vec::init(parts), ".");
273273
// We just checked that parts is non-empty
274-
let ext = "." + vec::last_unsafe(parts);
274+
let ext = "." + vec::last(parts);
275275

276276
fn is_dotfile(base: str) -> bool {
277277
str::is_empty(base)

src/rustc/front/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn meta_item_from_list(
256256
name: str
257257
) -> option<@ast::meta_item> {
258258
let items = attr::find_meta_items_by_name(items, name);
259-
vec::last(items)
259+
vec::last_opt(items)
260260
}
261261

262262
fn meta_item_value_from_list(

src/rustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn find_library_crate(sess: session::session, ident: ast::ident,
144144
let crate_name =
145145
{
146146
let name_items = attr::find_meta_items_by_name(metas, "name");
147-
alt vec::last(name_items) {
147+
alt vec::last_opt(name_items) {
148148
some(i) {
149149
alt attr::get_meta_item_value_str(i) {
150150
some(n) { n }

src/rustc/middle/ast_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ fn map_view_item(vi: @view_item, cx: ctx, _v: vt) {
189189
view_path_glob(pth, id) | view_path_list(pth, _, id) {
190190
// should be a constraint on the type
191191
assert (vec::is_not_empty(*pth));
192-
(id, vec::last_unsafe(*pth))
192+
(id, vec::last(*pth))
193193
}
194194
};
195195
cx.map.insert(id, node_export(vp, extend(cx, name)));

src/rustc/middle/pat_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ fn pat_binding_ids(dm: resolve::def_map, pat: @pat) -> [node_id] {
7070

7171
fn path_to_ident(p: @path) -> ident {
7272
assert (vec::is_not_empty(p.node.idents)); // should be a constraint on path
73-
vec::last_unsafe(p.node.idents)
73+
vec::last(p.node.idents)
7474
}

src/rustc/middle/tstate/bitvectors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn kill_poststate(fcx: fn_ctxt, id: node_id, c: tsconstr) -> bool {
183183
fn clear_in_poststate_expr(fcx: fn_ctxt, e: @expr, t: poststate) {
184184
alt e.node {
185185
expr_path(p) {
186-
alt vec::last(p.node.idents) {
186+
alt vec::last_opt(p.node.idents) {
187187
some(i) {
188188
alt local_node_id_to_def(fcx, e.id) {
189189
some(def_local(nid, _)) {

src/rustc/middle/tstate/states.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
406406
init_assign),
407407
exs, return_val);
408408

409-
let base_pres = alt vec::last(exs) { none { pres }
409+
let base_pres = alt vec::last_opt(exs) { none { pres }
410410
some(f) { expr_poststate(fcx.ccx, f) }};
411411
option::may(maybe_base, {|base|
412412
changed |= find_pre_post_state_expr(fcx, base_pres, base) |

src/rustc/middle/typeck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2642,7 +2642,7 @@ fn bind_params(fcx: @fn_ctxt, tp: ty::t, count: uint)
26422642
}
26432643

26442644
fn get_self_info(ccx: @crate_ctxt) -> option<self_info> {
2645-
ret vec::last(ccx.self_infos);
2645+
ret vec::last_opt(ccx.self_infos);
26462646
}
26472647

26482648
fn check_decl_initializer(fcx: @fn_ctxt, nid: ast::node_id,

src/rustc/util/common.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ fn local_rhs_span(l: @ast::local, def: span) -> span {
8989
}
9090

9191
fn is_main_name(path: middle::ast_map::path) -> bool {
92-
option::get(vec::last(path)) == middle::ast_map::path_name("main")
92+
// FIXME: path should be a constrained type, so we know
93+
// the call to last doesn't fail
94+
vec::last(path) == middle::ast_map::path_name("main")
9395
}
9496

9597
//

src/rustdoc/reexport_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn build_reexport_path_map(srv: astsrv::srv, -def_map: def_map) -> path_map {
163163
};
164164
// should be a constraint on the node_export constructor
165165
// that guarantees path is non-empty
166-
let name = alt check vec::last_unsafe(*path) {
166+
let name = alt check vec::last(*path) {
167167
ast_map::path_name(nm) { nm }
168168
};
169169
let modpath = ast_map::path_to_str(vec::init(*path));

src/test/run-pass/zip-same-length.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919

2020
check (is_not_empty(ps));
2121
assert (head(ps) == ('a', 1u));
22-
assert (last_unsafe(ps) == (j as char, 10u));
22+
assert (last(ps) == (j as char, 10u));
2323
}

0 commit comments

Comments
 (0)