From 108cad11237a82fdd72cc0db5b55d09e2cfa899c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20K=C3=A5rlin?= Date: Sun, 26 Feb 2023 12:07:20 +0100 Subject: [PATCH] auto_trait: dedup obligation predicates after elaboration --- .../src/traits/auto_trait.rs | 18 +++++++++++++++--- tests/rustdoc-ui/issue_107715.rs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/rustdoc-ui/issue_107715.rs diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 1fb8659bb27d3..d8b521e113649 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -7,6 +7,7 @@ use crate::errors::UnableToConstructConstantValue; use crate::infer::region_constraints::{Constraint, RegionConstraintData}; use crate::infer::InferCtxt; use crate::traits::project::ProjectAndUnifyResult; +use rustc_infer::traits::util::PredicateSet; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::visit::TypeVisitableExt; @@ -344,13 +345,24 @@ impl<'tcx> AutoTraitFinder<'tcx> { _ => panic!("Unexpected error for '{:?}': {:?}", ty, result), }; - let normalized_preds = elaborate_predicates( + let elaborated_preds = elaborate_predicates( tcx, computed_preds.clone().chain(user_computed_preds.iter().cloned()), ) - .map(|o| o.predicate); + .map(|obligation| obligation.predicate); + + // FIXME(generic_const_exprs): + // This deduplication is required only when generic_const_exprs is not active + // see #108397 for more information. + let mut seen_preds = PredicateSet::new(tcx); + let obctx = ObligationCtxt::new(infcx); + let deduped_preds = elaborated_preds + .into_iter() + .map(|pred| obctx.normalize(&dummy_cause, param_env, pred)) + .filter(|normalized_pred| seen_preds.insert(*normalized_pred)); + new_env = ty::ParamEnv::new( - tcx.mk_predicates_from_iter(normalized_preds), + tcx.mk_predicates_from_iter(deduped_preds), param_env.reveal(), param_env.constness(), ); diff --git a/tests/rustdoc-ui/issue_107715.rs b/tests/rustdoc-ui/issue_107715.rs new file mode 100644 index 0000000000000..416b804458a91 --- /dev/null +++ b/tests/rustdoc-ui/issue_107715.rs @@ -0,0 +1,18 @@ +// check-pass +#![crate_type = "lib"] + +const N: usize = 1; + +trait Supertrait { + type AssociatedType; +} + +trait Subtrait: Supertrait<[u8; N]> {} + +struct MapType, V> { + map: K::AssociatedType, +} + +struct Container { + _x: MapType, +}