From: Philip Herron <herron.phi...@googlemail.com>

We have the type information for the resolved call lets tell the user about
it in the diagnostic and apply the correct error code.

Fixes Rust-GCC#2035

gcc/rust/ChangeLog:

        * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): improve 
error diag

gcc/testsuite/ChangeLog:

        * rust/compile/generics4.rs: cleanup
        * rust/compile/generics6.rs: likewise
        * rust/compile/type-bindings1.rs: likewise
        * rust/compile/unconstrained_type_param.rs: likewise
        * rust/compile/issue-2035.rs: New test.

Signed-off-by: Philip Herron <herron.phi...@googlemail.com>
---
 gcc/rust/typecheck/rust-hir-type-check-expr.cc      | 13 +++++++++----
 gcc/testsuite/rust/compile/generics4.rs             |  1 -
 gcc/testsuite/rust/compile/generics6.rs             |  1 -
 gcc/testsuite/rust/compile/issue-2035.rs            | 10 ++++++++++
 gcc/testsuite/rust/compile/type-bindings1.rs        |  1 -
 .../rust/compile/unconstrained_type_param.rs        |  1 -
 6 files changed, 19 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/issue-2035.rs

diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc 
b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 5c38cb42b39..85d717535ed 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -225,12 +225,17 @@ TypeCheckExpr::visit (HIR::CallExpr &expr)
   if (resolved_fn_trait_call)
     return;
 
-  bool valid_tyty = function_tyty->get_kind () == TyTy::TypeKind::FNDEF
-                   || function_tyty->get_kind () == TyTy::TypeKind::FNPTR;
+  bool valid_tyty
+    = function_tyty->is<TyTy::FnType> () || function_tyty->is<TyTy::FnPtr> ();
   if (!valid_tyty)
     {
-      rust_error_at (expr.get_locus (),
-                    "Failed to resolve expression of function call");
+      bool emit_error = !function_tyty->is<TyTy::ErrorType> ();
+      if (emit_error)
+       {
+         rich_location r (line_table, expr.get_locus ());
+         rust_error_at (r, ErrorCode::E0618, "expected function, found %<%s%>",
+                        function_tyty->get_name ().c_str ());
+       }
       return;
     }
 
diff --git a/gcc/testsuite/rust/compile/generics4.rs 
b/gcc/testsuite/rust/compile/generics4.rs
index 31b681abb10..c4dbc432c07 100644
--- a/gcc/testsuite/rust/compile/generics4.rs
+++ b/gcc/testsuite/rust/compile/generics4.rs
@@ -6,7 +6,6 @@ struct GenericStruct<T>(T, usize);
 fn main() {
     let a2;
     a2 = GenericStruct::<i8, i32>(1, 456); // { dg-error "generic item takes 
at most 1 type arguments but 2 were supplied" }
-                                           // { dg-error {Failed to resolve 
expression of function call} "" { target *-*-* } .-1 }
 
     let b2: i32 = a2.0;
     // { dg-error {Expected Tuple or ADT got: T\?} "" { target *-*-* } .-1 }
diff --git a/gcc/testsuite/rust/compile/generics6.rs 
b/gcc/testsuite/rust/compile/generics6.rs
index 33093cf706b..d77c559db21 100644
--- a/gcc/testsuite/rust/compile/generics6.rs
+++ b/gcc/testsuite/rust/compile/generics6.rs
@@ -27,6 +27,5 @@ impl Foo<f32> {
 
 fn main() {
     let a: i32 = Foo::test(); // { dg-error "multiple applicable items in 
scope for: .test." }
-    // { dg-error {Failed to resolve expression of function call} "" { target 
*-*-* } .-1 }
 }
 
diff --git a/gcc/testsuite/rust/compile/issue-2035.rs 
b/gcc/testsuite/rust/compile/issue-2035.rs
new file mode 100644
index 00000000000..c0817d532cc
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2035.rs
@@ -0,0 +1,10 @@
+fn func(i: i32) {
+    i();
+    // { dg-error "expected function, found .i32. .E0618." "" { target *-*-* } 
.-1 }
+}
+
+fn main() {
+    let i = 0i32;
+    i();
+    // { dg-error "expected function, found .i32. .E0618." "" { target *-*-* } 
.-1 }
+}
diff --git a/gcc/testsuite/rust/compile/type-bindings1.rs 
b/gcc/testsuite/rust/compile/type-bindings1.rs
index 358035bbc17..ef0b47128df 100644
--- a/gcc/testsuite/rust/compile/type-bindings1.rs
+++ b/gcc/testsuite/rust/compile/type-bindings1.rs
@@ -7,5 +7,4 @@ fn main() {
     let a;
     a = Foo::<A = i32, B = f32>(123f32);
     // { dg-error "associated type bindings are not allowed here" "" { target 
*-*-* } .-1 }
-    // { dg-error {Failed to resolve expression of function call} "" { target 
*-*-* } .-2 }
 }
diff --git a/gcc/testsuite/rust/compile/unconstrained_type_param.rs 
b/gcc/testsuite/rust/compile/unconstrained_type_param.rs
index 1cef0b983b0..60554dac0e0 100644
--- a/gcc/testsuite/rust/compile/unconstrained_type_param.rs
+++ b/gcc/testsuite/rust/compile/unconstrained_type_param.rs
@@ -13,5 +13,4 @@ impl<X, Y> Foo<X> {
 fn main() {
     let a = Foo::test();
     // { dg-error "expected" "" { target *-*-* } .-1 }
-    // { dg-error "Failed to resolve expression of function call" "" { target 
*-*-* } .-2 }
 }
-- 
2.45.2

Reply via email to