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 <david@davidtw.co>
This commit is contained in:
parent
659d44a3b4
commit
d97f89b1a6
3 changed files with 58 additions and 13 deletions
|
@ -15,6 +15,7 @@ use rustc_middle::ty::{
|
||||||
self,
|
self,
|
||||||
fold::{TypeFoldable, TypeVisitor},
|
fold::{TypeFoldable, TypeVisitor},
|
||||||
query::Providers,
|
query::Providers,
|
||||||
|
subst::SubstsRef,
|
||||||
Const, Ty, TyCtxt,
|
Const, Ty, TyCtxt,
|
||||||
};
|
};
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
|
@ -205,6 +206,25 @@ struct UsedGenericParametersVisitor<'a, 'tcx> {
|
||||||
unused_parameters: &'a mut FiniteBitSet<u32>,
|
unused_parameters: &'a mut FiniteBitSet<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, 'tcx> UsedGenericParametersVisitor<'a, 'tcx> {
|
||||||
|
/// Invoke `unused_generic_params` on a body contained within the current item (e.g.
|
||||||
|
/// a closure, generator or constant).
|
||||||
|
fn visit_child_body(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) {
|
||||||
|
let unused = self.tcx.unused_generic_params(def_id);
|
||||||
|
debug!(
|
||||||
|
"visit_child_body: unused_parameters={:?} unused={:?}",
|
||||||
|
self.unused_parameters, unused
|
||||||
|
);
|
||||||
|
for (i, arg) in substs.iter().enumerate() {
|
||||||
|
let i = i.try_into().unwrap();
|
||||||
|
if !unused.contains(i).unwrap_or(false) {
|
||||||
|
arg.visit_with(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
debug!("visit_child_body: unused_parameters={:?}", self.unused_parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> Visitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
|
impl<'a, 'tcx> Visitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
|
||||||
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
|
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
|
||||||
debug!("visit_local_decl: local_decl={:?}", local_decl);
|
debug!("visit_local_decl: local_decl={:?}", local_decl);
|
||||||
|
@ -252,6 +272,10 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
|
||||||
self.visit_body(&promoted[p]);
|
self.visit_body(&promoted[p]);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
ty::ConstKind::Unevaluated(def_id, unevaluated_substs, None) => {
|
||||||
|
self.visit_child_body(def_id.did, unevaluated_substs);
|
||||||
|
false
|
||||||
|
}
|
||||||
_ => c.super_visit_with(self),
|
_ => c.super_visit_with(self),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,19 +296,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
|
||||||
|
|
||||||
// Consider any generic parameters used by any closures/generators as used in the
|
// Consider any generic parameters used by any closures/generators as used in the
|
||||||
// parent.
|
// parent.
|
||||||
let unused = self.tcx.unused_generic_params(def_id);
|
self.visit_child_body(def_id, substs);
|
||||||
debug!(
|
|
||||||
"visit_ty: unused_parameters={:?} unused={:?}",
|
|
||||||
self.unused_parameters, unused
|
|
||||||
);
|
|
||||||
for (i, arg) in substs.iter().enumerate() {
|
|
||||||
let i = i.try_into().unwrap();
|
|
||||||
if !unused.contains(i).unwrap_or(false) {
|
|
||||||
arg.visit_with(self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
debug!("visit_ty: unused_parameters={:?}", self.unused_parameters);
|
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
ty::Param(param) => {
|
ty::Param(param) => {
|
||||||
|
|
16
src/test/ui/polymorphization/promoted-function-2.rs
Normal file
16
src/test/ui/polymorphization/promoted-function-2.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// build-fail
|
||||||
|
// compile-flags:-Zpolymorphize=on
|
||||||
|
#![crate_type = "lib"]
|
||||||
|
#![feature(lazy_normalization_consts, rustc_attrs)]
|
||||||
|
//~^ WARN the feature `lazy_normalization_consts` is incomplete
|
||||||
|
|
||||||
|
#[rustc_polymorphize_error]
|
||||||
|
fn test<T>() {
|
||||||
|
//~^ ERROR item has unused generic parameters
|
||||||
|
let x = [0; 3 + 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn caller() {
|
||||||
|
test::<String>();
|
||||||
|
test::<Vec<String>>();
|
||||||
|
}
|
17
src/test/ui/polymorphization/promoted-function-2.stderr
Normal file
17
src/test/ui/polymorphization/promoted-function-2.stderr
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
warning: the feature `lazy_normalization_consts` is incomplete and may not be safe to use and/or cause compiler crashes
|
||||||
|
--> $DIR/promoted-function-2.rs:4:12
|
||||||
|
|
|
||||||
|
LL | #![feature(lazy_normalization_consts, rustc_attrs)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: `#[warn(incomplete_features)]` on by default
|
||||||
|
= note: see issue #72219 <https://github.com/rust-lang/rust/issues/72219> for more information
|
||||||
|
|
||||||
|
error: item has unused generic parameters
|
||||||
|
--> $DIR/promoted-function-2.rs:8:4
|
||||||
|
|
|
||||||
|
LL | fn test<T>() {
|
||||||
|
| ^^^^ - generic parameter `T` is unused
|
||||||
|
|
||||||
|
error: aborting due to previous error; 1 warning emitted
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue