Skip to content

Commit 7914284

Browse files
committed
Fix
1 parent 68b2e40 commit 7914284

File tree

11 files changed

+96
-88
lines changed

11 files changed

+96
-88
lines changed

compiler/rustc_mir_transform/src/mono_checks/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,12 @@ pub(super) fn check_mono_item<'tcx>(
1919
}
2020

2121
pub(super) fn provide(providers: &mut Providers) {
22-
*providers = Providers { skip_move_check_fns: move_check::skip_move_check_fns, ..*providers }
22+
*providers = Providers {
23+
skip_move_check_fns: move_check::skip_move_check_fns,
24+
check_mono_item: |tcx, instance| {
25+
let body = tcx.codegen_mir(instance);
26+
check_mono_item(tcx, instance, body)
27+
},
28+
..*providers
29+
}
2330
}

compiler/rustc_monomorphize/messages.ftl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,4 @@ monomorphize_start_not_found = using `fn main` requires the standard library
1717
1818
monomorphize_symbol_already_defined = symbol `{$symbol}` is already defined
1919
20-
monomorphize_wasm_c_abi_transition =
21-
this function {$is_call ->
22-
[true] call
23-
*[false] definition
24-
} involves an argument of type `{$ty}` which is affected by the wasm ABI transition
25-
.help = the "C" ABI Rust uses on wasm32-unknown-unknown will change to align with the standard "C" ABI for this target
26-
2720
monomorphize_written_to_path = the full type name has been written to '{$path}'

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
220220
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, GlobalAlloc, Scalar};
221221
use rustc_middle::mir::mono::{CollectionMode, InstantiationMode, MonoItem};
222222
use rustc_middle::mir::visit::Visitor as MirVisitor;
223-
use rustc_middle::mir::{self, Location, MentionedItem};
223+
use rustc_middle::mir::{self, Location, MentionedItem, traversal};
224224
use rustc_middle::query::TyCtxtAt;
225225
use rustc_middle::ty::adjustment::{CustomCoerceUnsized, PointerCoercion};
226226
use rustc_middle::ty::layout::ValidityRequirement;
227227
use rustc_middle::ty::print::{shrunk_instance_name, with_no_trimmed_paths};
228228
use rustc_middle::ty::{
229-
self, GenericArgs, GenericParamDefKind, Instance, InstanceKind, Ty, TyCtxt, TypeVisitableExt,
230-
VtblEntry,
229+
self, GenericArgs, GenericParamDefKind, Instance, InstanceKind, Ty, TyCtxt, TypeFoldable,
230+
TypeVisitableExt, VtblEntry,
231231
};
232232
use rustc_middle::util::Providers;
233233
use rustc_middle::{bug, span_bug};
@@ -642,24 +642,38 @@ struct MirUsedCollector<'a, 'tcx> {
642642
/// See the comment in `collect_items_of_instance` for the purpose of this set.
643643
/// Note that this contains *not-monomorphized* items!
644644
used_mentioned_items: &'a mut UnordSet<MentionedItem<'tcx>>,
645+
instance: Instance<'tcx>,
645646
}
646647

647648
impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
648-
/// Evaluates a monomorphized constant.
649+
fn monomorphize<T>(&self, value: T) -> T
650+
where
651+
T: TypeFoldable<TyCtxt<'tcx>>,
652+
{
653+
trace!("monomorphize: self.instance={:?}", self.instance);
654+
self.instance.instantiate_mir_and_normalize_erasing_regions(
655+
self.tcx,
656+
ty::TypingEnv::fully_monomorphized(),
657+
ty::EarlyBinder::bind(value),
658+
)
659+
}
660+
661+
/// Evaluates a *not yet monomorphized* constant.
649662
fn eval_constant(
650663
&mut self,
651664
constant: &mir::ConstOperand<'tcx>,
652665
) -> Option<mir::ConstValue<'tcx>> {
666+
let const_ = self.monomorphize(constant.const_);
653667
// Evaluate the constant. This makes const eval failure a collection-time error (rather than
654668
// a codegen-time error). rustc stops after collection if there was an error, so this
655669
// ensures codegen never has to worry about failing consts.
656670
// (codegen relies on this and ICEs will happen if this is violated.)
657-
match constant.const_.eval(self.tcx, ty::TypingEnv::fully_monomorphized(), constant.span) {
671+
match const_.eval(self.tcx, ty::TypingEnv::fully_monomorphized(), constant.span) {
658672
Ok(v) => Some(v),
659673
Err(ErrorHandled::TooGeneric(..)) => span_bug!(
660674
constant.span,
661675
"collection encountered polymorphic constant: {:?}",
662-
constant.const_
676+
const_
663677
),
664678
Err(err @ ErrorHandled::Reported(..)) => {
665679
err.emit_note(self.tcx);
@@ -689,6 +703,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
689703
// *Before* monomorphizing, record that we already handled this mention.
690704
self.used_mentioned_items
691705
.insert(MentionedItem::UnsizeCast { source_ty, target_ty });
706+
let target_ty = self.monomorphize(target_ty);
707+
let source_ty = self.monomorphize(source_ty);
692708
let (source_ty, target_ty) =
693709
find_tails_for_unsizing(self.tcx.at(span), source_ty, target_ty);
694710
// This could also be a different Unsize instruction, like
@@ -714,6 +730,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
714730
let fn_ty = operand.ty(self.body, self.tcx);
715731
// *Before* monomorphizing, record that we already handled this mention.
716732
self.used_mentioned_items.insert(MentionedItem::Fn(fn_ty));
733+
let fn_ty = self.monomorphize(fn_ty);
717734
visit_fn_use(self.tcx, fn_ty, false, span, self.used_items);
718735
}
719736
mir::Rvalue::Cast(
@@ -724,6 +741,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
724741
let source_ty = operand.ty(self.body, self.tcx);
725742
// *Before* monomorphizing, record that we already handled this mention.
726743
self.used_mentioned_items.insert(MentionedItem::Closure(source_ty));
744+
let source_ty = self.monomorphize(source_ty);
727745
if let ty::Closure(def_id, args) = *source_ty.kind() {
728746
let instance =
729747
Instance::resolve_closure(self.tcx, def_id, args, ty::ClosureKind::FnOnce);
@@ -775,12 +793,14 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
775793
let callee_ty = func.ty(self.body, tcx);
776794
// *Before* monomorphizing, record that we already handled this mention.
777795
self.used_mentioned_items.insert(MentionedItem::Fn(callee_ty));
796+
let callee_ty = self.monomorphize(callee_ty);
778797
visit_fn_use(self.tcx, callee_ty, true, source, &mut self.used_items)
779798
}
780799
mir::TerminatorKind::Drop { ref place, .. } => {
781800
let ty = place.ty(self.body, self.tcx).ty;
782801
// *Before* monomorphizing, record that we already handled this mention.
783802
self.used_mentioned_items.insert(MentionedItem::Drop(ty));
803+
let ty = self.monomorphize(ty);
784804
visit_drop_use(self.tcx, ty, true, source, self.used_items);
785805
}
786806
mir::TerminatorKind::InlineAsm { ref operands, .. } => {
@@ -790,6 +810,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
790810
let fn_ty = value.const_.ty();
791811
// *Before* monomorphizing, record that we already handled this mention.
792812
self.used_mentioned_items.insert(MentionedItem::Fn(fn_ty));
813+
let fn_ty = self.monomorphize(fn_ty);
793814
visit_fn_use(self.tcx, fn_ty, false, source, self.used_items);
794815
}
795816
mir::InlineAsmOperand::SymStatic { def_id } => {
@@ -1210,7 +1231,10 @@ fn collect_items_of_instance<'tcx>(
12101231
instance: Instance<'tcx>,
12111232
mode: CollectionMode,
12121233
) -> (MonoItems<'tcx>, MonoItems<'tcx>) {
1213-
let body = tcx.codegen_mir(instance);
1234+
// This item is getting monomorphized, do mono-time checks.
1235+
//tcx.ensure_ok().check_mono_item(instance);
1236+
1237+
let body = tcx.instance_mir(instance.def);
12141238
// Naively, in "used" collection mode, all functions get added to *both* `used_items` and
12151239
// `mentioned_items`. Mentioned items processing will then notice that they have already been
12161240
// visited, but at that point each mentioned item has been monomorphized, added to the
@@ -1229,6 +1253,7 @@ fn collect_items_of_instance<'tcx>(
12291253
body,
12301254
used_items: &mut used_items,
12311255
used_mentioned_items: &mut used_mentioned_items,
1256+
instance,
12321257
};
12331258

12341259
if mode == CollectionMode::UsedItems {
@@ -1237,7 +1262,7 @@ fn collect_items_of_instance<'tcx>(
12371262
collector.visit_var_debug_info(var_debug_info);
12381263
}
12391264
}
1240-
for (bb, data) in body.basic_blocks.iter_enumerated() {
1265+
for (bb, data) in traversal::reverse_postorder(body) {
12411266
collector.visit_basic_block_data(bb, data)
12421267
}
12431268
}
@@ -1254,7 +1279,8 @@ fn collect_items_of_instance<'tcx>(
12541279
// `used_items` above.
12551280
for item in body.mentioned_items() {
12561281
if !collector.used_mentioned_items.contains(&item.node) {
1257-
visit_mentioned_item(tcx, &item.node, item.span, &mut mentioned_items);
1282+
let item_mono = collector.monomorphize(item.node);
1283+
visit_mentioned_item(tcx, &item_mono, item.span, &mut mentioned_items);
12581284
}
12591285
}
12601286

tests/ui/abi/simd-abi-checks-avx.stderr

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
error: this function definition uses SIMD vector type `Wrapper` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
2+
--> $DIR/simd-abi-checks-avx.rs:14:1
3+
|
4+
LL | unsafe extern "C" fn w(_: Wrapper) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
6+
|
7+
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
8+
9+
error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
10+
--> $DIR/simd-abi-checks-avx.rs:19:1
11+
|
12+
LL | unsafe extern "C" fn f(_: __m256) {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
14+
|
15+
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
16+
17+
error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
18+
--> $DIR/simd-abi-checks-avx.rs:24:1
19+
|
20+
LL | unsafe extern "C" fn g() -> __m256 {
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
22+
|
23+
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
24+
25+
error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
26+
--> $DIR/simd-abi-checks-avx.rs:53:8
27+
|
28+
LL | || g()
29+
| ^^^ function called here
30+
|
31+
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
32+
133
error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
234
--> $DIR/simd-abi-checks-avx.rs:59:11
335
|
@@ -54,43 +86,5 @@ LL | some_extern();
5486
|
5587
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
5688

57-
error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
58-
--> $DIR/simd-abi-checks-avx.rs:24:1
59-
|
60-
LL | unsafe extern "C" fn g() -> __m256 {
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
62-
|
63-
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
64-
65-
error: this function definition uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
66-
--> $DIR/simd-abi-checks-avx.rs:19:1
67-
|
68-
LL | unsafe extern "C" fn f(_: __m256) {
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
70-
|
71-
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
72-
73-
error: this function definition uses SIMD vector type `Wrapper` which (with the chosen ABI) requires the `avx` target feature, which is not enabled
74-
--> $DIR/simd-abi-checks-avx.rs:14:1
75-
|
76-
LL | unsafe extern "C" fn w(_: Wrapper) {
77-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
78-
|
79-
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
80-
81-
error: this function call uses SIMD vector type `std::arch::x86_64::__m256` which (with the chosen ABI) requires the `avx` target feature, which is not enabled in the caller
82-
--> $DIR/simd-abi-checks-avx.rs:53:8
83-
|
84-
LL | || g()
85-
| ^^^ function called here
86-
|
87-
= help: consider enabling it globally (`-C target-feature=+avx`) or locally (`#[target_feature(enable="avx")]`)
88-
89-
note: the above error was encountered while instantiating `fn in_closure::{closure#0}`
90-
--> $DIR/simd-abi-checks-avx.rs:81:9
91-
|
92-
LL | in_closure()();
93-
| ^^^^^^^^^^^^^^
94-
9589
error: aborting due to 11 previous errors
9690

tests/ui/abi/simd-abi-checks-s390x.z13_no_vector.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
warning: unstable feature specified for `-Ctarget-feature`: `vector`
2+
|
3+
= note: this feature is not stably supported; its behavior can change in the future
4+
15
error: this function definition uses SIMD vector type `i8x8` which (with the chosen ABI) requires the `vector` target feature, which is not enabled
26
--> $DIR/simd-abi-checks-s390x.rs:38:1
37
|
@@ -82,5 +86,5 @@ LL | extern "C" fn vector_transparent_wrapper_arg(x: TransparentWrapper<i8x16>)
8286
|
8387
= help: consider enabling it globally (`-C target-feature=+vector`) or locally (`#[target_feature(enable="vector")]`)
8488

85-
error: aborting due to 10 previous errors
89+
error: aborting due to 10 previous errors; 1 warning emitted
8690

tests/ui/abi/simd-abi-checks-s390x.z13_soft_float.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
warning: unstable feature specified for `-Ctarget-feature`: `vector`
2+
|
3+
= note: this feature is not stably supported; its behavior can change in the future
4+
5+
warning: unknown and unstable feature specified for `-Ctarget-feature`: `soft-float`
6+
|
7+
= note: it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
8+
= help: consider filing a feature request
9+
110
error: this function definition uses SIMD vector type `i8x8` which (with the chosen ABI) requires the `vector` target feature, which is not enabled
211
--> $DIR/simd-abi-checks-s390x.rs:38:1
312
|
@@ -82,5 +91,5 @@ LL | extern "C" fn vector_transparent_wrapper_arg(x: TransparentWrapper<i8x16>)
8291
|
8392
= help: consider enabling it globally (`-C target-feature=+vector`) or locally (`#[target_feature(enable="vector")]`)
8493

85-
error: aborting due to 10 previous errors
94+
error: aborting due to 10 previous errors; 2 warnings emitted
8695

tests/ui/consts/mono-reachable-invalid-const.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ error[E0080]: evaluation of `Bar::<0>::ASSERT` failed
44
LL | ["oops"][b];
55
| ^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 1
66

7-
note: erroneous constant encountered
8-
--> $DIR/mono-reachable-invalid-const.rs:9:9
9-
|
10-
LL | ["oops"][b];
11-
| ^^^^^^^^^^^
12-
137
note: erroneous constant encountered
148
--> $DIR/mono-reachable-invalid-const.rs:14:19
159
|

tests/ui/inline-const/const-expr-generic-err.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ error[E0080]: evaluation of `bar::<0>::{constant#0}` failed
2222
LL | const { N - 1 }
2323
| ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
2424

25-
note: erroneous constant encountered
26-
--> $DIR/const-expr-generic-err.rs:9:13
27-
|
28-
LL | const { N - 1 }
29-
| ^^^^^
30-
3125
note: erroneous constant encountered
3226
--> $DIR/const-expr-generic-err.rs:9:5
3327
|

tests/ui/layout/post-mono-layout-cycle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Wrapper<T: Trait> {
1414
}
1515

1616
fn abi<T: Trait>(_: Option<Wrapper<T>>) {}
17+
//~^ ERROR: a cycle occurred during layout computation
1718

1819
fn indirect<T: Trait>() {
1920
abi::<T>(None);

tests/ui/layout/post-mono-layout-cycle.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ error[E0391]: cycle detected when computing layout of `Wrapper<()>`
55
= note: cycle used when computing layout of `core::option::Option<Wrapper<()>>`
66
= 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
77

8-
note: the above error was encountered while instantiating `fn abi::<()>`
9-
--> $DIR/post-mono-layout-cycle.rs:19:5
8+
error: a cycle occurred during layout computation
9+
--> $DIR/post-mono-layout-cycle.rs:16:1
1010
|
11-
LL | abi::<T>(None);
12-
| ^^^^^^^^^^^^^^
11+
LL | fn abi<T: Trait>(_: Option<Wrapper<T>>) {}
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

14-
error: aborting due to 1 previous error
14+
error: aborting due to 2 previous errors
1515

1616
For more information about this error, try `rustc --explain E0391`.

tests/ui/structs/default-field-values/post-mono.indirect.stderr

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ error[E0080]: evaluation of `Z::<1>::post_mono::{constant#0}` failed
44
LL | post_mono: usize = X / 0,
55
| ^^^^^ attempt to divide `1_usize` by zero
66

7-
note: erroneous constant encountered
8-
--> $DIR/post-mono.rs:7:24
9-
|
10-
LL | post_mono: usize = X / 0,
11-
| ^^^^^
12-
13-
note: erroneous constant encountered
14-
--> $DIR/post-mono.rs:7:24
15-
|
16-
LL | post_mono: usize = X / 0,
17-
| ^^^^^
18-
|
19-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20-
217
note: erroneous constant encountered
228
--> $DIR/post-mono.rs:12:19
239
|

0 commit comments

Comments
 (0)