rust-lang / rust-lang/rust

Multi-line diagnostic suggestions use LF in files that use CRLF line endings

Open
#119,482 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-suggestion-diagnostics C-bug O-windows T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Diagnostic suggestions that include line endings only use LF line endings. However, if the file is using CRLF line endings, applying the suggestion results in a file with mixed line endings.

I would expect that suggestions should match the line-ending style of the file.

For example:

mod a {
    pub fn f() {}
}

fn main() {
    f();
}

generates JSON:

{
    "$message_type": "diagnostic",
    "message": "cannot find function `f` in this scope",
    "code":
    {
        "code": "E0425",
        "explanation": "An unresolved name was used.\n\nErroneous code examples:\n\n```compile_fail,E0425\nsomething_that_doesnt_exist::foo;\n// error: unresolved name `something_that_doesnt_exist::foo`\n\n// or:\n\ntrait Foo {\n    fn bar() {\n        Self; // error: unresolved name `Self`\n    }\n}\n\n// or:\n\nlet x = unknown_variable;  // error: unresolved name `unknown_variable`\n```\n\nPlease verify that the name wasn't misspelled and ensure that the\nidentifier being referred to is valid for the given situation. Example:\n\n```\nenum something_that_does_exist {\n    Foo,\n}\n```\n\nOr:\n\n```\nmod something_that_does_exist {\n    pub static foo : i32 = 0i32;\n}\n\nsomething_that_does_exist::foo; // ok!\n```\n\nOr:\n\n```\nlet unknown_variable = 12u32;\nlet x = unknown_variable; // ok!\n```\n\nIf the item is not defined in the current module, it must be imported using a\n`use` statement, like so:\n\n```\n# mod foo { pub fn bar() {} }\n# fn main() {\nuse foo::bar;\nbar();\n# }\n```\n\nIf the item you are importing is not defined in some super-module of the\ncurrent module, then it must also be declared as public (e.g., `pub fn`).\n"
    },
    "level": "error",
    "spans":
    [
        {
            "file_name": "main.rs",
            "byte_start": 50,
            "byte_end": 51,
            "line_start": 6,
            "line_end": 6,
            "column_start": 5,
            "column_end": 6,
            "is_primary": true,
            "text":
            [
                {
                    "text": "    f();",
                    "highlight_start": 5,
                    "highlight_end": 6
                }
            ],
            "label": "not found in this scope",
            "suggested_replacement": null,
            "suggestion_applicability": null,
            "expansion": null
        }
    ],
    "children":
    [
        {
            "message": "consider importing this function",
            "code": null,
            "level": "help",
            "spans":
            [
                {
                    "file_name": "main.rs",
                    "byte_start": 0,
                    "byte_end": 0,
                    "line_start": 1,
                    "line_end": 1,
                    "column_start": 1,
                    "column_end": 1,
                    "is_primary": true,
                    "text":
                    [],
                    "label": null,
                    "suggested_replacement": "use a::f;\n\n",
                    "suggestion_applicability": "MaybeIncorrect",
                    "expansion": null
                }
            ],
            "children":
            [],
            "rendered": null
        }
    ],
    "rendered": "error[E0425]: cannot find function `f` in this scope\n --> main.rs:6:5\n  |\n6 |     f();\n  |     ^ not found in this scope\n  |\nhelp: consider importing this function\n  |\n1 + use a::f;\n  |\n\n"
}

Notice that the suggested_replacement uses \n despite the source file using \r\n line endings.

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (3cdd004e5 2023-12-29)
binary: rustc
commit-hash: [3cdd004e55c869faa2b7b25efd3becf50346e7d6](https://github.com/rust-lang/rust/commit/3cdd004e55c869faa2b7b25efd3becf50346e7d6)
commit-date: 2023-12-29
host: x86_64-pc-windows-msvc
release: 1.77.0-nightly
LLVM version: 17.0.6

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

Start by tracing rustc diagnostic suggestion generation using the CRLF example and the JSON suggested_replacement field shown in the report. Verify that a multi-line suggestion preserves the source file's CRLF style, and confirm the resulting diagnostic output no longer mixes line endings.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.