Skip to content

Commit

Permalink
Merge pull request #1436 from dtolnay/floatsubspan
Browse files Browse the repository at this point in the history
Improve spans of Expr::Field parsed from a float Literal
  • Loading branch information
dtolnay authored Apr 1, 2023
2 parents b0861b9 + 3b7c555 commit 9aae2b4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2716,22 +2716,36 @@ pub(crate) mod parsing {
}

fn multi_index(e: &mut Expr, dot_token: &mut Token![.], float: LitFloat) -> Result<bool> {
let mut float_repr = float.to_string();
let float_token = float.token();
let float_span = float_token.span();
let mut float_repr = float_token.to_string();
let trailing_dot = float_repr.ends_with('.');
if trailing_dot {
float_repr.truncate(float_repr.len() - 1);
}

let mut offset = 0;
for part in float_repr.split('.') {
let index = crate::parse_str(part).map_err(|err| Error::new(float.span(), err))?;
let mut index: Index =
crate::parse_str(part).map_err(|err| Error::new(float_span, err))?;
let part_end = offset + part.len();
index.span = float_token.subspan(offset..part_end).unwrap_or(float_span);

let base = mem::replace(e, Expr::DUMMY);
*e = Expr::Field(ExprField {
attrs: Vec::new(),
base: Box::new(base),
dot_token: Token![.](dot_token.span),
member: Member::Unnamed(index),
});
*dot_token = Token![.](float.span());

let dot_span = float_token
.subspan(part_end..part_end + 1)
.unwrap_or(float_span);
*dot_token = Token![.](dot_span);
offset = part_end + 1;
}

Ok(!trailing_dot)
}

Expand Down

0 comments on commit 9aae2b4

Please sign in to comment.