From e1f2703c60ada60d72b4123f6117ced05f1165fa Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Sat, 28 Dec 2024 19:39:36 -0800 Subject: [PATCH] Encode MIR for const ctors in metadata and allow eval for them This is needed for them to be usable under `min_generic_const_args` (mgca). Whenever a const ctor appears as an expression, like in `let x = Foo;`, it is directly lowered to a construction of an aggregate value in MIR, instead of using the const generated for it. The anon const approach for generic const exprs works similarly, by creating an anon const with the aggregate as its body. However, with mgca, we want to avoid anon consts, so we need to use the const item. They were not previously encoded in the metadata MIR, so they could not be used cross-crate, and they were not able to be evaluated. This change fixes that. --- .../src/const_eval/eval_queries.rs | 3 ++- compiler/rustc_mir_transform/src/lib.rs | 4 ++-- tests/crashes/132985.rs | 17 ----------------- tests/crashes/auxiliary/aux132985.rs | 6 ------ tests/ui-fulldeps/stable-mir/check_item_kind.rs | 2 +- .../auxiliary/xcrate-const-ctor-a.rs | 3 +++ tests/ui/const-generics/xcrate-const-ctor-b.rs | 2 ++ tests/ui/stable-mir-print/operands.stdout | 7 +++++++ 8 files changed, 17 insertions(+), 27 deletions(-) delete mode 100644 tests/crashes/132985.rs delete mode 100644 tests/crashes/auxiliary/aux132985.rs diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index ce8eceebdf8d2..266ac3f376b76 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -2,7 +2,7 @@ use std::sync::atomic::Ordering::Relaxed; use either::{Left, Right}; use rustc_abi::{self as abi, BackendRepr}; -use rustc_hir::def::DefKind; +use rustc_hir::def::{CtorKind, DefKind}; use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo, ReportedErrorInfo}; use rustc_middle::mir::{self, ConstAlloc, ConstValue}; use rustc_middle::query::TyCtxtAt; @@ -41,6 +41,7 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>( | DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst + | DefKind::Ctor(_, CtorKind::Const) ), "Unexpected DefKind: {:?}", ecx.tcx.def_kind(cid.instance.def_id()) diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index e1fba9be5bb6d..b7c7448491152 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -21,7 +21,7 @@ use rustc_const_eval::util; use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::steal::Steal; use rustc_hir as hir; -use rustc_hir::def::{CtorKind, DefKind}; +use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_index::IndexVec; use rustc_middle::mir::{ @@ -324,7 +324,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet { for item in tcx.hir_crate_items(()).free_items() { if let DefKind::Struct | DefKind::Enum = tcx.def_kind(item.owner_id) { for variant in tcx.adt_def(item.owner_id).variants() { - if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { + if let Some((_, ctor_def_id)) = variant.ctor { set.insert(ctor_def_id.expect_local()); } } diff --git a/tests/crashes/132985.rs b/tests/crashes/132985.rs deleted file mode 100644 index 2735074f44d12..0000000000000 --- a/tests/crashes/132985.rs +++ /dev/null @@ -1,17 +0,0 @@ -//@ known-bug: #132985 -//@ aux-build:aux132985.rs - -#![allow(incomplete_features)] -#![feature(min_generic_const_args)] -#![feature(adt_const_params)] - -extern crate aux132985; -use aux132985::Foo; - -fn bar() {} - -fn baz() { - bar::<{ Foo }>(); -} - -fn main() {} diff --git a/tests/crashes/auxiliary/aux132985.rs b/tests/crashes/auxiliary/aux132985.rs deleted file mode 100644 index 7ae5567bdc59d..0000000000000 --- a/tests/crashes/auxiliary/aux132985.rs +++ /dev/null @@ -1,6 +0,0 @@ -#![feature(adt_const_params)] - -use std::marker::ConstParamTy; - -#[derive(Eq, PartialEq, ConstParamTy)] -pub struct Foo; diff --git a/tests/ui-fulldeps/stable-mir/check_item_kind.rs b/tests/ui-fulldeps/stable-mir/check_item_kind.rs index 23b54e6c60b39..035d63593a53f 100644 --- a/tests/ui-fulldeps/stable-mir/check_item_kind.rs +++ b/tests/ui-fulldeps/stable-mir/check_item_kind.rs @@ -27,7 +27,7 @@ const CRATE_NAME: &str = "input"; /// This function uses the Stable MIR APIs to get information about the test crate. fn test_item_kind() -> ControlFlow<()> { let items = stable_mir::all_local_items(); - assert_eq!(items.len(), 4); + assert_eq!(items.len(), 5); // Constructor item. for item in items { let expected_kind = match item.name().as_str() { diff --git a/tests/ui/const-generics/auxiliary/xcrate-const-ctor-a.rs b/tests/ui/const-generics/auxiliary/xcrate-const-ctor-a.rs index 7ae5567bdc59d..3056611f5a979 100644 --- a/tests/ui/const-generics/auxiliary/xcrate-const-ctor-a.rs +++ b/tests/ui/const-generics/auxiliary/xcrate-const-ctor-a.rs @@ -1,4 +1,7 @@ +// NOTE: This aux file inherits revisions from its parent tests. + #![feature(adt_const_params)] +#![cfg_attr(mgca, feature(min_generic_const_args), allow(incomplete_features))] use std::marker::ConstParamTy; diff --git a/tests/ui/const-generics/xcrate-const-ctor-b.rs b/tests/ui/const-generics/xcrate-const-ctor-b.rs index dce2e43b316a4..bf33ef6340d8a 100644 --- a/tests/ui/const-generics/xcrate-const-ctor-b.rs +++ b/tests/ui/const-generics/xcrate-const-ctor-b.rs @@ -1,7 +1,9 @@ //@ check-pass +//@ revisions: normal mgca //@ aux-build:xcrate-const-ctor-a.rs #![feature(adt_const_params)] +#![cfg_attr(mgca, feature(min_generic_const_args), allow(incomplete_features))] extern crate xcrate_const_ctor_a; use xcrate_const_ctor_a::Foo; diff --git a/tests/ui/stable-mir-print/operands.stdout b/tests/ui/stable-mir-print/operands.stdout index c3b1151ae24a3..b3b3c0de76d73 100644 --- a/tests/ui/stable-mir-print/operands.stdout +++ b/tests/ui/stable-mir-print/operands.stdout @@ -250,6 +250,13 @@ fn closures::{closure#0}(_1: {closure@$DIR/operands.rs:47:5: 47:19}, _2: bool) - return; } } +fn Ctors::Unit() -> Ctors { + let mut _0: Ctors; + bb0: { + _0 = Ctors::Unit; + return; + } +} fn Ctors::TupLike(_1: bool) -> Ctors { let mut _0: Ctors; bb0: {