-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
r? @davidtwco (rust-highfive has picked a reviewer for you, use r? to override) |
@@ -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(_)); |
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.
why the : bool
here?
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.
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.
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.
Apologies for the delay in reviewing, this looks good to me.
@bors r+ |
📌 Commit 14f3ec2 has been approved by |
No problem, thanks for taking the time to review this (and #86164)! |
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.
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
…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
…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
…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
…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
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:
currently produces:
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.