From 484729478b65443447516ba2d0cfb44fe8f8e430 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 10 Dec 2017 01:20:40 +0100 Subject: [PATCH] Fix switched types in type mismatch --- src/librustc_typeck/check/mod.rs | 2 +- src/test/ui/switched-expectations.rs | 14 ++++++++++++++ src/test/ui/switched-expectations.stderr | 11 +++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/switched-expectations.rs create mode 100644 src/test/ui/switched-expectations.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index efcf498b72c14..24ffc0ca542cf 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4194,7 +4194,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // type of the lvalue it is referencing, and not some // supertype thereof. let init_ty = self.check_expr_with_lvalue_pref(init, LvaluePreference::from_mutbl(m)); - self.demand_eqtype(init.span, init_ty, local_ty); + self.demand_eqtype(init.span, local_ty, init_ty); init_ty } else { self.check_expr_coercable_to_type(init, local_ty) diff --git a/src/test/ui/switched-expectations.rs b/src/test/ui/switched-expectations.rs new file mode 100644 index 0000000000000..2a43679bdb87f --- /dev/null +++ b/src/test/ui/switched-expectations.rs @@ -0,0 +1,14 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let var = 10i32; + let ref string: String = var; //~ ERROR mismatched types [E0308] +} diff --git a/src/test/ui/switched-expectations.stderr b/src/test/ui/switched-expectations.stderr new file mode 100644 index 0000000000000..822ffeb0d32de --- /dev/null +++ b/src/test/ui/switched-expectations.stderr @@ -0,0 +1,11 @@ +error[E0308]: mismatched types + --> $DIR/switched-expectations.rs:13:30 + | +13 | let ref string: String = var; //~ ERROR mismatched types [E0308] + | ^^^ expected struct `std::string::String`, found i32 + | + = note: expected type `std::string::String` + found type `i32` + +error: aborting due to previous error +