Skip to content

Panic when using double nested numerical accessors for structs #4489

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

Closed
roberts-pumpurs opened this issue Oct 23, 2020 · 1 comment
Closed
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate

Comments

@roberts-pumpurs
Copy link

roberts-pumpurs commented Oct 23, 2020

Rustfmt can't parse nested structs nested numerical accessors for structs

File will not parse when it contains a line like let turnirs_instances = Turnirs::get(&mut conn, info.0.0).unwrap();. Note the variable info which contains a single element, which in turn, also contains a single element. Both accessed via the index notation. The sample code, where I actually encountered the error is using Actix web framework but that is not what we should focus on.

To Reproduce

#[get("/{turnirsid}")]
pub async fn turnirs_single_handler(
    info: web::Path<(u32,)>,
    tmpl: web::Data<tera::Tera>,
) -> Result<HttpResponse, Error> {
    let mut conn = DB_WRAPPER.get_conn();

    // this is where the error happens
    let turnirs_instances = Turnirs::get(&mut conn, info.0.0).unwrap();
    let turnirs_results = Turnirs::count_turnirs_summary(&mut conn, info.0.0).unwrap();
    let mut context = Context::new();

    context.insert("turnirs", &turnirs_instances);
    // Note the incorrecto formatting below
    context.


    insert("turnirs_result", &turnirs_results);

    let s = tmpl
        .render("turnirs/turnirs_single.html", &context)
        .map_err(|_| error::ErrorInternalServerError("Template error"))?;

    Ok(HttpResponse::Ok().content_type("text/html").body(s))
}

If (for testing purposes) the variable accessor is changed to from info.0.0 to info.0 then the code snippet formats properly

Expected behaviour

#[get("/{turnirsid}")]
pub async fn turnirs_single_handler(
    info: web::Path<(u32,)>,
    tmpl: web::Data<tera::Tera>,
) -> Result<HttpResponse, Error> {
    let mut conn = DB_WRAPPER.get_conn();

    let turnirs_instances = Turnirs::get(&mut conn, info.0.0).unwrap();
    let turnirs_results = Turnirs::count_turnirs_summary(&mut conn, info.0.0).unwrap();
    let mut context = Context::new();

    // Note the code is formatted correctly here
    context.insert("turnirs", &turnirs_instances);
    context.insert("turnirs_result", &turnirs_results);

    let s = tmpl
        .render("turnirs/turnirs_single.html", &context)
        .map_err(|_| error::ErrorInternalServerError("Template error"))?;

    Ok(HttpResponse::Ok().content_type("text/html").body(s))
}

Meta

Note the edition only exists because of the async next to the function.

$ rustfmt src/views/same.rs --edition 2018
thread 'main' panicked at 'bad span: `.`: ``', src/tools/rustfmt/src/source_map.rs:52:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  • rustfmt version: rustfmt 1.4.20-stable (48f6c32 2020-08-09)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, cargo-fmt, via VSCode. all of them fail to parse the file with selectors like this.

A workaround is to use accessors like (info.0).0, but if it wasn't enforced by the compiler then I would not write this bug report.

@roberts-pumpurs roberts-pumpurs added the bug Panic, non-idempotency, invalid code, etc. label Oct 23, 2020
@calebcartwright
Copy link
Member

Duplicate of #4355, resolved in the upcoming 1.4.23 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate
Projects
None yet
Development

No branches or pull requests

2 participants