Skip to content

Commit 2835d9d

Browse files
Simplify even more candidates
1 parent 1311bb5 commit 2835d9d

File tree

6 files changed

+88
-161
lines changed

6 files changed

+88
-161
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,13 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
781781
);
782782
return;
783783
}
784-
Ok(Some(ImplSource::Closure(data))) => {
785-
if !tcx.is_const_fn_raw(data.closure_def_id) {
784+
Ok(Some(ImplSource::Closure(_))) => {
785+
let ty::Closure(closure_def_id, substs) =
786+
*poly_trait_pred.self_ty().no_bound_vars().unwrap().kind()
787+
else {
788+
unreachable!()
789+
};
790+
if !tcx.is_const_fn_raw(closure_def_id) {
786791
self.check_op(ops::FnCallNonConst {
787792
caller,
788793
callee,

compiler/rustc_middle/src/traits/mod.rs

+26-75
Original file line numberDiff line numberDiff line change
@@ -664,16 +664,16 @@ pub enum ImplSource<'tcx, N> {
664664
/// ImplSource automatically generated for a closure. The `DefId` is the ID
665665
/// of the closure expression. This is an `ImplSource::UserDefined` in spirit, but the
666666
/// impl is generated by the compiler and does not appear in the source.
667-
Closure(ImplSourceClosureData<'tcx, N>),
667+
Closure(Vec<N>),
668668

669669
/// Same as above, but for a function pointer type with the given signature.
670-
FnPointer(ImplSourceFnPointerData<'tcx, N>),
670+
FnPointer(Vec<N>),
671671

672672
/// ImplSource automatically generated for a generator.
673-
Generator(ImplSourceGeneratorData<'tcx, N>),
673+
Generator(Vec<N>),
674674

675675
/// ImplSource automatically generated for a generator backing an async future.
676-
Future(ImplSourceFutureData<'tcx, N>),
676+
Future(Vec<N>),
677677

678678
/// ImplSource for a trait alias.
679679
TraitAlias(ImplSourceTraitAliasData<'tcx, N>),
@@ -683,12 +683,13 @@ impl<'tcx, N> ImplSource<'tcx, N> {
683683
pub fn nested_obligations(self) -> Vec<N> {
684684
match self {
685685
ImplSource::UserDefined(i) => i.nested,
686-
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
687-
ImplSource::Closure(c) => c.nested,
688-
ImplSource::Generator(c) => c.nested,
689-
ImplSource::Future(c) => c.nested,
686+
ImplSource::Param(n, _)
687+
| ImplSource::Builtin(n)
688+
| ImplSource::FnPointer(n)
689+
| ImplSource::Closure(n)
690+
| ImplSource::Generator(n)
691+
| ImplSource::Future(n) => n,
690692
ImplSource::Object(d) => d.nested,
691-
ImplSource::FnPointer(d) => d.nested,
692693
ImplSource::TraitAlias(d) => d.nested,
693694
ImplSource::TraitUpcasting(d) => d.nested,
694695
}
@@ -697,12 +698,13 @@ impl<'tcx, N> ImplSource<'tcx, N> {
697698
pub fn borrow_nested_obligations(&self) -> &[N] {
698699
match self {
699700
ImplSource::UserDefined(i) => &i.nested,
700-
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
701-
ImplSource::Closure(c) => &c.nested,
702-
ImplSource::Generator(c) => &c.nested,
703-
ImplSource::Future(c) => &c.nested,
701+
ImplSource::Param(n, _)
702+
| ImplSource::Builtin(n)
703+
| ImplSource::FnPointer(n)
704+
| ImplSource::Closure(n)
705+
| ImplSource::Generator(n)
706+
| ImplSource::Future(n) => &n,
704707
ImplSource::Object(d) => &d.nested,
705-
ImplSource::FnPointer(d) => &d.nested,
706708
ImplSource::TraitAlias(d) => &d.nested,
707709
ImplSource::TraitUpcasting(d) => &d.nested,
708710
}
@@ -711,12 +713,13 @@ impl<'tcx, N> ImplSource<'tcx, N> {
711713
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
712714
match self {
713715
ImplSource::UserDefined(i) => &mut i.nested,
714-
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
715-
ImplSource::Closure(c) => &mut c.nested,
716-
ImplSource::Generator(c) => &mut c.nested,
717-
ImplSource::Future(c) => &mut c.nested,
716+
ImplSource::Param(n, _)
717+
| ImplSource::Builtin(n)
718+
| ImplSource::FnPointer(n)
719+
| ImplSource::Closure(n)
720+
| ImplSource::Generator(n)
721+
| ImplSource::Future(n) => n,
718722
ImplSource::Object(d) => &mut d.nested,
719-
ImplSource::FnPointer(d) => &mut d.nested,
720723
ImplSource::TraitAlias(d) => &mut d.nested,
721724
ImplSource::TraitUpcasting(d) => &mut d.nested,
722725
}
@@ -739,25 +742,10 @@ impl<'tcx, N> ImplSource<'tcx, N> {
739742
vtable_base: o.vtable_base,
740743
nested: o.nested.into_iter().map(f).collect(),
741744
}),
742-
ImplSource::Closure(c) => ImplSource::Closure(ImplSourceClosureData {
743-
closure_def_id: c.closure_def_id,
744-
substs: c.substs,
745-
nested: c.nested.into_iter().map(f).collect(),
746-
}),
747-
ImplSource::Generator(c) => ImplSource::Generator(ImplSourceGeneratorData {
748-
generator_def_id: c.generator_def_id,
749-
substs: c.substs,
750-
nested: c.nested.into_iter().map(f).collect(),
751-
}),
752-
ImplSource::Future(c) => ImplSource::Future(ImplSourceFutureData {
753-
generator_def_id: c.generator_def_id,
754-
substs: c.substs,
755-
nested: c.nested.into_iter().map(f).collect(),
756-
}),
757-
ImplSource::FnPointer(p) => ImplSource::FnPointer(ImplSourceFnPointerData {
758-
fn_ty: p.fn_ty,
759-
nested: p.nested.into_iter().map(f).collect(),
760-
}),
745+
ImplSource::Closure(n) => ImplSource::Closure(n.into_iter().map(f).collect()),
746+
ImplSource::Generator(n) => ImplSource::Generator(n.into_iter().map(f).collect()),
747+
ImplSource::Future(n) => ImplSource::Future(n.into_iter().map(f).collect()),
748+
ImplSource::FnPointer(n) => ImplSource::FnPointer(n.into_iter().map(f).collect()),
761749
ImplSource::TraitAlias(d) => ImplSource::TraitAlias(ImplSourceTraitAliasData {
762750
alias_def_id: d.alias_def_id,
763751
substs: d.substs,
@@ -791,36 +779,6 @@ pub struct ImplSourceUserDefinedData<'tcx, N> {
791779
pub nested: Vec<N>,
792780
}
793781

794-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
795-
#[derive(TypeFoldable, TypeVisitable)]
796-
pub struct ImplSourceGeneratorData<'tcx, N> {
797-
pub generator_def_id: DefId,
798-
pub substs: SubstsRef<'tcx>,
799-
/// Nested obligations. This can be non-empty if the generator
800-
/// signature contains associated types.
801-
pub nested: Vec<N>,
802-
}
803-
804-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
805-
#[derive(TypeFoldable, TypeVisitable)]
806-
pub struct ImplSourceFutureData<'tcx, N> {
807-
pub generator_def_id: DefId,
808-
pub substs: SubstsRef<'tcx>,
809-
/// Nested obligations. This can be non-empty if the generator
810-
/// signature contains associated types.
811-
pub nested: Vec<N>,
812-
}
813-
814-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
815-
#[derive(TypeFoldable, TypeVisitable)]
816-
pub struct ImplSourceClosureData<'tcx, N> {
817-
pub closure_def_id: DefId,
818-
pub substs: SubstsRef<'tcx>,
819-
/// Nested obligations. This can be non-empty if the closure
820-
/// signature contains associated types.
821-
pub nested: Vec<N>,
822-
}
823-
824782
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
825783
#[derive(TypeFoldable, TypeVisitable)]
826784
pub struct ImplSourceTraitUpcastingData<N> {
@@ -848,13 +806,6 @@ pub struct ImplSourceObjectData<N> {
848806
pub nested: Vec<N>,
849807
}
850808

851-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
852-
#[derive(TypeFoldable, TypeVisitable)]
853-
pub struct ImplSourceFnPointerData<'tcx, N> {
854-
pub fn_ty: Ty<'tcx>,
855-
pub nested: Vec<N>,
856-
}
857-
858809
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
859810
#[derive(TypeFoldable, TypeVisitable)]
860811
pub struct ImplSourceTraitAliasData<'tcx, N> {

compiler/rustc_middle/src/traits/structural_impls.rs

-36
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,6 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceUserDefinedData<'tcx,
4242
}
4343
}
4444

45-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceGeneratorData<'tcx, N> {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
write!(
48-
f,
49-
"ImplSourceGeneratorData(generator_def_id={:?}, substs={:?}, nested={:?})",
50-
self.generator_def_id, self.substs, self.nested
51-
)
52-
}
53-
}
54-
55-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceFutureData<'tcx, N> {
56-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57-
write!(
58-
f,
59-
"ImplSourceFutureData(generator_def_id={:?}, substs={:?}, nested={:?})",
60-
self.generator_def_id, self.substs, self.nested
61-
)
62-
}
63-
}
64-
65-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceClosureData<'tcx, N> {
66-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
write!(
68-
f,
69-
"ImplSourceClosureData(closure_def_id={:?}, substs={:?}, nested={:?})",
70-
self.closure_def_id, self.substs, self.nested
71-
)
72-
}
73-
}
74-
7545
impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitUpcastingData<N> {
7646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7747
write!(
@@ -92,12 +62,6 @@ impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<N> {
9262
}
9363
}
9464

95-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceFnPointerData<'tcx, N> {
96-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97-
write!(f, "ImplSourceFnPointerData(fn_ty={:?}, nested={:?})", self.fn_ty, self.nested)
98-
}
99-
}
100-
10165
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx, N> {
10266
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10367
write!(

compiler/rustc_trait_selection/src/traits/project.rs

+28-16
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
use super::specialization_graph;
44
use super::translate_substs;
55
use super::util;
6+
use super::ImplSourceUserDefinedData;
67
use super::MismatchedProjectionTypes;
78
use super::Obligation;
89
use super::ObligationCause;
910
use super::PredicateObligation;
1011
use super::Selection;
1112
use super::SelectionContext;
1213
use super::SelectionError;
13-
use super::{
14-
ImplSourceClosureData, ImplSourceFnPointerData, ImplSourceFutureData, ImplSourceGeneratorData,
15-
ImplSourceUserDefinedData,
16-
};
1714
use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey};
1815

1916
use crate::errors::InherentProjectionNormalizationOverflow;
@@ -2015,9 +2012,14 @@ fn confirm_select_candidate<'cx, 'tcx>(
20152012
fn confirm_generator_candidate<'cx, 'tcx>(
20162013
selcx: &mut SelectionContext<'cx, 'tcx>,
20172014
obligation: &ProjectionTyObligation<'tcx>,
2018-
impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
2015+
nested: Vec<PredicateObligation<'tcx>>,
20192016
) -> Progress<'tcx> {
2020-
let gen_sig = impl_source.substs.as_generator().poly_sig();
2017+
let ty::Generator(_, substs, _) =
2018+
selcx.infcx.shallow_resolve(obligation.predicate.self_ty()).kind()
2019+
else {
2020+
unreachable!()
2021+
};
2022+
let gen_sig = substs.as_generator().poly_sig();
20212023
let Normalized { value: gen_sig, obligations } = normalize_with_depth(
20222024
selcx,
20232025
obligation.param_env,
@@ -2055,16 +2057,21 @@ fn confirm_generator_candidate<'cx, 'tcx>(
20552057
});
20562058

20572059
confirm_param_env_candidate(selcx, obligation, predicate, false)
2058-
.with_addl_obligations(impl_source.nested)
2060+
.with_addl_obligations(nested)
20592061
.with_addl_obligations(obligations)
20602062
}
20612063

20622064
fn confirm_future_candidate<'cx, 'tcx>(
20632065
selcx: &mut SelectionContext<'cx, 'tcx>,
20642066
obligation: &ProjectionTyObligation<'tcx>,
2065-
impl_source: ImplSourceFutureData<'tcx, PredicateObligation<'tcx>>,
2067+
nested: Vec<PredicateObligation<'tcx>>,
20662068
) -> Progress<'tcx> {
2067-
let gen_sig = impl_source.substs.as_generator().poly_sig();
2069+
let ty::Generator(_, substs, _) =
2070+
selcx.infcx.shallow_resolve(obligation.predicate.self_ty()).kind()
2071+
else {
2072+
unreachable!()
2073+
};
2074+
let gen_sig = substs.as_generator().poly_sig();
20682075
let Normalized { value: gen_sig, obligations } = normalize_with_depth(
20692076
selcx,
20702077
obligation.param_env,
@@ -2094,7 +2101,7 @@ fn confirm_future_candidate<'cx, 'tcx>(
20942101
});
20952102

20962103
confirm_param_env_candidate(selcx, obligation, predicate, false)
2097-
.with_addl_obligations(impl_source.nested)
2104+
.with_addl_obligations(nested)
20982105
.with_addl_obligations(obligations)
20992106
}
21002107

@@ -2155,9 +2162,9 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
21552162
fn confirm_fn_pointer_candidate<'cx, 'tcx>(
21562163
selcx: &mut SelectionContext<'cx, 'tcx>,
21572164
obligation: &ProjectionTyObligation<'tcx>,
2158-
fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
2165+
nested: Vec<PredicateObligation<'tcx>>,
21592166
) -> Progress<'tcx> {
2160-
let fn_type = selcx.infcx.shallow_resolve(fn_pointer_impl_source.fn_ty);
2167+
let fn_type = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
21612168
let sig = fn_type.fn_sig(selcx.tcx());
21622169
let Normalized { value: sig, obligations } = normalize_with_depth(
21632170
selcx,
@@ -2168,16 +2175,21 @@ fn confirm_fn_pointer_candidate<'cx, 'tcx>(
21682175
);
21692176

21702177
confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
2171-
.with_addl_obligations(fn_pointer_impl_source.nested)
2178+
.with_addl_obligations(nested)
21722179
.with_addl_obligations(obligations)
21732180
}
21742181

21752182
fn confirm_closure_candidate<'cx, 'tcx>(
21762183
selcx: &mut SelectionContext<'cx, 'tcx>,
21772184
obligation: &ProjectionTyObligation<'tcx>,
2178-
impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
2185+
nested: Vec<PredicateObligation<'tcx>>,
21792186
) -> Progress<'tcx> {
2180-
let closure_sig = impl_source.substs.as_closure().sig();
2187+
let ty::Closure(_, substs) =
2188+
selcx.infcx.shallow_resolve(obligation.predicate.self_ty()).kind()
2189+
else {
2190+
unreachable!()
2191+
};
2192+
let closure_sig = substs.as_closure().sig();
21812193
let Normalized { value: closure_sig, obligations } = normalize_with_depth(
21822194
selcx,
21832195
obligation.param_env,
@@ -2189,7 +2201,7 @@ fn confirm_closure_candidate<'cx, 'tcx>(
21892201
debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
21902202

21912203
confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
2192-
.with_addl_obligations(impl_source.nested)
2204+
.with_addl_obligations(nested)
21932205
.with_addl_obligations(obligations)
21942206
}
21952207

0 commit comments

Comments
 (0)