Skip to content

Commit

Permalink
Label mismatched parameters at the def site for foreign functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 6, 2025
1 parent 79f82ad commit 2dd6dc1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 12 deletions.
12 changes: 9 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};

Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/extern-fn-arg-names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/c-variadic/variadic-ffi-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
Expand All @@ -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 */);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0060.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 */); }
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/fn/param-mismatch-foreign.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions tests/ui/fn/param-mismatch-foreign.stderr
Original file line number Diff line number Diff line change
@@ -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`.
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/issue-26480.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/suggestions/suggest-null-ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 2dd6dc1

Please sign in to comment.