Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c38aca0

Browse files
delay_span_bug if const-checking an async function
This errors during AST lowering. Any errors we emit here are just noise.
1 parent 25c7753 commit c38aca0

File tree

5 files changed

+38
-61
lines changed

5 files changed

+38
-61
lines changed

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
33
use rustc_errors::struct_span_err;
4-
use rustc_hir::{self as hir, LangItem};
5-
use rustc_hir::{def_id::DefId, HirId};
4+
use rustc_hir::def_id::{DefId, LocalDefId};
5+
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_infer::infer::TyCtxtInferExt;
77
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
88
use rustc_middle::mir::*;
@@ -204,6 +204,13 @@ impl Validator<'mir, 'tcx> {
204204
pub fn check_body(&mut self) {
205205
let ConstCx { tcx, body, def_id, .. } = *self.ccx;
206206

207+
// `async` functions cannot be `const fn`. This is checked during AST lowering, so there's
208+
// no need to emit duplicate errors here.
209+
if is_async_fn(tcx, def_id) || body.generator_kind.is_some() {
210+
tcx.sess.delay_span_bug(body.span, "`async` functions cannot be `const fn`");
211+
return;
212+
}
213+
207214
// The local type and predicate checks are not free and only relevant for `const fn`s.
208215
if self.const_kind() == hir::ConstContext::ConstFn {
209216
// Prevent const trait methods from being annotated as `stable`.
@@ -877,3 +884,11 @@ fn place_as_reborrow(
877884
fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
878885
ty.is_bool() || ty.is_integral() || ty.is_char()
879886
}
887+
888+
fn is_async_fn(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
889+
let hir_map = tcx.hir();
890+
let hir_id = hir_map.local_def_id_to_hir_id(def_id);
891+
hir_map
892+
.fn_sig_by_hir_id(hir_id)
893+
.map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async)
894+
}

src/test/ui/async-await/no-const-async.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33

44
pub const async fn x() {}
55
//~^ ERROR functions cannot be both `const` and `async`
6-
//~| ERROR `impl Trait` in const fn is unstable

src/test/ui/async-await/no-const-async.stderr

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,5 @@ LL | pub const async fn x() {}
77
| | `async` because of this
88
| `const` because of this
99

10-
error[E0723]: `impl Trait` in const fn is unstable
11-
--> $DIR/no-const-async.rs:4:24
12-
|
13-
LL | pub const async fn x() {}
14-
| ^
15-
|
16-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
18-
19-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
2011

21-
For more information about this error, try `rustc --explain E0723`.

src/test/ui/parser/fn-header-semantic-fail.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ fn main() {
1212
extern "C" fn ff4() {} // OK.
1313
const async unsafe extern "C" fn ff5() {} // OK.
1414
//~^ ERROR functions cannot be both `const` and `async`
15-
//~| ERROR `from_generator` is not yet stable as a const fn
1615

1716
trait X {
1817
async fn ft1(); //~ ERROR functions in traits cannot be declared `async`
@@ -35,7 +34,6 @@ fn main() {
3534
const async unsafe extern "C" fn ft5() {}
3635
//~^ ERROR functions in traits cannot be declared `async`
3736
//~| ERROR functions in traits cannot be declared const
38-
//~| ERROR `from_generator` is not yet stable as a const fn
3937
//~| ERROR method `ft5` has an incompatible type for trait
4038
//~| ERROR functions cannot be both `const` and `async`
4139
}
@@ -47,7 +45,6 @@ fn main() {
4745
extern "C" fn fi4() {} // OK.
4846
const async unsafe extern "C" fn fi5() {}
4947
//~^ ERROR functions cannot be both `const` and `async`
50-
//~| ERROR `from_generator` is not yet stable as a const fn
5148
}
5249

5350
extern {

src/test/ui/parser/fn-header-semantic-fail.stderr

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | const async unsafe extern "C" fn ff5() {} // OK.
88
| `const` because of this
99

1010
error[E0706]: functions in traits cannot be declared `async`
11-
--> $DIR/fn-header-semantic-fail.rs:18:9
11+
--> $DIR/fn-header-semantic-fail.rs:17:9
1212
|
1313
LL | async fn ft1();
1414
| -----^^^^^^^^^^
@@ -19,19 +19,19 @@ LL | async fn ft1();
1919
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
2020

2121
error[E0379]: functions in traits cannot be declared const
22-
--> $DIR/fn-header-semantic-fail.rs:20:9
22+
--> $DIR/fn-header-semantic-fail.rs:19:9
2323
|
2424
LL | const fn ft3();
2525
| ^^^^^ functions in traits cannot be const
2626

2727
error[E0379]: functions in traits cannot be declared const
28-
--> $DIR/fn-header-semantic-fail.rs:22:9
28+
--> $DIR/fn-header-semantic-fail.rs:21:9
2929
|
3030
LL | const async unsafe extern "C" fn ft5();
3131
| ^^^^^ functions in traits cannot be const
3232

3333
error[E0706]: functions in traits cannot be declared `async`
34-
--> $DIR/fn-header-semantic-fail.rs:22:9
34+
--> $DIR/fn-header-semantic-fail.rs:21:9
3535
|
3636
LL | const async unsafe extern "C" fn ft5();
3737
| ^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -42,7 +42,7 @@ LL | const async unsafe extern "C" fn ft5();
4242
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
4343

4444
error: functions cannot be both `const` and `async`
45-
--> $DIR/fn-header-semantic-fail.rs:22:9
45+
--> $DIR/fn-header-semantic-fail.rs:21:9
4646
|
4747
LL | const async unsafe extern "C" fn ft5();
4848
| ^^^^^-^^^^^----------------------------
@@ -51,7 +51,7 @@ LL | const async unsafe extern "C" fn ft5();
5151
| `const` because of this
5252

5353
error[E0706]: functions in traits cannot be declared `async`
54-
--> $DIR/fn-header-semantic-fail.rs:30:9
54+
--> $DIR/fn-header-semantic-fail.rs:29:9
5555
|
5656
LL | async fn ft1() {}
5757
| -----^^^^^^^^^^^^
@@ -62,19 +62,19 @@ LL | async fn ft1() {}
6262
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
6363

6464
error[E0379]: functions in traits cannot be declared const
65-
--> $DIR/fn-header-semantic-fail.rs:33:9
65+
--> $DIR/fn-header-semantic-fail.rs:32:9
6666
|
6767
LL | const fn ft3() {}
6868
| ^^^^^ functions in traits cannot be const
6969

7070
error[E0379]: functions in traits cannot be declared const
71-
--> $DIR/fn-header-semantic-fail.rs:35:9
71+
--> $DIR/fn-header-semantic-fail.rs:34:9
7272
|
7373
LL | const async unsafe extern "C" fn ft5() {}
7474
| ^^^^^ functions in traits cannot be const
7575

7676
error[E0706]: functions in traits cannot be declared `async`
77-
--> $DIR/fn-header-semantic-fail.rs:35:9
77+
--> $DIR/fn-header-semantic-fail.rs:34:9
7878
|
7979
LL | const async unsafe extern "C" fn ft5() {}
8080
| ^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -85,7 +85,7 @@ LL | const async unsafe extern "C" fn ft5() {}
8585
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
8686

8787
error: functions cannot be both `const` and `async`
88-
--> $DIR/fn-header-semantic-fail.rs:35:9
88+
--> $DIR/fn-header-semantic-fail.rs:34:9
8989
|
9090
LL | const async unsafe extern "C" fn ft5() {}
9191
| ^^^^^-^^^^^------------------------------
@@ -94,7 +94,7 @@ LL | const async unsafe extern "C" fn ft5() {}
9494
| `const` because of this
9595

9696
error: functions cannot be both `const` and `async`
97-
--> $DIR/fn-header-semantic-fail.rs:48:9
97+
--> $DIR/fn-header-semantic-fail.rs:46:9
9898
|
9999
LL | const async unsafe extern "C" fn fi5() {}
100100
| ^^^^^-^^^^^------------------------------
@@ -103,7 +103,7 @@ LL | const async unsafe extern "C" fn fi5() {}
103103
| `const` because of this
104104

105105
error: functions in `extern` blocks cannot have qualifiers
106-
--> $DIR/fn-header-semantic-fail.rs:54:18
106+
--> $DIR/fn-header-semantic-fail.rs:51:18
107107
|
108108
LL | extern {
109109
| ------ in this `extern` block
@@ -113,7 +113,7 @@ LL | async fn fe1();
113113
| help: remove the qualifiers: `fn`
114114

115115
error: functions in `extern` blocks cannot have qualifiers
116-
--> $DIR/fn-header-semantic-fail.rs:55:19
116+
--> $DIR/fn-header-semantic-fail.rs:52:19
117117
|
118118
LL | extern {
119119
| ------ in this `extern` block
@@ -124,7 +124,7 @@ LL | unsafe fn fe2();
124124
| help: remove the qualifiers: `fn`
125125

126126
error: functions in `extern` blocks cannot have qualifiers
127-
--> $DIR/fn-header-semantic-fail.rs:56:18
127+
--> $DIR/fn-header-semantic-fail.rs:53:18
128128
|
129129
LL | extern {
130130
| ------ in this `extern` block
@@ -135,7 +135,7 @@ LL | const fn fe3();
135135
| help: remove the qualifiers: `fn`
136136

137137
error: functions in `extern` blocks cannot have qualifiers
138-
--> $DIR/fn-header-semantic-fail.rs:57:23
138+
--> $DIR/fn-header-semantic-fail.rs:54:23
139139
|
140140
LL | extern {
141141
| ------ in this `extern` block
@@ -146,7 +146,7 @@ LL | extern "C" fn fe4();
146146
| help: remove the qualifiers: `fn`
147147

148148
error: functions in `extern` blocks cannot have qualifiers
149-
--> $DIR/fn-header-semantic-fail.rs:58:42
149+
--> $DIR/fn-header-semantic-fail.rs:55:42
150150
|
151151
LL | extern {
152152
| ------ in this `extern` block
@@ -157,24 +157,16 @@ LL | const async unsafe extern "C" fn fe5();
157157
| help: remove the qualifiers: `fn`
158158

159159
error: functions cannot be both `const` and `async`
160-
--> $DIR/fn-header-semantic-fail.rs:58:9
160+
--> $DIR/fn-header-semantic-fail.rs:55:9
161161
|
162162
LL | const async unsafe extern "C" fn fe5();
163163
| ^^^^^-^^^^^----------------------------
164164
| | |
165165
| | `async` because of this
166166
| `const` because of this
167167

168-
error: `from_generator` is not yet stable as a const fn
169-
--> $DIR/fn-header-semantic-fail.rs:13:44
170-
|
171-
LL | const async unsafe extern "C" fn ff5() {} // OK.
172-
| ^^
173-
|
174-
= help: add `#![feature(gen_future)]` to the crate attributes to enable
175-
176168
error[E0053]: method `ft1` has an incompatible type for trait
177-
--> $DIR/fn-header-semantic-fail.rs:30:24
169+
--> $DIR/fn-header-semantic-fail.rs:29:24
178170
|
179171
LL | async fn ft1();
180172
| - type in trait
@@ -189,7 +181,7 @@ LL | async fn ft1() {}
189181
found fn pointer `fn() -> impl Future`
190182

191183
error[E0053]: method `ft5` has an incompatible type for trait
192-
--> $DIR/fn-header-semantic-fail.rs:35:48
184+
--> $DIR/fn-header-semantic-fail.rs:34:48
193185
|
194186
LL | const async unsafe extern "C" fn ft5();
195187
| - type in trait
@@ -203,23 +195,7 @@ LL | const async unsafe extern "C" fn ft5() {}
203195
= note: expected fn pointer `unsafe extern "C" fn()`
204196
found fn pointer `unsafe extern "C" fn() -> impl Future`
205197

206-
error: `from_generator` is not yet stable as a const fn
207-
--> $DIR/fn-header-semantic-fail.rs:35:48
208-
|
209-
LL | const async unsafe extern "C" fn ft5() {}
210-
| ^^
211-
|
212-
= help: add `#![feature(gen_future)]` to the crate attributes to enable
213-
214-
error: `from_generator` is not yet stable as a const fn
215-
--> $DIR/fn-header-semantic-fail.rs:48:48
216-
|
217-
LL | const async unsafe extern "C" fn fi5() {}
218-
| ^^
219-
|
220-
= help: add `#![feature(gen_future)]` to the crate attributes to enable
221-
222-
error: aborting due to 23 previous errors
198+
error: aborting due to 20 previous errors
223199

224200
Some errors have detailed explanations: E0053, E0379, E0706.
225201
For more information about an error, try `rustc --explain E0053`.

0 commit comments

Comments
 (0)