Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c4b1054
Move PointIndex to mir_dataflow.
cjgillot Dec 3, 2023
da235ce
Do not recompute liveness for DestinationPropagation.
cjgillot Dec 3, 2023
3082028
Fix comment.
cjgillot Dec 3, 2023
1d6723a
Use for_each instead of fold.
cjgillot Jan 7, 2024
adce3fd
Enable Static Builds for FreeBSD
rellerreller Jan 11, 2024
7df054b
Change return type of unstable `Waker::noop()` from `Waker` to `&Waker`.
kpreid Jan 15, 2024
219b00d
Remove unnecessary `let`s and borrowing from `Waker::noop()` usage.
kpreid Jan 15, 2024
9a8f117
Don't create the array type twice
oli-obk Jan 15, 2024
7052188
Document why `Waker::noop()`'s implementation is the shape it is.
kpreid Jan 15, 2024
ee370a1
Consistently unset RUSTC_BOOTSTRAP when compiling bootstrap
dtolnay Jan 15, 2024
3599c18
Skip dead code checks on items that failed typeck
oli-obk Jan 16, 2024
450cb5e
Don't ICE if TAIT-defining fn contains a closure with `_` in return type
Jan 14, 2024
22833c1
add test for non-defining use of TAIT in foreign function item
Jan 15, 2024
e12101c
Fix `rustc_abi` build on stable
Nadrieril Jan 16, 2024
37a5464
Eagerly instantiate closure ty
compiler-errors Jan 14, 2024
19d6f06
Don't rely on contiguous `VariantId`s outside of rustc
Nadrieril Jan 17, 2024
db7125f
Fix typo in comments (in_place_collect)
Storyyeller Jan 17, 2024
d6b99b9
Use FnOnceOutput instead of FnOnce where expected
oli-obk Jan 17, 2024
9f1b801
Rollup merge of #115291 - cjgillot:dest-prop-save, r=JakobDegen
matthiaskrgr Jan 17, 2024
c533a26
Rollup merge of #119855 - rellerreller:freebsd-static, r=wesleywiser
matthiaskrgr Jan 17, 2024
1a5cae1
Rollup merge of #119975 - lukas-code:inferring-return-types-and-opaqu…
matthiaskrgr Jan 17, 2024
5da8243
Rollup merge of #119984 - kpreid:waker-noop, r=dtolnay
matthiaskrgr Jan 17, 2024
6fadbcc
Rollup merge of #120001 - dtolnay:bootstrapbootstrap, r=onur-ozkan
matthiaskrgr Jan 17, 2024
4721d75
Rollup merge of #120020 - oli-obk:long_const_eval_err_taint, r=compil…
matthiaskrgr Jan 17, 2024
0ab16f9
Rollup merge of #120031 - compiler-errors:construct-closure-ty-eagerl…
matthiaskrgr Jan 17, 2024
16feb45
Rollup merge of #120032 - Nadrieril:fix-rustc_abi, r=Nilstrieb
matthiaskrgr Jan 17, 2024
0f21eba
Rollup merge of #120039 - Nadrieril:remove-idx, r=compiler-errors
matthiaskrgr Jan 17, 2024
42ccea6
Rollup merge of #120044 - Storyyeller:patch-2, r=lqd
matthiaskrgr Jan 17, 2024
e03780f
Rollup merge of #120056 - oli-obk:arg_mismatch_ice, r=compiler-errors
matthiaskrgr Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 70 additions & 76 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,58 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

debug!(?bound_sig, ?liberated_sig);

let parent_args =
GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id(expr_def_id.to_def_id()));

let tupled_upvars_ty = self.next_root_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::ClosureSynthetic,
span: expr_span,
});

// FIXME: We could probably actually just unify this further --
// instead of having a `FnSig` and a `Option<CoroutineTypes>`,
// we can have a `ClosureSignature { Coroutine { .. }, Closure { .. } }`,
// similar to how `ty::GenSig` is a distinct data structure.
let coroutine_types = match closure.kind {
hir::ClosureKind::Closure => None,
let (closure_ty, coroutine_types) = match closure.kind {
hir::ClosureKind::Closure => {
// Tuple up the arguments and insert the resulting function type into
// the `closures` table.
let sig = bound_sig.map_bound(|sig| {
tcx.mk_fn_sig(
[Ty::new_tup(tcx, sig.inputs())],
sig.output(),
sig.c_variadic,
sig.unsafety,
sig.abi,
)
});

debug!(?sig, ?expected_kind);

let closure_kind_ty = match expected_kind {
Some(kind) => Ty::from_closure_kind(tcx, kind),

// Create a type variable (for now) to represent the closure kind.
// It will be unified during the upvar inference phase (`upvar.rs`)
None => self.next_root_ty_var(TypeVariableOrigin {
// FIXME(eddyb) distinguish closure kind inference variables from the rest.
kind: TypeVariableOriginKind::ClosureSynthetic,
span: expr_span,
}),
};

let closure_args = ty::ClosureArgs::new(
tcx,
ty::ClosureArgsParts {
parent_args,
closure_kind_ty,
closure_sig_as_fn_ptr_ty: Ty::new_fn_ptr(tcx, sig),
tupled_upvars_ty,
},
);

(Ty::new_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
}
hir::ClosureKind::Coroutine(kind) => {
let yield_ty = match kind {
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
Expand Down Expand Up @@ -119,74 +165,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Resume type defaults to `()` if the coroutine has no argument.
let resume_ty = liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);

Some(CoroutineTypes { resume_ty, yield_ty })
}
};

check_fn(
&mut FnCtxt::new(self, self.param_env, closure.def_id),
liberated_sig,
coroutine_types,
closure.fn_decl,
expr_def_id,
body,
// Closure "rust-call" ABI doesn't support unsized params
false,
);

let parent_args =
GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id(expr_def_id.to_def_id()));

let tupled_upvars_ty = self.next_root_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::ClosureSynthetic,
span: expr_span,
});

match closure.kind {
hir::ClosureKind::Closure => {
assert_eq!(coroutine_types, None);
// Tuple up the arguments and insert the resulting function type into
// the `closures` table.
let sig = bound_sig.map_bound(|sig| {
tcx.mk_fn_sig(
[Ty::new_tup(tcx, sig.inputs())],
sig.output(),
sig.c_variadic,
sig.unsafety,
sig.abi,
)
});

debug!(?sig, ?expected_kind);

let closure_kind_ty = match expected_kind {
Some(kind) => Ty::from_closure_kind(tcx, kind),

// Create a type variable (for now) to represent the closure kind.
// It will be unified during the upvar inference phase (`upvar.rs`)
None => self.next_root_ty_var(TypeVariableOrigin {
// FIXME(eddyb) distinguish closure kind inference variables from the rest.
kind: TypeVariableOriginKind::ClosureSynthetic,
span: expr_span,
}),
};

let closure_args = ty::ClosureArgs::new(
tcx,
ty::ClosureArgsParts {
parent_args,
closure_kind_ty,
closure_sig_as_fn_ptr_ty: Ty::new_fn_ptr(tcx, sig),
tupled_upvars_ty,
},
);

Ty::new_closure(tcx, expr_def_id.to_def_id(), closure_args.args)
}
hir::ClosureKind::Coroutine(_) => {
let Some(CoroutineTypes { resume_ty, yield_ty }) = coroutine_types else {
bug!("expected coroutine to have yield/resume types");
};
let interior = self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: body.value.span,
Expand All @@ -209,9 +187,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
);

Ty::new_coroutine(tcx, expr_def_id.to_def_id(), coroutine_args.args)
(
Ty::new_coroutine(tcx, expr_def_id.to_def_id(), coroutine_args.args),
Some(CoroutineTypes { resume_ty, yield_ty }),
)
}
}
};

check_fn(
&mut FnCtxt::new(self, self.param_env, closure.def_id),
liberated_sig,
coroutine_types,
closure.fn_decl,
expr_def_id,
body,
// Closure "rust-call" ABI doesn't support unsized params
false,
);

closure_ty
}

/// Given the expected type, figures out what it can about this closure we
Expand Down Expand Up @@ -683,10 +677,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
})
}
// All `gen {}` and `async gen {}` must return unit.
hir::ClosureKind::Coroutine(
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
| hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _),
) => self.tcx.types.unit,
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
hir::CoroutineDesugaring::Gen | hir::CoroutineDesugaring::AsyncGen,
_,
)) => self.tcx.types.unit,

// For async blocks, we just fall back to `_` here.
// For closures/coroutines, we know nothing about the return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | let c1 : () = c;
| expected due to this
|
= note: expected unit type `()`
found closure `{mod1::f<T>::{closure#0} closure_args=(unavailable) args=[T, ?16t, extern "rust-call" fn(()), ?15t]}`
found closure `{mod1::f<T>::{closure#0} closure_args=(unavailable) args=[T, ?8t, extern "rust-call" fn(()), ?7t]}`
help: use parentheses to call this closure
|
LL | let c1 : () = c();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | let c1 : () = c;
| expected due to this
|
= note: expected unit type `()`
found closure `{f<T>::{closure#0} closure_args=(unavailable) args=[T, ?16t, extern "rust-call" fn(()), ?15t]}`
found closure `{f<T>::{closure#0} closure_args=(unavailable) args=[T, ?8t, extern "rust-call" fn(()), ?7t]}`
help: use parentheses to call this closure
|
LL | let c1 : () = c();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/print/closure-print-verbose.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
| expected due to this
|
= note: expected fn pointer `fn(u8) -> u8`
found closure `{main::{closure#0} closure_args=(unavailable) args=[i8, extern "rust-call" fn((u8,)) -> u8, ?6t]}`
found closure `{main::{closure#0} closure_args=(unavailable) args=[i8, extern "rust-call" fn((u8,)) -> u8, ?4t]}`
note: closures can only be coerced to `fn` types if they do not capture any variables
--> $DIR/closure-print-verbose.rs:10:39
|
Expand Down