Skip to content

cannot fix with overlapping suggestions #13026

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

Open
ehuss opened this issue Aug 28, 2021 · 3 comments
Open

cannot fix with overlapping suggestions #13026

ehuss opened this issue Aug 28, 2021 · 3 comments
Labels
Command-fix S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 28, 2021

Some suggestions include overlapping regions. These cause rustfix to fail with an error like:

Could not replace range 22...47 in file -- maybe parts of it were already replaced?

An example that triggers this is the following where multiple trait bounds are to be removed:

pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;

This results in the following suggestion:

warning: bounds on generic parameters are not enforced in type aliases
 --> src/main.rs:1:25
  |
1 | pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;
  |                         ^^^^^^^^^^^^^^^^^   ^^^^   ^^^^
  |
  = note: `#[warn(type_alias_bounds)]` on by default
help: the bound will not be checked when the type alias is used, and should be removed
  |
1 - pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;
1 + pub type MyResult<T, E> = Result<T, E>;
  |

With the suggested JSON:

{
    "reason": "compiler-message",
    "package_id": "z2 0.1.0 (path+file:///foo)",
    "manifest_path": "/foo/Cargo.toml",
    "target":
    {
        "kind":
        [
            "bin"
        ],
        "crate_types":
        [
            "bin"
        ],
        "name": "z2",
        "src_path": "/foo/src/main.rs",
        "edition": "2018",
        "doc": true,
        "doctest": false,
        "test": true
    },
    "message":
    {
        "rendered": "warning: bounds on generic parameters are not enforced in type aliases\n --> src/main.rs:1:25\n  |\n1 | pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;\n  |                         ^^^^^^^^^^^^^^^^^   ^^^^   ^^^^\n  |\n  = note: `#[warn(type_alias_bounds)]` on by default\nhelp: the bound will not be checked when the type alias is used, and should be removed\n  |\n1 - pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;\n1 + pub type MyResult<T, E> = Result<T, E>;\n  | \n\n",
        "children":
        [
            {
                "children":
                [],
                "code": null,
                "level": "note",
                "message": "`#[warn(type_alias_bounds)]` on by default",
                "rendered": null,
                "spans":
                []
            },
            {
                "children":
                [],
                "code": null,
                "level": "help",
                "message": "the bound will not be checked when the type alias is used, and should be removed",
                "rendered": null,
                "spans":
                [
                    {
                        "byte_end": 41,
                        "byte_start": 22,
                        "column_end": 42,
                        "column_start": 23,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 1,
                        "line_start": 1,
                        "suggested_replacement": "",
                        "suggestion_applicability": "MachineApplicable",
                        "text":
                        [
                            {
                                "highlight_end": 42,
                                "highlight_start": 23,
                                "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                            }
                        ]
                    },
                    {
                        "byte_end": 48,
                        "byte_start": 22,
                        "column_end": 49,
                        "column_start": 23,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 1,
                        "line_start": 1,
                        "suggested_replacement": "",
                        "suggestion_applicability": "MachineApplicable",
                        "text":
                        [
                            {
                                "highlight_end": 49,
                                "highlight_start": 23,
                                "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                            }
                        ]
                    },
                    {
                        "byte_end": 55,
                        "byte_start": 22,
                        "column_end": 56,
                        "column_start": 23,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 1,
                        "line_start": 1,
                        "suggested_replacement": "",
                        "suggestion_applicability": "MachineApplicable",
                        "text":
                        [
                            {
                                "highlight_end": 56,
                                "highlight_start": 23,
                                "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                            }
                        ]
                    }
                ]
            }
        ],
        "code":
        {
            "code": "type_alias_bounds",
            "explanation": null
        },
        "level": "warning",
        "message": "bounds on generic parameters are not enforced in type aliases",
        "spans":
        [
            {
                "byte_end": 41,
                "byte_start": 24,
                "column_end": 42,
                "column_start": 25,
                "expansion": null,
                "file_name": "src/main.rs",
                "is_primary": true,
                "label": null,
                "line_end": 1,
                "line_start": 1,
                "suggested_replacement": null,
                "suggestion_applicability": null,
                "text":
                [
                    {
                        "highlight_end": 42,
                        "highlight_start": 25,
                        "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                    }
                ]
            },
            {
                "byte_end": 48,
                "byte_start": 44,
                "column_end": 49,
                "column_start": 45,
                "expansion": null,
                "file_name": "src/main.rs",
                "is_primary": true,
                "label": null,
                "line_end": 1,
                "line_start": 1,
                "suggested_replacement": null,
                "suggestion_applicability": null,
                "text":
                [
                    {
                        "highlight_end": 49,
                        "highlight_start": 45,
                        "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                    }
                ]
            },
            {
                "byte_end": 55,
                "byte_start": 51,
                "column_end": 56,
                "column_start": 52,
                "expansion": null,
                "file_name": "src/main.rs",
                "is_primary": true,
                "label": null,
                "line_end": 1,
                "line_start": 1,
                "suggested_replacement": null,
                "suggestion_applicability": null,
                "text":
                [
                    {
                        "highlight_end": 56,
                        "highlight_start": 52,
                        "text": "pub type MyResult<T, E: std::error::Error + Send + Sync> = Result<T, E>;"
                    }
                ]
            }
        ]
    }
}

This has three overlapping spans that suggest a replacement of an empty string:

  • 22..41
  • 22..48
  • 22..55

I think it might be feasible to support this. At least, the new diff output from rustc is able to handle it, and clicking the "fix" suggestions in my editor is able to handle it (the regions are "smart" and know when the text within it is modified).

Note: This is relatively new behavior introduced by rust-lang/rustfix#195.

@ehuss ehuss transferred this issue from rust-lang/rustfix Nov 22, 2023
@ehuss ehuss added Command-fix A-build-dependencies Area: [build-dependencies] S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review and removed A-build-dependencies Area: [build-dependencies] labels Nov 22, 2023
@LuuuXXX
Copy link
Contributor

LuuuXXX commented Mar 12, 2024

Attempt to resolve. @rustbot claim

@LuuuXXX LuuuXXX removed their assignment Apr 17, 2024
@torhovland
Copy link
Contributor

The particular example mentioned in the issue is (now) being successfully handled by cargo fix. I think this issue needs an updated test case.

@rustbot rustbot added the S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. label Aug 21, 2024
@torhovland
Copy link
Contributor

@rustbot label: +S-needs-info, -S-accepted

@rustbot rustbot removed the S-accepted Status: Issue or feature is accepted, and has a team member available to help mentor or review label Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command-fix S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

4 participants