Skip to content

Commit 8d3f349

Browse files
committed
Update error message for const_gen_fn.rs
1 parent 49bc90a commit 8d3f349

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

compiler/rustc_const_eval/src/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'tcx> NonConstOp<'tcx> for Coroutine {
503503
}
504504

505505
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
506-
let msg = format!("{:#}s are not allowed in {}s", self.0, ccx.const_kind());
506+
let msg = format!("{} are not allowed in {}s", self.0.to_plural_string(), ccx.const_kind());
507507
if let Status::Unstable { gate, .. } = self.status_in_item(ccx) {
508508
ccx.tcx.sess.create_feature_err(errors::UnallowedOpInConstContext { span, msg }, gate)
509509
} else {

compiler/rustc_hir/src/hir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,15 @@ impl CoroutineKind {
19471947
pub fn is_fn_like(self) -> bool {
19481948
matches!(self, CoroutineKind::Desugared(_, CoroutineSource::Fn))
19491949
}
1950+
1951+
pub fn to_plural_string(&self) -> String {
1952+
match self {
1953+
CoroutineKind::Desugared(d, CoroutineSource::Fn) => format!("{d:#}fn bodies"),
1954+
CoroutineKind::Desugared(d, CoroutineSource::Block) => format!("{d:#}blocks"),
1955+
CoroutineKind::Desugared(d, CoroutineSource::Closure) => format!("{d:#}closure bodies"),
1956+
CoroutineKind::Coroutine(_) => "coroutines".to_string(),
1957+
}
1958+
}
19501959
}
19511960

19521961
impl fmt::Display for CoroutineKind {

tests/ui/coroutine/const_gen_fn.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const gen fn a() {}
66
//~^ ERROR functions cannot be both `const` and `gen`
7+
//~^^ ERROR `gen` fn bodies are not allowed in constant functions
78

89
const async gen fn b() {}
910
//~^ ERROR functions cannot be both `const` and `async gen`

tests/ui/coroutine/const_gen_fn.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ LL | const gen fn a() {}
88
| `const` because of this
99

1010
error: functions cannot be both `const` and `async gen`
11-
--> $DIR/const_gen_fn.rs:8:1
11+
--> $DIR/const_gen_fn.rs:9:1
1212
|
1313
LL | const async gen fn b() {}
1414
| ^^^^^-^^^^^^^^^----------
1515
| | |
1616
| | `async gen` because of this
1717
| `const` because of this
1818

19-
error: aborting due to 2 previous errors
19+
error: `gen` fn bodies are not allowed in constant functions
20+
--> $DIR/const_gen_fn.rs:5:18
21+
|
22+
LL | const gen fn a() {}
23+
| ^^
24+
25+
error: aborting due to 3 previous errors
2026

0 commit comments

Comments
 (0)