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 incorrect span when using byte-escaped rbrace #103828

Merged
merged 1 commit into from
Dec 26, 2022

Conversation

cassaundra
Copy link
Contributor

@cassaundra cassaundra commented Nov 1, 2022

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.

@rustbot
Copy link
Collaborator

rustbot commented Nov 1, 2022

r? @oli-obk

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 1, 2022
Comment on lines 847 to 852
// consume `\xAB` literal
if let Some((pos, _)) = s.next() {
skips.push(pos);
} else {
break;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: a small check removed here. The lexer will raise "numeric character escape is too short" before we ever get to this code if there aren't three characters following the \.

@oli-obk
Copy link
Contributor

oli-obk commented Nov 8, 2022

r? @cjgillot since you also reviewed #102214

@rustbot rustbot assigned cjgillot and unassigned oli-obk Nov 8, 2022
@bors
Copy link
Contributor

bors commented Nov 8, 2022

☔ The latest upstream changes (presumably #104138) made this pull request unmergeable. Please resolve the merge conflicts.

let snippet = match snippet {
Some(ref s) if s.starts_with('"') || s.starts_with("r\"") || s.starts_with("r#") => s,
_ => return (vec![], false),
};

fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> {
fn find_width_map(snippet: &str, is_raw: bool) -> Vec<InnerWidthMapping> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will be changed by #104193.
cc @TaKO8Ki

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that there are conflicts from both #103651 and #104193, I'll go ahead and resolve the other issues before I try sort those out.

/// Characters that need to be shifted
skips: Vec<usize>,
/// Inner span width of characters that need to be shifted
width_map: Vec<InnerWidthMapping>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you elaborate the comment? Notably: what is the index of the Vec?

pub struct InnerWidthMapping {
pub position: usize,
pub before: usize,
pub after: usize,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you comment the individual fields?
What are they: positions? Starting at which point of reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some comments.

For what it's worth, I think this could also be refactored to instead have a InnerSpan and a usize, which might make some of the intent more clear.

@@ -224,7 +237,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 = self.to_span_index(pos + 1);
let lbrace_end = InnerOffset(byte_pos.0 + self.to_span_width(pos));
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we wrap all creations of InnerOffset?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, not exactly sure what you mean.

Maybe related: a function that gives you both position and width could clean this up

pos += 1;
} else if pos == *skip && raw == 0 {
pos += 1;
fn remap_pos(&self, mut pos: usize) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment: what are the input and output usizes?
Should we introduce some type to differentiate "raw" from "remapped" positions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that should be InnerOffset, my bad.

@cassaundra cassaundra force-pushed the fix-format-args-span2 branch from af0d14c to 4ccc7f3 Compare November 18, 2022 05:27
@cassaundra cassaundra force-pushed the fix-format-args-span2 branch from 4ccc7f3 to 35c7939 Compare November 22, 2022 04:37
@cassaundra
Copy link
Contributor Author

Rebased against master.

@cjgillot
Copy link
Contributor

Thanks @cassaundra. Sorry for the slow review.
@bors r+

@bors
Copy link
Contributor

bors commented Dec 26, 2022

📌 Commit 35c7939 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 Dec 26, 2022
@bors
Copy link
Contributor

bors commented Dec 26, 2022

⌛ Testing commit 35c7939 with merge 731e0bf...

@bors
Copy link
Contributor

bors commented Dec 26, 2022

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 731e0bf to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 26, 2022
@bors bors merged commit 731e0bf into rust-lang:master Dec 26, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 26, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (731e0bf): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

This benchmark run did not return any relevant results for this metric.

Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
…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)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

Incorrect span produced when using encoded }
6 participants