Skip to content

Commit eb4cd4e

Browse files
authored
Unrolled build for rust-lang#122237
Rollup merge of rust-lang#122237 - fee1-dead-contrib:rmord, r=compiler-errors Remove `Ord` from `ClosureKind` Using `Ord` to accomplish a meaning of subset relationship can be hard to read. The existing uses for that are easily replaced with a `match`, and in my opinion, more readable without needing to resorting to comments to explain the intention. cc `@compiler-errors`
2 parents b054da8 + 7e1969a commit eb4cd4e

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_target::spec::abi::Abi;
1919
use rustc_trait_selection::traits;
2020
use rustc_trait_selection::traits::error_reporting::ArgKind;
2121
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
22-
use std::cmp;
22+
use rustc_type_ir::ClosureKind;
2323
use std::iter;
2424
use std::ops::ControlFlow;
2525

@@ -437,10 +437,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
437437
};
438438

439439
if let Some(found_kind) = found_kind {
440-
expected_kind = Some(
441-
expected_kind
442-
.map_or_else(|| found_kind, |current| cmp::min(current, found_kind)),
443-
);
440+
// always use the closure kind that is more permissive.
441+
match (expected_kind, found_kind) {
442+
(None, _) => expected_kind = Some(found_kind),
443+
(Some(ClosureKind::FnMut), ClosureKind::Fn) => {
444+
expected_kind = Some(ClosureKind::Fn)
445+
}
446+
(Some(ClosureKind::FnOnce), ClosureKind::Fn | ClosureKind::FnMut) => {
447+
expected_kind = Some(found_kind)
448+
}
449+
_ => {}
450+
}
444451
}
445452
}
446453
}

compiler/rustc_middle/src/ty/instance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Instance<'tcx> {
3030
pub args: GenericArgsRef<'tcx>,
3131
}
3232

33-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
33+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
3434
#[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable, Lift)]
3535
pub enum InstanceDef<'tcx> {
3636
/// A user-defined callable item.

compiler/rustc_ty_utils/src/instance.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::ty::GenericArgsRef;
77
use rustc_middle::ty::{self, Instance, TyCtxt, TypeVisitableExt};
88
use rustc_span::sym;
99
use rustc_trait_selection::traits;
10+
use rustc_type_ir::ClosureKind;
1011
use traits::{translate_args, Reveal};
1112

1213
use crate::errors::UnexpectedFnPtrAssociatedItem;
@@ -296,23 +297,25 @@ fn resolve_associated_item<'tcx>(
296297
{
297298
match *rcvr_args.type_at(0).kind() {
298299
ty::CoroutineClosure(coroutine_closure_def_id, args) => {
299-
// If we're computing `AsyncFnOnce`/`AsyncFnMut` for a by-ref closure,
300-
// or `AsyncFnOnce` for a by-mut closure, then construct a new body that
301-
// has the right return types.
302-
//
303-
// Specifically, `AsyncFnMut` for a by-ref coroutine-closure just needs
304-
// to have its input and output types fixed (`&mut self` and returning
305-
// `i16` coroutine kind).
306-
if target_kind > args.as_coroutine_closure().kind() {
307-
Some(Instance {
308-
def: ty::InstanceDef::ConstructCoroutineInClosureShim {
309-
coroutine_closure_def_id,
310-
target_kind,
311-
},
312-
args,
313-
})
314-
} else {
315-
Some(Instance::new(coroutine_closure_def_id, args))
300+
match (target_kind, args.as_coroutine_closure().kind()) {
301+
(ClosureKind::FnOnce | ClosureKind::FnMut, ClosureKind::Fn)
302+
| (ClosureKind::FnOnce, ClosureKind::FnMut) => {
303+
// If we're computing `AsyncFnOnce`/`AsyncFnMut` for a by-ref closure,
304+
// or `AsyncFnOnce` for a by-mut closure, then construct a new body that
305+
// has the right return types.
306+
//
307+
// Specifically, `AsyncFnMut` for a by-ref coroutine-closure just needs
308+
// to have its input and output types fixed (`&mut self` and returning
309+
// `i16` coroutine kind).
310+
Some(Instance {
311+
def: ty::InstanceDef::ConstructCoroutineInClosureShim {
312+
coroutine_closure_def_id,
313+
target_kind,
314+
},
315+
args,
316+
})
317+
}
318+
_ => Some(Instance::new(coroutine_closure_def_id, args)),
316319
}
317320
}
318321
ty::Closure(closure_def_id, args) => {

compiler/rustc_type_ir/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,9 @@ rustc_index::newtype_index! {
369369
///
370370
/// You can get the environment type of a closure using
371371
/// `tcx.closure_env_ty()`.
372-
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
372+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
373373
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))]
374374
pub enum ClosureKind {
375-
// Warning: Ordering is significant here! The ordering is chosen
376-
// because the trait Fn is a subtrait of FnMut and so in turn, and
377-
// hence we order it so that Fn < FnMut < FnOnce.
378375
Fn,
379376
FnMut,
380377
FnOnce,
@@ -394,8 +391,15 @@ impl ClosureKind {
394391

395392
/// Returns `true` if a type that impls this closure kind
396393
/// must also implement `other`.
394+
#[rustfmt::skip]
397395
pub fn extends(self, other: ClosureKind) -> bool {
398-
self <= other
396+
use ClosureKind::*;
397+
match (self, other) {
398+
(Fn, Fn | FnMut | FnOnce)
399+
| (FnMut, FnMut | FnOnce)
400+
| (FnOnce, FnOnce) => true,
401+
_ => false,
402+
}
399403
}
400404
}
401405

0 commit comments

Comments
 (0)