about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2024-11-17 22:30:48 -0500
committerGitHub <noreply@github.com>2024-11-17 22:30:48 -0500
commitc8741214873a48f7964a907952162dc20518095a (patch)
tree4c5b5a59d0278234fbce35f1cf1ffad5a1122107 /tests
parente2993cd06e7b35422fcf172b0ff247873ddbb163 (diff)
parent949cf61fdce2309920ebfa9f7e8363308fbdac08 (diff)
downloadrust-c8741214873a48f7964a907952162dc20518095a.tar.gz
rust-c8741214873a48f7964a907952162dc20518095a.zip
Rollup merge of #132944 - linyihai:needing-parenthases-issue-132924, r=chenyukang
add parentheses when unboxing suggestion needed

This PR tried to `add parentheses when unboxing suggestion needed`

Fixes #132924
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs18
-rw-r--r--tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr59
2 files changed, 77 insertions, 0 deletions
diff --git a/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs
new file mode 100644
index 00000000000..fc4258fc0af
--- /dev/null
+++ b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs
@@ -0,0 +1,18 @@
+//@ check-fail
+fn main() {
+    let x = Box::new(Some(1));
+
+    let test: Option<i32> = x;
+    //~^ ERROR mismatched types
+    let x = Box::new(Some(1));
+    let test: Option<i32> = { x as Box<Option<i32>> };
+    //~^ ERROR mismatched types
+
+    let x = Box::new(Some(1));
+    let test: Option<i32> = if true { x as Box<Option<i32>> } else { None };
+    //~^ ERROR mismatched types
+
+    let x = std::rc::Rc::new(Some(1));
+    let test: Option<i32> = x as std::rc::Rc<Option<i32>>;
+    //~^ ERROR mismatched types
+}
diff --git a/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr
new file mode 100644
index 00000000000..429b1b87357
--- /dev/null
+++ b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr
@@ -0,0 +1,59 @@
+error[E0308]: mismatched types
+  --> $DIR/unboxing-needing-parenthases-issue-132924.rs:5:29
+   |
+LL |     let test: Option<i32> = x;
+   |               -----------   ^ expected `Option<i32>`, found `Box<Option<{integer}>>`
+   |               |
+   |               expected due to this
+   |
+   = note: expected enum `Option<i32>`
+            found struct `Box<Option<{integer}>>`
+help: consider unboxing the value
+   |
+LL |     let test: Option<i32> = *x;
+   |                             +
+
+error[E0308]: mismatched types
+  --> $DIR/unboxing-needing-parenthases-issue-132924.rs:8:31
+   |
+LL |     let test: Option<i32> = { x as Box<Option<i32>> };
+   |                               ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
+   |
+   = note: expected enum `Option<_>`
+            found struct `Box<Option<_>>`
+help: consider unboxing the value
+   |
+LL |     let test: Option<i32> = { *(x as Box<Option<i32>>) };
+   |                               ++                     +
+
+error[E0308]: mismatched types
+  --> $DIR/unboxing-needing-parenthases-issue-132924.rs:12:39
+   |
+LL |     let test: Option<i32> = if true { x as Box<Option<i32>> } else { None };
+   |                                       ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
+   |
+   = note: expected enum `Option<_>`
+            found struct `Box<Option<_>>`
+help: consider unboxing the value
+   |
+LL |     let test: Option<i32> = if true { *(x as Box<Option<i32>>) } else { None };
+   |                                       ++                     +
+
+error[E0308]: mismatched types
+  --> $DIR/unboxing-needing-parenthases-issue-132924.rs:16:29
+   |
+LL |     let test: Option<i32> = x as std::rc::Rc<Option<i32>>;
+   |               -----------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Rc<Option<i32>>`
+   |               |
+   |               expected due to this
+   |
+   = note: expected enum `Option<_>`
+            found struct `Rc<Option<_>>`
+help: consider dereferencing the type
+   |
+LL |     let test: Option<i32> = *(x as std::rc::Rc<Option<i32>>);
+   |                             ++                             +
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0308`.