-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Diagnostic suggestions sometimes incorrectly point to external crates #88514
Comments
The regression of "Example 1" was introduced between 1.51.0 and 1.52.0. The bisect report gave me three rollup PRs. The bug can be reproduced after this rollup and the suspect may be #82655. I am not familiar with this part, so if anyone want to address it, just take it. (Or even better mentor me 😆, thanks) Report from cargo-bisect-rustcScript for cargo-bisect-rustc#!/usr/bin/env bash
# Run Example 1
cargo clean
cargo fix 2>&1 | grep "Fixed"
if [[ $? -gt 0 ]]; then
exit 0
else
exit 1
fi Report from cargo-bisect-rustc
|
Do not provide suggestions when the spans come from expanded code that doesn't point at user code Hide invalid proc-macro suggestions and track spans coming from proc-macros pointing at attribute. Effectively, unless the proc-macro keeps user spans, suggestions will not be produced for the code they produce. r? `@ghost` Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557. Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
Do not provide suggestions when the spans come from expanded code that doesn't point at user code Hide invalid proc-macro suggestions and track spans coming from proc-macros pointing at attribute. Effectively, unless the proc-macro keeps user spans, suggestions will not be produced for the code they produce. r? ``@ghost`` Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557. Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
This example attempts to make modifications to the standard library (if you have the source component installed). pub fn bug_report<W: std::fmt::Write>(w: &mut W) -> std::fmt::Result {
if true {
writeln!(w, "`;?` here ->")?;
} else {
writeln!(w, "but not here")
}
Ok(())
} JSON: SEE JSON{
"$message_type": "diagnostic",
"message": "mismatched types",
"code":
{
"code": "E0308",
"explanation": "Expected type did not match the received type.\n\nErroneous code examples:\n\n```compile_fail,E0308\nfn plus_one(x: i32) -> i32 {\n x + 1\n}\n\nplus_one(\"Not a number\");\n// ^^^^^^^^^^^^^^ expected `i32`, found `&str`\n\nif \"Not a bool\" {\n// ^^^^^^^^^^^^ expected `bool`, found `&str`\n}\n\nlet x: f32 = \"Not a float\";\n// --- ^^^^^^^^^^^^^ expected `f32`, found `&str`\n// |\n// expected due to this\n```\n\nThis error occurs when an expression was used in a place where the compiler\nexpected an expression of a different type. It can occur in several cases, the\nmost common being when calling a function and passing an argument which has a\ndifferent type than the matching type in the function declaration.\n"
},
"level": "error",
"spans":
[
{
"file_name": "/Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/macros/mod.rs",
"byte_start": 23568,
"byte_end": 23617,
"line_start": 670,
"line_end": 670,
"column_start": 9,
"column_end": 58,
"is_primary": true,
"text":
[
{
"text": " $dst.write_fmt($crate::format_args_nl!($($arg)*))",
"highlight_start": 9,
"highlight_end": 58
}
],
"label": "expected `()`, found `Result<(), Error>`",
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion":
{
"span":
{
"file_name": "lib.rs",
"byte_start": 144,
"byte_end": 171,
"line_start": 5,
"line_end": 5,
"column_start": 9,
"column_end": 36,
"is_primary": false,
"text":
[
{
"text": " writeln!(w, \"but not here\")",
"highlight_start": 9,
"highlight_end": 36
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
},
"macro_decl_name": "writeln!",
"def_site_span":
{
"file_name": "/Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/macros/mod.rs",
"byte_start": 23434,
"byte_end": 23454,
"line_start": 665,
"line_end": 665,
"column_start": 1,
"column_end": 21,
"is_primary": false,
"text":
[
{
"text": "macro_rules! writeln {",
"highlight_start": 1,
"highlight_end": 21
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
}
},
{
"file_name": "lib.rs",
"byte_start": 75,
"byte_end": 177,
"line_start": 2,
"line_end": 6,
"column_start": 5,
"column_end": 6,
"is_primary": false,
"text":
[
{
"text": " if true {",
"highlight_start": 5,
"highlight_end": 14
},
{
"text": " writeln!(w, \"`;?` here ->\")?;",
"highlight_start": 1,
"highlight_end": 38
},
{
"text": " } else {",
"highlight_start": 1,
"highlight_end": 13
},
{
"text": " writeln!(w, \"but not here\")",
"highlight_start": 1,
"highlight_end": 36
},
{
"text": " }",
"highlight_start": 1,
"highlight_end": 6
}
],
"label": "expected this to be `()`",
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children":
[
{
"message": "expected unit type `()`\n found enum `Result<(), std::fmt::Error>`",
"code": null,
"level": "note",
"spans":
[],
"children":
[],
"rendered": null
},
{
"message": "consider using a semicolon here",
"code": null,
"level": "help",
"spans":
[
{
"file_name": "lib.rs",
"byte_start": 177,
"byte_end": 177,
"line_start": 6,
"line_end": 6,
"column_start": 6,
"column_end": 6,
"is_primary": true,
"text":
[
{
"text": " }",
"highlight_start": 6,
"highlight_end": 6
}
],
"label": null,
"suggested_replacement": ";",
"suggestion_applicability": "MaybeIncorrect",
"expansion": null
}
],
"children":
[],
"rendered": null
},
{
"message": "you might have meant to return this value",
"code": null,
"level": "help",
"spans":
[
{
"file_name": "lib.rs",
"byte_start": 144,
"byte_end": 144,
"line_start": 5,
"line_end": 5,
"column_start": 9,
"column_end": 9,
"is_primary": true,
"text":
[
{
"text": " writeln!(w, \"but not here\")",
"highlight_start": 9,
"highlight_end": 9
}
],
"label": null,
"suggested_replacement": "return ",
"suggestion_applicability": "MaybeIncorrect",
"expansion": null
},
{
"file_name": "lib.rs",
"byte_start": 171,
"byte_end": 171,
"line_start": 5,
"line_end": 5,
"column_start": 36,
"column_end": 36,
"is_primary": true,
"text":
[
{
"text": " writeln!(w, \"but not here\")",
"highlight_start": 36,
"highlight_end": 36
}
],
"label": null,
"suggested_replacement": ";",
"suggestion_applicability": "MaybeIncorrect",
"expansion": null
}
],
"children":
[],
"rendered": null
},
{
"message": "use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller",
"code": null,
"level": "help",
"spans":
[
{
"file_name": "/Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/macros/mod.rs",
"byte_start": 23617,
"byte_end": 23617,
"line_start": 670,
"line_end": 670,
"column_start": 58,
"column_end": 58,
"is_primary": true,
"text":
[
{
"text": " $dst.write_fmt($crate::format_args_nl!($($arg)*))",
"highlight_start": 58,
"highlight_end": 58
}
],
"label": null,
"suggested_replacement": "?",
"suggestion_applicability": "HasPlaceholders",
"expansion":
{
"span":
{
"file_name": "lib.rs",
"byte_start": 144,
"byte_end": 171,
"line_start": 5,
"line_end": 5,
"column_start": 9,
"column_end": 36,
"is_primary": false,
"text":
[
{
"text": " writeln!(w, \"but not here\")",
"highlight_start": 9,
"highlight_end": 36
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
},
"macro_decl_name": "writeln!",
"def_site_span":
{
"file_name": "/Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/macros/mod.rs",
"byte_start": 23434,
"byte_end": 23454,
"line_start": 665,
"line_end": 665,
"column_start": 1,
"column_end": 21,
"is_primary": false,
"text":
[
{
"text": "macro_rules! writeln {",
"highlight_start": 1,
"highlight_end": 21
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
}
}
],
"children":
[],
"rendered": null
}
],
"rendered": "error[E0308]: mismatched types\n --> lib.rs:5:9\n |\n2 | / if true {\n3 | | writeln!(w, \"`;?` here ->\")?;\n4 | | } else {\n5 | | writeln!(w, \"but not here\")\n | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>`\n6 | | }\n | |_____- expected this to be `()`\n |\n = note: expected unit type `()`\n found enum `Result<(), std::fmt::Error>`\n = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)\nhelp: consider using a semicolon here\n |\n6 | };\n | +\nhelp: you might have meant to return this value\n |\n5 | return writeln!(w, \"but not here\");\n | ++++++ +\nhelp: use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller\n --> /Users/eric/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/macros/mod.rs:670:58\n |\n67| $dst.write_fmt($crate::format_args_nl!($($arg)*))?\n | +\n\n"
} Suggests to edit
|
The suggestion is slightly tweaked since the original diagnostic was rejected due to multiple files were involved. For the original diagnostic JSON, see rust-lang/rust#88514 (comment)
In some situations, rustc may provide a diagnostic suggestion in an external crate, which in general it shouldn't do. These external crates may be in cargo's registry cache, which the user should not be modifying. For example,
cargo fix
may stomp on changes outside of the package (which is a separate issue rust-lang/cargo#9857). Usually rustc is good about avoiding this, so I'm not sure what is wrong in these situations.In general, I expect rustc to not provide suggestions to modify dependencies.
The crater run found several instances of this, each for different underlying reasons (all dealing with macros).
Example 1 — unused tt
Example dependency:
Example local crate:
Resulting suggestion:
A key point of this example is that the macro uses
tt
instead ofident
.ident
will not issue an unused warning.Found in the 2021 crater run for:
Example 2 — Weird $body suggestion
The following makes a suggestion to remove unnecessary braces, but the suggestion doesn't actually remove any braces.
Suggestion:
Found in the 2021 crater run for:
Example 3 — Hidden JSON suggestion
In this example, the human-readable text doesn't mention the dependency, but the JSON includes a MachineApplicable suggestion.
This emits two duplicate diagnostics in human form:
Buried in the JSON output is a machine-applicable change which modifies diesel:
Found in the 2021 crater run for:
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: