-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Conversation
r? @oli-obk (rustbot has picked a reviewer for you, use r? to override) |
// consume `\xAB` literal | ||
if let Some((pos, _)) = s.next() { | ||
skips.push(pos); | ||
} else { | ||
break; | ||
} |
There was a problem hiding this comment.
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 \
.
☔ 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> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Characters that need to be shifted | ||
skips: Vec<usize>, | ||
/// Inner span width of characters that need to be shifted | ||
width_map: Vec<InnerWidthMapping>, |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 usize
s?
Should we introduce some type to differentiate "raw" from "remapped" positions?
There was a problem hiding this comment.
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.
af0d14c
to
4ccc7f3
Compare
4ccc7f3
to
35c7939
Compare
Rebased against master. |
Thanks @cassaundra. Sorry for the slow review. |
☀️ Test successful - checks-actions |
Finished benchmarking commit (731e0bf): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis 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. CyclesThis benchmark run did not return any relevant results for this metric. |
…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)).
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.