Skip to content

Panic when using double nested numerical accessors for structs #4489

Closed
@roberts-pumpurs

Description

@roberts-pumpurs

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions