From 96f9e33013082b0cae709ac6ceb85e26d6fcae27 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 19 Oct 2023 01:35:38 +0000 Subject: [PATCH] Uplift movability to rustc_type_ir --- compiler/rustc_borrowck/src/lib.rs | 2 +- compiler/rustc_borrowck/src/universal_regions.rs | 3 +-- compiler/rustc_hir_typeck/src/check.rs | 5 ++++- compiler/rustc_hir_typeck/src/lib.rs | 2 +- compiler/rustc_middle/src/mir/syntax.rs | 3 +-- compiler/rustc_middle/src/thir.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 1 - compiler/rustc_middle/src/ty/print/pretty.rs | 4 ++-- compiler/rustc_middle/src/ty/sty.rs | 2 +- compiler/rustc_mir_transform/src/generator.rs | 4 ++-- compiler/rustc_mir_transform/src/shim.rs | 2 +- compiler/rustc_smir/src/rustc_smir/mod.rs | 6 +++--- .../src/solve/assembly/structural_traits.rs | 6 +++--- .../rustc_trait_selection/src/solve/trait_goals.rs | 6 +++--- .../src/traits/select/candidate_assembly.rs | 4 ++-- .../rustc_trait_selection/src/traits/select/mod.rs | 4 ++-- compiler/rustc_type_ir/src/lib.rs | 1 - compiler/rustc_type_ir/src/structural_impls.rs | 1 + compiler/rustc_type_ir/src/sty.rs | 14 ++++++++++---- 19 files changed, 39 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index d274a3eea6ca9..2018cca433fd8 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -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 { diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index c73192f440451..604c854ab8459 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -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; @@ -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. diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index 26ea7b0fdb972..5cffcaea32d28 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -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 diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index cd6adb345e7a3..d46129b0c6150 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -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)] diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 7a645fb5d62ba..e7576f41fce93 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -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}; @@ -1344,7 +1343,7 @@ pub enum AggregateKind<'tcx> { Adt(DefId, VariantIdx, GenericArgsRef<'tcx>, Option, Option), 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)] diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index 67804998a3293..ffa98415ffccc 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -167,7 +167,7 @@ pub struct ClosureExpr<'tcx> { pub closure_id: LocalDefId, pub args: UpvarArgs<'tcx>, pub upvars: Box<[ExprId]>, - pub movability: Option, + pub movability: Option, pub fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>, } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 05706e331f3a7..3bcbb157dd0cc 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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>; type BinderListTy = Binder<'tcx, &'tcx List>>; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index d8010c714970d..67447ad7654a0 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -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 "), } } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index fc207a2c350de..f2c1f46a0e43b 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -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(), diff --git a/compiler/rustc_mir_transform/src/generator.rs b/compiler/rustc_mir_transform/src/generator.rs index a6693519e54b2..fa358b2b65d12 100644 --- a/compiler/rustc_mir_transform/src/generator.rs +++ b/compiler/rustc_mir_transform/src/generator.rs @@ -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), }; @@ -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}")); diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index e9895d97dfefa..a05f7a2d78a08 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -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), diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 94dc15b4767c1..249b8212b9c4e 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -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, } } } diff --git a/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs b/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs index 26c68acddffef..4b621cc897e47 100644 --- a/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs @@ -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::{ @@ -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(_, _) @@ -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()]) diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index d87e05ad02f36..bfc5db02da02d 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -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}; @@ -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)) } } diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index a8001577bcd5c..c400a3b1e3b80 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -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 }); diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 940ceca50d2d4..ae688f113cbf5 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -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, @@ -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()); diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 9f8d9f02ec24c..f0456dc56cb7d 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -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 + Hash + Ord; type ListBinderExistentialPredicate: Clone + DebugWithInfcx + Hash + Ord; type BinderListTy: Clone + DebugWithInfcx + Hash + Ord; diff --git a/compiler/rustc_type_ir/src/structural_impls.rs b/compiler/rustc_type_ir/src/structural_impls.rs index 08af96ea15f0f..31515f10feec2 100644 --- a/compiler/rustc_type_ir/src/structural_impls.rs +++ b/compiler/rustc_type_ir/src/structural_impls.rs @@ -28,6 +28,7 @@ TrivialTypeTraversalImpls! { u64, String, crate::DebruijnIndex, + crate::Movability, } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index 091b51440a6e1..a0cad02f03a60 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -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 @@ -141,7 +150,7 @@ pub enum TyKind { /// /// 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`. @@ -574,7 +583,6 @@ where I::Region: Encodable, I::TypeAndMut: Encodable, I::Mutability: Encodable, - I::Movability: Encodable, I::PolyFnSig: Encodable, I::ListBinderExistentialPredicate: Encodable, I::BinderListTy: Encodable, @@ -689,7 +697,6 @@ where I::Region: Decodable, I::TypeAndMut: Decodable, I::Mutability: Decodable, - I::Movability: Decodable, I::PolyFnSig: Decodable, I::ListBinderExistentialPredicate: Decodable, I::BinderListTy: Decodable, @@ -755,7 +762,6 @@ where I::PolyFnSig: HashStable, I::ListBinderExistentialPredicate: HashStable, I::Region: HashStable, - I::Movability: HashStable, I::Mutability: HashStable, I::BinderListTy: HashStable, I::ListTy: HashStable,