Skip to content

Commit ddce93d

Browse files
committed
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 evaluated. This change fixes that.
1 parent b76036c commit ddce93d

File tree

8 files changed

+17
-27
lines changed

8 files changed

+17
-27
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::atomic::Ordering::Relaxed;
22

33
use either::{Left, Right};
44
use rustc_abi::{self as abi, BackendRepr};
5-
use rustc_hir::def::DefKind;
5+
use rustc_hir::def::{CtorKind, DefKind};
66
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo, ReportedErrorInfo};
77
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
88
use rustc_middle::query::TyCtxtAt;
@@ -41,6 +41,7 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>(
4141
| DefKind::AnonConst
4242
| DefKind::InlineConst
4343
| DefKind::AssocConst
44+
| DefKind::Ctor(_, CtorKind::Const)
4445
),
4546
"Unexpected DefKind: {:?}",
4647
ecx.tcx.def_kind(cid.instance.def_id())

compiler/rustc_mir_transform/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_const_eval::util;
2121
use rustc_data_structures::fx::FxIndexSet;
2222
use rustc_data_structures::steal::Steal;
2323
use rustc_hir as hir;
24-
use rustc_hir::def::{CtorKind, DefKind};
24+
use rustc_hir::def::DefKind;
2525
use rustc_hir::def_id::LocalDefId;
2626
use rustc_index::IndexVec;
2727
use rustc_middle::mir::{
@@ -324,7 +324,7 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
324324
for item in tcx.hir_crate_items(()).free_items() {
325325
if let DefKind::Struct | DefKind::Enum = tcx.def_kind(item.owner_id) {
326326
for variant in tcx.adt_def(item.owner_id).variants() {
327-
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
327+
if let Some((_, ctor_def_id)) = variant.ctor {
328328
set.insert(ctor_def_id.expect_local());
329329
}
330330
}

tests/crashes/132985.rs

-17
This file was deleted.

tests/crashes/auxiliary/aux132985.rs

-6
This file was deleted.

tests/ui-fulldeps/stable-mir/check_item_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const CRATE_NAME: &str = "input";
2727
/// This function uses the Stable MIR APIs to get information about the test crate.
2828
fn test_item_kind() -> ControlFlow<()> {
2929
let items = stable_mir::all_local_items();
30-
assert_eq!(items.len(), 4);
30+
assert_eq!(items.len(), 5);
3131
// Constructor item.
3232
for item in items {
3333
let expected_kind = match item.name().as_str() {

tests/ui/const-generics/auxiliary/xcrate-const-ctor-a.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// NOTE: This aux file inherits revisions from its parent tests.
2+
13
#![feature(adt_const_params)]
4+
#![cfg_attr(mgca, feature(min_generic_const_args), allow(incomplete_features))]
25

36
use std::marker::ConstParamTy;
47

tests/ui/const-generics/xcrate-const-ctor-b.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//@ check-pass
2+
//@ revisions: normal mgca
23
//@ aux-build:xcrate-const-ctor-a.rs
34

45
#![feature(adt_const_params)]
6+
#![cfg_attr(mgca, feature(min_generic_const_args), allow(incomplete_features))]
57

68
extern crate xcrate_const_ctor_a;
79
use xcrate_const_ctor_a::Foo;

tests/ui/stable-mir-print/operands.stdout

+7
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ fn closures::{closure#0}(_1: {closure@$DIR/operands.rs:47:5: 47:19}, _2: bool) -
250250
return;
251251
}
252252
}
253+
fn Ctors::Unit() -> Ctors {
254+
let mut _0: Ctors;
255+
bb0: {
256+
_0 = Ctors::Unit;
257+
return;
258+
}
259+
}
253260
fn Ctors::TupLike(_1: bool) -> Ctors {
254261
let mut _0: Ctors;
255262
bb0: {

0 commit comments

Comments
 (0)