Skip to content

Commit 9d1cffd

Browse files
committed
---
yaml --- r: 58362 b: refs/heads/auto c: 830b945 h: refs/heads/master v: v3
1 parent c0b3a75 commit 9d1cffd

File tree

6 files changed

+186
-64
lines changed

6 files changed

+186
-64
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: 4f8084a363b352e4a1d75f9ab53f6defcb65d366
17+
refs/heads/auto: 830b945a9db072b68970b6f83dfafc1aaff8f837
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libcore/to_str.rs

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ The `ToStr` trait for converting to strings
1414
1515
*/
1616

17-
use str;
17+
use str::OwnedStr;
18+
use hashmap::HashMap;
19+
use hashmap::HashSet;
20+
use container::Map;
21+
use hash::Hash;
22+
use cmp::Eq;
1823

1924
pub trait ToStr {
2025
fn to_str(&self) -> ~str;
@@ -46,6 +51,44 @@ impl<A:ToStr> ToStr for (A,) {
4651
}
4752
}
4853

54+
impl<A:ToStr+Hash+Eq, B:ToStr+Hash+Eq> ToStr for HashMap<A, B> {
55+
#[inline(always)]
56+
fn to_str(&self) -> ~str {
57+
let mut acc = ~"{", first = true;
58+
for self.each |key, value| {
59+
if first {
60+
first = false;
61+
}
62+
else {
63+
acc.push_str(", ");
64+
}
65+
acc.push_str(key.to_str());
66+
acc.push_str(": ");
67+
acc.push_str(value.to_str());
68+
}
69+
acc.push_char('}');
70+
acc
71+
}
72+
}
73+
74+
impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
75+
#[inline(always)]
76+
fn to_str(&self) -> ~str {
77+
let mut acc = ~"{", first = true;
78+
for self.each |element| {
79+
if first {
80+
first = false;
81+
}
82+
else {
83+
acc.push_str(", ");
84+
}
85+
acc.push_str(element.to_str());
86+
}
87+
acc.push_char('}');
88+
acc
89+
}
90+
}
91+
4992
impl<A:ToStr,B:ToStr> ToStr for (A, B) {
5093
#[inline(always)]
5194
fn to_str(&self) -> ~str {
@@ -58,6 +101,7 @@ impl<A:ToStr,B:ToStr> ToStr for (A, B) {
58101
}
59102
}
60103
}
104+
61105
impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
62106
#[inline(always)]
63107
fn to_str(&self) -> ~str {
@@ -80,11 +124,15 @@ impl<'self,A:ToStr> ToStr for &'self [A] {
80124
fn to_str(&self) -> ~str {
81125
let mut acc = ~"[", first = true;
82126
for self.each |elt| {
83-
if first { first = false; }
84-
else { str::push_str(&mut acc, ~", "); }
85-
str::push_str(&mut acc, elt.to_str());
127+
if first {
128+
first = false;
129+
}
130+
else {
131+
acc.push_str(", ");
132+
}
133+
acc.push_str(elt.to_str());
86134
}
87-
str::push_char(&mut acc, ']');
135+
acc.push_char(']');
88136
acc
89137
}
90138
}
@@ -94,11 +142,15 @@ impl<A:ToStr> ToStr for ~[A] {
94142
fn to_str(&self) -> ~str {
95143
let mut acc = ~"[", first = true;
96144
for self.each |elt| {
97-
if first { first = false; }
98-
else { str::push_str(&mut acc, ~", "); }
99-
str::push_str(&mut acc, elt.to_str());
145+
if first {
146+
first = false;
147+
}
148+
else {
149+
acc.push_str(", ");
150+
}
151+
acc.push_str(elt.to_str());
100152
}
101-
str::push_char(&mut acc, ']');
153+
acc.push_char(']');
102154
acc
103155
}
104156
}
@@ -108,18 +160,25 @@ impl<A:ToStr> ToStr for @[A] {
108160
fn to_str(&self) -> ~str {
109161
let mut acc = ~"[", first = true;
110162
for self.each |elt| {
111-
if first { first = false; }
112-
else { str::push_str(&mut acc, ~", "); }
113-
str::push_str(&mut acc, elt.to_str());
163+
if first {
164+
first = false;
165+
}
166+
else {
167+
acc.push_str(", ");
168+
}
169+
acc.push_str(elt.to_str());
114170
}
115-
str::push_char(&mut acc, ']');
171+
acc.push_char(']');
116172
acc
117173
}
118174
}
119175
120176
#[cfg(test)]
121177
#[allow(non_implicitly_copyable_typarams)]
122178
mod tests {
179+
use hashmap::HashMap;
180+
use hashmap::HashSet;
181+
use container::Set;
123182
#[test]
124183
fn test_simple_types() {
125184
assert!(1i.to_str() == ~"1");
@@ -149,4 +208,32 @@ mod tests {
149208
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
150209
~"[[], [1], [1, 1]]");
151210
}
152-
}
211+
212+
#[test]
213+
fn test_hashmap() {
214+
let mut table: HashMap<int, int> = HashMap::new();
215+
let empty: HashMap<int, int> = HashMap::new();
216+
217+
table.insert(3, 4);
218+
table.insert(1, 2);
219+
220+
let table_str = table.to_str();
221+
222+
assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}");
223+
assert!(empty.to_str() == ~"{}");
224+
}
225+
226+
#[test]
227+
fn test_hashset() {
228+
let mut set: HashSet<int> = HashSet::new();
229+
let empty_set: HashSet<int> = HashSet::new();
230+
231+
set.insert(1);
232+
set.insert(2);
233+
234+
let set_str = set.to_str();
235+
236+
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
237+
assert!(empty_set.to_str() == ~"{}");
238+
}
239+
}

branches/auto/src/librustc/middle/typeck/check/_match.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,12 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: @ast::Path,
146146
kind_name = "variant";
147147
}
148148
None => {
149-
let resolved_expected =
150-
fcx.infcx().ty_to_str(fcx.infcx().resolve_type_vars_if_possible(expected));
151-
fcx.infcx().type_error_message_str(pat.span,
152-
|actual| {
149+
fcx.infcx().type_error_message_str_with_expected(pat.span,
150+
|expected, actual| {
151+
expected.map_default(~"", |&e| {
153152
fmt!("mismatched types: expected `%s` but found %s",
154-
resolved_expected, actual)},
155-
~"a structure pattern",
153+
e, actual)})},
154+
Some(expected), ~"a structure pattern",
156155
None);
157156
fcx.write_error(pat.id);
158157
kind_name = "[error]";
@@ -189,13 +188,12 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: @ast::Path,
189188
kind_name = "structure";
190189
}
191190
_ => {
192-
let resolved_expected =
193-
fcx.infcx().ty_to_str(fcx.infcx().resolve_type_vars_if_possible(expected));
194-
fcx.infcx().type_error_message_str(pat.span,
195-
|actual| {
191+
fcx.infcx().type_error_message_str_with_expected(pat.span,
192+
|expected, actual| {
193+
expected.map_default(~"", |&e| {
196194
fmt!("mismatched types: expected `%s` but found %s",
197-
resolved_expected, actual)},
198-
~"an enum or structure pattern",
195+
e, actual)})},
196+
Some(expected), ~"an enum or structure pattern",
199197
None);
200198
fcx.write_error(pat.id);
201199
kind_name = "[error]";
@@ -508,20 +506,17 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
508506
for elts.each |elt| {
509507
check_pat(pcx, *elt, ty::mk_err());
510508
}
511-
let actual = ty::mk_tup(tcx, elts.map(|pat_var| {
512-
fcx.node_ty(pat_var.id)
513-
}));
514509
// use terr_tuple_size if both types are tuples
515510
let type_error = match s {
516511
ty::ty_tup(ref ex_elts) =>
517512
ty::terr_tuple_size(ty::expected_found{expected: ex_elts.len(),
518513
found: e_count}),
519514
_ => ty::terr_mismatch
520515
};
521-
fcx.infcx().report_mismatched_types(pat.span,
522-
expected,
523-
actual,
524-
&type_error);
516+
fcx.infcx().type_error_message_str_with_expected(pat.span, |expected, actual| {
517+
expected.map_default(~"", |&e| {
518+
fmt!("mismatched types: expected `%s` but found %s",
519+
e, actual)})}, Some(expected), ~"tuple", Some(&type_error));
525520
fcx.write_error(pat.id);
526521
}
527522
}
@@ -566,14 +561,15 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
566561
for after.each |&elt| {
567562
check_pat(pcx, elt, ty::mk_err());
568563
}
569-
let resolved_expected =
570-
fcx.infcx().ty_to_str(fcx.infcx().resolve_type_vars_if_possible(expected));
571-
fcx.infcx().type_error_message_str(pat.span,
572-
|actual| {
573-
fmt!("mismatched types: expected `%s` but found %s",
574-
resolved_expected, actual)},
575-
~"a vector pattern",
576-
None);
564+
fcx.infcx().type_error_message_str_with_expected(
565+
pat.span,
566+
|expected, actual| {
567+
expected.map_default(~"", |&e| {
568+
fmt!("mismatched types: expected `%s` but found %s",
569+
e, actual)})},
570+
Some(expected),
571+
~"a vector pattern",
572+
None);
577573
fcx.write_error(pat.id);
578574
return;
579575
}
@@ -623,17 +619,19 @@ pub fn check_pointer_pat(pcx: &pat_ctxt,
623619
}
624620
_ => {
625621
check_pat(pcx, inner, ty::mk_err());
626-
let resolved_expected =
627-
fcx.infcx().ty_to_str(fcx.infcx().resolve_type_vars_if_possible(expected));
628-
fcx.infcx().type_error_message_str(span, |actual| {
629-
fmt!("mismatched types: expected `%s` but found %s",
630-
resolved_expected, actual)},
631-
fmt!("%s pattern", match pointer_kind {
632-
Managed => "an @-box",
633-
Owned => "a ~-box",
634-
Borrowed => "an &-pointer"
635-
}),
636-
None);
622+
fcx.infcx().type_error_message_str_with_expected(
623+
span,
624+
|expected, actual| {
625+
expected.map_default(~"", |&e| {
626+
fmt!("mismatched types: expected `%s` but found %s",
627+
e, actual)})},
628+
Some(expected),
629+
fmt!("%s pattern", match pointer_kind {
630+
Managed => "an @-box",
631+
Owned => "a ~-box",
632+
Borrowed => "an &-pointer"
633+
}),
634+
None);
637635
fcx.write_error(pat_id);
638636
}
639637
}

branches/auto/src/librustc/middle/typeck/infer/mod.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -728,30 +728,53 @@ pub impl InferCtxt {
728728
}
729729
}
730730

731+
fn type_error_message_str(@mut self,
732+
sp: span,
733+
mk_msg: &fn(Option<~str>, ~str) -> ~str,
734+
actual_ty: ~str, err: Option<&ty::type_err>) {
735+
self.type_error_message_str_with_expected(sp, mk_msg, None, actual_ty, err)
736+
}
737+
738+
fn type_error_message_str_with_expected(@mut self,
739+
sp: span,
740+
mk_msg: &fn(Option<~str>, ~str) -> ~str,
741+
expected_ty: Option<ty::t>, actual_ty: ~str,
742+
err: Option<&ty::type_err>) {
743+
debug!("hi! expected_ty = %?, actual_ty = %s", expected_ty, actual_ty);
731744

732-
fn type_error_message_str(@mut self, sp: span, mk_msg: &fn(~str) -> ~str,
733-
actual_ty: ~str, err: Option<&ty::type_err>) {
734745
let error_str = err.map_default(~"", |t_err|
735746
fmt!(" (%s)",
736747
ty::type_err_to_str(self.tcx, *t_err)));
737-
self.tcx.sess.span_err(sp,
738-
fmt!("%s%s", mk_msg(actual_ty), error_str));
739-
for err.each |err| {
740-
ty::note_and_explain_type_err(self.tcx, *err)
748+
let resolved_expected = expected_ty.map(|&e_ty|
749+
{ self.resolve_type_vars_if_possible(e_ty) });
750+
if !resolved_expected.map_default(false, |&e| { ty::type_is_error(e) }) {
751+
match resolved_expected {
752+
None => self.tcx.sess.span_err(sp,
753+
fmt!("%s%s", mk_msg(None, actual_ty), error_str)),
754+
Some(e) => {
755+
self.tcx.sess.span_err(sp,
756+
fmt!("%s%s", mk_msg(Some(self.ty_to_str(e)), actual_ty), error_str));
757+
}
758+
}
759+
for err.each |err| {
760+
ty::note_and_explain_type_err(self.tcx, *err)
761+
}
741762
}
742763
}
743764

744-
fn type_error_message(@mut self, sp: span, mk_msg: &fn(~str) -> ~str,
745-
actual_ty: ty::t, err: Option<&ty::type_err>) {
765+
fn type_error_message(@mut self,
766+
sp: span,
767+
mk_msg: &fn(~str) -> ~str,
768+
actual_ty: ty::t,
769+
err: Option<&ty::type_err>) {
746770
let actual_ty = self.resolve_type_vars_if_possible(actual_ty);
747771

748772
// Don't report an error if actual type is ty_err.
749773
if ty::type_is_error(actual_ty) {
750774
return;
751775
}
752776

753-
self.type_error_message_str(sp, mk_msg, self.ty_to_str(actual_ty),
754-
err);
777+
self.type_error_message_str(sp, |_e, a| { mk_msg(a) }, self.ty_to_str(actual_ty), err);
755778
}
756779

757780
fn report_mismatched_types(@mut self, sp: span, e: ty::t, a: ty::t,

branches/auto/src/test/compile-fail/issue-5100.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
}
1818

1919
match (true, false) {
20-
(true, false, false) => () //~ ERROR mismatched types: expected `(bool,bool)` but found `(bool,bool,bool)` (expected a tuple with 2 elements but found one with 3 elements)
20+
(true, false, false) => () //~ ERROR mismatched types: expected `(bool,bool)` but found tuple (expected a tuple with 2 elements but found one with 3 elements)
2121
}
2222

2323
match (true, false) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let (x, y) = (); //~ ERROR expected `()` but found tuple (types differ)
13+
return x;
14+
}

0 commit comments

Comments
 (0)