Skip to content

Commit 0fc051e

Browse files
committed
gccrs: [E0308] mismatch types on assignment
This errorcode emits when there are mismatch types between lhs & rhs of assignment operator & refactored message. This error code was used in many test cases, so updated the error comments. gcc/rust/ChangeLog: * typecheck/rust-unify.cc (UnifyRules::emit_type_mismatch): refactored & called error function. gcc/testsuite/ChangeLog: * rust/compile/arrays1.rs: changed comment to pass testcase. * rust/compile/bad_type1.rs: likewise. * rust/compile/bad_type2.rs: likewise. * rust/compile/const_generics_6.rs: likewise. * rust/compile/deadcode_err1.rs: likewise. * rust/compile/deadcode_err2.rs: likewise. * rust/compile/func1.rs: likewise. * rust/compile/func3.rs: likewise. * rust/compile/func4.rs: likewise. * rust/compile/func5.rs: likewise. * rust/compile/generics1.rs: likewise. * rust/compile/generics2.rs: likewise. * rust/compile/generics3.rs: likewise. * rust/compile/implicit_returns_err1.rs: likewise. * rust/compile/implicit_returns_err2.rs: likewise. * rust/compile/implicit_returns_err3.rs: likewise. * rust/compile/implicit_returns_err4.rs: likewise. * rust/compile/issue-1152.rs: likewise. * rust/compile/issue-2477.rs: likewise. * rust/compile/reference1.rs: likewise. * rust/compile/stmt_with_block_err1.rs: likewise. * rust/compile/traits1.rs: likewise. * rust/compile/traits2.rs: likewise. * rust/compile/tuple_mismatch.rs: likewise. * rust/compile/tuple_struct3.rs: likewise. * rust/compile/mismatched-types.rs: New test from rustc. Signed-off-by: Muhammad Mahad <[email protected]>
1 parent d20a2c2 commit 0fc051e

27 files changed

+40
-30
lines changed

gcc/rust/typecheck/rust-unify.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ UnifyRules::emit_type_mismatch () const
122122
rich_location r (line_table, locus);
123123
r.add_range (lhs.get_locus ());
124124
r.add_range (rhs.get_locus ());
125-
rust_error_at (r, "expected %<%s%> got %<%s%>",
125+
rust_error_at (r, ErrorCode::E0308,
126+
"mismatched types, expected %qs but got %qs",
126127
expected->get_name ().c_str (), expr->get_name ().c_str ());
127128
}
128129

gcc/testsuite/rust/compile/arrays1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
22
let xs: [i32; 5] = [1, 2, 3, 4, 5];
3-
let a: bool = xs[0]; // { dg-error "expected .bool. got .i32." }
3+
let a: bool = xs[0]; // { dg-error "mismatched types, expected .bool. but got .i32." }
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
let logical: bool = 123; // { dg-error "expected .bool. got .<integer>." }
2+
let logical: bool = 123; // { dg-error "mismatched types, expected .bool. but got .<integer>." }
33
}

gcc/testsuite/rust/compile/bad_type2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88

99
let mut x;
1010
x = 1;
11-
x = true; // { dg-error "expected .<integer>. got .bool." }
11+
x = true; // { dg-error "mismatched types, expected .<integer>. but got .bool." }
1212

1313
let call_test = test(1);
1414
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
struct Foo<const N: usize>;
2-
struct Bar<const N: usize = { 15i32 }>; // { dg-error "expected .usize. got .i32." }
2+
struct Bar<const N: usize = { 15i32 }>; // { dg-error "mismatched types, expected .usize. but got .i32." }

gcc/testsuite/rust/compile/deadcode_err1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn foo() -> i32 {
33

44
let mut a = 1; // { dg-warning "unreachable statement" }
55
a = 1.1; // { dg-warning "unreachable statement" }
6-
// { dg-error "expected .<integer>. got .<float>." "" { target *-*-* } .-1 }
6+
// { dg-error "mismatched types, expected .<integer>. but got .<float>." "" { target *-*-* } .-1 }
77
}
88

99
fn main() {

gcc/testsuite/rust/compile/deadcode_err2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
fn foo() -> i32 {
22
return 1;
3-
return 1.5; // { dg-error "expected .i32. got .<float>." }
3+
return 1.5; // { dg-error "mismatched types, expected .i32. but got .<float>." }
44
// { dg-warning "unreachable statement" "" { target *-*-* } .-1 }
55
}
66

77
fn bar() -> i32 {
8-
return 1.5; // { dg-error "expected .i32. got .<float>." }
8+
return 1.5; // { dg-error "mismatched types, expected .i32. but got .<float>." }
99
return 1;
1010
// { dg-warning "unreachable statement" "" { target *-*-* } .-1 }
1111
}

gcc/testsuite/rust/compile/func1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn test(x: i32) -> bool {
2-
return x + 1; // { dg-error "expected .bool. got .i32." }
2+
return x + 1; // { dg-error "mismatched types, expected .bool. but got .i32." }
33
}
44

55
fn main() {

gcc/testsuite/rust/compile/func3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ fn test(a: i32, b: i32) -> i32 {
44

55
fn main() {
66
let a = test(1, true);
7-
// { dg-error "expected .i32. got .bool." "" { target *-*-* } .-1 }
7+
// { dg-error "mismatched types, expected .i32. but got .bool." "" { target *-*-* } .-1 }
88
}

gcc/testsuite/rust/compile/func4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn func() -> i32 { // { dg-error "expected .i32. got ...." }
1+
fn func() -> i32 { // { dg-error "mismatched types, expected .i32. but got ...." }
22
}
33

44
fn main() {

gcc/testsuite/rust/compile/func5.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn func() -> i32 {
2-
return; // { dg-error "expected .i32. got ...." }
2+
return; // { dg-error "mismatched types, expected .i32. but got ...." }
33
}
44

55
fn main() {

gcc/testsuite/rust/compile/generics1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-error "expected .i32. got .i8." "" { target *-*-* } 0 }
1+
// { dg-error "mismatched types, expected .i32. but got .i8." "" { target *-*-* } 0 }
22

33
#[lang = "sized"]
44
pub trait Sized {}

gcc/testsuite/rust/compile/generics2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-error "expected .i32. got .i8." "" { target *-*-* } 0 }
1+
// { dg-error "mismatched types, expected .i32. but got .i8." "" { target *-*-* } 0 }
22

33
#[lang = "sized"]
44
pub trait Sized {}

gcc/testsuite/rust/compile/generics3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// { dg-error "expected .i32. got .i8." "" { target *-*-* } 0 }
1+
// { dg-error "mismatched types, expected .i32. but got .i8." "" { target *-*-* } 0 }
22
#[lang = "sized"]
33
pub trait Sized {}
44

gcc/testsuite/rust/compile/implicit_returns_err1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn test(x: i32) -> i32 {
2-
if x > 1 { // { dg-error "expected .... got .<integer>." }
2+
if x > 1 { // { dg-error "mismatched types, expected .... but got .<integer>." }
33
1
44
} else {
55
2

gcc/testsuite/rust/compile/implicit_returns_err2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn test(x: i32) -> i32 {
2-
// { dg-error "expected .i32. got .bool." "" { target *-*-* } .-1 }
2+
// { dg-error "mismatched types, expected .i32. but got .bool." "" { target *-*-* } .-1 }
33
return 1;
44
// { dg-warning "unreachable expression" "" { target *-*-* } .+1 }
55
true

gcc/testsuite/rust/compile/implicit_returns_err3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn test(x: i32) -> i32 { // { dg-error "expected .i32. got ...." }
1+
fn test(x: i32) -> i32 { // { dg-error "mismatched types, expected .i32. but got ...." }
22
if x > 1 {
33
1
44
}

gcc/testsuite/rust/compile/implicit_returns_err4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn test(x: bool) -> bool {
2-
// { dg-error "expected .bool. got ...." "" { target *-*-*} .-1 }
2+
// { dg-error "mismatched types, expected .bool. but got ...." "" { target *-*-*} .-1 }
33
return x;
44
// { dg-warning "unreachable expression" "" { target *-*-* } .+1 }
55
()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn test() {
22
let f = [0; -4_isize];
3-
// { dg-error "expected .usize. got .isize." "" { target *-*-* } .-1 }
3+
// { dg-error "mismatched types, expected .usize. but got .isize." "" { target *-*-* } .-1 }
44
let f = [0_usize; -1_isize];
5-
// { dg-error "expected .usize. got .isize." "" { target *-*-* } .-1 }
5+
// { dg-error "mismatched types, expected .usize. but got .isize." "" { target *-*-* } .-1 }
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const FOO: u32 = return 0;
22
// { dg-error "return statement outside of function body" "" { target *-*-* } .-1 }
3-
// { dg-error "expected .u32. got" "" { target *-*-* } .-2 }
3+
// { dg-error "mismatched types, expected .u32. but got" "" { target *-*-* } .-2 }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// ErrorCode::E0308
2+
#![allow(unused)]
3+
fn main() {
4+
fn plus_one(x: i32) -> i32 {
5+
x + 1
6+
}
7+
plus_one("Not a number"); // { dg-error "mismatched types, expected .i32. but got .& str." }
8+
let x: f32 = "Not a float"; // { dg-error "mismatched types, expected .f32. but got .& str." }
9+
}

gcc/testsuite/rust/compile/reference1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ fn main() {
22
let a = &123;
33
let b: &mut i32 = a;
44
// { dg-error "mismatched mutability" "" { target *-*-* } .-1 }
5-
// { dg-error "expected .&mut i32. got .& i32." "" { target *-*-* } .-2 }
5+
// { dg-error "mismatched types, expected .&mut i32. but got .& i32." "" { target *-*-* } .-2 }
66
}

gcc/testsuite/rust/compile/stmt_with_block_err1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
fn test(x: i32) -> i32 {
2-
if x > 1 { // { dg-error "expected .... got .<integer>." }
2+
if x > 1 { // { dg-error "mismatched types, expected .... but got .<integer>." }
33
1
44
} else {
55
2
66
}
77

8-
{ // { dg-error "expected .... got .<integer>." }
8+
{ // { dg-error "mismatched types, expected .... but got .<integer>." }
99
3
1010
}
1111

gcc/testsuite/rust/compile/traits1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub trait Sized {}
33

44
trait Foo {
55
fn Bar() -> i32 {}
6-
// { dg-error "expected .i32. got .()." "" { target *-*-* } .-1 }
6+
// { dg-error "mismatched types, expected .i32. but got .()." "" { target *-*-* } .-1 }
77
}
88

99
struct Baz;

gcc/testsuite/rust/compile/traits2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ pub trait Sized {}
33

44
trait Foo {
55
fn Bar() -> i32 {}
6-
// { dg-error "expected .i32. got .()." "" { target *-*-* } .-1 }
6+
// { dg-error "mismatched types, expected .i32. but got .()." "" { target *-*-* } .-1 }
77
}
88

99
struct Baz;
1010

1111
impl Foo for Baz {
1212
fn Bar() {}
13-
// { dg-error "expected" "" { target *-*-* } .-1 }
13+
// { dg-error "mismatched types, expected" "" { target *-*-* } .-1 }
1414
// { dg-error "method .Bar. has an incompatible type for trait .Foo." "" { target *-*-* } .-2 }
1515
}
1616

gcc/testsuite/rust/compile/tuple_mismatch.rs

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

88
// The lhs and rhs sizes don't match, but we still resolve 'a' to be bool, we don't
99
// error out immediately once we notice the size mismatch.
10-
fn foo() -> i32 { // { dg-error "expected .i32. got .bool." }
10+
fn foo() -> i32 { // { dg-error "mismatched types, expected .i32. but got .bool." }
1111
let (a, _) = (true, 2, 3); // { dg-error "expected a tuple with 3 elements, found one with 2 elements" }
1212
a
1313
}

gcc/testsuite/rust/compile/tuple_struct3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ struct Foo(i32, i32, bool);
22

33
fn main() {
44
let c = Foo(1, 2f32, true);
5-
// { dg-error "expected .i32. got .f32." "" { target *-*-* } .-1 }
5+
// { dg-error "mismatched types, expected .i32. but got .f32." "" { target *-*-* } .-1 }
66
}

0 commit comments

Comments
 (0)