Skip to content

Commit

Permalink
Use tuple inference
Browse files Browse the repository at this point in the history
Use shallow_resolve and is_valid to check if tuple has been resolved
Check upvars_mentioned instead of substs to coerce closure to fnptr

Co-authored-by: Aman Arora <me@aman-arora.com>
Co-authored-by: Jenny Wills <wills.jenniferg@gmail.com>
  • Loading branch information
3 people committed Sep 30, 2020
1 parent 867bd42 commit 30ab719
Show file tree
Hide file tree
Showing 25 changed files with 170 additions and 75 deletions.
18 changes: 14 additions & 4 deletions compiler/rustc_middle/src/ty/outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,25 @@ fn compute_components(
}

ty::Closure(_, ref substs) => {
for upvar_ty in substs.as_closure().upvar_tys() {
compute_components(tcx, upvar_ty, out, visited);
if substs.as_closure().is_valid() {
for upvar_ty in substs.as_closure().upvar_tys() {
compute_components(tcx, upvar_ty, out, visited);
}
} else {
let tupled_ty = substs.as_closure().tupled_upvars_ty();
compute_components(tcx, tupled_ty, out, visited);
}
}

ty::Generator(_, ref substs, _) => {
// Same as the closure case
for upvar_ty in substs.as_generator().upvar_tys() {
compute_components(tcx, upvar_ty, out, visited);
if substs.as_generator().is_valid() {
for upvar_ty in substs.as_generator().upvar_tys() {
compute_components(tcx, upvar_ty, out, visited);
}
} else {
let tupled_ty = substs.as_generator().tupled_upvars_ty();
compute_components(tcx, tupled_ty, out, visited);
}

// We ignore regions in the generator interior as we don't
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ impl<'tcx> UpvarSubsts<'tcx> {
};
tupled_upvars_ty.expect_ty().tuple_fields()
}

#[inline]
pub fn tupled_upvars_ty(self) -> Ty<'tcx> {
match self {
UpvarSubsts::Closure(substs) => substs.as_closure().tupled_upvars_ty(),
UpvarSubsts::Generator(substs) => substs.as_generator().tupled_upvars_ty(),
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Hash, TyEncodable, TyDecodable)]
Expand Down
40 changes: 28 additions & 12 deletions compiler/rustc_trait_selection/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {

for required_region in required_region_bounds {
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
infcx: self,
op: |r| self.sub_regions(infer::CallReturn(span), required_region, r),
});
}
Expand Down Expand Up @@ -508,6 +509,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
}
}
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
infcx: self,
op: |r| self.sub_regions(infer::CallReturn(span), least_region, r),
});
}
Expand Down Expand Up @@ -542,6 +544,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
);

concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
infcx: self,
op: |r| {
self.member_constraint(
opaque_type_def_id,
Expand Down Expand Up @@ -682,11 +685,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
//
// We ignore any type parameters because impl trait values are assumed to
// capture all the in-scope type parameters.
struct ConstrainOpaqueTypeRegionVisitor<OP> {
struct ConstrainOpaqueTypeRegionVisitor<'cx, 'tcx, OP> {
infcx: &'cx InferCtxt<'cx, 'tcx>,
op: OP,
}

impl<'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<OP>
impl<'cx, 'tcx, OP> TypeVisitor<'tcx> for ConstrainOpaqueTypeRegionVisitor<'cx, 'tcx, OP>
where
OP: FnMut(ty::Region<'tcx>),
{
Expand Down Expand Up @@ -716,24 +720,36 @@ where
ty::Closure(_, ref substs) => {
// Skip lifetime parameters of the enclosing item(s)

for upvar_ty in substs.as_closure().upvar_tys() {
upvar_ty.visit_with(self);
}
let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
// Not yet resolved.
ty.super_visit_with(self);
} else {
for upvar_ty in substs.as_closure().upvar_tys() {
upvar_ty.visit_with(self);
}

substs.as_closure().sig_as_fn_ptr_ty().visit_with(self);
substs.as_closure().sig_as_fn_ptr_ty().visit_with(self);
}
}

ty::Generator(_, ref substs, _) => {
// Skip lifetime parameters of the enclosing item(s)
// Also skip the witness type, because that has no free regions.

for upvar_ty in substs.as_generator().upvar_tys() {
upvar_ty.visit_with(self);
}
let ty = self.infcx.shallow_resolve(substs.as_generator().tupled_upvars_ty());
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
// Not yet resolved.
ty.super_visit_with(self);
} else {
for upvar_ty in substs.as_generator().upvar_tys() {
upvar_ty.visit_with(self);
}

substs.as_generator().return_ty().visit_with(self);
substs.as_generator().yield_ty().visit_with(self);
substs.as_generator().resume_ty().visit_with(self);
substs.as_generator().return_ty().visit_with(self);
substs.as_generator().yield_ty().visit_with(self);
substs.as_generator().resume_ty().visit_with(self);
}
}
_ => {
ty.super_visit_with(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ pub fn trivial_dropck_outlives<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
// check if *any* of those are trivial.
ty::Tuple(ref tys) => tys.iter().all(|t| trivial_dropck_outlives(tcx, t.expect_ty())),
ty::Closure(_, ref substs) => {
substs.as_closure().upvar_tys().all(|t| trivial_dropck_outlives(tcx, t))
if !substs.as_closure().is_valid() {
// Not yet resolved.
false
} else {
substs.as_closure().upvar_tys().all(|t| trivial_dropck_outlives(tcx, t))
}
}

ty::Adt(def, _) => {
Expand Down
34 changes: 30 additions & 4 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

ty::Closure(_, substs) => {
// (*) binder moved here
Where(ty::Binder::bind(substs.as_closure().upvar_tys().collect()))
let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
// Not yet resolved.
Ambiguous
} else {
Where(ty::Binder::bind(substs.as_closure().upvar_tys().collect()))
}
}

ty::Adt(..) | ty::Projection(..) | ty::Param(..) | ty::Opaque(..) => {
Expand Down Expand Up @@ -1747,11 +1753,31 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
tys.iter().map(|k| k.expect_ty()).collect()
}

ty::Closure(_, ref substs) => substs.as_closure().upvar_tys().collect(),
ty::Closure(_, ref substs) => {
let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
// The inference variable will be replaced by a tuple once capture analysis
// completes. If the tuple meets a bound, so do all the elements within it.
vec![ty]
} else {
substs.as_closure().upvar_tys().collect()
}
}

ty::Generator(_, ref substs, _) => {
let witness = substs.as_generator().witness();
substs.as_generator().upvar_tys().chain(iter::once(witness)).collect()
let upvar_tys_resolved =
self.infcx.shallow_resolve(substs.as_generator().tupled_upvars_ty());

if let ty::Infer(ty::TyVar(_)) = upvar_tys_resolved.kind() {
// The inference variable will be replaced by a tuple once capture analysis
// completes, if the tuple meets a bound, so do all the elements within it.
let witness_resolved =
self.infcx.shallow_resolve(substs.as_generator().witness());
vec![upvar_tys_resolved, witness_resolved]
} else {
let witness = substs.as_generator().witness();
substs.as_generator().upvar_tys().chain(iter::once(witness)).collect()
}
}

ty::GeneratorWitness(types) => {
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
// anyway, except via auto trait matching (which
// only inspects the upvar types).
walker.skip_current_subtree(); // subtree handled below
for upvar_ty in substs.as_closure().upvar_tys() {
// FIXME(eddyb) add the type to `walker` instead of recursing.
self.compute(upvar_ty.into());
}
self.compute(substs.as_closure().tupled_upvars_ty().into());
}

ty::FnPtr(_) => {
Expand Down
24 changes: 19 additions & 5 deletions compiler/rustc_traits/src/dropck_outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,20 @@ fn dtorck_constraint_for_ty<'tcx>(
Ok::<_, NoSolution>(())
})?,

ty::Closure(_, substs) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in substs.as_closure().upvar_tys() {
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
ty::Closure(_, substs) => {
if !substs.as_closure().is_valid() {
// By the time this code runs, all type variables ought to
// be fully resolved.
return Err(NoSolution);
}
Ok::<_, NoSolution>(())
})?,

rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in substs.as_closure().upvar_tys() {
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
}
Ok::<_, NoSolution>(())
})?
}

ty::Generator(_, substs, _movability) => {
// rust-lang/rust#49918: types can be constructed, stored
Expand All @@ -241,6 +249,12 @@ fn dtorck_constraint_for_ty<'tcx>(
// derived from lifetimes attached to the upvars and resume
// argument, and we *do* incorporate those here.

if !substs.as_generator().is_valid() {
// By the time this code runs, all type variables ought to
// be fully resolved.
return Err(NoSolution);
}

constraints.outlives.extend(
substs
.as_generator()
Expand Down
18 changes: 5 additions & 13 deletions compiler/rustc_typeck/src/check/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.closure_base_def_id(expr_def_id.to_def_id()),
);

let tupled_upvars_ty =
self.tcx.mk_tup(self.tcx.upvars_mentioned(expr_def_id).iter().flat_map(|upvars| {
upvars.iter().map(|(&var_hir_id, _)| {
// Create type variables (for now) to represent the transformed
// types of upvars. These will be unified during the upvar
// inference phase (`upvar.rs`).
self.infcx.next_ty_var(TypeVariableOrigin {
// FIXME(eddyb) distinguish upvar inference variables from the rest.
kind: TypeVariableOriginKind::ClosureSynthetic,
span: self.tcx.hir().span(var_hir_id),
})
})
}));
let tupled_upvars_ty = self.infcx.next_ty_var(TypeVariableOrigin {
// FIXME(eddyb) distinguish upvar inference variables from the rest.
kind: TypeVariableOriginKind::ClosureSynthetic,
span: self.tcx.hir().span(expr.hir_id),
});

if let Some(GeneratorTypes { resume_ty, yield_ty, interior, movability }) = generator_types
{
Expand Down
23 changes: 18 additions & 5 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::astconv::AstConv;
use crate::check::FnCtxt;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{Coercion, InferOk, InferResult};
use rustc_middle::ty::adjustment::{
Expand Down Expand Up @@ -221,11 +222,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// unsafe qualifier.
self.coerce_from_fn_pointer(a, a_f, b)
}
ty::Closure(_, substs_a) => {
ty::Closure(closure_def_id_a, substs_a) => {
// Non-capturing closures are coercible to
// function pointers or unsafe function pointers.
// It cannot convert closures that require unsafe.
self.coerce_closure_to_fn(a, substs_a, b)
self.coerce_closure_to_fn(a, closure_def_id_a, substs_a, b)
}
_ => {
// Otherwise, just use unification rules.
Expand Down Expand Up @@ -762,6 +763,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
fn coerce_closure_to_fn(
&self,
a: Ty<'tcx>,
closure_def_id_a: DefId,
substs_a: SubstsRef<'tcx>,
b: Ty<'tcx>,
) -> CoerceResult<'tcx> {
Expand All @@ -772,7 +774,18 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
let b = self.shallow_resolve(b);

match b.kind() {
ty::FnPtr(fn_ty) if substs_a.as_closure().upvar_tys().next().is_none() => {
// At this point we haven't done capture analysis, which means
// that the ClosureSubsts just contains an inference variable instead
// of tuple of captured types.
//
// All we care here is if any variable is being captured and not the exact paths,
// so we check `upvars_mentioned` for root variables being captured.
ty::FnPtr(fn_ty)
if self
.tcx
.upvars_mentioned(closure_def_id_a.expect_local())
.map_or(true, |u| u.is_empty()) =>
{
// We coerce the closure, which has fn type
// `extern "rust-call" fn((arg0,arg1,...)) -> _`
// to
Expand Down Expand Up @@ -906,8 +919,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Function items or non-capturing closures of differing IDs or InternalSubsts.
let (a_sig, b_sig) = {
let is_capturing_closure = |ty| {
if let &ty::Closure(_, substs) = ty {
substs.as_closure().upvar_tys().next().is_some()
if let &ty::Closure(closure_def_id, _substs) = ty {
self.tcx.upvars_mentioned(closure_def_id.expect_local()).is_some()
} else {
false
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
closure_hir_id, substs, final_upvar_tys
);
for (upvar_ty, final_upvar_ty) in substs.upvar_tys().zip(final_upvar_tys) {
self.demand_suptype(span, upvar_ty, final_upvar_ty);
}

// Build a tuple (U0..Un) of the final upvar types U0..Un
// and unify the upvar tupe type in the closure with it:
let final_tupled_upvars_type = self.tcx.mk_tup(final_upvar_tys.iter());
self.demand_suptype(span, substs.tupled_upvars_ty(), final_tupled_upvars_type);

// If we are also inferred the closure kind here,
// process any deferred resolutions.
Expand Down
17 changes: 11 additions & 6 deletions src/test/ui/async-await/issue-70818.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
error: future cannot be sent between threads safely
error[E0277]: `U` cannot be sent between threads safely
--> $DIR/issue-70818.rs:4:38
|
LL | fn foo<T: Send, U>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + Send {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future created by async block is not `Send`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `U` cannot be sent between threads safely
LL |
LL | async { (ty, ty1) }
| ------------------- this returned value is of type `impl Future`
|
::: $SRC_DIR/core/src/future/mod.rs:LL:COL
|
note: captured value is not `Send`
--> $DIR/issue-70818.rs:6:18
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
| ------------------------------- within this `impl Future`
|
LL | async { (ty, ty1) }
| ^^^ has type `U` which is not `Send`
= note: required because it appears within the type `(T, U)`
= note: required because it appears within the type `[static generator@$DIR/issue-70818.rs:6:11: 6:24 _]`
= note: required because it appears within the type `from_generator::GenFuture<[static generator@$DIR/issue-70818.rs:6:11: 6:24 _]>`
= note: required because it appears within the type `impl Future`
= note: the return type of a function must have a statically known size
help: consider restricting type parameter `U`
|
Expand All @@ -20,3 +24,4 @@ LL | fn foo<T: Send, U: Send>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + S

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
2 changes: 2 additions & 0 deletions src/test/ui/closures/closure-move-sync.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LL | F: Send + 'static,
|
= help: the trait `Sync` is not implemented for `std::sync::mpsc::Receiver<()>`
= note: required because of the requirements on the impl of `Send` for `&std::sync::mpsc::Receiver<()>`
= note: required because it appears within the type `(&std::sync::mpsc::Receiver<()>,)`
= note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:6:27: 9:6]`

error[E0277]: `Sender<()>` cannot be shared between threads safely
Expand All @@ -26,6 +27,7 @@ LL | F: Send + 'static,
|
= help: the trait `Sync` is not implemented for `Sender<()>`
= note: required because of the requirements on the impl of `Send` for `&Sender<()>`
= note: required because it appears within the type `(&Sender<()>,)`
= note: required because it appears within the type `[closure@$DIR/closure-move-sync.rs:18:19: 18:42]`

error: aborting due to 2 previous errors
Expand Down
Loading

0 comments on commit 30ab719

Please sign in to comment.