Skip to content

Minic rustc's new format_args! expansion #20056

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

Merged
merged 2 commits into from
Jun 22, 2025

Conversation

ShoyuVanilla
Copy link
Member

@ShoyuVanilla ShoyuVanilla commented Jun 21, 2025

Fixes #19929 and fixes #20051

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 21, 2025
@@ -31,6 +31,7 @@
//! eq: sized
//! error: fmt
//! fmt: option, result, transmute, coerce_unsized, copy, clone, derive
//! fmt_since_1_89_0: fmt
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's a bit weird if activating just fmt will activate the old fmt, better for it to activate the newest.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point. I'll fix those names along with the method names in expr_store/lower.rs

// Assume that rustc version >= 1.89.0 iff lang item `format_arguments` exists
// but `format_unsafe_arg` does not
let fmt_args =
crate::lang_item::lang_item(self.db, self.module.krate(), LangItem::FormatArguments);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit worried about the impact of searching for lang items in body lowering, as body lowering should stay fast.

I guess it's fine because lang items generally remain unchanged so they will be cached.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I agree to your both points: body lowering should be fast and in general, lang item caches persists.
But I think there would be no regression wrt this since we are already doing lang item query in current implementation:

let new_v1_formatted = LangItem::FormatArguments.ty_rel_path(
self.db,
self.module.krate(),
Name::new_symbol_root(sym::new_v1_formatted),
);
let unsafe_arg_new = LangItem::FormatUnsafeArg.ty_rel_path(
self.db,
self.module.krate(),
Name::new_symbol_root(sym::new),
);

}

/// `format_args!` expansion implementation for rustc versions < `1.89.0`
fn collect_format_args_impl(
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe better to call this collect_format_args_old()? Although I'd prefer the names to tell the toolchain version, I don't want collect_format_args_really_new() if we get yet another version.

.alloc_expr_desugared(Expr::Call { callee: new_v1_formatted, args: args.into() });

// We collect the unused expressions here so that we still infer them instead of
// dropping them out of the expression tree.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not good as it means they will be under an unsafe block, so will not need unsafe.

But the test testing that did not fire... So I don't know what's going on.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, you're right! I missed this point because it didn't failed orphan unsafe test.
I checked why the test didn't failed and found out that the following code:

fn main() {
    let p = 3735928559 as *const i32;
    format_args!("", *p);
}

is expanded into:

fn main() {
    let p = 3735928559 as *const i32;
    {
        let args = (&*p, );
                  //^^^ Orphans are once assigned into first `arg`
        let args = [];
        unsafe {
            *p;
            builtin#lang(Arguments::new_v1_formatted)(
                &[],
                &args,
                &[],
            )
        }
    };
}

And thus, deleting those unnecessary and incorrect lines

fn main() {
let are = "are";
let count = 10;
builtin#format_args("\u{1b}hello {count:02} {} friends, we {are:?} {0}{last}", "fancy", last = "!");
builtin#format_args("\u{1b}hello {count:02} {} friends, we {are:?} {0}{last}", "fancy", orphan = (), last = "!");
Copy link
Member Author

Choose a reason for hiding this comment

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

I added a new arg orphan to check expansion behaviour or orphans

@@ -1474,20 +1474,18 @@ fn main() {
}
"#,
expect![[r#"
me foo() fn(&self)
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Jun 22, 2025

Choose a reason for hiding this comment

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

So, with the new format_args!, the completion works even with formatting with invalid matching braces, but I'm not sure whether I should take this as a regression or improvement

r#"//- minicore: todo, unimplemented
// FIXME: Since we are lacking of `super let`, term search fails due to borrowck failure.
// Should implement super let and remove `fmt_before_1_89_0`
r#"//- minicore: todo, unimplemented, fmt_before_1_89_0
Copy link
Member Author

@ShoyuVanilla ShoyuVanilla Jun 22, 2025

Choose a reason for hiding this comment

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

Those regressions are caused by failed borrowcks here, rooted from new format_args! expansions without super let or other proper alternative expansions:

ScopeDef::Local(it) => {
if ctx.config.enable_borrowcheck {
let borrowck = db.borrowck(it.parent).ok()?;

I'll implement super let as a followup and fix these by then

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Jun 22, 2025
Merged via the queue into rust-lang:master with commit 0100bc7 Jun 22, 2025
14 checks passed
@ShoyuVanilla ShoyuVanilla deleted the fmt-args-new branch June 22, 2025 04:46
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

println! macro is unsafe rust-lang/rust#140748 will require us to adjust our format_args! lowering
3 participants