Label mismatched parameters at the def site for foreign functions.

This commit is contained in:
Jason Newcomb 2025-02-06 14:06:15 -05:00
parent 79f82ad5e8
commit 2dd6dc1f86
8 changed files with 48 additions and 12 deletions

View file

@ -2641,8 +2641,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
/// Returns the parameters of a function, with their generic parameters if those are the full
/// type of that parameter. Returns `None` if the function has no generics or the body is
/// unavailable (eg is an instrinsic).
/// type of that parameter.
///
/// Returns `None` if the body is not a named function (e.g. a closure).
fn get_hir_param_info(
&self,
def_id: DefId,
@ -2667,6 +2668,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
kind: hir::ItemKind::Fn { sig, generics, body, .. },
..
}) => (sig, generics, Some(body), None),
hir::Node::ForeignItem(&hir::ForeignItem {
kind: hir::ForeignItemKind::Fn(sig, params, generics),
..
}) => (sig, generics, None, Some(params)),
_ => return None,
};
@ -2700,7 +2705,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
))
}
(None, Some(params)) => {
let params = params.get(is_method as usize..)?;
let params =
params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?;
debug_assert_eq!(params.len(), fn_inputs.len());
Some((
fn_inputs.zip(params.iter().map(|param| FnParam::Name(param))).collect(),

View file

@ -14,7 +14,7 @@ note: function defined here
--> $DIR/extern-fn-arg-names.rs:2:8
|
LL | fn dstfn(src: i32, dst: err);
| ^^^^^
| ^^^^^ ---
help: provide the argument
|
LL | dstfn(1, /* dst */);

View file

@ -14,7 +14,7 @@ note: function defined here
--> $DIR/variadic-ffi-1.rs:15:8
|
LL | fn foo(f: isize, x: u8, ...);
| ^^^
| ^^^ - -
help: provide the arguments
|
LL | foo(/* isize */, /* u8 */);
@ -30,7 +30,7 @@ note: function defined here
--> $DIR/variadic-ffi-1.rs:15:8
|
LL | fn foo(f: isize, x: u8, ...);
| ^^^
| ^^^ -
help: provide the argument
|
LL | foo(1, /* u8 */);

View file

@ -8,7 +8,7 @@ note: function defined here
--> $DIR/E0060.rs:2:8
|
LL | fn printf(_: *const u8, ...) -> u32;
| ^^^^^^
| ^^^^^^ -
help: provide the argument
|
LL | unsafe { printf(/* *const u8 */); }

View file

@ -0,0 +1,11 @@
extern "C" {
fn foo(x: i32, y: u32, z: i32);
//~^ NOTE function defined here
}
fn main() {
foo(1i32, 2i32);
//~^ ERROR this function takes 3 arguments but 2 arguments were supplied
//~| NOTE argument #2 of type `u32` is missing
//~| HELP provide the argument
}

View file

@ -0,0 +1,19 @@
error[E0061]: this function takes 3 arguments but 2 arguments were supplied
--> $DIR/param-mismatch-foreign.rs:7:5
|
LL | foo(1i32, 2i32);
| ^^^ ---- argument #2 of type `u32` is missing
|
note: function defined here
--> $DIR/param-mismatch-foreign.rs:2:8
|
LL | fn foo(x: i32, y: u32, z: i32);
| ^^^ -
help: provide the argument
|
LL | foo(1i32, /* u32 */, 2i32);
| ~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0061`.

View file

@ -13,7 +13,7 @@ note: function defined here
--> $DIR/issue-26480.rs:2:8
|
LL | fn write(fildes: i32, buf: *const i8, nbyte: u64) -> i64;
| ^^^^^
| ^^^^^ -----
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can convert a `usize` to a `u64` and panic if the converted value doesn't fit
|

View file

@ -12,7 +12,7 @@ note: function defined here
--> $DIR/suggest-null-ptr.rs:7:8
|
LL | fn foo(ptr: *const u8);
| ^^^
| ^^^ ---
help: if you meant to create a null pointer, use `std::ptr::null()`
|
LL | foo(std::ptr::null());
@ -32,7 +32,7 @@ note: function defined here
--> $DIR/suggest-null-ptr.rs:9:8
|
LL | fn foo_mut(ptr: *mut u8);
| ^^^^^^^
| ^^^^^^^ ---
help: if you meant to create a null pointer, use `std::ptr::null_mut()`
|
LL | foo_mut(std::ptr::null_mut());
@ -52,7 +52,7 @@ note: function defined here
--> $DIR/suggest-null-ptr.rs:11:8
|
LL | fn usize(ptr: *const usize);
| ^^^^^
| ^^^^^ ---
help: if you meant to create a null pointer, use `std::ptr::null()`
|
LL | usize(std::ptr::null());
@ -72,7 +72,7 @@ note: function defined here
--> $DIR/suggest-null-ptr.rs:13:8
|
LL | fn usize_mut(ptr: *mut usize);
| ^^^^^^^^^
| ^^^^^^^^^ ---
help: if you meant to create a null pointer, use `std::ptr::null_mut()`
|
LL | usize_mut(std::ptr::null_mut());