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

Better diagnostic for fn items in variadic functions #133538

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dev-ardi
Copy link
Contributor

closes #69232

@rustbot
Copy link
Collaborator

rustbot commented Nov 27, 2024

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@dev-ardi dev-ardi changed the title 69232 better diag Better diagnostic for fn items in variadic functions Nov 27, 2024
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how you coerce a function item to a function pointer. Instead, you've suggested to turn something into a pointer-to-a-fn-item (i.e. a pointer to a ZST). You need to use as fn() to coerce a function item to a function pointer.

Please fix this, and also squash this into one commit.

let span = arg.span;
let (sugg_span, replace) =
if let Ok(snippet) = sess.source_map().span_to_snippet(span) {
(Some(span), format!("&{snippet}"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you also don't need to format the suggestion as a snippet. You can use span.shrink_to_{hi,lo}() to get just the beginning or end of the span to add just the part you needed (in this case &, but in the corrected case as ...)

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 exporting to docker image format
#16 sending tarball 27.1s done
#16 DONE 33.0s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
1 error[E0617]: can't pass a function item to a variadic function
-   --> $DIR/issue-69232.rs:12:21
+   --> $DIR/fn-item-diagnostic-issue-69232.rs:12:21
3    |
4 LL |     unsafe { foo(1, test) };
5    |                     ^^^^ help: use a function pointer instead: `&test`

The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args c-variadic/fn-item-diagnostic-issue-69232.rs`
To only update this specific test, also pass `--test-args c-variadic/fn-item-diagnostic-issue-69232.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/c-variadic/fn-item-diagnostic-issue-69232.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/c-variadic/fn-item-diagnostic-issue-69232" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0617]: can't pass a function item to a variadic function
##[error]  --> /checkout/tests/ui/c-variadic/fn-item-diagnostic-issue-69232.rs:12:21
   |
   |
LL |     unsafe { foo(1, test) }; //~ ERROR can't pass a function item to a variadic function
   |                     ^^^^ help: use a function pointer instead: `&test`
   = help: a function item is zero-sized and needs to be casted into a pointer-sized 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

error: aborting due to 1 previous error

@@ -79,6 +79,11 @@ hir_typeck_field_multiply_specified_in_initializer =
.label = used more than once
.previous_use_label = first use of `{$ident}`
hir_typeck_fn_item_to_variadic_function = can't pass a function item to a variadic function
.suggestion = use a function pointer instead
.help = a function item is zero-sized and needs to be casted into a pointer-sized function pointer to be used in FFI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, the past participle of cast is "cast"

Suggested change
.help = a function item is zero-sized and needs to be casted into a pointer-sized function pointer to be used in FFI
.help = a function item is zero-sized and needs to be cast into a function pointer to be used in FFI

Also, the fact that it's zero-sized doesn't seem to me to be the most important part of this issue. The issue here is more that the intention is wrong, since you're not passing a function pointer like you expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Printed function definition types can be confusing
5 participants