Skip to content

Commit a832c07

Browse files
committed
Auto merge of rust-lang#118120 - compiler-errors:closure-kind, r=<try>
Remove `PredicateKind::ClosureKind` We don't need the `ClosureKind` predicate kind -- instead, `Fn`-family trait goals are left as ambiguous, and we only need to make progress on `FnOnce` projection goals for inference purposes. This is similar to how we do confirmation of `Fn`-family trait and projection goals in the new trait solver, which also doesn't use the `ClosureKind` predicate. Some hacky logic is added in the second commit so that we can keep the error messages the same.
2 parents c5af061 + 3c03780 commit a832c07

File tree

18 files changed

+40
-109
lines changed

18 files changed

+40
-109
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
662662
// this closure yet; this is exactly why the other
663663
// code is looking for a self type of an unresolved
664664
// inference variable.
665-
| ty::PredicateKind::ClosureKind(..)
666665
| ty::PredicateKind::Ambiguous
667666
=> None,
668667
},

compiler/rustc_infer/src/infer/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -906,12 +906,14 @@ impl<'tcx> InferCtxt<'tcx> {
906906
self.inner.borrow().undo_log.opaque_types_in_snapshot(&snapshot.undo_snapshot)
907907
}
908908

909-
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
909+
pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, expected: T, actual: T) -> bool
910910
where
911911
T: at::ToTrace<'tcx>,
912912
{
913913
let origin = &ObligationCause::dummy();
914-
self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::No, a, b).is_ok())
914+
self.probe(|_| {
915+
self.at(origin, param_env).sub(DefineOpaqueTypes::No, expected, actual).is_ok()
916+
})
915917
}
916918

917919
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool

compiler/rustc_infer/src/traits/util.rs

-3
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
308308
ty::PredicateKind::Clause(ty::ClauseKind::Projection(..)) => {
309309
// Nothing to elaborate in a projection predicate.
310310
}
311-
ty::PredicateKind::ClosureKind(..) => {
312-
// Nothing to elaborate when waiting for a closure's kind to be inferred.
313-
}
314311
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..)) => {
315312
// Currently, we do not elaborate const-evaluatable
316313
// predicates.

compiler/rustc_middle/src/ty/flags.rs

-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,6 @@ impl FlagComputation {
263263
self.add_args(slice::from_ref(&arg));
264264
}
265265
ty::PredicateKind::ObjectSafe(_def_id) => {}
266-
ty::PredicateKind::ClosureKind(_def_id, args, _kind) => {
267-
self.add_args(args);
268-
}
269266
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(uv)) => {
270267
self.add_const(uv);
271268
}

compiler/rustc_middle/src/ty/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,6 @@ impl<'tcx> Predicate<'tcx> {
548548
| PredicateKind::Clause(ClauseKind::ConstArgHasType(..))
549549
| PredicateKind::AliasRelate(..)
550550
| PredicateKind::ObjectSafe(_)
551-
| PredicateKind::ClosureKind(_, _, _)
552551
| PredicateKind::Subtype(_)
553552
| PredicateKind::Coerce(_)
554553
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(_))
@@ -1276,7 +1275,6 @@ impl<'tcx> Predicate<'tcx> {
12761275
| PredicateKind::Clause(ClauseKind::RegionOutlives(..))
12771276
| PredicateKind::Clause(ClauseKind::WellFormed(..))
12781277
| PredicateKind::ObjectSafe(..)
1279-
| PredicateKind::ClosureKind(..)
12801278
| PredicateKind::Clause(ClauseKind::TypeOutlives(..))
12811279
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(..))
12821280
| PredicateKind::ConstEquate(..)
@@ -1296,7 +1294,6 @@ impl<'tcx> Predicate<'tcx> {
12961294
| PredicateKind::Clause(ClauseKind::RegionOutlives(..))
12971295
| PredicateKind::Clause(ClauseKind::WellFormed(..))
12981296
| PredicateKind::ObjectSafe(..)
1299-
| PredicateKind::ClosureKind(..)
13001297
| PredicateKind::Clause(ClauseKind::TypeOutlives(..))
13011298
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(..))
13021299
| PredicateKind::ConstEquate(..)
@@ -1317,7 +1314,6 @@ impl<'tcx> Predicate<'tcx> {
13171314
| PredicateKind::Clause(ClauseKind::RegionOutlives(..))
13181315
| PredicateKind::Clause(ClauseKind::WellFormed(..))
13191316
| PredicateKind::ObjectSafe(..)
1320-
| PredicateKind::ClosureKind(..)
13211317
| PredicateKind::Clause(ClauseKind::ConstEvaluatable(..))
13221318
| PredicateKind::ConstEquate(..)
13231319
| PredicateKind::Ambiguous => None,

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

-5
Original file line numberDiff line numberDiff line change
@@ -2781,11 +2781,6 @@ define_print! {
27812781
ty::PredicateKind::ObjectSafe(trait_def_id) => {
27822782
p!("the trait `", print_def_path(trait_def_id, &[]), "` is object-safe")
27832783
}
2784-
ty::PredicateKind::ClosureKind(closure_def_id, _closure_args, kind) => p!(
2785-
"the closure `",
2786-
print_value_path(closure_def_id, &[]),
2787-
write("` implements the trait `{}`", kind)
2788-
),
27892784
ty::PredicateKind::ConstEquate(c1, c2) => {
27902785
p!("the constant `", print(c1), "` equals `", print(c2), "`")
27912786
}

compiler/rustc_smir/src/rustc_smir/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1699,13 +1699,6 @@ impl<'tcx> Stable<'tcx> for ty::PredicateKind<'tcx> {
16991699
PredicateKind::ObjectSafe(did) => {
17001700
stable_mir::ty::PredicateKind::ObjectSafe(tables.trait_def(*did))
17011701
}
1702-
PredicateKind::ClosureKind(did, generic_args, closure_kind) => {
1703-
stable_mir::ty::PredicateKind::ClosureKind(
1704-
tables.closure_def(*did),
1705-
generic_args.stable(tables),
1706-
closure_kind.stable(tables),
1707-
)
1708-
}
17091702
PredicateKind::Subtype(subtype_predicate) => {
17101703
stable_mir::ty::PredicateKind::SubType(subtype_predicate.stable(tables))
17111704
}

compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
406406
ty::PredicateKind::Coerce(predicate) => {
407407
self.compute_coerce_goal(Goal { param_env, predicate })
408408
}
409-
ty::PredicateKind::ClosureKind(def_id, args, kind) => self
410-
.compute_closure_kind_goal(Goal { param_env, predicate: (def_id, args, kind) }),
411409
ty::PredicateKind::ObjectSafe(trait_def_id) => {
412410
self.compute_object_safe_goal(trait_def_id)
413411
}

compiler/rustc_trait_selection/src/solve/fulfill.rs

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
135135
}
136136
ty::PredicateKind::Clause(_)
137137
| ty::PredicateKind::ObjectSafe(_)
138-
| ty::PredicateKind::ClosureKind(_, _, _)
139138
| ty::PredicateKind::Ambiguous => {
140139
FulfillmentErrorCode::CodeSelectionError(
141140
SelectionError::Unimplemented,

compiler/rustc_trait_selection/src/traits/auto_trait.rs

-1
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ impl<'tcx> AutoTraitFinder<'tcx> {
822822
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
823823
| ty::PredicateKind::AliasRelate(..)
824824
| ty::PredicateKind::ObjectSafe(..)
825-
| ty::PredicateKind::ClosureKind(..)
826825
| ty::PredicateKind::Subtype(..)
827826
// FIXME(generic_const_exprs): you can absolutely add this as a where clauses
828827
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,31 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
411411
ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_predicate)) => {
412412
let trait_predicate = bound_predicate.rebind(trait_predicate);
413413
let trait_predicate = self.resolve_vars_if_possible(trait_predicate);
414+
let trait_ref = trait_predicate.to_poly_trait_ref();
415+
416+
if let ty::Closure(closure_def_id, closure_args) = *trait_ref.self_ty().skip_binder().kind()
417+
&& let Some(expected_kind) = self.tcx.fn_trait_kind_from_def_id(trait_ref.def_id())
418+
&& let Some(found_kind) = self.closure_kind(closure_args)
419+
&& !found_kind.extends(expected_kind)
420+
&& let sig = closure_args.as_closure().sig()
421+
&& self.can_sub(
422+
obligation.param_env,
423+
trait_ref,
424+
sig.map_bound(|sig| {
425+
ty::TraitRef::new(
426+
tcx,
427+
trait_ref.def_id(),
428+
[trait_ref.self_ty().skip_binder(), sig.inputs()[0]],
429+
)
430+
}),
431+
)
432+
{
433+
let mut err = self.report_closure_error(&obligation, closure_def_id, found_kind, expected_kind);
434+
self.note_obligation_cause(&mut err, &obligation);
435+
self.point_at_returns_when_relevant(&mut err, &obligation);
436+
err.emit();
437+
return;
438+
}
414439

415440
// FIXME(effects)
416441
let predicate_is_const = false;
@@ -425,7 +450,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
425450
// reported on the binding definition (#56607).
426451
return;
427452
}
428-
let trait_ref = trait_predicate.to_poly_trait_ref();
429453
let (post_message, pre_message, type_def, file_note) = self
430454
.get_parent_trait_ref(obligation.cause.code())
431455
.map(|(t, s)| {
@@ -786,11 +810,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
786810
report_object_safety_error(self.tcx, span, trait_def_id, violations)
787811
}
788812

789-
ty::PredicateKind::ClosureKind(closure_def_id, closure_args, kind) => {
790-
let found_kind = self.closure_kind(closure_args).unwrap();
791-
self.report_closure_error(&obligation, closure_def_id, found_kind, kind)
792-
}
793-
794813
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty)) => {
795814
let ty = self.resolve_vars_if_possible(ty);
796815
match self.tcx.sess.opts.unstable_opts.trait_solver {

compiler/rustc_trait_selection/src/traits/fulfill.rs

-14
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
350350
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
351351
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_))
352352
| ty::PredicateKind::ObjectSafe(_)
353-
| ty::PredicateKind::ClosureKind(..)
354353
| ty::PredicateKind::Subtype(_)
355354
| ty::PredicateKind::Coerce(_)
356355
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
@@ -411,19 +410,6 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
411410
}
412411
}
413412

414-
ty::PredicateKind::ClosureKind(_, closure_args, kind) => {
415-
match self.selcx.infcx.closure_kind(closure_args) {
416-
Some(closure_kind) => {
417-
if closure_kind.extends(kind) {
418-
ProcessResult::Changed(vec![])
419-
} else {
420-
ProcessResult::Error(CodeSelectionError(Unimplemented))
421-
}
422-
}
423-
None => ProcessResult::Unchanged,
424-
}
425-
}
426-
427413
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
428414
match wf::obligations(
429415
self.selcx.infcx,

compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
130130
| ty::PredicateKind::Subtype(..)
131131
| ty::PredicateKind::Coerce(..)
132132
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
133-
| ty::PredicateKind::ClosureKind(..)
134133
| ty::PredicateKind::ObjectSafe(..)
135134
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
136135
| ty::PredicateKind::ConstEquate(..)

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
291291
}
292292
}
293293
None => {
294-
debug!("assemble_unboxed_candidates: closure_kind not yet known");
295-
candidates.vec.push(ClosureCandidate { is_const });
294+
if kind == ty::ClosureKind::FnOnce {
295+
candidates.vec.push(ClosureCandidate { is_const });
296+
} else {
297+
candidates.ambiguous = true;
298+
}
296299
}
297300
}
298301
}

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
821821
&mut self,
822822
obligation: &PolyTraitObligation<'tcx>,
823823
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
824-
let kind = self
825-
.tcx()
826-
.fn_trait_kind_from_def_id(obligation.predicate.def_id())
827-
.unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation));
828-
829824
// Okay to skip binder because the args on closure types never
830825
// touch bound regions, they just capture the in-scope
831826
// type/region parameters.
@@ -835,15 +830,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
835830
};
836831

837832
let trait_ref = self.closure_trait_ref_unnormalized(obligation, args);
838-
let mut nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
833+
let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
839834

840835
debug!(?closure_def_id, ?trait_ref, ?nested, "confirm closure candidate obligations");
841836

842-
nested.push(obligation.with(
843-
self.tcx(),
844-
ty::Binder::dummy(ty::PredicateKind::ClosureKind(closure_def_id, args, kind)),
845-
));
846-
847837
Ok(nested)
848838
}
849839

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

-13
Original file line numberDiff line numberDiff line change
@@ -885,19 +885,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
885885
}
886886
}
887887

888-
ty::PredicateKind::ClosureKind(_, closure_args, kind) => {
889-
match self.infcx.closure_kind(closure_args) {
890-
Some(closure_kind) => {
891-
if closure_kind.extends(kind) {
892-
Ok(EvaluatedToOk)
893-
} else {
894-
Ok(EvaluatedToErr)
895-
}
896-
}
897-
None => Ok(EvaluatedToAmbig),
898-
}
899-
}
900-
901888
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(uv)) => {
902889
match const_evaluatable::is_const_evaluatable(
903890
self.infcx,

compiler/rustc_traits/src/normalize_erasing_regions.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ fn not_outlives_predicate(p: ty::Predicate<'_>) -> bool {
5858
| ty::PredicateKind::AliasRelate(..)
5959
| ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
6060
| ty::PredicateKind::ObjectSafe(..)
61-
| ty::PredicateKind::ClosureKind(..)
6261
| ty::PredicateKind::Subtype(..)
6362
| ty::PredicateKind::Coerce(..)
6463
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))

compiler/rustc_type_ir/src/predicate_kind.rs

+5-32
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ pub enum PredicateKind<I: Interner> {
172172
/// Trait must be object-safe.
173173
ObjectSafe(I::DefId),
174174

175-
/// No direct syntax. May be thought of as `where T: FnFoo<...>`
176-
/// for some generic args `...` and `T` being a closure type.
177-
/// Satisfied (or refuted) once we know the closure's kind.
178-
ClosureKind(I::DefId, I::GenericArgs, I::ClosureKind),
179-
180175
/// `T1 <: T2`
181176
///
182177
/// This obligation is created most often when we have two
@@ -226,9 +221,6 @@ impl<I: Interner> PartialEq for PredicateKind<I> {
226221
match (self, other) {
227222
(Self::Clause(l0), Self::Clause(r0)) => l0 == r0,
228223
(Self::ObjectSafe(l0), Self::ObjectSafe(r0)) => l0 == r0,
229-
(Self::ClosureKind(l0, l1, l2), Self::ClosureKind(r0, r1, r2)) => {
230-
l0 == r0 && l1 == r1 && l2 == r2
231-
}
232224
(Self::Subtype(l0), Self::Subtype(r0)) => l0 == r0,
233225
(Self::Coerce(l0), Self::Coerce(r0)) => l0 == r0,
234226
(Self::ConstEquate(l0, l1), Self::ConstEquate(r0, r1)) => l0 == r0 && l1 == r1,
@@ -247,12 +239,11 @@ fn predicate_kind_discriminant<I: Interner>(value: &PredicateKind<I>) -> usize {
247239
match value {
248240
PredicateKind::Clause(_) => 0,
249241
PredicateKind::ObjectSafe(_) => 1,
250-
PredicateKind::ClosureKind(_, _, _) => 2,
251-
PredicateKind::Subtype(_) => 3,
252-
PredicateKind::Coerce(_) => 4,
253-
PredicateKind::ConstEquate(_, _) => 5,
254-
PredicateKind::Ambiguous => 6,
255-
PredicateKind::AliasRelate(_, _, _) => 7,
242+
PredicateKind::Subtype(_) => 2,
243+
PredicateKind::Coerce(_) => 3,
244+
PredicateKind::ConstEquate(_, _) => 4,
245+
PredicateKind::Ambiguous => 5,
246+
PredicateKind::AliasRelate(_, _, _) => 6,
256247
}
257248
}
258249

@@ -273,11 +264,6 @@ where
273264
match self {
274265
PredicateKind::Clause(p) => p.hash_stable(hcx, hasher),
275266
PredicateKind::ObjectSafe(d) => d.hash_stable(hcx, hasher),
276-
PredicateKind::ClosureKind(d, g, k) => {
277-
d.hash_stable(hcx, hasher);
278-
g.hash_stable(hcx, hasher);
279-
k.hash_stable(hcx, hasher);
280-
}
281267
PredicateKind::Subtype(p) => p.hash_stable(hcx, hasher),
282268
PredicateKind::Coerce(p) => p.hash_stable(hcx, hasher),
283269
PredicateKind::ConstEquate(c1, c2) => {
@@ -309,11 +295,6 @@ where
309295
Ok(match self {
310296
PredicateKind::Clause(c) => PredicateKind::Clause(c.try_fold_with(folder)?),
311297
PredicateKind::ObjectSafe(d) => PredicateKind::ObjectSafe(d.try_fold_with(folder)?),
312-
PredicateKind::ClosureKind(d, g, k) => PredicateKind::ClosureKind(
313-
d.try_fold_with(folder)?,
314-
g.try_fold_with(folder)?,
315-
k.try_fold_with(folder)?,
316-
),
317298
PredicateKind::Subtype(s) => PredicateKind::Subtype(s.try_fold_with(folder)?),
318299
PredicateKind::Coerce(s) => PredicateKind::Coerce(s.try_fold_with(folder)?),
319300
PredicateKind::ConstEquate(a, b) => {
@@ -344,11 +325,6 @@ where
344325
match self {
345326
PredicateKind::Clause(p) => p.visit_with(visitor),
346327
PredicateKind::ObjectSafe(d) => d.visit_with(visitor),
347-
PredicateKind::ClosureKind(d, g, k) => {
348-
d.visit_with(visitor)?;
349-
g.visit_with(visitor)?;
350-
k.visit_with(visitor)
351-
}
352328
PredicateKind::Subtype(s) => s.visit_with(visitor),
353329
PredicateKind::Coerce(s) => s.visit_with(visitor),
354330
PredicateKind::ConstEquate(a, b) => {
@@ -408,9 +384,6 @@ impl<I: Interner> fmt::Debug for PredicateKind<I> {
408384
PredicateKind::ObjectSafe(trait_def_id) => {
409385
write!(f, "ObjectSafe({trait_def_id:?})")
410386
}
411-
PredicateKind::ClosureKind(closure_def_id, closure_args, kind) => {
412-
write!(f, "ClosureKind({closure_def_id:?}, {closure_args:?}, {kind:?})")
413-
}
414387
PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({c1:?}, {c2:?})"),
415388
PredicateKind::Ambiguous => write!(f, "Ambiguous"),
416389
PredicateKind::AliasRelate(t1, t2, dir) => {

0 commit comments

Comments
 (0)