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

New format args template #101272

Closed
wants to merge 1 commit into from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Sep 1, 2022

This PR is a template for new experimental fmt::Arguments implementations. If you want to try out a new fmt::Arguments implementation, this branch is a useful place to start from.

$ git fetch https://github.com/rust-lang/rust refs/pull/101272/head
$ git switch -c new-fmt-arguments FETCH_HEAD

To implement a new fmt::Arguments, you need to:

  • Implement the part of rustc that expands format_args!(), in compiler/rustc_builtin_macros/src/format/expand.rs.

    • The expand_parsed_format_args function takes a FormatArgs struct representing the already parsed/processed format_args!(), and you need to return the expanded ast expression that will create a new fmt::Arguments at runtime.
  • Implement the new fmt::Arguments struct in library/core/src/fmt/mod.rs.

    • You can't delete the old code here, since the beta/bootstrap compiler will be used to compile this for stage 0, with the old format_args!() macro implementation. Instead, you 'remove' old code by adding #[cfg(boostrap)] and add new code with #[cfg(not(bootstrap))]. (Old code is removed when the bootstrap/beta compiler is bumped to the next version.)
    • Look for the TODO comments, which point out the Arguments struct and the methods you need to implement. I've already put all the old code under #[cfg(bootstrap)] and created new empty placeholders for the new code.
    • Specifically, you need to implement:
      • struct Arguments<'a>: the structure that represents a complete format_args!() at runtime.
      • Arguments::new: the function used in the expansion of format_args!() to create this struct. Which arguments this takes is up to you, as it only needs to match your macro expansion.
      • Arguments::from_static_str: this should create an Arguments struct that represents exactly the given string.
      • Arguments::as_str: this should return the string in trivial cases like format_args!("hello") or Arguments::from_static_str("hello"), or None otherwise.
      • Arguments::estimated_capacity: this should return the estimated size of the complete output when displayed. It doesn't have to be very accurate. See the old implementation for an example.
      • write: This function takes your fmt::Arguments and should write/display it to a &mut dyn Write.

If you have something ready for sharing or testing, feel free to open a draft PR and drop a comment in #99012.

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 1, 2022
@rust-highfive

This comment was marked as outdated.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 1, 2022
@m-ou-se m-ou-se assigned m-ou-se and unassigned estebank Sep 1, 2022
@m-ou-se m-ou-se added S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 1, 2022
@m-ou-se m-ou-se force-pushed the new-format-args-template branch 2 times, most recently from 140952d to 70ff7a5 Compare September 8, 2022 10:31
@bors
Copy link
Contributor

bors commented Sep 21, 2022

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

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 21, 2022
@bors
Copy link
Contributor

bors commented Jan 26, 2023

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

@m-ou-se
Copy link
Member Author

m-ou-se commented Jan 27, 2023

Now that #106745 is merged, this is outdated.

@m-ou-se m-ou-se closed this Jan 27, 2023
@m-ou-se m-ou-se deleted the new-format-args-template branch January 27, 2023 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-experimental Status: Ongoing experiment that does not require reviewing and won't be merged in its current state. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants