Skip to content
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

Fix span calculation in format strings #86104

Merged
merged 1 commit into from
Jun 17, 2021
Merged

Conversation

FabianWolff
Copy link
Contributor

This pull request fixes #86085. The ICE described there is due to an error in the span calculation inside format strings, if the format string is the result of a macro invocation:

fn main() {
    format!(concat!("abc}"));
}

currently produces:

error: invalid format string: unmatched `}` found
 --> test.rs:2:17
  |
2 |     format!(concat!("abc}"));
  |                 ^ unmatched `}` in format string

which is obviously incorrect. This happens because the span of the entire concat!() is combined with the relative location of the unmatched `}` in the result of the macro invocation (i.e. 4).

In #86085, this has led to a span that starts or ends in the middle of a multibyte character, but the root cause was the same. This pull request fixes the problem.

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 7, 2021
@@ -939,6 +939,7 @@ pub fn expand_preparsed_format_args(

let msg = "format argument must be a string literal";
let fmt_sp = efmt.span;
let efmt_kind_is_lit: bool = matches!(efmt.kind, ast::ExprKind::Lit(_));
Copy link
Contributor

Choose a reason for hiding this comment

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

why the : bool here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason. This declaration first had a different name, type, and right-hand side when I started working on this, and then I eventually converged on this.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Apologies for the delay in reviewing, this looks good to me.

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 16, 2021

📌 Commit 14f3ec2 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2021
@FabianWolff
Copy link
Contributor Author

Apologies for the delay in reviewing, this looks good to me.

No problem, thanks for taking the time to review this (and #86164)!

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 16, 2021
Fix span calculation in format strings

This pull request fixes rust-lang#86085. The ICE described there is due to an error in the span calculation inside format strings, if the format string is the result of a macro invocation:
```rust
fn main() {
    format!(concat!("abc}"));
}
```
currently produces:
```
error: invalid format string: unmatched `}` found
 --> test.rs:2:17
  |
2 |     format!(concat!("abc}"));
  |                 ^ unmatched `}` in format string
```
which is obviously incorrect. This happens because the span of the entire `concat!()` is combined with the _relative_ location of the unmatched `` `}` `` in the _result_ of the macro invocation (i.e. 4).

In rust-lang#86085, this has led to a span that starts or ends in the middle of a multibyte character, but the root cause was the same. This pull request fixes the problem.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 16, 2021
Rollup of 10 pull requests

Successful merges:

 - rust-lang#85870 (Allow whitespace in dump_mir filter)
 - rust-lang#86104 (Fix span calculation in format strings)
 - rust-lang#86140 (Mention the `Borrow` guarantee on the `Hash` implementations for Arrays and `Vec`)
 - rust-lang#86141 (Link reference in `dyn` keyword documentation)
 - rust-lang#86260 (Open trait implementations' toggles by default.)
 - rust-lang#86339 (Mention rust-lang#79078 on compatibility notes of 1.52)
 - rust-lang#86341 (Stop returning a value from `report_assert_as_lint`)
 - rust-lang#86353 (Remove `projection_ty_from_predicates`)
 - rust-lang#86361 (Add missing backslashes to prevent unwanted newlines in rustdoc HTML)
 - rust-lang#86372 (Typo correction: s/is/its)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 4ff55ec into rust-lang:master Jun 17, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 17, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 9, 2021
…llot

Fix ICE on format string of macro with secondary-label

This generalizes the fix rust-lang#86104 to also correctly skip `Span::from_inner` for the `secondary_label` of a format macro parsing error as well.

We can alternatively skip the `span_label` diagnostic call for the secondary label as well, since that label probably only makes sense when the _proper_ span is computed.

Fixes rust-lang#91556
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…llot

Fix ICE on format string of macro with secondary-label

This generalizes the fix rust-lang#86104 to also correctly skip `Span::from_inner` for the `secondary_label` of a format macro parsing error as well.

We can alternatively skip the `span_label` diagnostic call for the secondary label as well, since that label probably only makes sense when the _proper_ span is computed.

Fixes rust-lang#91556
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…llot

Fix ICE on format string of macro with secondary-label

This generalizes the fix rust-lang#86104 to also correctly skip `Span::from_inner` for the `secondary_label` of a format macro parsing error as well.

We can alternatively skip the `span_label` diagnostic call for the secondary label as well, since that label probably only makes sense when the _proper_ span is computed.

Fixes rust-lang#91556
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 10, 2021
…llot

Fix ICE on format string of macro with secondary-label

This generalizes the fix rust-lang#86104 to also correctly skip `Span::from_inner` for the `secondary_label` of a format macro parsing error as well.

We can alternatively skip the `span_label` diagnostic call for the secondary label as well, since that label probably only makes sense when the _proper_ span is computed.

Fixes rust-lang#91556
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
6 participants