This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +12
-3
lines changed
compiler/rustc_mir/src/transform/check_consts Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -151,14 +151,15 @@ impl NonConstOp for FnPtrCast {
151
151
}
152
152
153
153
#[ derive( Debug ) ]
154
- pub struct Generator ;
154
+ pub struct Generator ( pub hir :: GeneratorKind ) ;
155
155
impl NonConstOp for Generator {
156
156
fn status_in_item ( & self , _: & ConstCx < ' _ , ' _ > ) -> Status {
157
157
Status :: Forbidden
158
158
}
159
159
160
160
fn build_error ( & self , ccx : & ConstCx < ' _ , ' tcx > , span : Span ) -> DiagnosticBuilder < ' tcx > {
161
- ccx. tcx . sess . struct_span_err ( span, "Generators and `async` functions cannot be `const`" )
161
+ let msg = format ! ( "{}s are not allowed in {}s" , self . 0 , ccx. const_kind( ) ) ;
162
+ ccx. tcx . sess . struct_span_err ( span, & msg)
162
163
}
163
164
}
164
165
Original file line number Diff line number Diff line change @@ -770,6 +770,14 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
770
770
return ;
771
771
}
772
772
773
+ // `async` blocks get lowered to `std::future::from_generator(/* a closure */)`.
774
+ let is_async_block = Some ( callee) == tcx. lang_items ( ) . from_generator_fn ( ) ;
775
+ if is_async_block {
776
+ let kind = hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Block ) ;
777
+ self . check_op ( ops:: Generator ( kind) ) ;
778
+ return ;
779
+ }
780
+
773
781
// HACK: This is to "unstabilize" the `transmute` intrinsic
774
782
// within const fns. `transmute` is allowed in all other const contexts.
775
783
// This won't really scale to more intrinsics or functions. Let's allow const
@@ -869,7 +877,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
869
877
TerminatorKind :: Abort => self . check_op ( ops:: Abort ) ,
870
878
871
879
TerminatorKind :: GeneratorDrop | TerminatorKind :: Yield { .. } => {
872
- self . check_op ( ops:: Generator )
880
+ self . check_op ( ops:: Generator ( hir :: GeneratorKind :: Gen ) )
873
881
}
874
882
875
883
TerminatorKind :: Assert { .. }
You can’t perform that action at this time.
0 commit comments