Skip to content

Commit 4578435

Browse files
committed
Auto merge of #116874 - compiler-errors:elaborator-nits, r=wesleywiser
Some small elaborator nits Didn't want to fold these into a totally unrelated pr.
2 parents 94c4e5c + 232f314 commit 4578435

File tree

5 files changed

+39
-42
lines changed

5 files changed

+39
-42
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_infer::infer::{
2626
RegionVariableOrigin,
2727
};
2828
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
29-
use rustc_middle::traits::util::supertraits;
3029
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
3130
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
3231
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
@@ -40,7 +39,7 @@ use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplem
4039
use rustc_trait_selection::traits::error_reporting::on_unimplemented::TypeErrCtxtExt as _;
4140
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
4241
use rustc_trait_selection::traits::{
43-
FulfillmentError, Obligation, ObligationCause, ObligationCauseCode,
42+
supertraits, FulfillmentError, Obligation, ObligationCause, ObligationCauseCode,
4443
};
4544
use std::borrow::Cow;
4645

compiler/rustc_infer/src/traits/util.rs

+29-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use smallvec::smallvec;
22

33
use crate::infer::outlives::components::{push_outlives_components, Component};
44
use crate::traits::{self, Obligation, PredicateObligation};
5-
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
5+
use rustc_data_structures::fx::FxHashSet;
66
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
77
use rustc_span::symbol::Ident;
88
use rustc_span::Span;
@@ -76,7 +76,13 @@ impl<'tcx> Extend<ty::Predicate<'tcx>> for PredicateSet<'tcx> {
7676
pub struct Elaborator<'tcx, O> {
7777
stack: Vec<O>,
7878
visited: PredicateSet<'tcx>,
79-
only_self: bool,
79+
mode: Filter,
80+
}
81+
82+
enum Filter {
83+
All,
84+
OnlySelf,
85+
OnlySelfThatDefines(Ident),
8086
}
8187

8288
/// Describes how to elaborate an obligation into a sub-obligation.
@@ -224,7 +230,7 @@ pub fn elaborate<'tcx, O: Elaboratable<'tcx>>(
224230
obligations: impl IntoIterator<Item = O>,
225231
) -> Elaborator<'tcx, O> {
226232
let mut elaborator =
227-
Elaborator { stack: Vec::new(), visited: PredicateSet::new(tcx), only_self: false };
233+
Elaborator { stack: Vec::new(), visited: PredicateSet::new(tcx), mode: Filter::All };
228234
elaborator.extend_deduped(obligations);
229235
elaborator
230236
}
@@ -242,7 +248,13 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
242248
/// Filter to only the supertraits of trait predicates, i.e. only the predicates
243249
/// that have `Self` as their self type, instead of all implied predicates.
244250
pub fn filter_only_self(mut self) -> Self {
245-
self.only_self = true;
251+
self.mode = Filter::OnlySelf;
252+
self
253+
}
254+
255+
/// Filter to only the supertraits of trait predicates that define the assoc_ty.
256+
pub fn filter_only_self_that_defines(mut self, assoc_ty: Ident) -> Self {
257+
self.mode = Filter::OnlySelfThatDefines(assoc_ty);
246258
self
247259
}
248260

@@ -257,10 +269,12 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
257269
return;
258270
}
259271
// Get predicates implied by the trait, or only super predicates if we only care about self predicates.
260-
let predicates = if self.only_self {
261-
tcx.super_predicates_of(data.def_id())
262-
} else {
263-
tcx.implied_predicates_of(data.def_id())
272+
let predicates = match self.mode {
273+
Filter::All => tcx.implied_predicates_of(data.def_id()),
274+
Filter::OnlySelf => tcx.super_predicates_of(data.def_id()),
275+
Filter::OnlySelfThatDefines(ident) => {
276+
tcx.super_predicates_that_define_assoc_item((data.def_id(), ident))
277+
}
264278
};
265279

266280
let obligations =
@@ -409,14 +423,14 @@ impl<'tcx, O: Elaboratable<'tcx>> Iterator for Elaborator<'tcx, O> {
409423
pub fn supertraits<'tcx>(
410424
tcx: TyCtxt<'tcx>,
411425
trait_ref: ty::PolyTraitRef<'tcx>,
412-
) -> impl Iterator<Item = ty::PolyTraitRef<'tcx>> {
426+
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
413427
elaborate(tcx, [trait_ref.to_predicate(tcx)]).filter_only_self().filter_to_traits()
414428
}
415429

416430
pub fn transitive_bounds<'tcx>(
417431
tcx: TyCtxt<'tcx>,
418432
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
419-
) -> impl Iterator<Item = ty::PolyTraitRef<'tcx>> {
433+
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
420434
elaborate(tcx, trait_refs.map(|trait_ref| trait_ref.to_predicate(tcx)))
421435
.filter_only_self()
422436
.filter_to_traits()
@@ -429,31 +443,12 @@ pub fn transitive_bounds<'tcx>(
429443
/// `T::Item` and helps to avoid cycle errors (see e.g. #35237).
430444
pub fn transitive_bounds_that_define_assoc_item<'tcx>(
431445
tcx: TyCtxt<'tcx>,
432-
bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
446+
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
433447
assoc_name: Ident,
434-
) -> impl Iterator<Item = ty::PolyTraitRef<'tcx>> {
435-
let mut stack: Vec<_> = bounds.collect();
436-
let mut visited = FxIndexSet::default();
437-
438-
std::iter::from_fn(move || {
439-
while let Some(trait_ref) = stack.pop() {
440-
let anon_trait_ref = tcx.anonymize_bound_vars(trait_ref);
441-
if visited.insert(anon_trait_ref) {
442-
let super_predicates =
443-
tcx.super_predicates_that_define_assoc_item((trait_ref.def_id(), assoc_name));
444-
for (super_predicate, _) in super_predicates.predicates {
445-
let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
446-
if let Some(binder) = subst_predicate.as_trait_clause() {
447-
stack.push(binder.map_bound(|t| t.trait_ref));
448-
}
449-
}
450-
451-
return Some(trait_ref);
452-
}
453-
}
454-
455-
return None;
456-
})
448+
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
449+
elaborate(tcx, trait_refs.map(|trait_ref| trait_ref.to_predicate(tcx)))
450+
.filter_only_self_that_defines(assoc_name)
451+
.filter_to_traits()
457452
}
458453

459454
///////////////////////////////////////////////////////////////////////////

compiler/rustc_lint/src/deref_into_dyn_supertrait.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::{
44
};
55

66
use rustc_hir as hir;
7-
use rustc_middle::{traits::util::supertraits, ty};
7+
use rustc_middle::ty;
88
use rustc_session::lint::FutureIncompatibilityReason;
99
use rustc_span::sym;
10+
use rustc_trait_selection::traits::supertraits;
1011

1112
declare_lint! {
1213
/// The `deref_into_dyn_supertrait` lint is output whenever there is a use of the

compiler/rustc_middle/src/traits/util.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use rustc_data_structures::fx::FxHashSet;
33
use crate::ty::{PolyTraitRef, TyCtxt};
44

55
/// Given a PolyTraitRef, get the PolyTraitRefs of the trait's (transitive) supertraits.
6-
///
7-
/// A simplified version of the same function at `rustc_infer::traits::util::supertraits`.
8-
pub fn supertraits<'tcx>(
6+
/// This only exists in `rustc_middle` because the more powerful elaborator depends on
7+
/// `rustc_infer` for elaborating outlives bounds -- this should only be used for pretty
8+
/// printing.
9+
pub fn supertraits_for_pretty_printing<'tcx>(
910
tcx: TyCtxt<'tcx>,
1011
trait_ref: PolyTraitRef<'tcx>,
1112
) -> impl Iterator<Item = PolyTraitRef<'tcx>> {

compiler/rustc_middle/src/ty/print/pretty.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
22
use crate::query::IntoQueryParam;
33
use crate::query::Providers;
4+
use crate::traits::util::supertraits_for_pretty_printing;
45
use crate::ty::{
56
self, ConstInt, ParamConst, ScalarInt, Term, TermKind, Ty, TyCtxt, TypeFoldable,
67
TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
@@ -1150,14 +1151,14 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11501151
entry.has_fn_once = true;
11511152
return;
11521153
} else if Some(trait_def_id) == self.tcx().lang_items().fn_mut_trait() {
1153-
let super_trait_ref = crate::traits::util::supertraits(self.tcx(), trait_ref)
1154+
let super_trait_ref = supertraits_for_pretty_printing(self.tcx(), trait_ref)
11541155
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
11551156
.unwrap();
11561157

11571158
fn_traits.entry(super_trait_ref).or_default().fn_mut_trait_ref = Some(trait_ref);
11581159
return;
11591160
} else if Some(trait_def_id) == self.tcx().lang_items().fn_trait() {
1160-
let super_trait_ref = crate::traits::util::supertraits(self.tcx(), trait_ref)
1161+
let super_trait_ref = supertraits_for_pretty_printing(self.tcx(), trait_ref)
11611162
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
11621163
.unwrap();
11631164

0 commit comments

Comments
 (0)