rust-lang / rust-lang/rustfmt

rustfmt gives up formatting of call chain if an inner string literal exceeds max width

Open
#7,108 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-call-chains A-strings C-bug I-max-width
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Reproduced on the recent main (1ed2df61a19042f231709eb05d032ae9e2cb2084).

In mentioned file, in resolve_implementation function I did the following changes:

diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index fc723586c1a..1e8126dc2b0 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -3501,7 +3501,7 @@ fn resolve_implementation(
     ) {
         debug!("resolve_implementation");
         // If applicable, create a rib for the type parameters.
-        self.with_generic_param_rib(
+        self.                    with_generic_param_rib(
             &generics.params,
             RibKind::Item(HasGenericParams::Yes(generics.span), self.r.tcx.def_kind(self.r.current_owner.def_id)),
             item_id,

Next, I run ./x t tidy (and with --bless flag) but this formatting error is not fixed or spotted.

Output of `./x t tidy`
Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.07s
Building stage1 tidy (stage0 -> stage1, x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 0.27s
fmt check
fmt: checked modified file compiler\rustc_resolve\src\late.rs
tidy check
tidy: All tidy checks succeeded
x.py completions check
x.py help check
Build completed successfully in 0:00:07

If nesting of this function is reduced then it works fine:

diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index fc723586c1a..f7808e10100 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -3501,21 +3501,13 @@ fn resolve_implementation(
     ) {
         debug!("resolve_implementation");
         // If applicable, create a rib for the type parameters.
-        self.with_generic_param_rib(
+        self.                    with_generic_param_rib(
             &generics.params,
             RibKind::Item(HasGenericParams::Yes(generics.span), self.r.tcx.def_kind(self.r.current_owner.def_id)),
             item_id,
             LifetimeBinderKind::ImplBlock,
             generics.span,
             |this| {
-                // Dummy self type for better errors if `Self` is used in the trait path.
-                this.with_self_rib(Res::SelfTyParam { trait_: LOCAL_CRATE.as_def_id() }, |this| {
-                    this.with_lifetime_rib(
-                        LifetimeRibKind::AnonymousCreateParameter {
-                            binder: item_id,
-                            report_in_path: true
-                        },
-                        |this| {
                             // Resolve the trait reference, if necessary.
                             this.with_optional_trait_ref(
                                 of_trait.map(|t| &t.trait_ref),
@@ -3566,9 +3558,6 @@ fn resolve_implementation(
                             )
                         },
                     );
-                });
-            },
-        );
     }
 
     fn resolve_impl_item(
Output of `./x t tidy`
Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.10s
Building stage1 tidy (stage0 -> stage1, x86_64-pc-windows-msvc)
    Finished `release` profile [optimized + debuginfo] target(s) in 0.31s
fmt check
fmt: checked modified file compiler\rustc_resolve\src\late.rs
Diff in \\?\C:\work\rust\compiler\rustc_resolve\src\late.rs:3501:
     ) {
         debug!("resolve_implementation");
         // If applicable, create a rib for the type parameters.
-        self.                    with_generic_param_rib(
+        self.with_generic_param_rib(
             &generics.params,
-            RibKind::Item(HasGenericParams::Yes(generics.span), self.r.tcx.def_kind(self.r.current_owner.def_id)),
+            RibKind::Item(
+                HasGenericParams::Yes(generics.span),
+                self.r.tcx.def_kind(self.r.current_owner.def_id),
+            ),
             item_id,
             LifetimeBinderKind::ImplBlock,
             generics.span,
Diff in \\?\C:\work\rust\compiler\rustc_resolve\src\late.rs:3510:
             |this| {
-                            // Resolve the trait reference, if necessary.
-                            this.with_optional_trait_ref(
-                                of_trait.map(|t| &t.trait_ref),
-                                self_type,
-                                |this, trait_id| {
-                                    this.resolve_doc_links(attrs, MaybeExported::Impl(trait_id));
+                // Resolve the trait reference, if necessary.
+                this.with_optional_trait_ref(
+                    of_trait.map(|t| &t.trait_ref),
+                    self_type,
+                    |this, trait_id| {
+                        this.resolve_doc_links(attrs, MaybeExported::Impl(trait_id));
 
-                                    let item_def_id = this.r.current_owner.def_id;
+                        let item_def_id = this.r.current_owner.def_id;
 
-                                    // Register the trait definitions from here.
-                                    if let Some(trait_id) = trait_id {
-                                        this.r
-                                            .trait_impls
-                                            .entry(trait_id)
-                                            .or_default()
-                                            .push(item_def_id);
-                                    }
+                        // Register the trait definitions from here.
+                        if let Some(trait_id) = trait_id {
+                            this.r.trait_impls.entry(trait_id).or_default().push(item_def_id);
+                        }
 
-                                    let item_def_id = item_def_id.to_def_id();
-                                    let res = Res::SelfTyAlias {
-                                        alias_to: item_def_id,
-                                        is_trait_impl: trait_id.is_some(),
-                                    };
-                                    this.with_self_rib(res, |this| {
-                                        if let Some(of_trait) = of_trait {
-                                            // Resolve type arguments in the trait path.
-                                            visit::walk_trait_ref(this, &of_trait.trait_ref);
-                                        }
-                                        // Resolve the self type.
-                                        this.visit_ty(self_type);
-                                        // Resolve the generic parameters.
-                                        this.visit_generics(generics);
+                        let item_def_id = item_def_id.to_def_id();
+                        let res = Res::SelfTyAlias {
+                            alias_to: item_def_id,
+                            is_trait_impl: trait_id.is_some(),
+                        };
+                        this.with_self_rib(res, |this| {
+                            if let Some(of_trait) = of_trait {
+                                // Resolve type arguments in the trait path.
+                                visit::walk_trait_ref(this, &of_trait.trait_ref);
+                            }
+                            // Resolve the self type.
+                            this.visit_ty(self_type);
+                            // Resolve the generic parameters.
+                            this.visit_generics(generics);
 
-                                        // Resolve the items within the impl.
-                                        this.with_current_self_type(self_type, |this| {
-                                            this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| {
-                                                debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)");
-                                                let mut seen_trait_items = Default::default();
-                                                for item in impl_items {
-                                                    with_owner(this, item.id, |this| {
-                                                        this.resolve_impl_item(&**item, &mut seen_trait_items, trait_id, of_trait.is_some());
-                                                    })
-                                                }
-                                            });
-                                        });
-                                    });
-                                },
-                            )
-                        },
-                    );
+                            // Resolve the items within the impl.
+                            this.with_current_self_type(self_type, |this| {
+                                this.with_self_rib_ns(
+                                    ValueNS,
+                                    Res::SelfCtor(item_def_id),
+                                    |this| {
+                                        debug!(
+                                            "resolve_implementation with_self_rib_ns(ValueNS, ...)"
+                                        );
+                                        let mut seen_trait_items = Default::default();
+                                        for item in impl_items {
+                                            with_owner(this, item.id, |this| {
+                                                this.resolve_impl_item(
+                                                    &**item,
+                                                    &mut seen_trait_items,
+                                                    trait_id,
+                                                    of_trait.is_some(),
+                                                );
+                                            })
+                                        }
+                                    },
+                                );
+                            });
+                        });
+                    },
+                )
+            },
+        );
     }
 
     fn resolve_impl_item(

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the formatting issue using compiler/rustc_resolve/src/late.rs, focusing on resolve_implementation and the malformed spacing before with_generic_param_rib. Run ./x t tidy with and without --bless, then trace how the nested call chain and over-width string literal are handled. Done means the malformed call chain is reformatted or reported consistently, with regression coverage for the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.