Skip to content

Commit 6be68ae

Browse files
committed
monormophize required_itms
1 parent 3c7d61a commit 6be68ae

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<'tcx> CoroutineInfo<'tcx> {
311311
}
312312

313313
/// Some item that needs to monomorphize successfully for a MIR body to be considered well-formed.
314-
#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
314+
#[derive(Copy, Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
315315
pub enum RequiredItem<'tcx> {
316316
Fn(DefId, GenericArgsRef<'tcx>),
317317
Drop(Ty<'tcx>),

compiler/rustc_monomorphize/src/collector.rs

+30
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,17 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
734734
}
735735

736736
impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
737+
fn visit_body(&mut self, body: &mir::Body<'tcx>) {
738+
for item in &body.required_items {
739+
// All these also need monomorphization to ensure that if that leads to error, we find
740+
// those errors.
741+
let item = self.monomorphize(*item);
742+
visit_required_item(self.tcx, item, self.output);
743+
}
744+
745+
self.super_body(body);
746+
}
747+
737748
fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
738749
debug!("visiting rvalue {:?}", *rvalue);
739750

@@ -915,6 +926,25 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
915926
}
916927
}
917928

929+
fn visit_required_item<'tcx>(
930+
tcx: TyCtxt<'tcx>,
931+
item: mir::RequiredItem<'tcx>,
932+
output: &mut MonoItems<'tcx>,
933+
) {
934+
let instance = match item {
935+
mir::RequiredItem::Fn(def_id, args) => {
936+
Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args)
937+
}
938+
mir::RequiredItem::Drop(ty) => Instance::resolve_drop_in_place(tcx, ty),
939+
};
940+
// We pretend this is a direct call, just to make sure this is visited at all.
941+
// "indirect" would mean we also generate some shims, but we don't care about the
942+
// generated code, just about the side-effect of code generation causing errors, so we
943+
// can skip the shims.
944+
// FIXME: track the span so that we can show it here.
945+
visit_instance_use(tcx, instance, /*is_direct_call*/ true, DUMMY_SP, output);
946+
}
947+
918948
fn visit_drop_use<'tcx>(
919949
tcx: TyCtxt<'tcx>,
920950
ty: Ty<'tcx>,

tests/ui/consts/monomorphization/dead-code-in-called-fn.no-opt.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ LL | const C: () = panic!();
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
note: the above error was encountered while instantiating `fn called::<i32>`
10-
--> $DIR/dead-code-in-called-fn.rs:22:5
11-
|
12-
LL | called::<i32>();
13-
| ^^^^^^^^^^^^^^^
1410

1511
error: aborting due to 1 previous error
1612

tests/ui/consts/monomorphization/dead-code-in-called-fn.opt.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ LL | const C: () = panic!();
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
note: the above error was encountered while instantiating `fn called::<i32>`
10-
--> $DIR/dead-code-in-called-fn.rs:22:5
11-
|
12-
LL | called::<i32>();
13-
| ^^^^^^^^^^^^^^^
1410

1511
error: aborting due to 1 previous error
1612

tests/ui/consts/monomorphization/dead-code-in-dead-fn.no-opt.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ LL | const C: () = panic!();
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
note: the above error was encountered while instantiating `fn not_called::<i32>`
10-
--> $DIR/dead-code-in-dead-fn.rs:22:9
11-
|
12-
LL | not_called::<T>();
13-
| ^^^^^^^^^^^^^^^^^
1410

1511
error: aborting due to 1 previous error
1612

tests/ui/consts/monomorphization/dead-code-in-dead-fn.opt.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | const C: () = panic!();
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
note: the above error was encountered while instantiating `fn not_called::<i32>`
10+
911
error: aborting due to 1 previous error
1012

1113
For more information about this error, try `rustc --explain E0080`.

tests/ui/generics/post_monomorphization_error_backtrace.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
77
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
note: the above error was encountered while instantiating `fn assert_zst::<u32>`
10-
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
11-
|
12-
LL | assert_zst::<U>()
13-
| ^^^^^^^^^^^^^^^^^
1410

1511
error[E0080]: evaluation of `assert_zst::F::<i32>::V` failed
1612
--> $DIR/post_monomorphization_error_backtrace.rs:6:23
@@ -21,10 +17,6 @@ LL | const V: () = assert!(std::mem::size_of::<T>() == 0);
2117
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
2218

2319
note: the above error was encountered while instantiating `fn assert_zst::<i32>`
24-
--> $DIR/post_monomorphization_error_backtrace.rs:18:5
25-
|
26-
LL | assert_zst::<U>()
27-
| ^^^^^^^^^^^^^^^^^
2820

2921
error: aborting due to 2 previous errors
3022

0 commit comments

Comments
 (0)