about summary refs log tree commit diff
path: root/tests/ui/structs/struct-fn-in-definition.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/structs/struct-fn-in-definition.rs')
-rw-r--r--tests/ui/structs/struct-fn-in-definition.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/ui/structs/struct-fn-in-definition.rs b/tests/ui/structs/struct-fn-in-definition.rs
new file mode 100644
index 00000000000..7f48f55fec9
--- /dev/null
+++ b/tests/ui/structs/struct-fn-in-definition.rs
@@ -0,0 +1,34 @@
+// It might be intuitive for a user coming from languages like Java
+// to declare a method directly in a struct's definition. Make sure
+// rustc can give a helpful suggestion.
+// Suggested in issue #76421
+
+struct S {
+    field: usize,
+
+    fn foo() {}
+    //~^ ERROR functions are not allowed in struct definitions
+    //~| HELP unlike in C++, Java, and C#, functions are declared in `impl` blocks
+    //~| HELP see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
+}
+
+union U {
+    variant: usize,
+
+    fn foo() {}
+    //~^ ERROR functions are not allowed in union definitions
+    //~| HELP unlike in C++, Java, and C#, functions are declared in `impl` blocks
+    //~| HELP see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
+}
+
+enum E {
+    Variant,
+
+    fn foo() {}
+    //~^ ERROR functions are not allowed in enum definitions
+    //~| HELP unlike in C++, Java, and C#, functions are declared in `impl` blocks
+    //~| HELP see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information
+    //~| HELP enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
+}
+
+fn main() {}