Skip to content

Commit adac3bc

Browse files
committed
Expand to other const DefKinds and fix pretty-printing
1 parent 978fe3c commit adac3bc

File tree

5 files changed

+41
-55
lines changed

5 files changed

+41
-55
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,15 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
10881088

10891089
sess.time("MIR_borrow_checking", || {
10901090
tcx.par_hir_body_owners(|def_id| {
1091+
let not_typeck_child = !tcx.is_typeck_child(def_id.to_def_id());
1092+
if not_typeck_child {
1093+
// Child unsafety and borrowck happens together with the parent
1094+
tcx.ensure_ok().check_unsafety(def_id);
1095+
}
10911096
if tcx.is_trivial_const(def_id) {
10921097
return;
10931098
}
1094-
if !tcx.is_typeck_child(def_id.to_def_id()) {
1095-
// Child unsafety and borrowck happens together with the parent
1096-
tcx.ensure_ok().check_unsafety(def_id);
1099+
if not_typeck_child {
10971100
tcx.ensure_ok().mir_borrowck(def_id);
10981101
tcx.ensure_ok().check_transmutes(def_id);
10991102
}

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,13 @@ pub fn write_mir_pretty<'tcx>(
353353
// are shared between mir_for_ctfe and optimized_mir
354354
writer.write_mir_fn(tcx.mir_for_ctfe(def_id), w)?;
355355
} else {
356-
if !tcx.is_trivial_const(def_id) {
356+
if let Some((val, ty)) = tcx.trivial_const(def_id) {
357+
ty::print::with_forced_impl_filename_line! {
358+
// see notes on #41697 elsewhere
359+
write!(w, "const {}", tcx.def_path_str(def_id))?
360+
}
361+
writeln!(w, ": {} = const {};", ty, Const::Val(val, ty))?;
362+
} else {
357363
let instance_mir = tcx.instance_mir(ty::InstanceKind::Item(def_id));
358364
render_body(w, instance_mir)?;
359365
}

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,7 @@ pub fn provide(providers: &mut Providers) {
226226
promoted_mir,
227227
deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
228228
coroutine_by_move_body_def_id: coroutine::coroutine_by_move_body_def_id,
229-
trivial_const: |tcx, def| {
230-
if tcx.def_kind(def) != DefKind::Const {
231-
return None;
232-
}
233-
trivial_const(&tcx.mir_built(def).borrow())
234-
},
229+
trivial_const: trivial_const_provider,
235230
..providers.queries
236231
};
237232
}
@@ -382,6 +377,20 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
382377
validator.qualifs_in_return_place()
383378
}
384379

380+
fn trivial_const_provider<'tcx>(
381+
tcx: TyCtxt<'tcx>,
382+
def: LocalDefId,
383+
) -> Option<(ConstValue, Ty<'tcx>)> {
384+
match tcx.def_kind(def) {
385+
DefKind::AssocConst
386+
| DefKind::Const
387+
//| DefKind::Static { .. }
388+
| DefKind::InlineConst
389+
| DefKind::AnonConst => trivial_const(&tcx.mir_built(def).borrow()),
390+
_ => None,
391+
}
392+
}
393+
385394
fn trivial_const<'tcx>(body: &Body<'tcx>) -> Option<(ConstValue, Ty<'tcx>)> {
386395
if body.has_opaque_types() {
387396
return None;
@@ -419,7 +428,15 @@ fn trivial_const<'tcx>(body: &Body<'tcx>) -> Option<(ConstValue, Ty<'tcx>)> {
419428

420429
fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
421430
let mut body = build_mir(tcx, def);
422-
if tcx.def_kind(def) == DefKind::Const && trivial_const(&body).is_some() {
431+
if matches!(
432+
tcx.def_kind(def),
433+
DefKind::AssocConst
434+
| DefKind::Const
435+
//| DefKind::Static { .. }
436+
| DefKind::InlineConst
437+
| DefKind::AnonConst
438+
) && trivial_const(&body).is_some()
439+
{
423440
return tcx.alloc_steal_mir(body);
424441
}
425442

tests/incremental/issue-54242.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Tr for str {
1414
type Arr = [u8; 8];
1515
#[cfg(cfail)]
1616
type Arr = [u8; Self::C];
17-
//[cfail]~^ ERROR cycle detected when caching mir
17+
//[cfail]~^ ERROR cycle detected when
1818
}
1919

2020
fn main() {}

tests/ui/traits/const-traits/unsatisfied-const-trait-bound.stderr

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,7 @@ note: ...which requires const-evaluating + checking `accept0::{constant#0}`...
1717
|
1818
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
1919
| ^^^^^^^^^^^^^
20-
note: ...which requires caching mir of `accept0::{constant#0}` for CTFE...
21-
--> $DIR/unsatisfied-const-trait-bound.rs:29:35
22-
|
23-
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
24-
| ^^^^^^^^^^^^^
25-
note: ...which requires elaborating drops for `accept0::{constant#0}`...
26-
--> $DIR/unsatisfied-const-trait-bound.rs:29:35
27-
|
28-
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
29-
| ^^^^^^^^^^^^^
30-
note: ...which requires borrow-checking `accept0::{constant#0}`...
31-
--> $DIR/unsatisfied-const-trait-bound.rs:29:35
32-
|
33-
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
34-
| ^^^^^^^^^^^^^
35-
note: ...which requires promoting constants in MIR for `accept0::{constant#0}`...
36-
--> $DIR/unsatisfied-const-trait-bound.rs:29:35
37-
|
38-
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
39-
| ^^^^^^^^^^^^^
40-
note: ...which requires const checking `accept0::{constant#0}`...
20+
note: ...which requires checking if `accept0::{constant#0}` is a trivial const...
4121
--> $DIR/unsatisfied-const-trait-bound.rs:29:35
4222
|
4323
LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
@@ -70,32 +50,12 @@ LL | fn accept0<T: Trait>(_: Container<{ T::make() }>) {}
7050
| ^^^^^^^^^^^^^
7151
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
7252

73-
error[E0391]: cycle detected when caching mir of `accept1::{constant#0}` for CTFE
53+
error[E0391]: cycle detected when checking if `accept1::{constant#0}` is a trivial const
7454
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
7555
|
7656
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
7757
| ^^^^^^^^^^^^^
7858
|
79-
note: ...which requires elaborating drops for `accept1::{constant#0}`...
80-
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
81-
|
82-
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
83-
| ^^^^^^^^^^^^^
84-
note: ...which requires borrow-checking `accept1::{constant#0}`...
85-
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
86-
|
87-
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
88-
| ^^^^^^^^^^^^^
89-
note: ...which requires promoting constants in MIR for `accept1::{constant#0}`...
90-
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
91-
|
92-
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
93-
| ^^^^^^^^^^^^^
94-
note: ...which requires const checking `accept1::{constant#0}`...
95-
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
96-
|
97-
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
98-
| ^^^^^^^^^^^^^
9959
note: ...which requires building MIR for `accept1::{constant#0}`...
10060
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
10161
|
@@ -126,7 +86,7 @@ note: ...which requires const-evaluating + checking `accept1::{constant#0}`...
12686
|
12787
LL | const fn accept1<T: [const] Trait>(_: Container<{ T::make() }>) {}
12888
| ^^^^^^^^^^^^^
129-
= note: ...which again requires caching mir of `accept1::{constant#0}` for CTFE, completing the cycle
89+
= note: ...which again requires checking if `accept1::{constant#0}` is a trivial const, completing the cycle
13090
note: cycle used when const-evaluating + checking `accept1::{constant#0}`
13191
--> $DIR/unsatisfied-const-trait-bound.rs:33:49
13292
|

0 commit comments

Comments
 (0)