Skip to content

Commit

Permalink
librustc: Disallow multiple patterns from appearing in a "let" declar…
Browse files Browse the repository at this point in the history
…ation.

You can still initialize multiple variables at once with "let (x, y) = (1, 2)".
  • Loading branch information
pcwalton committed Jun 5, 2013
1 parent 16086ec commit 8114d0e
Show file tree
Hide file tree
Showing 80 changed files with 425 additions and 263 deletions.
6 changes: 4 additions & 2 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,9 @@ An example of a for loop over the contents of a vector:
~~~~
# type foo = int;
# fn bar(f: foo) { }
# let a = 0, b = 0, c = 0;
# let a = 0;
# let b = 0;
# let c = 0;
let v: &[foo] = &[a, b, c];
Expand Down Expand Up @@ -3000,7 +3002,7 @@ allocated within the stack's memory. The value is a part of the stack frame.

Local variables are immutable unless declared with `let mut`. The
`mut` keyword applies to all local variables declared within that
declaration (so `let mut x, y` declares two mutable variables, `x` and
declaration (so `let mut (x, y) = ...` declares two mutable variables, `x` and
`y`).

Function parameters are immutable unless declared with `mut`. The
Expand Down
8 changes: 4 additions & 4 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub struct Unique<T> {
priv ptr: *mut T
}
pub impl<T: Owned> Unique<T> {
fn new(value: T) -> Unique<T> {
impl<T: Owned> Unique<T> {
pub fn new(value: T) -> Unique<T> {
unsafe {
let ptr = malloc(std::sys::size_of::<T>() as size_t) as *mut T;
assert!(!ptr::is_null(ptr));
Expand All @@ -171,12 +171,12 @@ pub impl<T: Owned> Unique<T> {
}
// the 'r lifetime results in the same semantics as `&*x` with ~T
fn borrow<'r>(&'r self) -> &'r T {
pub fn borrow<'r>(&'r self) -> &'r T {
unsafe { cast::copy_lifetime(self, &*self.ptr) }
}
// the 'r lifetime results in the same semantics as `&mut *x` with ~T
fn borrow_mut<'r>(&'r mut self) -> &'r mut T {
pub fn borrow_mut<'r>(&'r mut self) -> &'r mut T {
unsafe { cast::copy_mut_lifetime(self, &mut *self.ptr) }
}
}
Expand Down
9 changes: 6 additions & 3 deletions doc/tutorial-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ doing nothing otherwise:
~~~~
# enum t { special_a(uint), special_b(uint) };
# fn f() -> uint {
# let input_1 = special_a(0), input_2 = special_a(0);
# let input_1 = special_a(0);
# let input_2 = special_a(0);
match input_1 {
special_a(x) => { return x; }
_ => {}
Expand All @@ -38,7 +39,8 @@ the pattern in the above code:
~~~~
# enum t { special_a(uint), special_b(uint) };
# fn f() -> uint {
# let input_1 = special_a(0), input_2 = special_a(0);
# let input_1 = special_a(0);
# let input_2 = special_a(0);
macro_rules! early_return(
($inp:expr $sp:ident) => ( // invoke it like `(input_5 special_e)`
match $inp {
Expand Down Expand Up @@ -155,7 +157,8 @@ instead of `*` to mean "at least one".
~~~~
# enum t { special_a(uint),special_b(uint),special_c(uint),special_d(uint)};
# fn f() -> uint {
# let input_1 = special_a(0), input_2 = special_a(0);
# let input_1 = special_a(0);
# let input_2 = special_a(0);
macro_rules! early_return(
($inp:expr, [ $($sp:ident)|+ ]) => (
match $inp {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
while idx < fill {
let tydesc_data: *uint = transmute(ptr::offset(buf, idx));
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
let size = (*tydesc).size, align = (*tydesc).align;
let (size, align) = ((*tydesc).size, (*tydesc).align);

let after_tydesc = idx + sys::size_of::<*TypeDesc>();

Expand Down
24 changes: 13 additions & 11 deletions src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ impl FileInput {
arguments. `"-"` represents `stdin`.
*/
pub fn from_args() -> FileInput {
let args = os::args(),
pathed = pathify(args.tail(), true);
let args = os::args();
let pathed = pathify(args.tail(), true);
FileInput::from_vec(pathed)
}

Expand All @@ -222,11 +222,11 @@ impl FileInput {
return false;
}

let path_option = self.fi.files.shift(),
file = match path_option {
None => io::stdin(),
Some(ref path) => io::file_reader(path).get()
};
let path_option = self.fi.files.shift();
let file = match path_option {
None => io::stdin(),
Some(ref path) => io::file_reader(path).get()
};

self.fi.current_reader = Some(file);
self.fi.state.current_path = path_option;
Expand Down Expand Up @@ -431,8 +431,8 @@ mod test {
#[test]
fn test_pathify() {
let strs = [~"some/path",
~"some/other/path"],
paths = ~[Some(Path("some/path")),
~"some/other/path"];
let paths = ~[Some(Path("some/path")),
Some(Path("some/other/path"))];

assert_eq!(pathify(strs, true), copy paths);
Expand Down Expand Up @@ -561,8 +561,10 @@ mod test {
#[test]
fn test_no_trailing_newline() {
let f1 = Some(Path("tmp/lib-fileinput-test-no-trailing-newline-1.tmp")),
f2 = Some(Path("tmp/lib-fileinput-test-no-trailing-newline-2.tmp"));
let f1 =
Some(Path("tmp/lib-fileinput-test-no-trailing-newline-1.tmp"));
let f2 =
Some(Path("tmp/lib-fileinput-test-no-trailing-newline-2.tmp"));
let wr = io::file_writer(f1.get_ref(), [io::Create, io::Truncate]).get();
wr.write_str("1\n2");
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/md4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub fn md4(msg: &[u8]) -> Quad {
let e = msg.len();
let mut x = vec::from_elem(16u, 0u32);
while i < e {
let aa = a, bb = b, cc = c, dd = d;
let (aa, bb, cc, dd) = (a, b, c, d);

let mut j = 0u, base = i;
let mut (j, base) = (0u, i);
while j < 16u {
x[j] = (msg[base] as u32) + (msg[base + 1u] as u32 << 8u32) +
(msg[base + 2u] as u32 << 16u32) +
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/net_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ fn get_authority(rawurl: &str) ->
let mut port = None;

let mut colon_count = 0;
let mut pos = 0, begin = 2, end = len;
let mut (pos, begin, end) = (0, 2, len);

for str::each_chari(rawurl) |i,c| {
if i < 2 { loop; } // ignore the leading //
Expand Down
10 changes: 5 additions & 5 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Ord for BigUint {
impl TotalOrd for BigUint {

fn cmp(&self, other: &BigUint) -> Ordering {
let s_len = self.data.len(), o_len = other.data.len();
let (s_len, o_len) = (self.data.len(), other.data.len());
if s_len < o_len { return Less; }
if s_len > o_len { return Greater; }

Expand Down Expand Up @@ -255,7 +255,7 @@ impl Mul<BigUint, BigUint> for BigUint {
fn mul(&self, other: &BigUint) -> BigUint {
if self.is_zero() || other.is_zero() { return Zero::zero(); }

let s_len = self.data.len(), o_len = other.data.len();
let (s_len, o_len) = (self.data.len(), other.data.len());
if s_len == 1 { return mul_digit(other, self.data[0]); }
if o_len == 1 { return mul_digit(self, other.data[0]); }

Expand Down Expand Up @@ -447,7 +447,7 @@ impl Integer for BigUint {

fn gcd(&self, other: &BigUint) -> BigUint {
// Use Euclid's algorithm
let mut m = copy *self, n = copy *other;
let mut (m, n) = (copy *self, copy *other);
while !m.is_zero() {
let temp = m;
m = n % temp;
Expand Down Expand Up @@ -1002,8 +1002,8 @@ impl Integer for BigInt {
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
// m.sign == other.sign
let (d_ui, m_ui) = self.data.div_rem(&other.data);
let d = BigInt::from_biguint(Plus, d_ui),
m = BigInt::from_biguint(Plus, m_ui);
let d = BigInt::from_biguint(Plus, d_ui);
let m = BigInt::from_biguint(Plus, m_ui);
match (self.sign, other.sign) {
(_, Zero) => fail!(),
(Plus, Plus) | (Zero, Plus) => (d, m),
Expand Down
4 changes: 3 additions & 1 deletion src/libextra/terminfo/parser/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ pub static stringnames: &'static[&'static str] = &'static[ "cbt", "_", "cr", "cs

/// Parse a compiled terminfo entry, using long capability names if `longnames` is true
pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
let bnames, snames, nnames;
let bnames;
let snames;
let nnames;

if longnames {
bnames = boolfnames;
Expand Down
13 changes: 11 additions & 2 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,18 @@ fn fold_block(
b.stmts.filter_mapped(|a| filter_stmt(cx, *a));
let filtered_view_items =
b.view_items.filter_mapped(|a| filter_view_item(cx, *a));
let filtered_view_items =
filtered_view_items.map(|x| fld.fold_view_item(*x));
let mut resulting_stmts = ~[];
for filtered_stmts.each |stmt| {
match fld.fold_stmt(*stmt) {
None => {}
Some(stmt) => resulting_stmts.push(stmt),
}
}
ast::blk_ {
view_items: vec::map(filtered_view_items, |x| fld.fold_view_item(*x)),
stmts: vec::map(filtered_stmts, |x| fld.fold_stmt(*x)),
view_items: filtered_view_items,
stmts: resulting_stmts,
expr: b.expr.map(|x| fld.fold_expr(*x)),
id: b.id,
rules: b.rules,
Expand Down
30 changes: 17 additions & 13 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ pub fn missing_ctor(cx: @MatchCheckCtxt,
}
ty::ty_nil => None,
ty::ty_bool => {
let mut true_found = false, false_found = false;
let mut true_found = false;
let mut false_found = false;
for m.each |r| {
match pat_ctor_id(cx, r[0]) {
None => (),
Expand Down Expand Up @@ -513,10 +514,12 @@ pub fn specialize(cx: @MatchCheckCtxt,
}
},
range(ref c_lo, ref c_hi) => {
let m1 = compare_const_vals(c_lo, &e_v),
m2 = compare_const_vals(c_hi, &e_v);
let m1 = compare_const_vals(c_lo, &e_v);
let m2 = compare_const_vals(c_hi, &e_v);
match (m1, m2) {
(Some(val1), Some(val2)) => (val1 >= 0 && val2 <= 0),
(Some(val1), Some(val2)) => {
(val1 >= 0 && val2 <= 0)
}
_ => {
cx.tcx.sess.span_err(pat_span,
"mismatched types between ranges");
Expand Down Expand Up @@ -560,8 +563,8 @@ pub fn specialize(cx: @MatchCheckCtxt,
}
},
range(ref c_lo, ref c_hi) => {
let m1 = compare_const_vals(c_lo, &e_v),
m2 = compare_const_vals(c_hi, &e_v);
let m1 = compare_const_vals(c_lo, &e_v);
let m2 = compare_const_vals(c_hi, &e_v);
match (m1, m2) {
(Some(val1), Some(val2)) => (val1 >= 0 && val2 <= 0),
_ => {
Expand Down Expand Up @@ -622,7 +625,8 @@ pub fn specialize(cx: @MatchCheckCtxt,
}
_ => {
// Grab the class data that we care about.
let class_fields, class_id;
let class_fields;
let class_id;
match ty::get(left_ty).sty {
ty::ty_struct(cid, _) => {
class_id = cid;
Expand Down Expand Up @@ -667,8 +671,8 @@ pub fn specialize(cx: @MatchCheckCtxt,
}
},
range(ref c_lo, ref c_hi) => {
let m1 = compare_const_vals(c_lo, &e_v),
m2 = compare_const_vals(c_hi, &e_v);
let m1 = compare_const_vals(c_lo, &e_v);
let m2 = compare_const_vals(c_hi, &e_v);
match (m1, m2) {
(Some(val1), Some(val2)) => (val1 >= 0 && val2 <= 0),
_ => {
Expand All @@ -691,11 +695,11 @@ pub fn specialize(cx: @MatchCheckCtxt,
single => return Some(vec::to_owned(r.tail())),
_ => fail!("type error")
};
let v_lo = eval_const_expr(cx.tcx, lo),
v_hi = eval_const_expr(cx.tcx, hi);
let v_lo = eval_const_expr(cx.tcx, lo);
let v_hi = eval_const_expr(cx.tcx, hi);

let m1 = compare_const_vals(&c_lo, &v_lo),
m2 = compare_const_vals(&c_hi, &v_hi);
let m1 = compare_const_vals(&c_lo, &v_lo);
let m2 = compare_const_vals(&c_hi, &v_hi);
match (m1, m2) {
(Some(val1), Some(val2)) if val1 >= 0 && val2 <= 0 => {
Some(vec::to_owned(r.tail()))
Expand Down
8 changes: 3 additions & 5 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,9 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> {
in_out: &mut [uint],
loop_scopes: &mut ~[LoopScope]) {
match decl.node {
ast::decl_local(ref locals) => {
for locals.each |local| {
self.walk_pat(local.node.pat, in_out, loop_scopes);
self.walk_opt_expr(local.node.init, in_out, loop_scopes);
}
ast::decl_local(local) => {
self.walk_pat(local.node.pat, in_out, loop_scopes);
self.walk_opt_expr(local.node.init, in_out, loop_scopes);
}

ast::decl_item(_) => {}
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,10 @@ impl Liveness {
pub fn propagate_through_decl(&self, decl: @decl, succ: LiveNode)
-> LiveNode {
match decl.node {
decl_local(ref locals) => {
do locals.foldr(succ) |local, succ| {
decl_local(ref local) => {
self.propagate_through_local(*local, succ)
}
}
decl_item(_) => {
succ
}
decl_item(_) => succ,
}
}

Expand Down
Loading

5 comments on commit 8114d0e

@bors
Copy link
Contributor

@bors bors commented on 8114d0e Jun 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from nikomatsakis
at pcwalton@8114d0e

@bors
Copy link
Contributor

@bors bors commented on 8114d0e Jun 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging pcwalton/rust/multiple-patterns-in-let = 8114d0e into auto

@bors
Copy link
Contributor

@bors bors commented on 8114d0e Jun 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pcwalton/rust/multiple-patterns-in-let = 8114d0e merged ok, testing candidate = de82bde

@bors
Copy link
Contributor

@bors bors commented on 8114d0e Jun 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 8114d0e Jun 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = de82bde

Please sign in to comment.