about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-04-08 21:18:51 +0000
committer许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2024-04-10 19:37:48 +0000
commita809ec96f3cf5f5a73db24129392e64c8285bda8 (patch)
treecd0419414050c07bb173ffb273659fd4695450bf
parentb14d8b2ef20c64c1002e2c6c724025c3d0846b91 (diff)
downloadrust-a809ec96f3cf5f5a73db24129392e64c8285bda8.tar.gz
rust-a809ec96f3cf5f5a73db24129392e64c8285bda8.zip
test: check that `?` suggestion has local span
This can cause rustfix to crash because the `?` suggestion previously
could point into non-local spans, such as into the stdlib.
-rw-r--r--tests/ui/typeck/question-mark-operator-suggestion-span.rs22
-rw-r--r--tests/ui/typeck/question-mark-operator-suggestion-span.stderr31
2 files changed, 53 insertions, 0 deletions
diff --git a/tests/ui/typeck/question-mark-operator-suggestion-span.rs b/tests/ui/typeck/question-mark-operator-suggestion-span.rs
new file mode 100644
index 00000000000..7aea6e63dd1
--- /dev/null
+++ b/tests/ui/typeck/question-mark-operator-suggestion-span.rs
@@ -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(())
+}
diff --git a/tests/ui/typeck/question-mark-operator-suggestion-span.stderr b/tests/ui/typeck/question-mark-operator-suggestion-span.stderr
new file mode 100644
index 00000000000..089b3bcd198
--- /dev/null
+++ b/tests/ui/typeck/question-mark-operator-suggestion-span.stderr
@@ -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`.