Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uplift generator movability to rustc_type_ir #116918

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ fn do_mir_borrowck<'tcx>(
// The first argument is the generator type passed by value
if let Some(local) = body.local_decls.raw.get(1)
// Get the interior types and args which typeck computed
&& let ty::Generator(_, _, hir::Movability::Static) = local.ty.kind()
&& let ty::Generator(_, _, ty::Movability::Static) = local.ty.kind()
{
false
} else {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Diagnostic;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::BodyOwnerKind;
Expand Down Expand Up @@ -94,7 +93,7 @@ pub enum DefiningTy<'tcx> {
/// The MIR is a generator. The signature is that generators take
/// no parameters and return the result of
/// `ClosureArgs::generator_return_ty`.
Generator(DefId, GenericArgsRef<'tcx>, hir::Movability),
Generator(DefId, GenericArgsRef<'tcx>, ty::Movability),

/// The MIR is a fn item with the given `DefId` and args. The signature
/// of the function can be bound then with the `fn_sig` query.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ pub(super) fn check_fn<'a, 'tcx>(
resume_ty,
yield_ty,
interior,
movability: can_be_generator.unwrap(),
movability: match can_be_generator.unwrap() {
hir::Movability::Static => ty::Movability::Static,
hir::Movability::Movable => ty::Movability::Movable,
},
})
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ struct GeneratorTypes<'tcx> {
interior: Ty<'tcx>,

/// Indicates if the generator is movable or static (immovable).
movability: hir::Movability,
movability: ty::Movability,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/mir/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::ty::{Region, UserTypeAnnotationIndex};

use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir};
use rustc_hir::{self, GeneratorKind};
use rustc_index::IndexVec;
use rustc_target::abi::{FieldIdx, VariantIdx};
Expand Down Expand Up @@ -1344,7 +1343,7 @@ pub enum AggregateKind<'tcx> {
Adt(DefId, VariantIdx, GenericArgsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<FieldIdx>),

Closure(DefId, GenericArgsRef<'tcx>),
Generator(DefId, GenericArgsRef<'tcx>, hir::Movability),
Generator(DefId, GenericArgsRef<'tcx>, ty::Movability),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub struct ClosureExpr<'tcx> {
pub closure_id: LocalDefId,
pub args: UpvarArgs<'tcx>,
pub upvars: Box<[ExprId]>,
pub movability: Option<hir::Movability>,
pub movability: Option<ty::Movability>,
pub fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>,
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type Predicate = Predicate<'tcx>;
type TypeAndMut = TypeAndMut<'tcx>;
type Mutability = hir::Mutability;
type Movability = hir::Movability;
type PolyFnSig = PolyFnSig<'tcx>;
type ListBinderExistentialPredicate = &'tcx List<PolyExistentialPredicate<'tcx>>;
type BinderListTy = Binder<'tcx, &'tcx List<Ty<'tcx>>>;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,8 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {

if should_print_movability {
match movability {
hir::Movability::Movable => {}
hir::Movability::Static => p!("static "),
ty::Movability::Movable => {}
ty::Movability::Static => p!("static "),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ impl<'tcx> Ty<'tcx> {
tcx: TyCtxt<'tcx>,
def_id: DefId,
generator_args: GenericArgsRef<'tcx>,
movability: hir::Movability,
movability: ty::Movability,
) -> Ty<'tcx> {
debug_assert_eq!(
generator_args.len(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ pub(crate) fn mir_generator_witnesses<'tcx>(

// Get the interior types and args which typeck computed
let movable = match *gen_ty.kind() {
ty::Generator(_, _, movability) => movability == hir::Movability::Movable,
ty::Generator(_, _, movability) => movability == ty::Movability::Movable,
ty::Error(_) => return None,
_ => span_bug!(body.span, "unexpected generator type {}", gen_ty),
};
Expand Down Expand Up @@ -1430,7 +1430,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
let (discr_ty, movable) = match *gen_ty.kind() {
ty::Generator(_, args, movability) => {
let args = args.as_generator();
(args.discr_ty(tcx), movability == hir::Movability::Movable)
(args.discr_ty(tcx), movability == ty::Movability::Movable)
}
_ => {
tcx.sess.delay_span_bug(body.span, format!("unexpected generator type {gen_ty}"));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ fn build_clone_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'tcx>) -
_ if is_copy => builder.copy_shim(),
ty::Closure(_, args) => builder.tuple_like_shim(dest, src, args.as_closure().upvar_tys()),
ty::Tuple(..) => builder.tuple_like_shim(dest, src, self_ty.tuple_fields()),
ty::Generator(gen_def_id, args, hir::Movability::Movable) => {
ty::Generator(gen_def_id, args, ty::Movability::Movable) => {
builder.generator_shim(dest, src, *gen_def_id, args.as_generator())
}
_ => bug!("clone shim for `{:?}` which is not `Copy` and is not an aggregate", self_ty),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,13 +1175,13 @@ impl<'tcx> Stable<'tcx> for ty::FloatTy {
}
}

impl<'tcx> Stable<'tcx> for hir::Movability {
impl<'tcx> Stable<'tcx> for ty::Movability {
type T = Movability;

fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
match self {
hir::Movability::Static => Movability::Static,
hir::Movability::Movable => Movability::Movable,
ty::Movability::Static => Movability::Static,
ty::Movability::Movable => Movability::Movable,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Code which is used by built-in goals that match "structurally", such a auto
//! traits, `Copy`/`Clone`.
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::{def_id::DefId, Movability, Mutability};
use rustc_hir::{def_id::DefId, Mutability};
use rustc_infer::traits::query::NoSolution;
use rustc_middle::traits::solve::Goal;
use rustc_middle::ty::{
Expand Down Expand Up @@ -174,7 +174,7 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(
ty::Dynamic(..)
| ty::Str
| ty::Slice(_)
| ty::Generator(_, _, Movability::Static)
| ty::Generator(_, _, ty::Movability::Static)
| ty::Foreign(..)
| ty::Ref(_, _, Mutability::Mut)
| ty::Adt(_, _)
Expand All @@ -191,7 +191,7 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_copy_clone_trait<'tcx>(

ty::Closure(_, args) => Ok(vec![args.as_closure().tupled_upvars_ty()]),

ty::Generator(_, args, Movability::Movable) => {
ty::Generator(_, args, ty::Movability::Movable) => {
if ecx.tcx().features().generator_clone {
let generator = args.as_generator();
Ok(vec![generator.tupled_upvars_ty(), generator.witness()])
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::assembly::{self, structural_traits};
use super::{EvalCtxt, SolverMode};
use rustc_hir::def_id::DefId;
use rustc_hir::{LangItem, Movability};
use rustc_hir::LangItem;
use rustc_infer::traits::query::NoSolution;
use rustc_middle::traits::solve::inspect::ProbeKind;
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult};
Expand Down Expand Up @@ -851,8 +851,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
if Some(goal.predicate.def_id()) == self.tcx().lang_items().unpin_trait() =>
{
match movability {
Movability::Static => Some(Err(NoSolution)),
Movability::Movable => {
ty::Movability::Static => Some(Err(NoSolution)),
ty::Movability::Movable => {
Some(self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
if self.tcx().lang_items().unpin_trait() == Some(def_id) =>
{
match movability {
hir::Movability::Static => {
ty::Movability::Static => {
// Immovable generators are never `Unpin`, so
// suppress the normal auto-impl candidate for it.
}
hir::Movability::Movable => {
ty::Movability::Movable => {
// Movable generators are always `Unpin`, so add an
// unconditional builtin candidate.
candidates.vec.push(BuiltinCandidate { has_nested: false });
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
ty::Dynamic(..)
| ty::Str
| ty::Slice(..)
| ty::Generator(_, _, hir::Movability::Static)
| ty::Generator(_, _, ty::Movability::Static)
| ty::Foreign(..)
| ty::Ref(_, _, hir::Mutability::Mut) => None,

Expand All @@ -2189,7 +2189,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
Where(obligation.predicate.rebind(tys.iter().collect()))
}

ty::Generator(_, args, hir::Movability::Movable) => {
ty::Generator(_, args, ty::Movability::Movable) => {
if self.tcx().features().generator_clone {
let resolved_upvars =
self.infcx.shallow_resolve(args.as_generator().tupled_upvars_ty());
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub trait Interner: Sized {
type Predicate;
type TypeAndMut: Clone + Debug + Hash + Ord;
type Mutability: Clone + Debug + Hash + Ord;
type Movability: Clone + Debug + Hash + Ord;
type PolyFnSig: Clone + DebugWithInfcx<Self> + Hash + Ord;
type ListBinderExistentialPredicate: Clone + DebugWithInfcx<Self> + Hash + Ord;
type BinderListTy: Clone + DebugWithInfcx<Self> + Hash + Ord;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/src/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TrivialTypeTraversalImpls! {
u64,
String,
crate::DebruijnIndex,
crate::Movability,
}

///////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_type_ir/src/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ pub enum AliasKind {
Weak,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum Movability {
/// May contain self-references, `!Unpin`.
Static,
/// Must not contain self-references, `Unpin`.
Movable,
}

/// Defines the kinds of types used by the type system.
///
/// Types written by the user start out as `hir::TyKind` and get
Expand Down Expand Up @@ -141,7 +150,7 @@ pub enum TyKind<I: Interner> {
///
/// For more info about generator args, visit the documentation for
/// `GeneratorArgs`.
Generator(I::DefId, I::GenericArgsRef, I::Movability),
Generator(I::DefId, I::GenericArgsRef, Movability),

/// A type representing the types stored inside a generator.
/// This should only appear as part of the `GeneratorArgs`.
Expand Down Expand Up @@ -574,7 +583,6 @@ where
I::Region: Encodable<E>,
I::TypeAndMut: Encodable<E>,
I::Mutability: Encodable<E>,
I::Movability: Encodable<E>,
I::PolyFnSig: Encodable<E>,
I::ListBinderExistentialPredicate: Encodable<E>,
I::BinderListTy: Encodable<E>,
Expand Down Expand Up @@ -689,7 +697,6 @@ where
I::Region: Decodable<D>,
I::TypeAndMut: Decodable<D>,
I::Mutability: Decodable<D>,
I::Movability: Decodable<D>,
I::PolyFnSig: Decodable<D>,
I::ListBinderExistentialPredicate: Decodable<D>,
I::BinderListTy: Decodable<D>,
Expand Down Expand Up @@ -755,7 +762,6 @@ where
I::PolyFnSig: HashStable<CTX>,
I::ListBinderExistentialPredicate: HashStable<CTX>,
I::Region: HashStable<CTX>,
I::Movability: HashStable<CTX>,
I::Mutability: HashStable<CTX>,
I::BinderListTy: HashStable<CTX>,
I::ListTy: HashStable<CTX>,
Expand Down
Loading