about summary refs log tree commit diff
path: root/tests/ui/parser
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-01-04 12:50:38 +0000
committerbors <bors@rust-lang.org>2025-01-04 12:50:38 +0000
commitfd127a3a84e3874979aa6f52acfb184e3ecce210 (patch)
treeaffac6759c02287640b9cf4a87e93a0dfc625651 /tests/ui/parser
parentf17cf744f5f757ef9dc5f45508332c75d74ffad0 (diff)
parent3cd3649c6cd370ac20e47f40bd5f210c9fe63f92 (diff)
Auto merge of #135031 - RalfJung:intrinsics-without-body, r=oli-obk
rustc_intrinsic: support functions without body

We synthesize a HIR body `loop {}` but such bodyless intrinsics.

Most of the diff is due to turning `ItemKind::Fn` into a brace (named-field) enum variant, because it carries a `bool`-typed field now. This is to remember whether the function has a body. MIR building panics to avoid ever translating the fake `loop {}` body, and the intrinsic logic uses the lack of a body to implicitly mark that intrinsic as must-be-overridden.

I first tried actually having no body rather than generating the fake body, but there's a *lot* of code that assumes that all function items have HIR and MIR, so this didn't work very well. Then I noticed that even `rustc_intrinsic_must_be_overridden` intrinsics have MIR generated (they are filled with an `Unreachable` terminator) so I guess I am not the first to discover this. ;)

r? `@oli-obk`
Diffstat (limited to 'tests/ui/parser')
-rw-r--r--tests/ui/parser/fn-body-optional-semantic-fail.rs4
-rw-r--r--tests/ui/parser/fn-body-optional-semantic-fail.stderr18
2 files changed, 17 insertions, 5 deletions
diff --git a/tests/ui/parser/fn-body-optional-semantic-fail.rs b/tests/ui/parser/fn-body-optional-semantic-fail.rs
index 12df488802e..46f5d7e96d1 100644
--- a/tests/ui/parser/fn-body-optional-semantic-fail.rs
+++ b/tests/ui/parser/fn-body-optional-semantic-fail.rs
@@ -1,7 +1,11 @@
 // Tests the different rules for `fn` forms requiring the presence or lack of a body.
+// Also ensures that functions without a body don't show other odd errors.
+
+trait Trait {}
 
 fn main() {
     fn f1(); //~ ERROR free function without a body
+    fn f1_rpit() -> impl Trait; //~ ERROR free function without a body
     fn f2() {} // OK.
 
     trait X {
diff --git a/tests/ui/parser/fn-body-optional-semantic-fail.stderr b/tests/ui/parser/fn-body-optional-semantic-fail.stderr
index 14bcd7c16fa..525a0a0f681 100644
--- a/tests/ui/parser/fn-body-optional-semantic-fail.stderr
+++ b/tests/ui/parser/fn-body-optional-semantic-fail.stderr
@@ -1,13 +1,21 @@
 error: free function without a body
-  --> $DIR/fn-body-optional-semantic-fail.rs:4:5
+  --> $DIR/fn-body-optional-semantic-fail.rs:7:5
    |
 LL |     fn f1();
    |     ^^^^^^^-
    |            |
    |            help: provide a definition for the function: `{ <body> }`
 
+error: free function without a body
+  --> $DIR/fn-body-optional-semantic-fail.rs:8:5
+   |
+LL |     fn f1_rpit() -> impl Trait;
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^-
+   |                               |
+   |                               help: provide a definition for the function: `{ <body> }`
+
 error: associated function in `impl` without body
-  --> $DIR/fn-body-optional-semantic-fail.rs:14:9
+  --> $DIR/fn-body-optional-semantic-fail.rs:18:9
    |
 LL |         fn f1();
    |         ^^^^^^^-
@@ -15,7 +23,7 @@ LL |         fn f1();
    |                help: provide a definition for the function: `{ <body> }`
 
 error: associated function in `impl` without body
-  --> $DIR/fn-body-optional-semantic-fail.rs:19:9
+  --> $DIR/fn-body-optional-semantic-fail.rs:23:9
    |
 LL |         fn f3();
    |         ^^^^^^^-
@@ -23,7 +31,7 @@ LL |         fn f3();
    |                help: provide a definition for the function: `{ <body> }`
 
 error: incorrect function inside `extern` block
-  --> $DIR/fn-body-optional-semantic-fail.rs:25:12
+  --> $DIR/fn-body-optional-semantic-fail.rs:29:12
    |
 LL |     extern "C" {
    |     ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
@@ -36,5 +44,5 @@ LL |         fn f6() {}
    = help: you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block
    = note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
 
-error: aborting due to 4 previous errors
+error: aborting due to 5 previous errors