Skip to content

Commit 659d44a

Browse files
committed
polymorphize: visit promoted MIR
This commit makes polymorphization visited the MIR of unevaluated constants with available promoted MIR instead of visiting the substitutions of that constant - which will mark all of the generic parameters as used. Signed-off-by: David Wood <[email protected]>
1 parent 1e0e618 commit 659d44a

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
lines changed

src/librustc_mir/monomorphize/polymorphize.rs

+7
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
245245
self.unused_parameters.clear(param.index);
246246
false
247247
}
248+
ty::ConstKind::Unevaluated(_, _, Some(p)) => {
249+
// If there is a promoted, don't look at the substs - since it will always contain
250+
// the generic parameters, instead, traverse the promoted MIR.
251+
let promoted = self.tcx.promoted_mir(self.def_id);
252+
self.visit_body(&promoted[p]);
253+
false
254+
}
248255
_ => c.super_visit_with(self),
249256
}
250257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// build-fail
2+
// compile-flags: -Zpolymorphize=on
3+
#![crate_type = "lib"]
4+
#![feature(rustc_attrs)]
5+
6+
fn foo<'a>(_: &'a ()) {}
7+
8+
#[rustc_polymorphize_error]
9+
pub fn test<T>() {
10+
//~^ ERROR item has unused generic parameters
11+
foo(&());
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: item has unused generic parameters
2+
--> $DIR/promoted-function-1.rs:9:8
3+
|
4+
LL | pub fn test<T>() {
5+
| ^^^^ - generic parameter `T` is unused
6+
7+
error: aborting due to previous error
8+

src/test/ui/polymorphization/promoted-function.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// run-pass
2+
// compile-flags:-Zpolymorphize=on
3+
24
fn fop<T>() {}
35

46
fn bar<T>() -> &'static fn() {

src/test/ui/polymorphization/unsized_cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn foo<T: Default>() {
1717
fn foo2<T: Default>() {
1818
let _: T = Default::default();
1919
(|| {
20+
//~^ ERROR item has unused generic parameters
2021
let call: extern "rust-call" fn(_, _) = Fn::call;
2122
call(&|| {}, ());
2223
//~^ ERROR item has unused generic parameters

src/test/ui/polymorphization/unsized_cast.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ LL | (|| Box::new(|| {}) as Box<dyn Fn()>)();
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: item has unused generic parameters
20-
--> $DIR/unsized_cast.rs:21:15
20+
--> $DIR/unsized_cast.rs:22:15
2121
|
2222
LL | fn foo2<T: Default>() {
2323
| - generic parameter `T` is unused
2424
...
2525
LL | call(&|| {}, ());
2626
| ^^^^^
2727

28-
error: aborting due to 3 previous errors
28+
error: item has unused generic parameters
29+
--> $DIR/unsized_cast.rs:19:5
30+
|
31+
LL | fn foo2<T: Default>() {
32+
| - generic parameter `T` is unused
33+
LL | let _: T = Default::default();
34+
LL | / (|| {
35+
LL | |
36+
LL | | let call: extern "rust-call" fn(_, _) = Fn::call;
37+
LL | | call(&|| {}, ());
38+
LL | |
39+
LL | | })();
40+
| |______^
41+
42+
error: aborting due to 4 previous errors
2943

0 commit comments

Comments
 (0)