Skip to content
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

Simplify hir_typeck_pass_to_variadic_function #133620

Merged
Merged
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
1 change: 0 additions & 1 deletion compiler/rustc_hir_typeck/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ hir_typeck_option_result_copied = use `{$def_path}::copied` to copy the value in

hir_typeck_pass_to_variadic_function = can't pass `{$ty}` to variadic function
.suggestion = cast the value to `{$cast_ty}`
.help = cast the value to `{$cast_ty}`
.teach_help = certain types, like `{$ty}`, must be casted before passing them to a variadic function, because of arcane ABI rules dictated by the C standard

hir_typeck_ptr_cast_add_auto_to_object = adding {$traits_len ->
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,8 @@ pub(crate) struct PassToVariadicFunction<'a, 'tcx> {
pub span: Span,
pub ty: Ty<'tcx>,
pub cast_ty: &'a str,
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
pub sugg_span: Option<Span>,
pub replace: String,
#[help]
pub help: bool,
#[suggestion(code = " as {cast_ty}", applicability = "machine-applicable", style = "verbose")]
pub sugg_span: Span,
#[note(hir_typeck_teach_help)]
pub(crate) teach: bool,
}
Expand Down
11 changes: 1 addition & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,20 +440,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty: Ty<'tcx>,
cast_ty: &str,
) {
let (sugg_span, replace, help) =
if let Ok(snippet) = sess.source_map().span_to_snippet(span) {
(Some(span), format!("{snippet} as {cast_ty}"), false)
} else {
(None, "".to_string(), true)
};

sess.dcx().emit_err(errors::PassToVariadicFunction {
span,
ty,
cast_ty,
help,
replace,
sugg_span,
sugg_span: span.shrink_to_hi(),
teach: sess.teach(E0617),
});
}
Expand Down
42 changes: 36 additions & 6 deletions tests/ui/c-variadic/variadic-ffi-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,67 @@ error[E0617]: can't pass `f32` to variadic function
--> $DIR/variadic-ffi-1.rs:28:19
|
LL | foo(1, 2, 3f32);
| ^^^^ help: cast the value to `c_double`: `3f32 as c_double`
| ^^^^
|
help: cast the value to `c_double`
|
LL | foo(1, 2, 3f32 as c_double);
| +++++++++++

error[E0617]: can't pass `bool` to variadic function
--> $DIR/variadic-ffi-1.rs:29:19
|
LL | foo(1, 2, true);
| ^^^^ help: cast the value to `c_int`: `true as c_int`
| ^^^^
|
help: cast the value to `c_int`
|
LL | foo(1, 2, true as c_int);
| ++++++++

error[E0617]: can't pass `i8` to variadic function
--> $DIR/variadic-ffi-1.rs:30:19
|
LL | foo(1, 2, 1i8);
| ^^^ help: cast the value to `c_int`: `1i8 as c_int`
| ^^^
|
help: cast the value to `c_int`
|
LL | foo(1, 2, 1i8 as c_int);
| ++++++++

error[E0617]: can't pass `u8` to variadic function
--> $DIR/variadic-ffi-1.rs:31:19
|
LL | foo(1, 2, 1u8);
| ^^^ help: cast the value to `c_uint`: `1u8 as c_uint`
| ^^^
|
help: cast the value to `c_uint`
|
LL | foo(1, 2, 1u8 as c_uint);
| +++++++++

error[E0617]: can't pass `i16` to variadic function
--> $DIR/variadic-ffi-1.rs:32:19
|
LL | foo(1, 2, 1i16);
| ^^^^ help: cast the value to `c_int`: `1i16 as c_int`
| ^^^^
|
help: cast the value to `c_int`
|
LL | foo(1, 2, 1i16 as c_int);
| ++++++++

error[E0617]: can't pass `u16` to variadic function
--> $DIR/variadic-ffi-1.rs:33:19
|
LL | foo(1, 2, 1u16);
| ^^^^ help: cast the value to `c_uint`: `1u16 as c_uint`
| ^^^^
|
help: cast the value to `c_uint`
|
LL | foo(1, 2, 1u16 as c_uint);
| +++++++++

error: aborting due to 11 previous errors

Expand Down
35 changes: 30 additions & 5 deletions tests/ui/error-codes/E0617.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,56 @@ error[E0617]: can't pass `f32` to variadic function
--> $DIR/E0617.rs:7:36
|
LL | printf(::std::ptr::null(), 0f32);
| ^^^^ help: cast the value to `c_double`: `0f32 as c_double`
| ^^^^
|
help: cast the value to `c_double`
|
LL | printf(::std::ptr::null(), 0f32 as c_double);
| +++++++++++

error[E0617]: can't pass `i8` to variadic function
--> $DIR/E0617.rs:10:36
|
LL | printf(::std::ptr::null(), 0i8);
| ^^^ help: cast the value to `c_int`: `0i8 as c_int`
| ^^^
|
help: cast the value to `c_int`
|
LL | printf(::std::ptr::null(), 0i8 as c_int);
| ++++++++

error[E0617]: can't pass `i16` to variadic function
--> $DIR/E0617.rs:13:36
|
LL | printf(::std::ptr::null(), 0i16);
| ^^^^ help: cast the value to `c_int`: `0i16 as c_int`
| ^^^^
|
help: cast the value to `c_int`
|
LL | printf(::std::ptr::null(), 0i16 as c_int);
| ++++++++

error[E0617]: can't pass `u8` to variadic function
--> $DIR/E0617.rs:16:36
|
LL | printf(::std::ptr::null(), 0u8);
| ^^^ help: cast the value to `c_uint`: `0u8 as c_uint`
| ^^^
|
help: cast the value to `c_uint`
|
LL | printf(::std::ptr::null(), 0u8 as c_uint);
| +++++++++

error[E0617]: can't pass `u16` to variadic function
--> $DIR/E0617.rs:19:36
|
LL | printf(::std::ptr::null(), 0u16);
| ^^^^ help: cast the value to `c_uint`: `0u16 as c_uint`
| ^^^^
|
help: cast the value to `c_uint`
|
LL | printf(::std::ptr::null(), 0u16 as c_uint);
| +++++++++

error[E0617]: can't pass a function item to a variadic function
--> $DIR/E0617.rs:22:36
Expand Down
Loading