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

Fix #5350 and #4910: make BaseCompiler.preProcess able to modify filters #6051

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

OfekShilon
Copy link
Member

The additional logic requested for rust is "if the source contains 'fn main()' don't build as --crate-type lib", since it would trigger warning: function main is never used.

Some potential hooks for intervention are:

  1. fixIncompatibleOptions - which would have required reading the source from disk,
  2. A new hook with no-op BaseCompiler implementation, somewhere in BaseCompiler.compile
  3. preProcess.

I opted for preProcess since it is already used and does sort-of-similar things in other places, eg

override preProcess(source: string, filters: CompilerOutputOptions): string {
if (this.needsForcedBinary) {
// note: zig versions > 0.6 don't emit asm, only binary works - https://github.com/ziglang/zig/issues/8153
filters.binary = true;
}
return super.preProcess(source, filters);
}

This did require a change of return type (now returns filters too) and some adaptations around.

Would appreciate another opinion on this choice.

…ler.preProcess able to modify `filters`
// #4910, #5350: Avoid rustc warnings when building source with `fn main()` as `--crate-type lib`
const fnMainRe = /fn\s+main\(/;
if (source.match(fnMainRe)) {
filters.binary = true;
Copy link
Member

@dkm dkm Jan 28, 2024

Choose a reason for hiding this comment

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

Are you forcing the link when there's a main function in the input source? Or maybe I'm not reading this correctly?

If fear that it breaks a bit the semantic of the "filter" (that's already badly named). Everywhere in the code, it's used for "please create an executable". If simply writing fn main() triggers this behavior, it may also have unexpected side effects.

Copy link
Member Author

Choose a reason for hiding this comment

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

@dkm I re-read now the previous discussions, in particular #4910 (comment), and I probably misunderstood it originally. The logic you guys agreed on was 'if fn main() is detected*, use --crate-type bin AND --emit asm' ?

-* need to also exclude appearance in comments. The default rust code includes it.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not reading it like that. We can use --crate-type bin for the common case (nothing selected in the filters). As we get the assembly using --emit asm, we won't display external code, but we'll benefit from --crate-type bin not garbage collecting the fn main, if any. In particular, I'm not reading that we are trying to use different options based on the detection of main in the code. The only part where we look for main is when the user selects link to binary and we need to stub it with something if not present (and this is optional as it's not always as trivial as in C).

(sorry for the delay, the notification got flooded and I missed it)

@partouf partouf marked this pull request as draft May 29, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants