diff options
| author | Michael Goulet <michael@errs.io> | 2023-01-18 18:00:29 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-18 18:00:29 -0500 |
| commit | a637e2a950d3274755233efbace05f837a4c5d17 (patch) | |
| tree | 5f23000d57a4f98020b64a677489fa91b41328d0 | |
| parent | 685c77305c4109508b52d6642a7d11b9005a0abb (diff) | |
| parent | 3a4fdcf86cea75796ed947853ea9544f2d11afff (diff) | |
| download | rust-a637e2a950d3274755233efbace05f837a4c5d17.tar.gz rust-a637e2a950d3274755233efbace05f837a4c5d17.zip | |
Rollup merge of #106917 - compiler-errors:const-closure-foreign, r=tmiasko
Encode const mir for closures if they're const Fixes #106913
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 13 | ||||
| -rw-r--r-- | tests/ui/consts/auxiliary/closure-in-foreign-crate.rs | 8 | ||||
| -rw-r--r-- | tests/ui/consts/closure-in-foreign-crate.rs | 8 |
3 files changed, 18 insertions, 11 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 58b91e7b4f8..a3d44fa890d 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -888,8 +888,8 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) { | DefKind::AssocConst | DefKind::Static(..) | DefKind::Const => (true, false), - // Full-fledged functions - DefKind::AssocFn | DefKind::Fn => { + // Full-fledged functions + closures + DefKind::AssocFn | DefKind::Fn | DefKind::Closure => { let generics = tcx.generics_of(def_id); let needs_inline = (generics.requires_monomorphization(tcx) || tcx.codegen_fn_attrs(def_id).requests_inline()) @@ -900,15 +900,6 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) { let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir; (is_const_fn, needs_inline || always_encode_mir) } - // Closures can't be const fn. - DefKind::Closure => { - let generics = tcx.generics_of(def_id); - let needs_inline = (generics.requires_monomorphization(tcx) - || tcx.codegen_fn_attrs(def_id).requests_inline()) - && tcx.sess.opts.output_types.should_codegen(); - let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir; - (false, needs_inline || always_encode_mir) - } // Generators require optimized MIR to compute layout. DefKind::Generator => (false, true), // The others don't have MIR. diff --git a/tests/ui/consts/auxiliary/closure-in-foreign-crate.rs b/tests/ui/consts/auxiliary/closure-in-foreign-crate.rs new file mode 100644 index 00000000000..edc7fa81abb --- /dev/null +++ b/tests/ui/consts/auxiliary/closure-in-foreign-crate.rs @@ -0,0 +1,8 @@ +#![crate_type = "lib"] +#![feature(const_closures, const_trait_impl)] +#![allow(incomplete_features)] + +pub const fn test() { + let cl = const || {}; + cl(); +} diff --git a/tests/ui/consts/closure-in-foreign-crate.rs b/tests/ui/consts/closure-in-foreign-crate.rs new file mode 100644 index 00000000000..fc8f480e706 --- /dev/null +++ b/tests/ui/consts/closure-in-foreign-crate.rs @@ -0,0 +1,8 @@ +// aux-build:closure-in-foreign-crate.rs +// build-pass + +extern crate closure_in_foreign_crate; + +const _: () = closure_in_foreign_crate::test(); + +fn main() {} |
