Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polymorphize: I used if T used in I: Foo<T> #75518

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 119 additions & 31 deletions src/librustc_mir/monomorphize/polymorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ fn unused_generic_params(tcx: TyCtxt<'_>, def_id: DefId) -> FiniteBitSet<u32> {

// Visit MIR and accumululate used generic parameters.
let body = tcx.optimized_mir(def_id);
let mut vis =
UsedGenericParametersVisitor { tcx, def_id, unused_parameters: &mut unused_parameters };
let mut vis = MarkUsedGenericParams { tcx, def_id, unused_parameters: &mut unused_parameters };
vis.visit_body(body);
debug!("unused_generic_params: (after visitor) unused_parameters={:?}", unused_parameters);

Expand Down Expand Up @@ -120,45 +119,101 @@ fn mark_used_by_predicates<'tcx>(
def_id: DefId,
unused_parameters: &mut FiniteBitSet<u32>,
) {
let def_id = tcx.closure_base_def_id(def_id);

let is_self_ty_used = |unused_parameters: &mut FiniteBitSet<u32>, self_ty: Ty<'tcx>| {
debug!("unused_generic_params: self_ty={:?}", self_ty);
if let ty::Param(param) = self_ty.kind {
!unused_parameters.contains(param.index).unwrap_or(false)
} else {
false
}
let is_ty_used = |unused_parameters: &FiniteBitSet<u32>, ty: Ty<'tcx>| -> bool {
let mut vis = IsUsedGenericParams { unused_parameters };
ty.visit_with(&mut vis)
};

let mark_ty = |unused_parameters: &mut FiniteBitSet<u32>, ty: Ty<'tcx>| {
let mut vis = UsedGenericParametersVisitor { tcx, def_id, unused_parameters };
let mut vis = MarkUsedGenericParams { tcx, def_id, unused_parameters };
ty.visit_with(&mut vis);
};

let def_id = tcx.closure_base_def_id(def_id);
let predicates = tcx.explicit_predicates_of(def_id);
debug!("mark_parameters_used_in_predicates: predicates_of={:?}", predicates);
for (predicate, _) in predicates.predicates {
match predicate.skip_binders() {
ty::PredicateAtom::Trait(predicate, ..) => {
let trait_ref = predicate.trait_ref;
if is_self_ty_used(unused_parameters, trait_ref.self_ty()) {
debug!("mark_used_by_predicates: predicates_of={:?}", predicates);

let mut current_unused_parameters = FiniteBitSet::new_empty();
// Run to a fixed point to support `where T: Trait<U>, U: Trait<V>`, starting with an empty
// bit set so that this is skipped if all parameters are already used.
while current_unused_parameters != *unused_parameters {
debug!(
"mark_used_by_predicates: current_unused_parameters={:?} = unused_parameters={:?}",
current_unused_parameters, unused_parameters
);
current_unused_parameters = *unused_parameters;

for (predicate, _) in predicates.predicates {
match predicate.skip_binders() {
ty::PredicateAtom::Trait(predicate, ..) => {
let trait_ref = predicate.trait_ref;
debug!("mark_used_by_predicates: (trait) trait_ref={:?}", trait_ref);

// Consider `T` used if `I` is used in predicates of the form
// `I: Iterator<Item = T>`
debug!("mark_used_by_predicates: checking self");
if is_ty_used(unused_parameters, trait_ref.self_ty()) {
debug!("mark_used_by_predicates: used!");
for ty in trait_ref.substs.types() {
mark_ty(unused_parameters, ty);
}

// No need to check for a type being used in the substs if `self_ty` was
// used.
continue;
}

// Consider `I` used if `T` is used in predicates of the form
// `I: Iterator<Item = &'a (T, E)>` (see rust-lang/rust#75326)
debug!("mark_used_by_predicates: checking substs");
for ty in trait_ref.substs.types() {
debug!("unused_generic_params: (trait) ty={:?}", ty);
mark_ty(unused_parameters, ty);
if is_ty_used(unused_parameters, ty) {
debug!("mark_used_by_predicates: used!");
mark_ty(unused_parameters, trait_ref.self_ty());
}
}
}
Comment on lines +152 to 175
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like an overly complicated way to handle "if any parameters are used, all are".
Notable strangeness:

  • substs.types() will include self_ty so some of this code already seems redundant with itself
  • .types() makes me wonder: what about const generics?

}
ty::PredicateAtom::Projection(proj, ..) => {
let self_ty = proj.projection_ty.self_ty();
if is_self_ty_used(unused_parameters, self_ty) {
debug!("unused_generic_params: (projection ty={:?}", proj.ty);
mark_ty(unused_parameters, proj.ty);
ty::PredicateAtom::Projection(proj, ..) => {
let self_ty = proj.projection_ty.self_ty();
debug!(
"mark_used_by_predicates: (projection) self_ty={:?} proj.ty={:?}",
self_ty, proj.ty
);

// Consider `T` used if `I` is used in predicates of the form
// `<I as Iterator>::Item = T`
debug!("mark_used_by_predicates: checking self");
if is_ty_used(unused_parameters, self_ty) {
debug!("mark_used_by_predicates: used!");
mark_ty(unused_parameters, proj.ty);

// No need to check for projection type being used if `self_ty` was used.
continue;
}

// Consider `I` used if `T` is used in predicates of the form
// `<I as Iterator>::Item = &'a (T, E)` (see rust-lang/rust#75326)
debug!("mark_used_by_predicates: checking projection ty");
if is_ty_used(unused_parameters, proj.ty) {
debug!("mark_used_by_predicates: used!");
mark_ty(unused_parameters, self_ty);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is overly simplistic, I suspect you can replace I: Iterator<...> with (): WeirdIterator<I, ...>.
Any treatment of the Self parameter as special is suspicious by default, IMO.

}
}
ty::PredicateAtom::RegionOutlives(..)
| ty::PredicateAtom::TypeOutlives(..)
| ty::PredicateAtom::WellFormed(..)
| ty::PredicateAtom::ObjectSafe(..)
| ty::PredicateAtom::ClosureKind(..)
| ty::PredicateAtom::Subtype(..)
| ty::PredicateAtom::ConstEvaluatable(..)
| ty::PredicateAtom::ConstEquate(..) => (),
}
_ => (),
}
}

if let Some(parent) = predicates.parent {
mark_used_by_predicates(tcx, parent, unused_parameters);
}
}

/// Emit errors for the function annotated by `#[rustc_polymorphize_error]`, labelling each generic
Expand Down Expand Up @@ -204,13 +259,13 @@ fn emit_unused_generic_params_error<'tcx>(
}

/// Visitor used to aggregate generic parameter uses.
struct UsedGenericParametersVisitor<'a, 'tcx> {
struct MarkUsedGenericParams<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
def_id: DefId,
unused_parameters: &'a mut FiniteBitSet<u32>,
}

impl<'a, 'tcx> UsedGenericParametersVisitor<'a, 'tcx> {
impl<'a, 'tcx> MarkUsedGenericParams<'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>) {
Expand All @@ -229,7 +284,7 @@ impl<'a, 'tcx> UsedGenericParametersVisitor<'a, 'tcx> {
}
}

impl<'a, 'tcx> Visitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
impl<'a, 'tcx> Visitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
debug!("visit_local_decl: local_decl={:?}", local_decl);
if local == Local::from_usize(1) {
Expand All @@ -256,7 +311,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
}
}

impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
debug!("visit_const: c={:?}", c);
if !c.has_param_types_or_consts() {
Expand Down Expand Up @@ -318,3 +373,36 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
}
}
}

/// Visitor used to check if a generic parameter is used.
struct IsUsedGenericParams<'a> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be called HasUsedGenericParams?

unused_parameters: &'a FiniteBitSet<u32>,
}

impl<'a, 'tcx> TypeVisitor<'tcx> for IsUsedGenericParams<'a> {
fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> bool {
debug!("visit_const: c={:?}", c);
if !c.has_param_types_or_consts() {
return false;
}

match c.val {
ty::ConstKind::Param(param) => {
!self.unused_parameters.contains(param.index).unwrap_or(false)
}
_ => c.super_visit_with(self),
}
}

fn visit_ty(&mut self, ty: Ty<'tcx>) -> bool {
debug!("visit_ty: ty={:?}", ty);
if !ty.has_param_types_or_consts() {
return false;
}

match ty.kind {
ty::Param(param) => !self.unused_parameters.contains(param.index).unwrap_or(false),
_ => ty.super_visit_with(self),
}
}
}
48 changes: 48 additions & 0 deletions src/test/ui/polymorphization/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,55 @@ where
bar::<I>()
}

#[rustc_polymorphize_error]
fn baz<I, T>(_: I)
where
std::iter::Repeat<I>: Iterator<Item = T>,
{
bar::<I>()
}

// In addition, check that `I` is considered used in `next::{{closure}}`, because `T` is used and
// `T` is really just `I::Item`. `E` is used due to the fixed-point marking of predicates.

pub(crate) struct Foo<'a, I, E>(I, &'a E);

impl<'a, I, T: 'a, E> Iterator for Foo<'a, I, E>
where
I: Iterator<Item = &'a (T, E)>,
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
{
type Item = T;

#[rustc_polymorphize_error]
fn next(&mut self) -> Option<Self::Item> {
self.find(|_| true)
}
}

// Furthermore, check that `B` is considered used because `C` is used, and that `A` is considered
// used because `B` is now used.

trait Baz<Z> {}

impl Baz<u16> for u8 {}
impl Baz<u32> for u16 {}

#[rustc_polymorphize_error]
fn quux<A, B, C: Default>() -> usize
where
A: Baz<B>,
B: Baz<C>,
{
std::mem::size_of::<C>()
}

fn main() {
let x = &[2u32];
foo(x.iter());
baz(x.iter());

let mut a = Foo([(1u32, 1u16)].iter(), &1u16);
let _ = a.next();

let _ = quux::<u8, u16, u32>();
}
22 changes: 22 additions & 0 deletions src/test/ui/polymorphization/symbol-ambiguity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// build-pass
// compile-flags: -Zpolymorphize=on -Zsymbol-mangling-version=v0

pub(crate) struct Foo<'a, I, E>(I, &'a E);

impl<'a, I, T: 'a, E> Iterator for Foo<'a, I, E>
where
I: Iterator<Item = &'a (T, E)>,
{
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
self.find(|_| true)
}
}

fn main() {
let mut a = Foo([(1u32, 1u16)].iter(), &1u16);
let mut b = Foo([(1u16, 1u32)].iter(), &1u32);
let _ = a.next();
let _ = b.next();
}