forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#123654 - jieyouxu:question-mark-span, r=Nadrieril typeck: fix `?` suggestion span Noticed in <rust-lang#112043 (comment)>, if the ``` use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller ``` suggestion is applied to a macro that comes from a non-local crate (e.g. the stdlib), the suggestion span can become non-local, which will cause newer rustfix versions to fail. This PR tries to remedy the problem by recursively probing ancestors of the expression span, trying to identify the most ancestor span that is (1) still local, and (2) still shares the same syntax context as the expression. This is the same strategy used in rust-lang#112043. The test unfortunately cannot `//@ run-rustfix` because there are two conflicting MaybeIncorrect suggestions that when collectively applied, cause the fixed source file to become non-compilable. Also avoid running `//@ run-rustfix` for `tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.rs` because that also contains conflicting suggestions. cc `@ehuss` who noticed this. This question mark span fix + not running rustfix on the tests containing conflicting MaybeIncorrect suggestions should hopefully unblock rustfix from updating.
- Loading branch information
Showing
7 changed files
with
118 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.fixed
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Check that we don't construct a span for `?` suggestions that point into non-local macros | ||
// like into the stdlib where the user has no control over. | ||
// | ||
// FIXME(jieyouxu): this test is currently NOT run-rustfix because there are conflicting | ||
// MaybeIncorrect suggestions: | ||
// | ||
// 1. adding `return ... ;`, and | ||
// 2. adding `?`. | ||
// | ||
// When rustfix puts those together, the fixed file now contains uncompilable code. | ||
|
||
#![crate_type = "lib"] | ||
|
||
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") | ||
//~^ ERROR mismatched types | ||
} | ||
Ok(()) | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/ui/typeck/question-mark-operator-suggestion-span.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/question-mark-operator-suggestion-span.rs:18:9 | ||
| | ||
LL | / if true { | ||
LL | | writeln!(w, "`;?` here ->")?; | ||
LL | | } else { | ||
LL | | writeln!(w, "but not here") | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<(), Error>` | ||
LL | | | ||
LL | | } | ||
| |_____- expected this to be `()` | ||
| | ||
= note: expected unit type `()` | ||
found enum `Result<(), std::fmt::Error>` | ||
= note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider using a semicolon here | ||
| | ||
LL | }; | ||
| + | ||
help: you might have meant to return this value | ||
| | ||
LL | return writeln!(w, "but not here"); | ||
| ++++++ + | ||
help: use the `?` operator to extract the `Result<(), std::fmt::Error>` value, propagating a `Result::Err` value to the caller | ||
| | ||
LL | writeln!(w, "but not here")? | ||
| + | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |