Skip to content

Named format arguments can be used as positional #93415

Closed
@CAD97

Description

@CAD97

I tried this code:

fn main() {
    let what = "what?";
    println!("{a}{}", a=what);
}

I expected to see this happen:

error: 1 positional argument in format string, but no arguments were given
 --> <source>:3:18
  |
3 |     println!("{a}{}", a=what);
  |                  ^^

Instead, this happened: explanation

Standard Error

   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.94s
     Running `target/debug/playground`

Standard Output

what?what?

Meta

This happens on current 1.60.0-nightly (2022-01-27 21b4a9c), and back to rustc 1.12.01. On rustc 1.11.0, it gives

<std macros>:3:11: 3:36 error: invalid reference to argument `0` (no arguments given)
<std macros>:3 print ! ( concat ! ( $ fmt , "\n" ) , $ ( $ arg ) * ) ) ;
                         ^~~~~~~~~~~~~~~~~~~~~~~~~
<std macros>:3:11: 3:36 note: in this expansion of concat!
<std macros>:2:27: 2:58 note: in this expansion of format_args!
<std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>)
<source>:3:5: 3:37 note: in this expansion of println! (defined in <std macros>)

Given how old this "support" is, I don't know if this can be classified as a bug anymore, or if it's just more of a misfeature at this point. IMHO, this should at least get a lint, or even a future-compat lint if that's considered reasonable.

Footnotes

  1. So I guess this would be considered regression-from-stable-to-stable? 🙃

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2024Area: The 2024 editionA-fmtArea: `core::fmt`A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-future-incompatibilityCategory: Future-incompatibility lintsI-needs-decisionIssue: In need of a decision.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions