Skip to content

Commit cca2bda

Browse files
committed
Auto merge of rust-lang#118966 - matthiaskrgr:rollup-sdvjwy6, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - rust-lang#116888 (Add discussion that concurrent access to the environment is unsafe) - rust-lang#118888 (Uplift `TypeAndMut` and `ClosureKind` to `rustc_type_ir`) - rust-lang#118929 (coverage: Tidy up early parts of the instrumentor pass) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1559dd2 + 6659b5e commit cca2bda

File tree

19 files changed

+255
-192
lines changed

19 files changed

+255
-192
lines changed

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,9 @@ pub struct IndicateAnonymousLifetime {
378378
pub count: usize,
379379
pub suggestion: String,
380380
}
381+
382+
impl IntoDiagnosticArg for type_ir::ClosureKind {
383+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
384+
DiagnosticArgValue::Str(self.as_str().into())
385+
}
386+
}

compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
141141
debug!(?sig, ?opt_kind);
142142

143143
let closure_kind_ty = match opt_kind {
144-
Some(kind) => kind.to_ty(self.tcx),
144+
Some(kind) => Ty::from_closure_kind(self.tcx, kind),
145145

146146
// Create a type variable (for now) to represent the closure kind.
147147
// It will be unified during the upvar inference phase (`upvar.rs`)

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20182018
let new_def_id = self.probe(|_| {
20192019
let trait_ref = ty::TraitRef::new(
20202020
self.tcx,
2021-
call_kind.to_def_id(self.tcx),
2021+
self.tcx.fn_trait_kind_to_def_id(call_kind)?,
20222022
[
20232023
callee_ty,
20242024
self.next_ty_var(TypeVariableOrigin {

compiler/rustc_hir_typeck/src/upvar.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
261261
// Unify the (as yet unbound) type variable in the closure
262262
// args with the kind we inferred.
263263
let closure_kind_ty = closure_args.as_closure().kind_ty();
264-
self.demand_eqtype(span, closure_kind.to_ty(self.tcx), closure_kind_ty);
264+
self.demand_eqtype(
265+
span,
266+
Ty::from_closure_kind(self.tcx, closure_kind),
267+
closure_kind_ty,
268+
);
265269

266270
// If we have an origin, store it.
267271
if let Some(mut origin) = origin {

compiler/rustc_middle/src/middle/lang_items.rs

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ impl<'tcx> TyCtxt<'tcx> {
3636
}
3737
}
3838

39+
/// Given a [`ty::ClosureKind`], get the [`DefId`] of its corresponding `Fn`-family
40+
/// trait, if it is defined.
41+
pub fn fn_trait_kind_to_def_id(self, kind: ty::ClosureKind) -> Option<DefId> {
42+
let items = self.lang_items();
43+
match kind {
44+
ty::ClosureKind::Fn => items.fn_trait(),
45+
ty::ClosureKind::FnMut => items.fn_mut_trait(),
46+
ty::ClosureKind::FnOnce => items.fn_once_trait(),
47+
}
48+
}
49+
3950
/// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits.
4051
pub fn is_fn_trait(self, id: DefId) -> bool {
4152
self.fn_trait_kind_from_def_id(id).is_some()

compiler/rustc_middle/src/mir/type_foldable.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ TrivialTypeTraversalImpls! {
1515
UserTypeAnnotationIndex,
1616
BorrowKind,
1717
CastKind,
18-
hir::Movability,
1918
BasicBlock,
2019
SwitchTargets,
2120
CoroutineKind,

compiler/rustc_middle/src/ty/closure.rs

+3-70
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ use std::fmt::Write;
77

88
use crate::query::Providers;
99
use rustc_data_structures::fx::FxIndexMap;
10-
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
11-
use rustc_hir::def_id::{DefId, LocalDefId};
12-
use rustc_hir::{self as hir, LangItem};
10+
use rustc_hir as hir;
11+
use rustc_hir::def_id::LocalDefId;
1312
use rustc_span::def_id::LocalDefIdMap;
1413
use rustc_span::symbol::Ident;
1514
use rustc_span::{Span, Symbol};
1615

17-
use super::{Ty, TyCtxt};
16+
use super::TyCtxt;
1817

1918
use self::BorrowKind::*;
2019

@@ -73,72 +72,6 @@ pub type RootVariableMinCaptureList<'tcx> = FxIndexMap<hir::HirId, MinCaptureLis
7372
/// Part of `MinCaptureInformationMap`; List of `CapturePlace`s.
7473
pub type MinCaptureList<'tcx> = Vec<CapturedPlace<'tcx>>;
7574

76-
/// Represents the various closure traits in the language. This
77-
/// will determine the type of the environment (`self`, in the
78-
/// desugaring) argument that the closure expects.
79-
///
80-
/// You can get the environment type of a closure using
81-
/// `tcx.closure_env_ty()`.
82-
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
83-
#[derive(HashStable)]
84-
pub enum ClosureKind {
85-
// Warning: Ordering is significant here! The ordering is chosen
86-
// because the trait Fn is a subtrait of FnMut and so in turn, and
87-
// hence we order it so that Fn < FnMut < FnOnce.
88-
Fn,
89-
FnMut,
90-
FnOnce,
91-
}
92-
93-
impl ClosureKind {
94-
/// This is the initial value used when doing upvar inference.
95-
pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn;
96-
97-
pub const fn as_str(self) -> &'static str {
98-
match self {
99-
ClosureKind::Fn => "Fn",
100-
ClosureKind::FnMut => "FnMut",
101-
ClosureKind::FnOnce => "FnOnce",
102-
}
103-
}
104-
105-
/// Returns `true` if a type that impls this closure kind
106-
/// must also implement `other`.
107-
pub fn extends(self, other: ty::ClosureKind) -> bool {
108-
self <= other
109-
}
110-
111-
/// Converts `self` to a [`DefId`] of the corresponding trait.
112-
///
113-
/// Note: the inverse of this function is [`TyCtxt::fn_trait_kind_from_def_id`].
114-
pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
115-
tcx.require_lang_item(
116-
match self {
117-
ClosureKind::Fn => LangItem::Fn,
118-
ClosureKind::FnMut => LangItem::FnMut,
119-
ClosureKind::FnOnce => LangItem::FnOnce,
120-
},
121-
None,
122-
)
123-
}
124-
125-
/// Returns the representative scalar type for this closure kind.
126-
/// See `Ty::to_opt_closure_kind` for more details.
127-
pub fn to_ty<'tcx>(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
128-
match self {
129-
ClosureKind::Fn => tcx.types.i8,
130-
ClosureKind::FnMut => tcx.types.i16,
131-
ClosureKind::FnOnce => tcx.types.i32,
132-
}
133-
}
134-
}
135-
136-
impl IntoDiagnosticArg for ClosureKind {
137-
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
138-
DiagnosticArgValue::Str(self.as_str().into())
139-
}
140-
}
141-
14275
/// A composite describing a `Place` that is captured by a closure.
14376
#[derive(PartialEq, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
14477
#[derive(TypeFoldable, TypeVisitable)]

compiler/rustc_middle/src/ty/context.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
2929
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
3030
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31-
TypeAndMut, Visibility,
31+
Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -88,7 +88,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
8888
type Term = ty::Term<'tcx>;
8989

9090
type Binder<T> = Binder<'tcx, T>;
91-
type TypeAndMut = TypeAndMut<'tcx>;
9291
type CanonicalVars = CanonicalVarInfos<'tcx>;
9392

9493
type Ty = Ty<'tcx>;
@@ -128,12 +127,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
128127
type CoercePredicate = ty::CoercePredicate<'tcx>;
129128
type ClosureKind = ty::ClosureKind;
130129

131-
fn ty_and_mut_to_parts(
132-
TypeAndMut { ty, mutbl }: TypeAndMut<'tcx>,
133-
) -> (Self::Ty, ty::Mutability) {
134-
(ty, mutbl)
135-
}
136-
137130
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
138131
self.mk_canonical_var_infos(infos)
139132
}

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub use self::binding::BindingMode;
7575
pub use self::binding::BindingMode::*;
7676
pub use self::closure::{
7777
is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo,
78-
CapturedPlace, ClosureKind, ClosureTypeInfo, MinCaptureInformationMap, MinCaptureList,
78+
CapturedPlace, ClosureTypeInfo, MinCaptureInformationMap, MinCaptureList,
7979
RootVariableMinCaptureList, UpvarCapture, UpvarId, UpvarPath, CAPTURE_STRUCT_LOCAL,
8080
};
8181
pub use self::consts::{Const, ConstData, ConstInt, Expr, ScalarInt, UnevaluatedConst, ValTree};

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
16801680
self.wrap_binder(&sig, |sig, cx| {
16811681
define_scoped_cx!(cx);
16821682

1683-
p!(print(kind), "(");
1683+
p!(write("{kind}("));
16841684
for (i, arg) in sig.inputs()[0].tuple_fields().iter().enumerate() {
16851685
if i > 0 {
16861686
p!(", ");
@@ -2754,6 +2754,10 @@ forward_display_to_print! {
27542754
define_print! {
27552755
(self, cx):
27562756

2757+
ty::TypeAndMut<'tcx> {
2758+
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
2759+
}
2760+
27572761
ty::ClauseKind<'tcx> {
27582762
match *self {
27592763
ty::ClauseKind::Trait(ref data) => {
@@ -2799,10 +2803,6 @@ define_print_and_forward_display! {
27992803
p!("{{", comma_sep(self.iter()), "}}")
28002804
}
28012805

2802-
ty::TypeAndMut<'tcx> {
2803-
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
2804-
}
2805-
28062806
ty::ExistentialTraitRef<'tcx> {
28072807
// Use a type that can't appear in defaults of type parameters.
28082808
let dummy_self = Ty::new_fresh(cx.tcx(),0);
@@ -2943,10 +2943,6 @@ define_print_and_forward_display! {
29432943
}
29442944
}
29452945

2946-
ty::ClosureKind {
2947-
p!(write("{}", self.as_str()))
2948-
}
2949-
29502946
ty::Predicate<'tcx> {
29512947
p!(print(self.kind()))
29522948
}

compiler/rustc_middle/src/ty/structural_impls.rs

-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ TrivialTypeTraversalImpls! {
440440
// interners).
441441
TrivialTypeTraversalAndLiftImpls! {
442442
::rustc_hir::def_id::DefId,
443-
::rustc_hir::Mutability,
444443
::rustc_hir::Unsafety,
445444
::rustc_target::spec::abi::Abi,
446445
crate::ty::ClosureKind,

compiler/rustc_middle/src/ty/sty.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,18 @@ use rustc_type_ir::PredicateKind as IrPredicateKind;
4444
use rustc_type_ir::RegionKind as IrRegionKind;
4545
use rustc_type_ir::TyKind as IrTyKind;
4646
use rustc_type_ir::TyKind::*;
47+
use rustc_type_ir::TypeAndMut as IrTypeAndMut;
4748

4849
use super::GenericParamDefKind;
4950

50-
// Re-export the `TyKind` from `rustc_type_ir` here for convenience
51+
// Re-export and re-parameterize some `I = TyCtxt<'tcx>` types here
5152
#[rustc_diagnostic_item = "TyKind"]
5253
pub type TyKind<'tcx> = IrTyKind<TyCtxt<'tcx>>;
5354
pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
5455
pub type ConstKind<'tcx> = IrConstKind<TyCtxt<'tcx>>;
5556
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
5657
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
57-
58-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
59-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
60-
pub struct TypeAndMut<'tcx> {
61-
pub ty: Ty<'tcx>,
62-
pub mutbl: hir::Mutability,
63-
}
58+
pub type TypeAndMut<'tcx> = IrTypeAndMut<TyCtxt<'tcx>>;
6459

6560
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, TyEncodable, TyDecodable, Copy)]
6661
#[derive(HashStable)]
@@ -2818,6 +2813,15 @@ impl<'tcx> Ty<'tcx> {
28182813
}
28192814
}
28202815

2816+
/// Inverse of [`Ty::to_opt_closure_kind`].
2817+
pub fn from_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
2818+
match kind {
2819+
ty::ClosureKind::Fn => tcx.types.i8,
2820+
ty::ClosureKind::FnMut => tcx.types.i16,
2821+
ty::ClosureKind::FnOnce => tcx.types.i32,
2822+
}
2823+
}
2824+
28212825
/// Fast path helper for testing if a type is `Sized`.
28222826
///
28232827
/// Returning true means the type is known to be sized. Returning

0 commit comments

Comments
 (0)