Skip to content

Commit 3cf00c1

Browse files
committed
Correct error message for argument mode mismatch
If you use a function expecting an alias argument in a context that expects a function expecting a value argument, or vice versa, the previous error message complained that the number of arguments was wrong. Fixed the error message to be accurate.
1 parent 5bd65de commit 3cf00c1

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/comp/middle/ty.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import middle::metadata;
2222
import util::common::*;
2323
import util::data::interner;
2424
import pretty::ppaux::ty_to_str;
25+
import pretty::ppaux::mode_str_1;
2526

2627

2728
export node_id_to_monotype;
@@ -292,6 +293,7 @@ tag type_err {
292293
terr_meth_count;
293294
terr_obj_meths(ast::ident, ast::ident);
294295
terr_arg_count;
296+
terr_mode_mismatch(mode, mode);
295297
}
296298

297299
type ty_param_count_and_ty = tup(uint, t);
@@ -1984,9 +1986,8 @@ mod unify {
19841986

19851987
auto result_mode;
19861988
if (expected_input.mode != actual_input.mode) {
1987-
// FIXME this is the wrong error
1988-
1989-
ret fn_common_res_err(ures_err(terr_arg_count));
1989+
ret fn_common_res_err(ures_err(terr_mode_mismatch(
1990+
expected_input.mode, actual_input.mode)));
19901991
} else { result_mode = expected_input.mode; }
19911992
auto result = unify_step(cx, expected_input.ty, actual_input.ty);
19921993
alt (result) {
@@ -2584,6 +2585,11 @@ fn type_err_to_str(&ty::type_err err) -> str {
25842585
ret "expected an obj with method '" + e_meth +
25852586
"' but found one with method '" + a_meth + "'";
25862587
}
2588+
case (terr_mode_mismatch(?e_mode, ?a_mode)) {
2589+
ret "expected argument mode " + mode_str_1(e_mode) + " but found "
2590+
+ mode_str_1(a_mode);
2591+
fail;
2592+
}
25872593
}
25882594
}
25892595

src/comp/pretty/ppaux.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import std::str;
55
import std::option;
66
import std::option::none;
77
import std::option::some;
8+
import middle::ty;
89
import middle::ty::*;
910
import front::lexer;
1011
import front::ast;
@@ -18,15 +19,25 @@ import util::common::istr;
1819
import util::common::uistr;
1920
import util::common::ty_mach_to_str;
2021

22+
fn mode_str(&ty::mode m) -> str {
23+
alt (m) {
24+
case (mo_val) { "" }
25+
case (mo_alias(false)) { "&" }
26+
case (mo_alias(true)) { "&mutable " }
27+
}
28+
}
29+
30+
fn mode_str_1(&ty::mode m) -> str {
31+
alt (m) {
32+
case (mo_val) { "val" }
33+
case (_) { mode_str(m) }
34+
}
35+
}
36+
2137
fn ty_to_str(&ctxt cx, &t typ) -> str {
2238
fn fn_input_to_str(&ctxt cx, &rec(middle::ty::mode mode, t ty) input) ->
2339
str {
24-
auto s =
25-
alt (input.mode) {
26-
case (mo_val) { "" }
27-
case (mo_alias(false)) { "&" }
28-
case (mo_alias(true)) { "&mutable " }
29-
};
40+
auto s = mode_str(input.mode);
3041
ret s + ty_to_str(cx, input.ty);
3142
}
3243
fn fn_to_str(&ctxt cx, ast::proto proto, option::t[ast::ident] ident,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// error-pattern:expected argument mode
2+
use std;
3+
import std::vec::map;
4+
5+
fn main() {
6+
fn f(uint i) -> bool { true }
7+
8+
auto a = map(f, [5u]);
9+
}

0 commit comments

Comments
 (0)