-
Notifications
You must be signed in to change notification settings - Fork 908
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
Try to solve issue3456. #3556
Try to solve issue3456. #3556
Conversation
I read code and find this snippet relevant: Lines 1251 to 1277 in cc97eaf
This branch of code indents strings even when config.format_strings() is false. I git log and find #1724 is the pull request introduce this condition branch, and #1661 is the issues that #1724 solve. Unfortunately, the code reference is not working, so I can't see why that change is make. What can I do? Any suggestion? |
Oh, multi-line string literal end with \, and '\\' is character \ in a escape form. |
@xiongmao86 exactly. It checks that every line ends with |
There are 4 tests failed in current code. 1 self test, and 3 system tests. The self test indent the second line of a string lit that was re-indented by the code before change. So the second line changed by some function before the rewrite_string_lit call was changed back by the origin code. The system tests pertain to the argument processing of function and macro invocation. The code possibly rely on this changing snippet to make sure the invocation never pass the line limit. |
Identify snippet that make system tests to failed, but don't know how wrapping string lit would make that happen: Lines 470 to 484 in cc97eaf
|
It's so hard to trace. What mistake did I make? |
In the part of the code you changed, there was a gated change. Keeping it resolve the issues: --- a/src/expr.rs
+++ b/src/expr.rs
@@ -1250,7 +1250,11 @@ fn rewrite_string_lit(context: &RewriteContext<'_>, span: Span, shape: Shape) ->
let string_lit = context.snippet(span);
if !context.config.format_strings() {
- return valid_str(string_lit.to_owned(), context.config.max_width(), shape);
+ return if context.config.version() == Version::Two {
+ Some(string_lit.to_owned())
+ } else {
+ valid_str(string_lit.to_owned(), context.config.max_width(), shape)
+ };
} However, since your fix modifies a the formatting of a default option, you need to gate what you did as well. See https://github.com/rust-lang/rustfmt/blob/master/Contributing.md#version-gate-formatting-changes for extra infos. |
@scampi, I think I have tried that option before, and it came up with new issues. I still don't know why changing in this way and what cause changing the layout of the string. I don't know how to narrow down to the location of relevant code. |
When format_strings is false, the next line is indented. Just opposite to what I thought. |
What issues did you get with the proposed #3556 (comment) ? |
Well, I try your suggestion and find the second line of the multiline string returned at the version two condition is indented with 4 more space, I was thinking that additional indent is because that second line was process by I can not find out since the string is return early as what it is and not changed by the |
Formatting in |
@scampi, I take a look at the log of
In: macro_rules! assert_approx_eq {
($a:expr, $b:expr, $eps:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < $eps,
"assertion failed: `(left !== right)` \
(left: `{:?}`, right: `{:?}`, expect diff: `{:?}`, real diff: `{:?}`)",
*a, *b, $eps, (*a - *b).abs());
})
} Update: looking at the wrong crate. |
Oh, that failed test probably ended up like the old self test in |
Currently when processing the macro definition, the macro branch block is wrap into a main function by pretending function declaration and brace and by appending a close brace before using I found the second line is not affected when I add more "block_indent()` to the block shape. So I think the problem could probably be missing to chop the second line of the multi-line string for one unit of indention. I will try to narrow down in the following days. |
Test passed. @scampi can you review my commit? |
If @scampi is not available, are there anyone have time to review my pr? |
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.
@xiongmao86 My apologies for the late review, and thank you for the multiple updates!
The code looks good to me.
@@ -600,9 +600,8 @@ pub(crate) fn trim_left_preserve_layout( | |||
|
|||
/// Based on the given line, determine if the next line can be indented or not. | |||
/// This allows to preserve the indentation of multi-line literals. | |||
pub(crate) fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool { | |||
pub(crate) fn indent_next_line(kind: FullCodeCharKind, _line: &str, config: &Config) -> bool { |
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.
Nit: if the line
is not used, then we can remove it from the parameters.
Close #3456.
Close #3860.