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

Presence of macro definition causes incorrect width settings warnings #4919

Closed
zmill opened this issue Jul 25, 2021 · 2 comments
Closed

Presence of macro definition causes incorrect width settings warnings #4919

zmill opened this issue Jul 25, 2021 · 2 comments
Labels
help wanted only-with-option requires a non-default option value to reproduce

Comments

@zmill
Copy link

zmill commented Jul 25, 2021

With the following src/lib.rs:

macro_rules! mac {
    () => {};
}

rustfmt seems to be getting incorrect width settings from somewhere:

$ rustfmt --config max_width=80,chain_width=72 --print-config current . | grep '\(\<max\|chain\)_width'
max_width = 80
chain_width = 72

$ rustfmt --config max_width=80,chain_width=72 --check src/lib.rs
(no output)
$ rustfmt --config max_width=80,chain_width=73 --print-config current . | grep '\(\<max\|chain\)_width'
max_width = 80
chain_width = 73

$ rustfmt --config max_width=80,chain_width=73 --check src/lib.rs
`chain_width` cannot have a value that exceeds `max_width`. `chain_width` will be set to the same value as `max_width`

If I don't also override max_width, the warning occurs with chain_width >= 93 instead of 101 as expected; I checked several other max_widths as well, and it seems the highest chain_width accepted without warning is always 8 less than max_width.


If src/lib.rs doesn't contain a macro, e.g.:

fn f() {}

Then I get the warning (albeit twice) starting with a chain_width of one more than max_width as expected:

$ rustfmt --config max_width=80,chain_width=80 --print-config current . | grep '\(\<max\|chain\)_width'
max_width = 80
chain_width = 80

$ rustfmt --config max_width=80,chain_width=80 --check src/lib.rs
(no output)
$ rustfmt --config max_width=80,chain_width=81 --print-config current . | grep '\(\<max\|chain\)_width'
`chain_width` cannot have a value that exceeds `max_width`. `chain_width` will be set to the same value as `max_width`
max_width = 80
chain_width = 80

$ rustfmt --config max_width=80,chain_width=81 --check src/lib.rs
`chain_width` cannot have a value that exceeds `max_width`. `chain_width` will be set to the same value as `max_width`
`chain_width` cannot have a value that exceeds `max_width`. `chain_width` will be set to the same value as `max_width`

Behavior is the same for array_width and struct_lit_width; I didn't check the rest of the width settings. Behavior is also the same on stable and nightly.

$ rustfmt -V
rustfmt 1.4.37-stable (2a3635d5 2021-05-04)

$ rustfmt +nightly -V
rustfmt 1.4.37-nightly (d9aa2876 2021-07-24)

Full config in case it's helpful (should all be defaults):

$ rustfmt --print-config current .
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
indent_style = "Block"
use_small_heuristics = "Default"
fn_call_width = 60
attr_fn_like_width = 70
struct_lit_width = 18
struct_variant_width = 35
array_width = 60
chain_width = 60
single_line_if_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
license_template_path = ""
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Preserve"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
inline_attribute_width = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "1.4.37"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
@calebcartwright
Copy link
Member

The correlation with the presence of a macro def is baffling since the evaluation of the config settings should be happening long before any parsing stages.

For anyone interested in taking a look at fixing this the comparison point that's emitting the warnings can be found here:

fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
let max_width = self.max_width.2;
let get_width_value = |
was_set: bool,
override_value: usize,
heuristic_value: usize,
config_key: &str,
| -> usize {
if !was_set {
return heuristic_value;
}
if override_value > max_width {
eprintln!(
"`{0}` cannot have a value that exceeds `max_width`. \
`{0}` will be set to the same value as `max_width`",
config_key,
);
return max_width;
}
override_value
};

@calebcartwright calebcartwright added help wanted only-with-option requires a non-default option value to reproduce labels Aug 26, 2021
@ytmimi
Copy link
Contributor

ytmimi commented Jul 26, 2022

going to close this in favor of #5404 as there's more discussion there

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

3 participants