Skip to content

Commit 92f6b92

Browse files
committed
auto merge of #13657 : edwardw/rust/ppaux-ice, r=alexcrichton
Closes #13599
2 parents 7730310 + 741142e commit 92f6b92

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

src/librustc/middle/ty.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -3331,6 +3331,13 @@ pub fn type_err_to_str(cx: &ctxt, err: &type_err) -> ~str {
33313331
}
33323332
}
33333333

3334+
fn tstore_to_closure(s: &TraitStore) -> ~str {
3335+
match s {
3336+
&UniqTraitStore => "proc".to_owned(),
3337+
&RegionTraitStore(..) => "closure".to_owned()
3338+
}
3339+
}
3340+
33343341
match *err {
33353342
terr_mismatch => "types differ".to_owned(),
33363343
terr_fn_style_mismatch(values) => {
@@ -3346,9 +3353,9 @@ pub fn type_err_to_str(cx: &ctxt, err: &type_err) -> ~str {
33463353
values.expected.to_str(), values.found.to_str())
33473354
}
33483355
terr_sigil_mismatch(values) => {
3349-
format!("expected {} closure, found {} closure",
3350-
values.expected.to_str(),
3351-
values.found.to_str())
3356+
format!("expected {}, found {}",
3357+
tstore_to_closure(&values.expected),
3358+
tstore_to_closure(&values.found))
33523359
}
33533360
terr_mutability => "values differ in mutability".to_owned(),
33543361
terr_box_mutability => "boxed values differ in mutability".to_owned(),

src/librustc/middle/typeck/check/mod.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,6 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
21822182
let expected_sty = unpack_expected(fcx,
21832183
expected,
21842184
|x| Some((*x).clone()));
2185-
let error_happened = false;
21862185
let (expected_sig,
21872186
expected_onceness,
21882187
expected_bounds) = {
@@ -2192,7 +2191,24 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
21922191
replace_late_bound_regions_in_fn_sig(
21932192
tcx, &cenv.sig,
21942193
|_| fcx.inh.infcx.fresh_bound_region(expr.id));
2195-
(Some(sig), cenv.onceness, cenv.bounds)
2194+
let onceness = match (&store, &cenv.store) {
2195+
// As the closure type and onceness go, only three
2196+
// combinations are legit:
2197+
// once closure
2198+
// many closure
2199+
// once proc
2200+
// If the actual and expected closure type disagree with
2201+
// each other, set expected onceness to be always Once or
2202+
// Many according to the actual type. Otherwise, it will
2203+
// yield either an illegal "many proc" or a less known
2204+
// "once closure" in the error message.
2205+
(&ty::UniqTraitStore, &ty::UniqTraitStore) |
2206+
(&ty::RegionTraitStore(..), &ty::RegionTraitStore(..)) =>
2207+
cenv.onceness,
2208+
(&ty::UniqTraitStore, _) => ast::Once,
2209+
(&ty::RegionTraitStore(..), _) => ast::Many,
2210+
};
2211+
(Some(sig), onceness, cenv.bounds)
21962212
}
21972213
_ => {
21982214
// Not an error! Means we're inferring the closure type
@@ -2218,23 +2234,9 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
22182234
store,
22192235
decl,
22202236
expected_sig);
2221-
2222-
let fty_sig;
2223-
let fty = if error_happened {
2224-
fty_sig = FnSig {
2225-
binder_id: ast::CRATE_NODE_ID,
2226-
inputs: fn_ty.sig.inputs.iter().map(|_| ty::mk_err()).collect(),
2227-
output: ty::mk_err(),
2228-
variadic: false
2229-
};
2230-
ty::mk_err()
2231-
} else {
2232-
fty_sig = fn_ty.sig.clone();
2233-
ty::mk_closure(tcx, fn_ty.clone())
2234-
};
2235-
2236-
debug!("check_expr_fn_with_unifier fty={}",
2237-
fcx.infcx().ty_to_str(fty));
2237+
let fty_sig = fn_ty.sig.clone();
2238+
let fty = ty::mk_closure(tcx, fn_ty);
2239+
debug!("check_expr_fn fty={}", fcx.infcx().ty_to_str(fty));
22382240

22392241
fcx.write_ty(expr.id, fty);
22402242

src/test/compile-fail/issue-13599.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012-2014 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+
// Test that a mismatched proc / closure type is correctly reported.
12+
13+
fn expect_closure(_: ||) {}
14+
15+
fn expect_proc(_: proc()) {}
16+
17+
fn main() {
18+
expect_closure(proc() {});
19+
//~^ ERROR mismatched types: expected `||` but found `proc()` (expected closure, found proc)
20+
21+
expect_proc(|| {});
22+
//~^ ERROR mismatched types: expected `proc()` but found `||` (expected proc, found closure)
23+
}

0 commit comments

Comments
 (0)