Skip to content

Commit f2d8d79

Browse files
lcnrcompiler-errors
authored andcommitted
remove has_nested
1 parent 54c93ab commit f2d8d79

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

compiler/rustc_middle/src/traits/select.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,15 @@ pub type EvaluationCache<'tcx, ENV> = Cache<(ENV, ty::PolyTraitPredicate<'tcx>),
9797
pub enum SelectionCandidate<'tcx> {
9898
/// A built-in implementation for the `Sized` trait. This is preferred
9999
/// over all other candidates.
100-
SizedCandidate {
101-
has_nested: bool,
102-
},
100+
SizedCandidate,
103101

104102
/// A builtin implementation for some specific traits, used in cases
105103
/// where we cannot rely an ordinary library implementations.
106104
///
107105
/// The most notable examples are `Copy` and `Clone`. This is also
108106
/// used for the `DiscriminantKind` and `Pointee` trait, both of which have
109107
/// an associated type.
110-
BuiltinCandidate {
111-
/// `false` if there are no *further* obligations.
112-
has_nested: bool,
113-
},
108+
BuiltinCandidate,
114109

115110
/// Implementation of transmutability trait.
116111
TransmutabilityCandidate,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
8080
}
8181
Some(LangItem::DiscriminantKind) => {
8282
// `DiscriminantKind` is automatically implemented for every type.
83-
candidates.vec.push(BuiltinCandidate { has_nested: false });
83+
candidates.vec.push(BuiltinCandidate);
8484
}
8585
Some(LangItem::PointeeTrait) => {
8686
// `Pointee` is automatically implemented for every type.
87-
candidates.vec.push(BuiltinCandidate { has_nested: false });
87+
candidates.vec.push(BuiltinCandidate);
8888
}
8989
Some(LangItem::Sized) => {
9090
self.assemble_builtin_sized_candidate(
@@ -365,7 +365,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
365365
{
366366
debug!(?self_ty, ?obligation, "assemble_fused_iterator_candidates",);
367367

368-
candidates.vec.push(BuiltinCandidate { has_nested: false });
368+
candidates.vec.push(BuiltinCandidate);
369369
}
370370
}
371371

@@ -810,7 +810,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
810810
hir::Movability::Movable => {
811811
// Movable coroutines are always `Unpin`, so add an
812812
// unconditional builtin candidate.
813-
candidates.vec.push(BuiltinCandidate { has_nested: false });
813+
candidates.vec.push(BuiltinCandidate);
814814
}
815815
}
816816
}
@@ -1122,10 +1122,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11221122
sizedness: SizedTraitKind,
11231123
) {
11241124
match self.sizedness_conditions(obligation, sizedness) {
1125-
BuiltinImplConditions::Where(nested) => {
1126-
candidates
1127-
.vec
1128-
.push(SizedCandidate { has_nested: !nested.skip_binder().is_empty() });
1125+
BuiltinImplConditions::Where(_nested) => {
1126+
candidates.vec.push(SizedCandidate);
11291127
}
11301128
BuiltinImplConditions::None => {}
11311129
BuiltinImplConditions::Ambiguous => {
@@ -1143,10 +1141,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11431141
candidates: &mut SelectionCandidateSet<'tcx>,
11441142
) {
11451143
match conditions {
1146-
BuiltinImplConditions::Where(nested) => {
1147-
candidates
1148-
.vec
1149-
.push(BuiltinCandidate { has_nested: !nested.skip_binder().is_empty() });
1144+
BuiltinImplConditions::Where(_) => {
1145+
candidates.vec.push(BuiltinCandidate);
11501146
}
11511147
BuiltinImplConditions::None => {}
11521148
BuiltinImplConditions::Ambiguous => {
@@ -1160,7 +1156,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11601156
_obligation: &PolyTraitObligation<'tcx>,
11611157
candidates: &mut SelectionCandidateSet<'tcx>,
11621158
) {
1163-
candidates.vec.push(BuiltinCandidate { has_nested: false });
1159+
candidates.vec.push(BuiltinCandidate);
11641160
}
11651161

11661162
fn assemble_candidate_for_tuple(
@@ -1171,7 +1167,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11711167
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
11721168
match self_ty.kind() {
11731169
ty::Tuple(_) => {
1174-
candidates.vec.push(BuiltinCandidate { has_nested: false });
1170+
candidates.vec.push(BuiltinCandidate);
11751171
}
11761172
ty::Infer(ty::TyVar(_)) => {
11771173
candidates.ambiguous = true;
@@ -1215,7 +1211,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12151211
let self_ty = self.infcx.resolve_vars_if_possible(obligation.self_ty());
12161212

12171213
match self_ty.skip_binder().kind() {
1218-
ty::FnPtr(..) => candidates.vec.push(BuiltinCandidate { has_nested: false }),
1214+
ty::FnPtr(..) => candidates.vec.push(BuiltinCandidate),
12191215
ty::Bool
12201216
| ty::Char
12211217
| ty::Int(_)

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
3737
candidate: SelectionCandidate<'tcx>,
3838
) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
3939
Ok(match candidate {
40-
SizedCandidate { has_nested } => {
41-
let data = self.confirm_builtin_candidate(obligation, has_nested);
40+
SizedCandidate => {
41+
let data = self.confirm_builtin_candidate(obligation);
4242
ImplSource::Builtin(BuiltinImplSource::Misc, data)
4343
}
4444

45-
BuiltinCandidate { has_nested } => {
46-
let data = self.confirm_builtin_candidate(obligation, has_nested);
45+
BuiltinCandidate => {
46+
let data = self.confirm_builtin_candidate(obligation);
4747
ImplSource::Builtin(BuiltinImplSource::Misc, data)
4848
}
4949

@@ -249,50 +249,47 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
249249
}
250250
}
251251

252+
#[instrument(level = "debug", skip(self), ret)]
252253
fn confirm_builtin_candidate(
253254
&mut self,
254255
obligation: &PolyTraitObligation<'tcx>,
255-
has_nested: bool,
256256
) -> PredicateObligations<'tcx> {
257-
debug!(?obligation, ?has_nested, "confirm_builtin_candidate");
258-
257+
debug!(?obligation, "confirm_builtin_candidate");
259258
let tcx = self.tcx();
260-
let obligations = if has_nested {
261-
let trait_def = obligation.predicate.def_id();
262-
let conditions = match tcx.as_lang_item(trait_def) {
263-
Some(LangItem::Sized) => {
264-
self.sizedness_conditions(obligation, SizedTraitKind::Sized)
265-
}
266-
Some(LangItem::MetaSized) => {
267-
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
268-
}
269-
Some(LangItem::PointeeSized) => {
270-
bug!("`PointeeSized` is removing during lowering");
271-
}
272-
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation),
273-
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),
274-
other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"),
275-
};
276-
let BuiltinImplConditions::Where(types) = conditions else {
277-
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
278-
};
279-
let types = self.infcx.enter_forall_and_leak_universe(types);
280-
281-
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
282-
self.collect_predicates_for_types(
283-
obligation.param_env,
284-
cause,
285-
obligation.recursion_depth + 1,
286-
trait_def,
287-
types,
288-
)
289-
} else {
290-
PredicateObligations::new()
259+
let trait_def = obligation.predicate.def_id();
260+
let conditions = match tcx.as_lang_item(trait_def) {
261+
Some(LangItem::Sized) => self.sizedness_conditions(obligation, SizedTraitKind::Sized),
262+
Some(LangItem::MetaSized) => {
263+
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
264+
}
265+
Some(LangItem::PointeeSized) => {
266+
bug!("`PointeeSized` is removing during lowering");
267+
}
268+
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation),
269+
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),
270+
Some(
271+
LangItem::Destruct
272+
| LangItem::DiscriminantKind
273+
| LangItem::FnPtrTrait
274+
| LangItem::PointeeTrait
275+
| LangItem::Tuple
276+
| LangItem::Unpin,
277+
) => BuiltinImplConditions::Where(ty::Binder::dummy(vec![])),
278+
other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"),
291279
};
280+
let BuiltinImplConditions::Where(types) = conditions else {
281+
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
282+
};
283+
let types = self.infcx.enter_forall_and_leak_universe(types);
292284

293-
debug!(?obligations);
294-
295-
obligations
285+
let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
286+
self.collect_predicates_for_types(
287+
obligation.param_env,
288+
cause,
289+
obligation.recursion_depth + 1,
290+
trait_def,
291+
types,
292+
)
296293
}
297294

298295
#[instrument(level = "debug", skip(self))]

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
18341834

18351835
// We prefer `Sized` candidates over everything.
18361836
let mut sized_candidates =
1837-
candidates.iter().filter(|c| matches!(c.candidate, SizedCandidate { has_nested: _ }));
1837+
candidates.iter().filter(|c| matches!(c.candidate, SizedCandidate));
18381838
if let Some(sized_candidate) = sized_candidates.next() {
18391839
// There should only ever be a single sized candidate
18401840
// as they would otherwise overlap.
@@ -1986,8 +1986,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
19861986
// Don't use impl candidates which overlap with other candidates.
19871987
// This should pretty much only ever happen with malformed impls.
19881988
if candidates.iter().all(|c| match c.candidate {
1989-
SizedCandidate { has_nested: _ }
1990-
| BuiltinCandidate { has_nested: _ }
1989+
SizedCandidate
1990+
| BuiltinCandidate
19911991
| TransmutabilityCandidate
19921992
| AutoImplCandidate
19931993
| ClosureCandidate { .. }

0 commit comments

Comments
 (0)