Skip to content

improve c-variadic errors #143546

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

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

Conversation

folkertdev
Copy link
Contributor

@folkertdev folkertdev commented Jul 6, 2025

tracking issue: #44930

Make some errors more specific, and clean up the logic

@rustbot
Copy link
Collaborator

rustbot commented Jul 6, 2025

joshtriplett is not on the review rotation at the moment.
They may take a while to respond.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 6, 2025
Comment on lines 30 to 32
unsafe extern "C" fn trait_method(&self, mut ap: ...) -> i32 {
unsafe { ap.arg() }
}
Copy link
Member

Choose a reason for hiding this comment

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

This raises questions about how we should handle the codegen for its self parameter, dyn compatibility, and so on. I would prefer we not get into those for now and continue to reject this case, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So reject in traits if there is a self parameter of some kind? reject in traits in general?

Previously also just normal associated methods/functions (like in impl S { }) were rejected, that is unproblematic right?

Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to continue to reject in all of the associated function cases for consistency and visit relaxation of this constraint in a deliberate fashion.

There have already been cases where people who work primarily with more frontend-oriented aspects of the compiler have misunderstood C's varargs as purely some syntactic quirk, and thus e.g. easily moved from function to function, instead of the nightmare it really is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough, I'll just add a custom error message then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This now generates. At least we have tests now, none of this was exercised apparently.

error: associated functions cannot have a C-variadic argument
  --> $DIR/no-associated-function.rs:9:46
   |
LL |     unsafe extern "C" fn associated_function(mut ap: ...) -> i32 {
   |  

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from b2e08e9 to 241fdb9 Compare July 6, 2025 22:38
Comment on lines 656 to 658
// Closures cannot be c-variadic (they can't even be `extern "C"`).
self.dcx().emit_err(errors::CVariadicClosure { span: variadic_param.span });
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is currently unreachable in practice, the parser already rejects ... in closures. So, this could be a span_bug instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

huh, span_bug is only defined in rustc_middle, so we can't use that here. panic! or unreachable! also don't occur in this crate.

Copy link
Member

Choose a reason for hiding this comment

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

The AST is weird on this point, yes.

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from 241fdb9 to 81a4a70 Compare July 6, 2025 23:08
@beetrees
Copy link
Contributor

beetrees commented Jul 6, 2025

I think ideally we'd want to reject this in the parser itself (the same way we reject #[cfg(false)] fn f(u32) {}). However, it seems all the syntax gating for c_variadic has been done post-expansion meaning that this has compiled on stable since Rust 1.35.0 (although it seems unlikely anyone is relying on it):

#[cfg(any())] // Equivalent to the more recent #[cfg(false)]
unsafe extern "C" fn bar(_: u32, ...) {}

Given that this is essentially a stability hole, I think it would probably be ok to break it, but it would need a crater run and a lang FCP.

@workingjubilee
Copy link
Member

Sigh. I hate post-expansion syntax gating.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 7, 2025

☔ The latest upstream changes (presumably #143556) made this pull request unmergeable. Please resolve the merge conflicts.

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from 81a4a70 to f92ba67 Compare July 7, 2025 10:15
@folkertdev folkertdev changed the title improve c-variadic errors and reject plain ... improve c-variadic errors Jul 7, 2025
@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from f92ba67 to 8951c42 Compare July 7, 2025 11:02
Copy link
Contributor Author

@folkertdev folkertdev left a comment

Choose a reason for hiding this comment

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

allright, let's handle _: ... separately then.

r? @workingjubilee now that this is no longer making any changes that are relevant for T-lang

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from 8951c42 to 2483477 Compare July 7, 2025 18:01
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from 2483477 to bfc1d8b Compare July 7, 2025 18:57
@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the c-variadic-improve-errors branch from bfc1d8b to f139dd6 Compare July 7, 2025 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants