Skip to content

Commit 594de02

Browse files
Properly deny const gen/async gen fns
1 parent 8d6b88b commit 594de02

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,14 @@ impl CoroutineKind {
26022602
}
26032603
}
26042604

2605+
pub fn as_str(self) -> &'static str {
2606+
match self {
2607+
CoroutineKind::Async { .. } => "async",
2608+
CoroutineKind::Gen { .. } => "gen",
2609+
CoroutineKind::AsyncGen { .. } => "async gen",
2610+
}
2611+
}
2612+
26052613
pub fn is_async(self) -> bool {
26062614
matches!(self, CoroutineKind::Async { .. })
26072615
}

Diff for: compiler/rustc_ast_passes/messages.ftl

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ ast_passes_body_in_extern = incorrect `{$kind}` inside `extern` block
4040
4141
ast_passes_bound_in_context = bounds on `type`s in {$ctx} have no effect
4242
43-
ast_passes_const_and_async = functions cannot be both `const` and `async`
44-
.const = `const` because of this
45-
.async = `async` because of this
46-
.label = {""}
47-
4843
ast_passes_const_and_c_variadic = functions cannot be both `const` and C-variadic
4944
.const = `const` because of this
5045
.variadic = C-variadic because of this
5146
47+
ast_passes_const_and_coroutine = functions cannot be both `const` and `{$coroutine_kind}`
48+
.const = `const` because of this
49+
.coroutine = `{$coroutine_kind}` because of this
50+
.label = {""}
51+
5252
ast_passes_const_bound_trait_object = const trait bounds are not allowed in trait object types
5353
5454
ast_passes_const_without_body =

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1418,21 +1418,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14181418

14191419
// Functions cannot both be `const async` or `const gen`
14201420
if let Some(&FnHeader {
1421-
constness: Const::Yes(cspan),
1421+
constness: Const::Yes(const_span),
14221422
coroutine_kind: Some(coroutine_kind),
14231423
..
14241424
}) = fk.header()
14251425
{
1426-
let aspan = match coroutine_kind {
1427-
CoroutineKind::Async { span: aspan, .. }
1428-
| CoroutineKind::Gen { span: aspan, .. }
1429-
| CoroutineKind::AsyncGen { span: aspan, .. } => aspan,
1430-
};
1431-
// FIXME(gen_blocks): Report a different error for `const gen`
1432-
self.dcx().emit_err(errors::ConstAndAsync {
1433-
spans: vec![cspan, aspan],
1434-
cspan,
1435-
aspan,
1426+
self.dcx().emit_err(errors::ConstAndCoroutine {
1427+
spans: vec![coroutine_kind.span(), const_span],
1428+
const_span,
1429+
coroutine_span: coroutine_kind.span(),
1430+
coroutine_kind: coroutine_kind.as_str(),
14361431
span,
14371432
});
14381433
}

Diff for: compiler/rustc_ast_passes/src/errors.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -657,16 +657,17 @@ pub(crate) enum TildeConstReason {
657657
}
658658

659659
#[derive(Diagnostic)]
660-
#[diag(ast_passes_const_and_async)]
661-
pub(crate) struct ConstAndAsync {
660+
#[diag(ast_passes_const_and_coroutine)]
661+
pub(crate) struct ConstAndCoroutine {
662662
#[primary_span]
663663
pub spans: Vec<Span>,
664664
#[label(ast_passes_const)]
665-
pub cspan: Span,
666-
#[label(ast_passes_async)]
667-
pub aspan: Span,
665+
pub const_span: Span,
666+
#[label(ast_passes_coroutine)]
667+
pub coroutine_span: Span,
668668
#[label]
669669
pub span: Span,
670+
pub coroutine_kind: &'static str,
670671
}
671672

672673
#[derive(Diagnostic)]

Diff for: tests/ui/coroutine/const_gen_fn.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ edition:2024
2+
//@ compile-flags: -Zunstable-options
3+
4+
#![feature(gen_blocks)]
5+
6+
const gen fn a() {}
7+
//~^ ERROR functions cannot be both `const` and `gen`
8+
9+
const async gen fn b() {}
10+
//~^ ERROR functions cannot be both `const` and `async gen`
11+
12+
fn main() {}

Diff for: tests/ui/coroutine/const_gen_fn.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: functions cannot be both `const` and `gen`
2+
--> $DIR/const_gen_fn.rs:6:1
3+
|
4+
LL | const gen fn a() {}
5+
| ^^^^^-^^^----------
6+
| | |
7+
| | `gen` because of this
8+
| `const` because of this
9+
10+
error: functions cannot be both `const` and `async gen`
11+
--> $DIR/const_gen_fn.rs:9:1
12+
|
13+
LL | const async gen fn b() {}
14+
| ^^^^^-^^^^^^^^^----------
15+
| | |
16+
| | `async gen` because of this
17+
| `const` because of this
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)