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 of byte-escaped left format args brace #102214

Merged
merged 1 commit into from
Sep 29, 2022

Conversation

cassaundra
Copy link
Contributor

Fix #102057 (see issue for example).

Previously, the use of escaped left braces (\x7B) in format args resulted in an incorrectly offset span. This patch fixes that by considering any escaped characters within the string instead of using a constant offset.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 23, 2022
@rust-highfive
Copy link
Collaborator

r? @cjgillot

(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 Sep 23, 2022
@@ -224,7 +224,7 @@ impl<'a> Iterator for Parser<'a> {
'{' => {
let curr_last_brace = self.last_opening_brace;
let byte_pos = self.to_span_index(pos);
let lbrace_end = InnerOffset(byte_pos.0 + 1);
let lbrace_end = self.to_span_index(pos + 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a manual creation of an InnerOffset just below (github won't let me comment on it). Could it trigger a similar bug? Should it be changed in a similar way?

Copy link
Contributor Author

@cassaundra cassaundra Sep 26, 2022

Choose a reason for hiding this comment

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

That's a good point. Originally I had assumed not since I wasn't able to find any breaking examples, but I just found that following:

println!("{\x7D");

gives the following error:

error: 1 positional argument in format string, but no arguments were given
 --> src/main.rs:2:15
  |
2 |     println!("{\x7D");
  |               ^^

I can go ahead and apply the fix to that line as well, and add some more tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please, with the test if possible. That's a good opportunity to fix several identical bugs at once. I'll approve once this is done.

Copy link
Contributor Author

@cassaundra cassaundra Sep 26, 2022

Choose a reason for hiding this comment

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

Fixed in efc6df2 (edit: this resulted in a regression, see follow-up comment).

@cassaundra
Copy link
Contributor Author

Looking more into this issue, it seems like the way that format strings handle skipped characters erases some necessary information about the source string.

Consider these two statements:

println!("{\x7D"};

println!("{}\


");

In both cases, the resultant interned string looks like {}, with skips at positions 2, 3, and 4, so we can't correctly determine the width of } by just looking at the skips.

I'm thinking that it would be best to refactor this skipping mechanism to instead deal with a sequence of character widths (essentially squashing together some of the skips). It would make this PR larger than it initially was, but that's fine with me. Thoughts?

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor

@cassaundra a follow-up PR sounds good. If you don't mind, let's go back to the previous version of the patch, so we can get it merged in the mean time.

@cassaundra
Copy link
Contributor Author

cassaundra commented Sep 28, 2022

Done.

@cassaundra cassaundra changed the title Fix span of byte-escaped format args brace Fix span of byte-escaped left format args brace Sep 28, 2022
@cjgillot
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 29, 2022

📌 Commit e5096d4 has been approved by cjgillot

It is now in the queue for this repository.

@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 Sep 29, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Sep 29, 2022
…=cjgillot

Fix span of byte-escaped left format args brace

Fix rust-lang#102057 (see issue for example).

Previously, the use of escaped left braces (`\x7B`) in format args resulted in an incorrectly offset span. This patch fixes that by considering any escaped characters within the string instead of using a constant offset.
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 29, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#102214 (Fix span of byte-escaped left format args brace)
 - rust-lang#102426 (Don't export `__wasm_init_memory` on WebAssembly.)
 - rust-lang#102437 (rustdoc: cut margin-top from first header in docblock)
 - rust-lang#102442 (rustdoc: remove bad CSS font-weight on `.impl`, `.method`, etc)
 - rust-lang#102447 (rustdoc: add method spacing to trait methods)
 - rust-lang#102468 (tidy: make rustc dependency error less confusing)
 - rust-lang#102476 (Split out the error reporting logic into a separate function)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0878bee into rust-lang:master Sep 29, 2022
@rustbot rustbot added this to the 1.66.0 milestone Sep 29, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 26, 2022
…cjgillot

Fix incorrect span when using byte-escaped rbrace

Fix rust-lang#103826, a format args span issue introduced in rust-lang#102214.

The current solution for tracking skipped characters made it so that certain situations were ambiguous enough that the original span couldn't be worked out later. This PR improves on the original solution by keeping track of groups of skipped characters using a map, and fixes the previous bug. See an example of this ambiguity in the [previous PR's discussion](rust-lang#102214 (comment)).
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Dec 28, 2022
Fix incorrect span when using byte-escaped rbrace

Fix #103826, a format args span issue introduced in #102214.

The current solution for tracking skipped characters made it so that certain situations were ambiguous enough that the original span couldn't be worked out later. This PR improves on the original solution by keeping track of groups of skipped characters using a map, and fixes the previous bug. See an example of this ambiguity in the [previous PR's discussion](rust-lang/rust#102214 (comment)).
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 20, 2024
Fix incorrect span when using byte-escaped rbrace

Fix #103826, a format args span issue introduced in #102214.

The current solution for tracking skipped characters made it so that certain situations were ambiguous enough that the original span couldn't be worked out later. This PR improves on the original solution by keeping track of groups of skipped characters using a map, and fixes the previous bug. See an example of this ambiguity in the [previous PR's discussion](rust-lang/rust#102214 (comment)).
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
Fix incorrect span when using byte-escaped rbrace

Fix #103826, a format args span issue introduced in #102214.

The current solution for tracking skipped characters made it so that certain situations were ambiguous enough that the original span couldn't be worked out later. This PR improves on the original solution by keeping track of groups of skipped characters using a map, and fixes the previous bug. See an example of this ambiguity in the [previous PR's discussion](rust-lang/rust#102214 (comment)).
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

format! error has wrong span when using encoded {
6 participants