Skip to content

Commit 61bfe36

Browse files
authored
Rollup merge of #89021 - WaffleLapkin:separate_error_for_dyn_trait_in_const_fn, r=estebank
Add a separate error for `dyn Trait` in `const fn` Previously "trait bounds other than `Sized` on const fn parameters are unstable" error was used for both trait bounds (`<T: Trait>`) and trait objects (`dyn Trait`). This was pretty confusing. This PR adds a separate error for trait objects: "trait objects in const fn are unstable". The error for trait bounds is otherwise intact. This is follow up to #88907 r? ``@estebank`` ``@rustbot`` label: +A-diagnostics
2 parents 91c5e7c + f84000d commit 61bfe36

File tree

8 files changed

+62
-23
lines changed

8 files changed

+62
-23
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ impl Checker<'mir, 'tcx> {
384384
match pred.skip_binder() {
385385
ty::ExistentialPredicate::AutoTrait(_)
386386
| ty::ExistentialPredicate::Projection(_) => {
387-
self.check_op(ops::ty::TraitBound(kind))
387+
self.check_op(ops::ty::DynTrait(kind))
388388
}
389389
ty::ExistentialPredicate::Trait(trait_ref) => {
390390
if Some(trait_ref.def_id) != self.tcx.lang_items().sized_trait() {
391-
self.check_op(ops::ty::TraitBound(kind))
391+
self.check_op(ops::ty::DynTrait(kind))
392392
}
393393
}
394394
}

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ pub mod ty {
599599
}
600600

601601
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
602-
let mut builder = feature_err(
602+
let mut err = feature_err(
603603
&ccx.tcx.sess.parse_sess,
604604
sym::const_fn_trait_bound,
605605
span,
@@ -608,12 +608,51 @@ pub mod ty {
608608

609609
match ccx.fn_sig() {
610610
Some(fn_sig) if !fn_sig.span.contains(span) => {
611-
builder.span_label(fn_sig.span, "function declared as const here");
611+
err.span_label(fn_sig.span, "function declared as const here");
612612
}
613613
_ => {}
614614
}
615615

616-
builder
616+
err
617+
}
618+
}
619+
620+
#[derive(Debug)]
621+
pub struct DynTrait(pub mir::LocalKind);
622+
impl NonConstOp for DynTrait {
623+
fn importance(&self) -> DiagnosticImportance {
624+
match self.0 {
625+
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
626+
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
627+
DiagnosticImportance::Primary
628+
}
629+
}
630+
}
631+
632+
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
633+
if ccx.const_kind() != hir::ConstContext::ConstFn {
634+
Status::Allowed
635+
} else {
636+
Status::Unstable(sym::const_fn_trait_bound)
637+
}
638+
}
639+
640+
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
641+
let mut err = feature_err(
642+
&ccx.tcx.sess.parse_sess,
643+
sym::const_fn_trait_bound,
644+
span,
645+
"trait objects in const fn are unstable",
646+
);
647+
648+
match ccx.fn_sig() {
649+
Some(fn_sig) if !fn_sig.span.contains(span) => {
650+
err.span_label(fn_sig.span, "function declared as const here");
651+
}
652+
_ => {}
653+
}
654+
655+
err
617656
}
618657
}
619658

src/test/ui/consts/const_fn_trait_bound.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
const fn test1<T: std::ops::Add>() {}
99
//[stock]~^ trait bounds
1010
const fn test2(_x: &dyn Send) {}
11-
//[stock]~^ trait bounds
11+
//[stock]~^ trait objects in const fn are unstable
1212
const fn test3() -> &'static dyn Send { loop {} }
13-
//[stock]~^ trait bounds
13+
//[stock]~^ trait objects in const fn are unstable
1414

1515

1616
#[rustc_error]

src/test/ui/consts/const_fn_trait_bound.stock.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | const fn test1<T: std::ops::Add>() {}
77
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
88
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
99

10-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
10+
error[E0658]: trait objects in const fn are unstable
1111
--> $DIR/const_fn_trait_bound.rs:10:16
1212
|
1313
LL | const fn test2(_x: &dyn Send) {}
@@ -16,7 +16,7 @@ LL | const fn test2(_x: &dyn Send) {}
1616
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
1717
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
1818

19-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
19+
error[E0658]: trait objects in const fn are unstable
2020
--> $DIR/const_fn_trait_bound.rs:12:21
2121
|
2222
LL | const fn test3() -> &'static dyn Send { loop {} }

src/test/ui/consts/min_const_fn/min_const_fn.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ const fn no_apit(_x: impl std::fmt::Debug) {}
130130
//~^ ERROR trait bounds other than `Sized`
131131
//~| ERROR destructor
132132
const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {}
133-
//~^ ERROR trait bounds other than `Sized`
133+
//~^ ERROR trait objects in const fn are unstable
134134
const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
135-
//~^ ERROR trait bounds other than `Sized`
135+
//~^ ERROR trait objects in const fn are unstable
136136

137137
const fn no_unsafe() { unsafe {} }
138138

139139
const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
140-
//~^ ERROR trait bounds other than `Sized`
141-
//~| ERROR trait bounds other than `Sized`
142-
//~| ERROR trait bounds other than `Sized`
140+
//~^ ERROR trait objects in const fn are unstable
141+
//~| ERROR trait objects in const fn are unstable
142+
//~| ERROR trait objects in const fn are unstable
143143

144144
const fn no_fn_ptrs(_x: fn()) {}
145145
//~^ ERROR function pointer

src/test/ui/consts/min_const_fn/min_const_fn.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ LL | const fn no_apit(_x: impl std::fmt::Debug) {}
279279
| |
280280
| constant functions cannot evaluate destructors
281281

282-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
282+
error[E0658]: trait objects in const fn are unstable
283283
--> $DIR/min_const_fn.rs:132:23
284284
|
285285
LL | const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {}
@@ -288,7 +288,7 @@ LL | const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {}
288288
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
289289
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
290290

291-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
291+
error[E0658]: trait objects in const fn are unstable
292292
--> $DIR/min_const_fn.rs:134:32
293293
|
294294
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
@@ -297,7 +297,7 @@ LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
297297
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
298298
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
299299

300-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
300+
error[E0658]: trait objects in const fn are unstable
301301
--> $DIR/min_const_fn.rs:139:41
302302
|
303303
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
@@ -308,7 +308,7 @@ LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1
308308
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
309309
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
310310

311-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
311+
error[E0658]: trait objects in const fn are unstable
312312
--> $DIR/min_const_fn.rs:139:42
313313
|
314314
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
@@ -319,7 +319,7 @@ LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1
319319
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
320320
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
321321

322-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
322+
error[E0658]: trait objects in const fn are unstable
323323
--> $DIR/min_const_fn.rs:139:42
324324
|
325325
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }

src/test/ui/consts/min_const_fn/min_const_fn_dyn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ struct Hide(HasDyn);
77
const fn no_inner_dyn_trait(_x: Hide) {}
88
const fn no_inner_dyn_trait2(x: Hide) {
99
x.0.field;
10-
//~^ ERROR trait bounds other than `Sized`
10+
//~^ ERROR trait objects in const fn are unstable
1111
}
1212
const fn no_inner_dyn_trait_ret() -> Hide { Hide(HasDyn { field: &0 }) }
13-
//~^ ERROR trait bounds other than `Sized`
13+
//~^ ERROR trait objects in const fn are unstable
1414

1515
fn main() {}

src/test/ui/consts/min_const_fn/min_const_fn_dyn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
1+
error[E0658]: trait objects in const fn are unstable
22
--> $DIR/min_const_fn_dyn.rs:9:5
33
|
44
LL | const fn no_inner_dyn_trait2(x: Hide) {
@@ -9,7 +9,7 @@ LL | x.0.field;
99
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
1010
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
1111

12-
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
12+
error[E0658]: trait objects in const fn are unstable
1313
--> $DIR/min_const_fn_dyn.rs:12:66
1414
|
1515
LL | const fn no_inner_dyn_trait_ret() -> Hide { Hide(HasDyn { field: &0 }) }

0 commit comments

Comments
 (0)