Skip to content

Commit

Permalink
Yeet PolyGenSig
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Dec 6, 2023
1 parent f32d298 commit 1ec3ef7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 85 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ pub use self::sty::{
CoroutineArgs, CoroutineArgsParts, EarlyParamRegion, ExistentialPredicate,
ExistentialProjection, ExistentialTraitRef, FnSig, GenSig, InlineConstArgs,
InlineConstArgsParts, LateParamRegion, ParamConst, ParamTy, PolyExistentialPredicate,
PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef,
PredicateKind, Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, UpvarArgs,
VarianceDiagInfo,
PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyTraitRef, PredicateKind,
Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, UpvarArgs, VarianceDiagInfo,
};
pub use self::trait_def::TraitDef;
pub use self::typeck_results::{
Expand Down
12 changes: 0 additions & 12 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,6 @@ impl<'tcx> CoroutineArgs<'tcx> {
self.split().return_ty.expect_ty()
}

/// Returns the "coroutine signature", which consists of its yield
/// and return types.
///
/// N.B., some bits of the code prefers to see this wrapped in a
/// binder, but it never contains bound regions. Probably this
/// function should be removed.
pub fn poly_sig(self) -> PolyGenSig<'tcx> {
ty::Binder::dummy(self.sig())
}

/// Returns the "coroutine signature", which consists of its resume, yield
/// and return types.
pub fn sig(self) -> GenSig<'tcx> {
Expand Down Expand Up @@ -1352,8 +1342,6 @@ pub struct GenSig<'tcx> {
pub return_ty: Ty<'tcx>,
}

pub type PolyGenSig<'tcx> = Binder<'tcx, GenSig<'tcx>>;

/// Signature of a function type, which we have arbitrarily
/// decided to use to refer to the input/output types.
///
Expand Down
77 changes: 37 additions & 40 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ fn confirm_coroutine_candidate<'cx, 'tcx>(
else {
unreachable!()
};
let coroutine_sig = args.as_coroutine().poly_sig();
let coroutine_sig = args.as_coroutine().sig();
let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
selcx,
obligation.param_env,
Expand All @@ -2091,29 +2091,28 @@ fn confirm_coroutine_candidate<'cx, 'tcx>(

let coroutine_def_id = tcx.require_lang_item(LangItem::Coroutine, None);

let predicate = super::util::coroutine_trait_ref_and_outputs(
let (trait_ref, yield_ty, return_ty) = super::util::coroutine_trait_ref_and_outputs(
tcx,
coroutine_def_id,
obligation.predicate.self_ty(),
coroutine_sig,
)
.map_bound(|(trait_ref, yield_ty, return_ty)| {
let name = tcx.associated_item(obligation.predicate.def_id).name;
let ty = if name == sym::Return {
return_ty
} else if name == sym::Yield {
yield_ty
} else {
bug!()
};
);

ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: ty.into(),
}
});
let name = tcx.associated_item(obligation.predicate.def_id).name;
let ty = if name == sym::Return {
return_ty
} else if name == sym::Yield {
yield_ty
} else {
bug!()
};

let predicate = ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: ty.into(),
};

confirm_param_env_candidate(selcx, obligation, predicate, false)
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
}
Expand All @@ -2128,7 +2127,7 @@ fn confirm_future_candidate<'cx, 'tcx>(
else {
unreachable!()
};
let coroutine_sig = args.as_coroutine().poly_sig();
let coroutine_sig = args.as_coroutine().sig();
let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
selcx,
obligation.param_env,
Expand All @@ -2142,22 +2141,21 @@ fn confirm_future_candidate<'cx, 'tcx>(
let tcx = selcx.tcx();
let fut_def_id = tcx.require_lang_item(LangItem::Future, None);

let predicate = super::util::future_trait_ref_and_outputs(
let (trait_ref, return_ty) = super::util::future_trait_ref_and_outputs(
tcx,
fut_def_id,
obligation.predicate.self_ty(),
coroutine_sig,
)
.map_bound(|(trait_ref, return_ty)| {
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);
);

ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: return_ty.into(),
}
});
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Output);

let predicate = ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: return_ty.into(),
};

confirm_param_env_candidate(selcx, obligation, predicate, false)
confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
}
Expand All @@ -2172,7 +2170,7 @@ fn confirm_iterator_candidate<'cx, 'tcx>(
else {
unreachable!()
};
let gen_sig = args.as_coroutine().poly_sig();
let gen_sig = args.as_coroutine().sig();
let Normalized { value: gen_sig, obligations } = normalize_with_depth(
selcx,
obligation.param_env,
Expand All @@ -2186,22 +2184,21 @@ fn confirm_iterator_candidate<'cx, 'tcx>(
let tcx = selcx.tcx();
let iter_def_id = tcx.require_lang_item(LangItem::Iterator, None);

let predicate = super::util::iterator_trait_ref_and_outputs(
let (trait_ref, yield_ty) = super::util::iterator_trait_ref_and_outputs(
tcx,
iter_def_id,
obligation.predicate.self_ty(),
gen_sig,
)
.map_bound(|(trait_ref, yield_ty)| {
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Item);
);

ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: yield_ty.into(),
}
});
debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name, sym::Item);

confirm_param_env_candidate(selcx, obligation, predicate, false)
let predicate = ty::ProjectionPredicate {
projection_ty: ty::AliasTy::new(tcx, obligation.predicate.def_id, trait_ref.args),
term: yield_ty.into(),
};

confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
}
Expand Down
27 changes: 12 additions & 15 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

debug!(?obligation, ?coroutine_def_id, ?args, "confirm_coroutine_candidate");

let coroutine_sig = args.as_coroutine().poly_sig();
let coroutine_sig = args.as_coroutine().sig();

// NOTE: The self-type is a coroutine type and hence is
// in fact unparameterized (or at least does not reference any
Expand All @@ -742,15 +742,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
.no_bound_vars()
.expect("unboxed closure type should not capture bound vars from the predicate");

let trait_ref = super::util::coroutine_trait_ref_and_outputs(
let (trait_ref, _, _) = super::util::coroutine_trait_ref_and_outputs(
self.tcx(),
obligation.predicate.def_id(),
self_ty,
coroutine_sig,
)
.map_bound(|(trait_ref, ..)| trait_ref);
);

let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
let nested = self.confirm_poly_trait_refs(obligation, ty::Binder::dummy(trait_ref))?;
debug!(?trait_ref, ?nested, "coroutine candidate obligations");

Ok(nested)
Expand All @@ -770,17 +769,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

debug!(?obligation, ?coroutine_def_id, ?args, "confirm_future_candidate");

let coroutine_sig = args.as_coroutine().poly_sig();
let coroutine_sig = args.as_coroutine().sig();

let trait_ref = super::util::future_trait_ref_and_outputs(
let (trait_ref, _) = super::util::future_trait_ref_and_outputs(
self.tcx(),
obligation.predicate.def_id(),
obligation.predicate.no_bound_vars().expect("future has no bound vars").self_ty(),
coroutine_sig,
)
.map_bound(|(trait_ref, ..)| trait_ref);
);

let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
let nested = self.confirm_poly_trait_refs(obligation, ty::Binder::dummy(trait_ref))?;
debug!(?trait_ref, ?nested, "future candidate obligations");

Ok(nested)
Expand All @@ -800,17 +798,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

debug!(?obligation, ?coroutine_def_id, ?args, "confirm_iterator_candidate");

let gen_sig = args.as_coroutine().poly_sig();
let gen_sig = args.as_coroutine().sig();

let trait_ref = super::util::iterator_trait_ref_and_outputs(
let (trait_ref, _) = super::util::iterator_trait_ref_and_outputs(
self.tcx(),
obligation.predicate.def_id(),
obligation.predicate.no_bound_vars().expect("iterator has no bound vars").self_ty(),
gen_sig,
)
.map_bound(|(trait_ref, ..)| trait_ref);
);

let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
let nested = self.confirm_poly_trait_refs(obligation, ty::Binder::dummy(trait_ref))?;
debug!(?trait_ref, ?nested, "iterator candidate obligations");

Ok(nested)
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,33 +279,33 @@ pub fn coroutine_trait_ref_and_outputs<'tcx>(
tcx: TyCtxt<'tcx>,
fn_trait_def_id: DefId,
self_ty: Ty<'tcx>,
sig: ty::PolyGenSig<'tcx>,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
sig: ty::GenSig<'tcx>,
) -> (ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>) {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, sig.skip_binder().resume_ty]);
sig.map_bound(|sig| (trait_ref, sig.yield_ty, sig.return_ty))
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty, sig.resume_ty]);
(trait_ref, sig.yield_ty, sig.return_ty)
}

pub fn future_trait_ref_and_outputs<'tcx>(
tcx: TyCtxt<'tcx>,
fn_trait_def_id: DefId,
self_ty: Ty<'tcx>,
sig: ty::PolyGenSig<'tcx>,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
sig: ty::GenSig<'tcx>,
) -> (ty::TraitRef<'tcx>, Ty<'tcx>) {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = ty::TraitRef::new(tcx, fn_trait_def_id, [self_ty]);
sig.map_bound(|sig| (trait_ref, sig.return_ty))
(trait_ref, sig.return_ty)
}

pub fn iterator_trait_ref_and_outputs<'tcx>(
tcx: TyCtxt<'tcx>,
iterator_def_id: DefId,
self_ty: Ty<'tcx>,
sig: ty::PolyGenSig<'tcx>,
) -> ty::Binder<'tcx, (ty::TraitRef<'tcx>, Ty<'tcx>)> {
sig: ty::GenSig<'tcx>,
) -> (ty::TraitRef<'tcx>, Ty<'tcx>) {
assert!(!self_ty.has_escaping_bound_vars());
let trait_ref = ty::TraitRef::new(tcx, iterator_def_id, [self_ty]);
sig.map_bound(|sig| (trait_ref, sig.yield_ty))
(trait_ref, sig.yield_ty)
}

pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool {
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ fn fn_sig_for_fn_abi<'tcx>(
}
ty::Coroutine(did, args, _) => {
let coroutine_kind = tcx.coroutine_kind(did).unwrap();
let sig = args.as_coroutine().poly_sig();
let sig = args.as_coroutine().sig();

let bound_vars = tcx.mk_bound_variable_kinds_from_iter(
sig.bound_vars().iter().chain(iter::once(ty::BoundVariableKind::Region(ty::BrEnv))),
);
let bound_vars = tcx.mk_bound_variable_kinds_from_iter(iter::once(
ty::BoundVariableKind::Region(ty::BrEnv),
));
let br = ty::BoundRegion {
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
kind: ty::BoundRegionKind::BrEnv,
Expand All @@ -124,7 +124,6 @@ fn fn_sig_for_fn_abi<'tcx>(
}
};

let sig = sig.skip_binder();
// The `FnSig` and the `ret_ty` here is for a coroutines main
// `Coroutine::resume(...) -> CoroutineState` function in case we
// have an ordinary coroutine, the `Future::poll(...) -> Poll`
Expand Down

0 comments on commit 1ec3ef7

Please sign in to comment.