Skip to content

Commit 66eb346

Browse files
Get rid of the redundant elaboration in middle
1 parent 90423a7 commit 66eb346

File tree

7 files changed

+40
-100
lines changed

7 files changed

+40
-100
lines changed

compiler/rustc_middle/src/traits/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub mod select;
77
pub mod solve;
88
pub mod specialization_graph;
99
mod structural_impls;
10-
pub mod util;
1110

1211
use crate::mir::ConstraintCategory;
1312
use crate::ty::abstract_const::NotConstEvaluatable;

compiler/rustc_middle/src/traits/util.rs

-62
This file was deleted.

compiler/rustc_middle/src/ty/context.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3737
use rustc_ast::{self as ast, attr};
3838
use rustc_data_structures::defer;
3939
use rustc_data_structures::fingerprint::Fingerprint;
40-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
40+
use rustc_data_structures::fx::FxHashMap;
4141
use rustc_data_structures::intern::Interned;
4242
use rustc_data_structures::profiling::SelfProfilerRef;
4343
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
@@ -532,10 +532,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
532532
self.trait_def(trait_def_id).implement_via_object
533533
}
534534

535-
fn supertrait_def_ids(self, trait_def_id: DefId) -> impl IntoIterator<Item = DefId> {
536-
self.supertrait_def_ids(trait_def_id)
537-
}
538-
539535
fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
540536
self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
541537
}
@@ -2495,25 +2491,7 @@ impl<'tcx> TyCtxt<'tcx> {
24952491
/// to identify which traits may define a given associated type to help avoid cycle errors,
24962492
/// and to make size estimates for vtable layout computation.
24972493
pub fn supertrait_def_ids(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
2498-
let mut set = FxHashSet::default();
2499-
let mut stack = vec![trait_def_id];
2500-
2501-
set.insert(trait_def_id);
2502-
2503-
iter::from_fn(move || -> Option<DefId> {
2504-
let trait_did = stack.pop()?;
2505-
let generic_predicates = self.explicit_super_predicates_of(trait_did);
2506-
2507-
for (predicate, _) in generic_predicates.predicates {
2508-
if let ty::ClauseKind::Trait(data) = predicate.kind().skip_binder() {
2509-
if set.insert(data.def_id()) {
2510-
stack.push(data.def_id());
2511-
}
2512-
}
2513-
}
2514-
2515-
Some(trait_did)
2516-
})
2494+
rustc_type_ir::elaborate::supertrait_def_ids(self, trait_def_id)
25172495
}
25182496

25192497
/// Given a closure signature, returns an equivalent fn signature. Detuples

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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::{super_predicates_for_pretty_printing, supertraits_for_pretty_printing};
54
use crate::ty::GenericArgKind;
65
use crate::ty::{
76
ConstInt, Expr, ParamConst, ScalarInt, Term, TermKind, TypeFoldable, TypeSuperFoldable,
@@ -23,6 +22,7 @@ use rustc_span::symbol::{kw, Ident, Symbol};
2322
use rustc_span::FileNameDisplayPreference;
2423
use rustc_target::abi::Size;
2524
use rustc_target::spec::abi::Abi;
25+
use rustc_type_ir::{elaborate, Upcast as _};
2626
use smallvec::SmallVec;
2727

2828
use std::cell::Cell;
@@ -1255,14 +1255,14 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12551255
entry.has_fn_once = true;
12561256
return;
12571257
} else if self.tcx().is_lang_item(trait_def_id, LangItem::FnMut) {
1258-
let super_trait_ref = supertraits_for_pretty_printing(self.tcx(), trait_ref)
1258+
let super_trait_ref = elaborate::supertraits(self.tcx(), trait_ref)
12591259
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
12601260
.unwrap();
12611261

12621262
fn_traits.entry(super_trait_ref).or_default().fn_mut_trait_ref = Some(trait_ref);
12631263
return;
12641264
} else if self.tcx().is_lang_item(trait_def_id, LangItem::Fn) {
1265-
let super_trait_ref = supertraits_for_pretty_printing(self.tcx(), trait_ref)
1265+
let super_trait_ref = elaborate::supertraits(self.tcx(), trait_ref)
12661266
.find(|super_trait_ref| super_trait_ref.def_id() == fn_once_trait)
12671267
.unwrap();
12681268

@@ -1343,10 +1343,11 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
13431343
let bound_principal_with_self = bound_principal
13441344
.with_self_ty(cx.tcx(), cx.tcx().types.trait_object_dummy_self);
13451345

1346-
let super_projections: Vec<_> =
1347-
super_predicates_for_pretty_printing(cx.tcx(), bound_principal_with_self)
1348-
.filter_map(|clause| clause.as_projection_clause())
1349-
.collect();
1346+
let clause: ty::Clause<'tcx> = bound_principal_with_self.upcast(cx.tcx());
1347+
let super_projections: Vec<_> = elaborate::elaborate(cx.tcx(), [clause])
1348+
.filter_only_self()
1349+
.filter_map(|clause| clause.as_projection_clause())
1350+
.collect();
13501351

13511352
let mut projections: Vec<_> = predicates
13521353
.projection_bounds()

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_type_ir::fast_reject::{DeepRejectCtxt, TreatParams};
66
use rustc_type_ir::inherent::*;
77
use rustc_type_ir::lang_items::TraitSolverLangItem;
88
use rustc_type_ir::visit::TypeVisitableExt as _;
9-
use rustc_type_ir::{self as ty, Interner, TraitPredicate, Upcast as _};
9+
use rustc_type_ir::{self as ty, elaborate, Interner, TraitPredicate, Upcast as _};
1010
use tracing::{instrument, trace};
1111

1212
use crate::delegate::SolverDelegate;
@@ -862,8 +862,7 @@ where
862862
.auto_traits()
863863
.into_iter()
864864
.chain(a_data.principal_def_id().into_iter().flat_map(|principal_def_id| {
865-
self.cx()
866-
.supertrait_def_ids(principal_def_id)
865+
elaborate::supertrait_def_ids(self.cx(), principal_def_id)
867866
.into_iter()
868867
.filter(|def_id| self.cx().trait_is_auto(*def_id))
869868
}))

compiler/rustc_type_ir/src/elaborate.rs

+28
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,34 @@ impl<I: Interner, O: Elaboratable<I>> Iterator for Elaborator<I, O> {
229229
// Supertrait iterator
230230
///////////////////////////////////////////////////////////////////////////
231231

232+
/// Computes the def-ids of the transitive supertraits of `trait_def_id`. This (intentionally)
233+
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
234+
/// to identify which traits may define a given associated type to help avoid cycle errors,
235+
/// and to make size estimates for vtable layout computation.
236+
pub fn supertrait_def_ids<I: Interner>(
237+
cx: I,
238+
trait_def_id: I::DefId,
239+
) -> impl Iterator<Item = I::DefId> {
240+
let mut set = HashSet::default();
241+
let mut stack = vec![trait_def_id];
242+
243+
set.insert(trait_def_id);
244+
245+
std::iter::from_fn(move || {
246+
let trait_def_id = stack.pop()?;
247+
248+
for (predicate, _) in cx.explicit_super_predicates_of(trait_def_id).iter_identity() {
249+
if let ty::ClauseKind::Trait(data) = predicate.kind().skip_binder() {
250+
if set.insert(data.def_id()) {
251+
stack.push(data.def_id());
252+
}
253+
}
254+
}
255+
256+
Some(trait_def_id)
257+
})
258+
}
259+
232260
pub fn supertraits<I: Interner>(
233261
tcx: I,
234262
trait_ref: ty::Binder<I, ty::TraitRef<I>>,

compiler/rustc_type_ir/src/interner.rs

-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,6 @@ pub trait Interner:
253253

254254
fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool;
255255

256-
fn supertrait_def_ids(self, trait_def_id: Self::DefId)
257-
-> impl IntoIterator<Item = Self::DefId>;
258-
259256
fn delay_bug(self, msg: impl ToString) -> Self::ErrorGuaranteed;
260257

261258
fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool;

0 commit comments

Comments
 (0)