Skip to content

Commit d97f89b

Browse files
committed
polymorphize: non-promoted unevaluated constants
This commit makes polymorphization visit non-promoted unevaluated constants rather than visit their substs directly. Signed-off-by: David Wood <[email protected]>
1 parent 659d44a commit d97f89b

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/librustc_mir/monomorphize/polymorphize.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::ty::{
1515
self,
1616
fold::{TypeFoldable, TypeVisitor},
1717
query::Providers,
18+
subst::SubstsRef,
1819
Const, Ty, TyCtxt,
1920
};
2021
use rustc_span::symbol::sym;
@@ -205,6 +206,25 @@ struct UsedGenericParametersVisitor<'a, 'tcx> {
205206
unused_parameters: &'a mut FiniteBitSet<u32>,
206207
}
207208

209+
impl<'a, 'tcx> UsedGenericParametersVisitor<'a, 'tcx> {
210+
/// Invoke `unused_generic_params` on a body contained within the current item (e.g.
211+
/// a closure, generator or constant).
212+
fn visit_child_body(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) {
213+
let unused = self.tcx.unused_generic_params(def_id);
214+
debug!(
215+
"visit_child_body: unused_parameters={:?} unused={:?}",
216+
self.unused_parameters, unused
217+
);
218+
for (i, arg) in substs.iter().enumerate() {
219+
let i = i.try_into().unwrap();
220+
if !unused.contains(i).unwrap_or(false) {
221+
arg.visit_with(self);
222+
}
223+
}
224+
debug!("visit_child_body: unused_parameters={:?}", self.unused_parameters);
225+
}
226+
}
227+
208228
impl<'a, 'tcx> Visitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
209229
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
210230
debug!("visit_local_decl: local_decl={:?}", local_decl);
@@ -252,6 +272,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
252272
self.visit_body(&promoted[p]);
253273
false
254274
}
275+
ty::ConstKind::Unevaluated(def_id, unevaluated_substs, None) => {
276+
self.visit_child_body(def_id.did, unevaluated_substs);
277+
false
278+
}
255279
_ => c.super_visit_with(self),
256280
}
257281
}
@@ -272,19 +296,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
272296

273297
// Consider any generic parameters used by any closures/generators as used in the
274298
// parent.
275-
let unused = self.tcx.unused_generic_params(def_id);
276-
debug!(
277-
"visit_ty: unused_parameters={:?} unused={:?}",
278-
self.unused_parameters, unused
279-
);
280-
for (i, arg) in substs.iter().enumerate() {
281-
let i = i.try_into().unwrap();
282-
if !unused.contains(i).unwrap_or(false) {
283-
arg.visit_with(self);
284-
}
285-
}
286-
debug!("visit_ty: unused_parameters={:?}", self.unused_parameters);
287-
299+
self.visit_child_body(def_id, substs);
288300
false
289301
}
290302
ty::Param(param) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build-fail
2+
// compile-flags:-Zpolymorphize=on
3+
#![crate_type = "lib"]
4+
#![feature(lazy_normalization_consts, rustc_attrs)]
5+
//~^ WARN the feature `lazy_normalization_consts` is incomplete
6+
7+
#[rustc_polymorphize_error]
8+
fn test<T>() {
9+
//~^ ERROR item has unused generic parameters
10+
let x = [0; 3 + 4];
11+
}
12+
13+
pub fn caller() {
14+
test::<String>();
15+
test::<Vec<String>>();
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: the feature `lazy_normalization_consts` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/promoted-function-2.rs:4:12
3+
|
4+
LL | #![feature(lazy_normalization_consts, rustc_attrs)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
= note: see issue #72219 <https://github.com/rust-lang/rust/issues/72219> for more information
9+
10+
error: item has unused generic parameters
11+
--> $DIR/promoted-function-2.rs:8:4
12+
|
13+
LL | fn test<T>() {
14+
| ^^^^ - generic parameter `T` is unused
15+
16+
error: aborting due to previous error; 1 warning emitted
17+

0 commit comments

Comments
 (0)