about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-05-14 18:43:40 +0200
committerGitHub <noreply@github.com>2025-05-14 18:43:40 +0200
commite39ab25bf4e1f7143d0f9034ec4aa2759b965809 (patch)
tree61bd006113b87774295a21cd8f0969e3648d2aaf /tests
parent8395461fa435d8e1c6b61ff1d5be390fbd9aec23 (diff)
parent32be4599090d62cd7f70767e27c289583fd6501d (diff)
downloadrust-e39ab25bf4e1f7143d0f9034ec4aa2759b965809.tar.gz
rust-e39ab25bf4e1f7143d0f9034ec4aa2759b965809.zip
Rollup merge of #140989 - xizheyin:issue-139631, r=compiler-errors
Suggest replace f with f: Box<f> when expr field is short hand

Fixes #139631

r? compiler
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/box/suggest-box-for-expr-field-issue-139631.rs14
-rw-r--r--tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr44
2 files changed, 58 insertions, 0 deletions
diff --git a/tests/ui/box/suggest-box-for-expr-field-issue-139631.rs b/tests/ui/box/suggest-box-for-expr-field-issue-139631.rs
new file mode 100644
index 00000000000..8d040da1ef7
--- /dev/null
+++ b/tests/ui/box/suggest-box-for-expr-field-issue-139631.rs
@@ -0,0 +1,14 @@
+struct X {
+    a: Box<u32>,
+}
+
+struct Y {
+    y: Box<u32>,
+}
+
+fn main() {
+    let a = 8;
+    let v2 = X { a }; //~ ERROR mismatched types [E0308]
+    let v3 = Y { y: a }; //~ ERROR mismatched types [E0308]
+    let v4 = Y { a }; //~ ERROR struct `Y` has no field named `a` [E0560]
+}
diff --git a/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr b/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
new file mode 100644
index 00000000000..01bd0523a16
--- /dev/null
+++ b/tests/ui/box/suggest-box-for-expr-field-issue-139631.stderr
@@ -0,0 +1,44 @@
+error[E0308]: mismatched types
+  --> $DIR/suggest-box-for-expr-field-issue-139631.rs:11:18
+   |
+LL |     let v2 = X { a };
+   |                  ^ expected `Box<u32>`, found integer
+   |
+   = note: expected struct `Box<u32>`
+                found type `{integer}`
+   = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
+help: store this in the heap by calling `Box::new`
+   |
+LL |     let v2 = X { a: Box::new(a) };
+   |                  ++++++++++++ +
+
+error[E0308]: mismatched types
+  --> $DIR/suggest-box-for-expr-field-issue-139631.rs:12:21
+   |
+LL |     let v3 = Y { y: a };
+   |                     ^ expected `Box<u32>`, found integer
+   |
+   = note: expected struct `Box<u32>`
+                found type `{integer}`
+   = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
+help: store this in the heap by calling `Box::new`
+   |
+LL |     let v3 = Y { y: Box::new(a) };
+   |                     +++++++++ +
+
+error[E0560]: struct `Y` has no field named `a`
+  --> $DIR/suggest-box-for-expr-field-issue-139631.rs:13:18
+   |
+LL |     let v4 = Y { a };
+   |                  ^ unknown field
+   |
+help: a field with a similar name exists
+   |
+LL -     let v4 = Y { a };
+LL +     let v4 = Y { y };
+   |
+
+error: aborting due to 3 previous errors
+
+Some errors have detailed explanations: E0308, E0560.
+For more information about an error, try `rustc --explain E0308`.