Skip to content

Improve E0617 to Distinguish Between Fn Item and Fn Pointer in FFI #120197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{errors, structured_errors::StructuredDiagnostic};
use rustc_errors::{codes::*, DiagnosticBuilder, ErrCode};
use rustc_errors::{codes::*, Applicability, DiagnosticBuilder, ErrCode};
use rustc_middle::ty::{Ty, TypeVisitableExt};
use rustc_session::Session;
use rustc_span::Span;
Expand Down Expand Up @@ -41,6 +41,20 @@ impl<'tcx> StructuredDiagnostic<'tcx> for MissingCastForVariadicArg<'tcx, '_> {
err.downgrade_to_delayed_bug();
}

let msg = if self.ty.is_fn() {
err.help("a function item is zero-sized and needs to be casted into a function pointer to be used in FFI")
.note("for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html");
"cast the value into a function pointer".to_string()
} else {
format!("cast the value to `{}`", self.cast_ty)
};
err.span_suggestion_verbose(
self.span.shrink_to_hi(),
msg,
format!(" as {}", self.cast_ty),
Applicability::MachineApplicable,
);

err
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
p!(print_def_path(def_id, args));
} else {
let sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args);
p!(print(sig), " {{", print_value_path(def_id, args), "}}");
p!("{{fn item ", print_value_path(def_id, args), ": ", print(sig), "}}");
}
}
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/asm/x86_64/type-check-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() {
let mut r = &mut 0;
asm!("{}", in(reg) f);
asm!("{}", inout(reg) f);
//~^ ERROR cannot use value of type `fn() {main}` for inline assembly
//~^ ERROR cannot use value of type `{fn item main: fn()}` for inline assembly
asm!("{}", in(reg) r);
asm!("{}", inout(reg) r);
//~^ ERROR cannot use value of type `&mut i32` for inline assembly
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/asm/x86_64/type-check-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ LL | asm!("{}", in(reg) [1, 2, 3]);
|
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly

error: cannot use value of type `fn() {main}` for inline assembly
error: cannot use value of type `{fn item main: fn()}` for inline assembly
--> $DIR/type-check-2.rs:66:31
|
LL | asm!("{}", inout(reg) f);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-types/substs-ppaux.normal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
found fn item `{fn item <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
Expand All @@ -28,7 +28,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
found fn item `{fn item <i8 as Foo<'static, 'static>>::bar::<'static, char>: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
Expand All @@ -46,7 +46,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
found fn item `{fn item <i8 as Foo<'static, 'static, u8>>::baz: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
Expand All @@ -64,7 +64,7 @@ LL | let x: () = foo::<'static>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {foo::<'static>}`
found fn item `{fn item foo::<'static>: fn()}`
help: use parentheses to call this function
|
LL | let x: () = foo::<'static>();
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/associated-types/substs-ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ fn foo<'z>() where &'z (): Sized {
let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>}`
//[verbose]~| found fn item `{fn item <i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>: fn()}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::bar::<'static, char>}`
//[normal]~| found fn item `{fn item <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>: fn()}`


let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
//[verbose]~| found fn item `{fn item <i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>: fn()}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`
//[normal]~| found fn item `{fn item <i8 as Foo<'static, 'static>>::bar::<'static, char>: fn()}`

let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz}`
//[verbose]~| found fn item `{fn item <i8 as Foo<ReStatic, ReStatic, u8>>::baz: fn()}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {<i8 as Foo<'static, 'static, u8>>::baz}`
//[normal]~| found fn item `{fn item <i8 as Foo<'static, 'static, u8>>::baz: fn()}`

let x: () = foo::<'static>;
//[verbose]~^ ERROR mismatched types
//[verbose]~| expected unit type `()`
//[verbose]~| found fn item `fn() {foo::<ReStatic>}`
//[verbose]~| found fn item `{fn item foo::<ReStatic>: fn()}`
//[normal]~^^^^ ERROR mismatched types
//[normal]~| expected unit type `()`
//[normal]~| found fn item `fn() {foo::<'static>}`
//[normal]~| found fn item `{fn item foo::<'static>: fn()}`

<str as Foo<u8>>::bar;
//[verbose]~^ ERROR the size for values of type
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-types/substs-ppaux.verbose.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>}`
found fn item `{fn item <i8 as Foo<ReStatic, ReStatic, u8>>::bar::<ReStatic, char>: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::bar::<'static, char>();
Expand All @@ -28,7 +28,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
found fn item `{fn item <i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>();
Expand All @@ -46,7 +46,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {<i8 as Foo<ReStatic, ReStatic, u8>>::baz}`
found fn item `{fn item <i8 as Foo<ReStatic, ReStatic, u8>>::baz: fn()}`
help: use parentheses to call this associated function
|
LL | let x: () = <i8 as Foo<'static, 'static, u8>>::baz();
Expand All @@ -64,7 +64,7 @@ LL | let x: () = foo::<'static>;
| expected due to this
|
= note: expected unit type `()`
found fn item `fn() {foo::<ReStatic>}`
found fn item `{fn item foo::<ReStatic>: fn()}`
help: use parentheses to call this function
|
LL | let x: () = foo::<'static>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | | Next(&Buffered(Map(Empty(PhantomData), ready::<&()>), FuturesOrde
LL | | });
| |______^ implementation of `FnOnce` is not general enough
|
= note: `fn(&'0 ()) -> std::future::Ready<&'0 ()> {std::future::ready::<&'0 ()>}` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
= note: `{fn item std::future::ready::<&'0 ()>: fn(&'0 ()) -> std::future::Ready<&'0 ()>}` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `FnOnce<(&(),)>`

error: implementation of `FnOnce` is not general enough
Expand All @@ -21,7 +21,7 @@ LL | | Next(&Buffered(Map(Empty(PhantomData), ready::<&()>), FuturesOrde
LL | | });
| |______^ implementation of `FnOnce` is not general enough
|
= note: `fn(&'0 ()) -> std::future::Ready<&'0 ()> {std::future::ready::<&'0 ()>}` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
= note: `{fn item std::future::ready::<&'0 ()>: fn(&'0 ()) -> std::future::Ready<&'0 ()>}` must implement `FnOnce<(&'1 (),)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `FnOnce<(&(),)>`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/binop/issue-77910-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn main() {
// we shouldn't ice with the bound var here.
assert_eq!(foo, y);
//~^ ERROR binary operation `==` cannot be applied to type
//~| ERROR `for<'a> fn(&'a i32) -> &'a i32 {foo}` doesn't implement `Debug`
//~| ERROR `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}` doesn't implement `Debug`
}
10 changes: 5 additions & 5 deletions tests/ui/binop/issue-77910-1.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
error[E0369]: binary operation `==` cannot be applied to type `for<'a> fn(&'a i32) -> &'a i32 {foo}`
error[E0369]: binary operation `==` cannot be applied to type `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}`
--> $DIR/issue-77910-1.rs:8:5
|
LL | assert_eq!(foo, y);
| ^^^^^^^^^^^^^^^^^^
| |
| for<'a> fn(&'a i32) -> &'a i32 {foo}
| {fn item foo: for<'a> fn(&'a i32) -> &'a i32}
| _
|
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: `for<'a> fn(&'a i32) -> &'a i32 {foo}` doesn't implement `Debug`
error[E0277]: `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}` doesn't implement `Debug`
--> $DIR/issue-77910-1.rs:8:5
|
LL | fn foo(s: &i32) -> &i32 {
| --- consider calling this function
...
LL | assert_eq!(foo, y);
| ^^^^^^^^^^^^^^^^^^ `for<'a> fn(&'a i32) -> &'a i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| ^^^^^^^^^^^^^^^^^^ `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
= help: the trait `Debug` is not implemented for fn item `for<'a> fn(&'a i32) -> &'a i32 {foo}`
= help: the trait `Debug` is not implemented for fn item `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}`
= help: use parentheses to call this function: `foo(/* &i32 */)`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/binop/issue-77910-2.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0369]: binary operation `==` cannot be applied to type `for<'a> fn(&'a i32) -> &'a i32 {foo}`
error[E0369]: binary operation `==` cannot be applied to type `{fn item foo: for<'a> fn(&'a i32) -> &'a i32}`
--> $DIR/issue-77910-2.rs:7:12
|
LL | if foo == y {}
| --- ^^ - _
| |
| for<'a> fn(&'a i32) -> &'a i32 {foo}
| {fn item foo: for<'a> fn(&'a i32) -> &'a i32}
|
help: use parentheses to call this function
|
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/c-variadic/issue-32201.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ fn bar(_: *const u8) {}
fn main() {
unsafe {
foo(0, bar);
//~^ ERROR can't pass `fn(*const u8) {bar}` to variadic function
//~| HELP cast the value to `fn(*const u8)`
//~^ ERROR can't pass `{fn item bar: fn(*const u8)}` to variadic function
//~| HELP a function item is zero-sized and needs to be casted into a function pointer to be used in FFI
//~| HELP cast the value into a function pointer
//~| HELP cast the value to `fn(*const u8)
}
}
15 changes: 13 additions & 2 deletions tests/ui/c-variadic/issue-32201.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
error[E0617]: can't pass `fn(*const u8) {bar}` to variadic function
error[E0617]: can't pass `{fn item bar: fn(*const u8)}` to variadic function
--> $DIR/issue-32201.rs:9:16
|
LL | foo(0, bar);
| ^^^ help: cast the value to `fn(*const u8)`: `bar as fn(*const u8)`
| ^^^
|
= help: a function item is zero-sized and needs to be casted into a function pointer to be used in FFI
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
help: cast the value to `fn(*const u8)`
|
LL | foo(0, bar as fn(*const u8));
| ~~~~~~~~~~~~~~~~~~~~
help: cast the value into a function pointer
|
LL | foo(0, bar as fn(*const u8));
| ++++++++++++++++

error: aborting due to 1 previous error

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/c-variadic/issue-69232.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extern "C" {
fn foo(x: usize, ...);
}

fn test() -> u8 {
127
}

fn main() {
foo(1, test);
//~^ ERROR can't pass `{fn item test: fn() -> u8}` to variadic function
}
20 changes: 20 additions & 0 deletions tests/ui/c-variadic/issue-69232.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0617]: can't pass `{fn item test: fn() -> u8}` to variadic function
--> $DIR/issue-69232.rs:10:12
|
LL | foo(1, test);
| ^^^^
|
= help: a function item is zero-sized and needs to be casted into a function pointer to be used in FFI
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
help: cast the value to `fn() -> u8`
|
LL | foo(1, test as fn() -> u8);
| ~~~~~~~~~~~~~~~~~~
help: cast the value into a function pointer
|
LL | foo(1, test as fn() -> u8);
| +++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0617`.
Loading