From 35baf5b2021fab393ff4d52581c55a0f6477bfb8 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 25 Feb 2013 00:15:53 +0900 Subject: [PATCH] Fix reversed current/expected type Fix some reversed type of arm pattern and type of search pattern in error message. --- src/librustc/middle/typeck/check/_match.rs | 8 ++++---- src/librustc/middle/typeck/check/demand.rs | 17 +++++++++++------ src/librustc/middle/typeck/check/mod.rs | 3 ++- src/test/compile-fail/issue-3680.rs | 2 +- src/test/compile-fail/match-struct.rs | 3 +-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/typeck/check/_match.rs b/src/librustc/middle/typeck/check/_match.rs index 82f9828db3f..795ea4323fe 100644 --- a/src/librustc/middle/typeck/check/_match.rs +++ b/src/librustc/middle/typeck/check/_match.rs @@ -103,7 +103,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path, // check that the type of the value being matched is a subtype // of the type of the pattern: let pat_ty = fcx.node_ty(pat.id); - demand::suptype(fcx, pat.span, pat_ty, expected); + demand::subtype(fcx, pat.span, expected, pat_ty); // Get the expected types of the arguments. arg_types = { @@ -142,7 +142,7 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path, // Check that the type of the value being matched is a subtype of // the type of the pattern. let pat_ty = fcx.node_ty(pat.id); - demand::suptype(fcx, pat.span, pat_ty, expected); + demand::subtype(fcx, pat.span, expected, pat_ty); // Get the expected types of the arguments. let class_fields = ty::struct_fields( @@ -154,8 +154,8 @@ pub fn check_pat_variant(pcx: pat_ctxt, pat: @ast::pat, path: @ast::path, _ => { tcx.sess.span_fatal( pat.span, - fmt!("mismatched types: expected enum or structure but \ - found `%s`", + fmt!("mismatched types: expected `%s` but found enum or \ + structure", fcx.infcx().ty_to_str(expected))); } } diff --git a/src/librustc/middle/typeck/check/demand.rs b/src/librustc/middle/typeck/check/demand.rs index 5f1a3b5c17c..a3fac8b4e1c 100644 --- a/src/librustc/middle/typeck/check/demand.rs +++ b/src/librustc/middle/typeck/check/demand.rs @@ -21,20 +21,25 @@ use syntax::codemap::span; // Requires that the two types unify, and prints an error message if they // don't. pub fn suptype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) { - suptype_with_fn(fcx, sp, expected, actual, + suptype_with_fn(fcx, sp, false, expected, actual, |sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) }) } +pub fn subtype(fcx: @mut FnCtxt, sp: span, expected: ty::t, actual: ty::t) { + suptype_with_fn(fcx, sp, true, actual, expected, + |sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) }) +} + pub fn suptype_with_fn(fcx: @mut FnCtxt, - sp: span, - expected: ty::t, actual: ty::t, + sp: span, b_is_expected: bool, + ty_a: ty::t, ty_b: ty::t, handle_err: fn(span, ty::t, ty::t, &ty::type_err)) { // n.b.: order of actual, expected is reversed - match infer::mk_subty(fcx.infcx(), false, sp, - actual, expected) { + match infer::mk_subty(fcx.infcx(), b_is_expected, sp, + ty_b, ty_a) { result::Ok(()) => { /* ok */ } result::Err(ref err) => { - handle_err(sp, expected, actual, err); + handle_err(sp, ty_a, ty_b, err); } } } diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index e63e46ace3d..7cf8b31ffc8 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -355,7 +355,8 @@ pub fn check_fn(ccx: @mut CrateCtxt, let tail_expr_ty = fcx.expr_ty(tail_expr); // Special case: we print a special error if there appears // to be do-block/for-loop confusion - demand::suptype_with_fn(fcx, tail_expr.span, fcx.ret_ty, tail_expr_ty, + demand::suptype_with_fn(fcx, tail_expr.span, false, + fcx.ret_ty, tail_expr_ty, |sp, e, a, s| { fcx.report_mismatched_return_types(sp, e, a, s) }); } diff --git a/src/test/compile-fail/issue-3680.rs b/src/test/compile-fail/issue-3680.rs index 9044c6b6d79..18b5d290f3d 100644 --- a/src/test/compile-fail/issue-3680.rs +++ b/src/test/compile-fail/issue-3680.rs @@ -10,6 +10,6 @@ fn main() { match None { - Err(_) => () //~ ERROR expected `core::result + Err(_) => () //~ ERROR mismatched types: expected `core::option::Option<>` but found `core::result::Result<,>` } } diff --git a/src/test/compile-fail/match-struct.rs b/src/test/compile-fail/match-struct.rs index fa406aa278e..6e9bf603aef 100644 --- a/src/test/compile-fail/match-struct.rs +++ b/src/test/compile-fail/match-struct.rs @@ -1,11 +1,10 @@ -// error-pattern: mismatched types struct S { a: int } enum E { C(int) } fn main() { match S { a: 1 } { - C(_) => (), + C(_) => (), //~ ERROR mismatched types: expected `S` but found `E` _ => () } }