Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cb024ba

Browse files
is_closure_like
1 parent 899c895 commit cb024ba

File tree

17 files changed

+40
-37
lines changed

17 files changed

+40
-37
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,7 +3161,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
31613161
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
31623162
// Define a fallback for when we can't match a closure.
31633163
let fallback = || {
3164-
let is_closure = self.infcx.tcx.is_closure_or_coroutine(self.mir_def_id().to_def_id());
3164+
let is_closure = self.infcx.tcx.is_closure_like(self.mir_def_id().to_def_id());
31653165
if is_closure {
31663166
None
31673167
} else {
@@ -3372,7 +3372,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
33723372
sig: ty::PolyFnSig<'tcx>,
33733373
) -> Option<AnnotatedBorrowFnSignature<'tcx>> {
33743374
debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig);
3375-
let is_closure = self.infcx.tcx.is_closure_or_coroutine(did.to_def_id());
3375+
let is_closure = self.infcx.tcx.is_closure_like(did.to_def_id());
33763376
let fn_hir_id = self.infcx.tcx.local_def_id_to_hir_id(did);
33773377
let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?;
33783378

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
6767
local,
6868
projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
6969
} => {
70-
debug_assert!(is_closure_or_coroutine(
70+
debug_assert!(is_closure_like(
7171
Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
7272
));
7373

@@ -126,9 +126,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
126126
{
127127
item_msg = access_place_desc;
128128
debug_assert!(self.body.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty.is_ref());
129-
debug_assert!(is_closure_or_coroutine(
130-
the_place_err.ty(self.body, self.infcx.tcx).ty
131-
));
129+
debug_assert!(is_closure_like(the_place_err.ty(self.body, self.infcx.tcx).ty));
132130

133131
reason = if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
134132
", as it is a captured variable in a `Fn` closure".to_string()
@@ -389,7 +387,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
389387
local,
390388
projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
391389
} => {
392-
debug_assert!(is_closure_or_coroutine(
390+
debug_assert!(is_closure_like(
393391
Place::ty_from(local, proj_base, self.body, self.infcx.tcx).ty
394392
));
395393

@@ -1474,7 +1472,8 @@ fn suggest_ampmut<'tcx>(
14741472
}
14751473
}
14761474

1477-
fn is_closure_or_coroutine(ty: Ty<'_>) -> bool {
1475+
/// If the type is a `Coroutine`, `Closure`, or `CoroutineClosure`
1476+
fn is_closure_like(ty: Ty<'_>) -> bool {
14781477
ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure()
14791478
}
14801479

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
2929
pub(super) fn check_signature_annotation(&mut self, body: &Body<'tcx>) {
3030
let mir_def_id = body.source.def_id().expect_local();
3131

32-
if !self.tcx().is_closure_or_coroutine(mir_def_id.to_def_id()) {
32+
if !self.tcx().is_closure_like(mir_def_id.to_def_id()) {
3333
return;
3434
}
3535

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
479479
// `+multivalue` feature because the purpose of the wasm abi is to match
480480
// the WebAssembly specification, which has this feature. This won't be
481481
// needed when LLVM enables this `multivalue` feature by default.
482-
if !cx.tcx.is_closure_or_coroutine(instance.def_id()) {
482+
if !cx.tcx.is_closure_like(instance.def_id()) {
483483
let abi = cx.tcx.fn_sig(instance.def_id()).skip_binder().abi();
484484
if abi == Abi::Wasm {
485485
function_features.push("+multivalue".to_string());

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
229229
}
230230
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
231231
sym::track_caller => {
232-
let is_closure = tcx.is_closure_or_coroutine(did.to_def_id());
232+
let is_closure = tcx.is_closure_like(did.to_def_id());
233233

234234
if !is_closure
235235
&& let Some(fn_sig) = fn_sig()
@@ -274,7 +274,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
274274
}
275275
}
276276
sym::target_feature => {
277-
if !tcx.is_closure_or_coroutine(did.to_def_id())
277+
if !tcx.is_closure_like(did.to_def_id())
278278
&& let Some(fn_sig) = fn_sig()
279279
&& fn_sig.skip_binder().unsafety() == hir::Unsafety::Normal
280280
{
@@ -529,7 +529,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
529529
// would result in this closure being compiled without the inherited target features, but this
530530
// is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
531531
if tcx.features().target_feature_11
532-
&& tcx.is_closure_or_coroutine(did.to_def_id())
532+
&& tcx.is_closure_like(did.to_def_id())
533533
&& codegen_fn_attrs.inline != InlineAttr::Always
534534
{
535535
let owner_id = tcx.parent(did.to_def_id());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
7272

7373
pub fn fn_sig(&self) -> PolyFnSig<'tcx> {
7474
let did = self.def_id().to_def_id();
75-
if self.tcx.is_closure_or_coroutine(did) {
75+
if self.tcx.is_closure_like(did) {
7676
let ty = self.tcx.type_of(did).instantiate_identity();
7777
let ty::Closure(_, args) = ty.kind() else { bug!("type_of closure not ty::Closure") };
7878
args.as_closure().sig()

compiler/rustc_hir/src/def.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ pub enum DefKind {
115115
Impl {
116116
of_trait: bool,
117117
},
118+
/// A closure, coroutine, or coroutine-closure.
119+
///
120+
/// These are all represented with the same `ExprKind::Closure` in the AST and HIR,
121+
/// which makes it difficult to distinguish these during def collection. Therefore,
122+
/// we treat them all the same, and code which needs to distinguish them can match
123+
/// or `hir::ClosureKind` or `type_of`.
118124
Closure,
119125
}
120126

compiler/rustc_hir_typeck/src/check.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ pub(super) fn check_fn<'a, 'tcx>(
9090
// ty.span == binding_span iff this is a closure parameter with no type ascription,
9191
// or if it's an implicit `self` parameter
9292
traits::SizedArgumentType(
93-
if ty_span == Some(param.span) && tcx.is_closure_or_coroutine(fn_def_id.into())
94-
{
93+
if ty_span == Some(param.span) && tcx.is_closure_like(fn_def_id.into()) {
9594
None
9695
} else {
9796
ty.map(|ty| ty.hir_id)

compiler/rustc_hir_typeck/src/gather_locals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
153153
// ascription, or if it's an implicit `self` parameter
154154
traits::SizedArgumentType(
155155
if ty_span == ident.span
156-
&& self.fcx.tcx.is_closure_or_coroutine(self.fcx.body_id.into())
156+
&& self.fcx.tcx.is_closure_like(self.fcx.body_id.into())
157157
{
158158
None
159159
} else {

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
491491
let kind = tcx.def_kind(def_id);
492492
let is_function = match kind {
493493
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..) => true,
494-
_ => tcx.is_closure_or_coroutine(def_id),
494+
_ => tcx.is_closure_like(def_id),
495495
};
496496
match (kind, body.source.promoted) {
497497
(_, Some(i)) => write!(w, "{i:?} in ")?,

0 commit comments

Comments
 (0)