From c3a2302fb068fe61c7c7fb1bc751abe2daa631e4 Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Wed, 13 Dec 2023 16:33:26 -0800 Subject: [PATCH 1/4] Erase late bound regions from instance `fn_sig()` Late bound regions were still part of the signature. --- compiler/rustc_smir/src/rustc_smir/context.rs | 15 ++++++++++++--- compiler/stable_mir/src/compiler_interface.rs | 9 ++++++--- compiler/stable_mir/src/mir/mono.rs | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/context.rs b/compiler/rustc_smir/src/rustc_smir/context.rs index 241a0c2231064..5516ca0a5993f 100644 --- a/compiler/rustc_smir/src/rustc_smir/context.rs +++ b/compiler/rustc_smir/src/rustc_smir/context.rs @@ -13,8 +13,8 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef}; use stable_mir::mir::Body; use stable_mir::target::{MachineInfo, MachineSize}; use stable_mir::ty::{ - AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs, - LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef, + AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig, + GenericArgs, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef, }; use stable_mir::{Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol}; use std::cell::RefCell; @@ -324,7 +324,16 @@ impl<'tcx> Context for TablesWrapper<'tcx> { fn instance_ty(&self, def: InstanceDef) -> stable_mir::ty::Ty { let mut tables = self.0.borrow_mut(); let instance = tables.instances[def]; - instance.ty(tables.tcx, ParamEnv::empty()).stable(&mut *tables) + instance.ty(tables.tcx, ParamEnv::reveal_all()).stable(&mut *tables) + } + + fn instance_sig(&self, def: InstanceDef) -> FnSig { + let mut tables = self.0.borrow_mut(); + let instance = tables.instances[def]; + let ty = instance.ty(tables.tcx, ParamEnv::reveal_all()); + let sig = if ty.is_fn() { ty.fn_sig(tables.tcx) } else { instance.args.as_closure().sig() }; + // Erase late bound regions. + tables.tcx.instantiate_bound_regions_with_erased(sig).stable(&mut *tables) } fn instance_def_id(&self, def: InstanceDef) -> stable_mir::DefId { diff --git a/compiler/stable_mir/src/compiler_interface.rs b/compiler/stable_mir/src/compiler_interface.rs index 2fac59e71fd5b..8ee82c241308b 100644 --- a/compiler/stable_mir/src/compiler_interface.rs +++ b/compiler/stable_mir/src/compiler_interface.rs @@ -10,9 +10,9 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef}; use crate::mir::Body; use crate::target::MachineInfo; use crate::ty::{ - AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs, - GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, - TraitDef, Ty, TyKind, VariantDef, + AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig, + GenericArgs, GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, + Span, TraitDecl, TraitDef, Ty, TyKind, VariantDef, }; use crate::{ mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol, @@ -121,6 +121,9 @@ pub trait Context { /// Get the instance type with generic substitutions applied and lifetimes erased. fn instance_ty(&self, instance: InstanceDef) -> Ty; + /// Get the instance signature with . + fn instance_sig(&self, def: InstanceDef) -> FnSig; + /// Get the instance. fn instance_def_id(&self, instance: InstanceDef) -> DefId; diff --git a/compiler/stable_mir/src/mir/mono.rs b/compiler/stable_mir/src/mir/mono.rs index bc5d4a3b8f477..6b8c69d14eec2 100644 --- a/compiler/stable_mir/src/mir/mono.rs +++ b/compiler/stable_mir/src/mir/mono.rs @@ -117,7 +117,7 @@ impl Instance { /// Get this function signature with all types already instantiated. pub fn fn_sig(&self) -> FnSig { - self.ty().kind().fn_sig().unwrap().skip_binder() + with(|cx| cx.instance_sig(self.def)) } /// Check whether this instance is an empty shim. From a66cac92ccde181dfeb5278595b99e2d2394feb2 Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Wed, 13 Dec 2023 16:35:41 -0800 Subject: [PATCH 2/4] Add spread arg and missing CoroutineKind --- .../rustc_smir/src/rustc_smir/convert/mir.rs | 1 + .../rustc_smir/src/rustc_smir/convert/mod.rs | 4 +- compiler/stable_mir/src/mir/body.rs | 69 ++++++++++++++++++- compiler/stable_mir/src/mir/pretty.rs | 9 ++- compiler/stable_mir/src/mir/visit.rs | 2 +- compiler/stable_mir/src/ty.rs | 10 ++- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs index 8c1767501d9a0..41ab4007a6759 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs @@ -36,6 +36,7 @@ impl<'tcx> Stable<'tcx> for mir::Body<'tcx> { .collect(), self.arg_count, self.var_debug_info.iter().map(|info| info.stable(tables)).collect(), + self.spread_arg.stable(tables), ) } } diff --git a/compiler/rustc_smir/src/rustc_smir/convert/mod.rs b/compiler/rustc_smir/src/rustc_smir/convert/mod.rs index 7d8339ab50366..7021bdda73501 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/mod.rs @@ -57,7 +57,9 @@ impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind { stable_mir::mir::CoroutineKind::Gen(source.stable(tables)) } CoroutineKind::Coroutine => stable_mir::mir::CoroutineKind::Coroutine, - CoroutineKind::AsyncGen(_) => todo!(), + CoroutineKind::AsyncGen(source) => { + stable_mir::mir::CoroutineKind::AsyncGen(source.stable(tables)) + } } } } diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 3dfe70963994c..5023af9ab79ec 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -22,6 +22,11 @@ pub struct Body { /// Debug information pertaining to user variables, including captures. pub var_debug_info: Vec, + + /// Mark an argument (which must be a tuple) as getting passed as its individual components. + /// + /// This is used for the "rust-call" ABI such as closures. + pub(super) spread_arg: Option, } pub type BasicBlockIdx = usize; @@ -36,6 +41,7 @@ impl Body { locals: LocalDecls, arg_count: usize, var_debug_info: Vec, + spread_arg: Option, ) -> Self { // If locals doesn't contain enough entries, it can lead to panics in // `ret_local`, `arg_locals`, and `inner_locals`. @@ -43,7 +49,7 @@ impl Body { locals.len() > arg_count, "A Body must contain at least a local for the return value and each of the function's arguments" ); - Self { blocks, locals, arg_count, var_debug_info } + Self { blocks, locals, arg_count, var_debug_info, spread_arg } } /// Return local that holds this function's return value. @@ -75,6 +81,11 @@ impl Body { self.locals.get(local) } + /// Get an iterator for all local declarations. + pub fn local_decls(&self) -> impl Iterator { + self.locals.iter().enumerate() + } + pub fn dump(&self, w: &mut W) -> io::Result<()> { writeln!(w, "{}", function_body(self))?; self.blocks @@ -98,6 +109,10 @@ impl Body { .collect::, _>>()?; Ok(()) } + + pub fn spread_arg(&self) -> Option { + self.spread_arg + } } type LocalDecls = Vec; @@ -248,6 +263,57 @@ pub enum AssertMessage { MisalignedPointerDereference { required: Operand, found: Operand }, } +impl AssertMessage { + pub fn description(&self) -> Result<&'static str, Error> { + match self { + AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow"), + AssertMessage::Overflow(BinOp::Sub, _, _) => Ok("attempt to subtract with overflow"), + AssertMessage::Overflow(BinOp::Mul, _, _) => Ok("attempt to multiply with overflow"), + AssertMessage::Overflow(BinOp::Div, _, _) => Ok("attempt to divide with overflow"), + AssertMessage::Overflow(BinOp::Rem, _, _) => { + Ok("attempt to calculate the remainder with overflow") + } + AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow"), + AssertMessage::Overflow(BinOp::Shr, _, _) => Ok("attempt to shift right with overflow"), + AssertMessage::Overflow(BinOp::Shl, _, _) => Ok("attempt to shift left with overflow"), + AssertMessage::Overflow(op, _, _) => Err(error!("`{:?}` cannot overflow", op)), + AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero"), + AssertMessage::RemainderByZero(_) => { + Ok("attempt to calculate the remainder with a divisor of zero") + } + AssertMessage::ResumedAfterReturn(CoroutineKind::Coroutine) => { + Ok("coroutine resumed after completion") + } + AssertMessage::ResumedAfterReturn(CoroutineKind::Async(_)) => { + Ok("`async fn` resumed after completion") + } + AssertMessage::ResumedAfterReturn(CoroutineKind::Gen(_)) => { + Ok("`async gen fn` resumed after completion") + } + AssertMessage::ResumedAfterReturn(CoroutineKind::AsyncGen(_)) => { + Ok("`gen fn` should just keep returning `AssertMessage::None` after completion") + } + AssertMessage::ResumedAfterPanic(CoroutineKind::Coroutine) => { + Ok("coroutine resumed after panicking") + } + AssertMessage::ResumedAfterPanic(CoroutineKind::Async(_)) => { + Ok("`async fn` resumed after panicking") + } + AssertMessage::ResumedAfterPanic(CoroutineKind::Gen(_)) => { + Ok("`async gen fn` resumed after panicking") + } + AssertMessage::ResumedAfterPanic(CoroutineKind::AsyncGen(_)) => { + Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking") + } + + AssertMessage::BoundsCheck { .. } => Ok("index out of bounds"), + AssertMessage::MisalignedPointerDereference { .. } => { + Ok("misaligned pointer dereference") + } + } + } +} + #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum BinOp { Add, @@ -325,6 +391,7 @@ pub enum CoroutineKind { Async(CoroutineSource), Coroutine, Gen(CoroutineSource), + AsyncGen(CoroutineSource), } #[derive(Copy, Clone, Debug, Eq, PartialEq)] diff --git a/compiler/stable_mir/src/mir/pretty.rs b/compiler/stable_mir/src/mir/pretty.rs index 3a0eed521dc6e..e1cad1fa536fa 100644 --- a/compiler/stable_mir/src/mir/pretty.rs +++ b/compiler/stable_mir/src/mir/pretty.rs @@ -243,6 +243,7 @@ pub fn pretty_assert_message(msg: &AssertMessage) -> String { ); pretty } + AssertMessage::Overflow(op, _, _) => unreachable!("`{:?}` cannot overflow", op), AssertMessage::OverflowNeg(op) => { let pretty_op = pretty_operand(op); pretty.push_str( @@ -262,17 +263,15 @@ pub fn pretty_assert_message(msg: &AssertMessage) -> String { ); pretty } - AssertMessage::ResumedAfterReturn(_) => { - format!("attempt to resume a generator after completion") - } - AssertMessage::ResumedAfterPanic(_) => format!("attempt to resume a panicked generator"), AssertMessage::MisalignedPointerDereference { required, found } => { let pretty_required = pretty_operand(required); let pretty_found = pretty_operand(found); pretty.push_str(format!("\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\",{pretty_required}, {pretty_found}").as_str()); pretty } - _ => todo!(), + AssertMessage::ResumedAfterReturn(_) | AssertMessage::ResumedAfterPanic(_) => { + msg.description().unwrap().to_string() + } } } diff --git a/compiler/stable_mir/src/mir/visit.rs b/compiler/stable_mir/src/mir/visit.rs index d46caad9a016b..98336a729009e 100644 --- a/compiler/stable_mir/src/mir/visit.rs +++ b/compiler/stable_mir/src/mir/visit.rs @@ -133,7 +133,7 @@ pub trait MirVisitor { } fn super_body(&mut self, body: &Body) { - let Body { blocks, locals: _, arg_count, var_debug_info } = body; + let Body { blocks, locals: _, arg_count, var_debug_info, spread_arg: _ } = body; for bb in blocks { self.visit_basic_block(bb); diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index f473fd8dbb773..1fb32621119a4 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -6,6 +6,7 @@ use super::{ use crate::crate_def::CrateDef; use crate::mir::alloc::{read_target_int, read_target_uint, AllocId}; use crate::target::MachineInfo; +use crate::ty::UintTy::U8; use crate::{Filename, Opaque}; use std::fmt::{self, Debug, Display, Formatter}; use std::ops::Range; @@ -22,9 +23,7 @@ impl Debug for Ty { /// Constructors for `Ty`. impl Ty { /// Create a new type from a given kind. - /// - /// Note that not all types may be supported at this point. - fn from_rigid_kind(kind: RigidTy) -> Ty { + pub fn from_rigid_kind(kind: RigidTy) -> Ty { with(|cx| cx.new_rigid_ty(kind)) } @@ -77,6 +76,11 @@ impl Ty { pub fn bool_ty() -> Ty { Ty::from_rigid_kind(RigidTy::Bool) } + + /// Create a type representing `u8`. + pub fn u8_ty() -> Ty { + Ty::from_rigid_kind(RigidTy::Uint(U8)) + } } impl Ty { From c0f1207a125e1cf6b1290eb06561050b94d9d36e Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Thu, 14 Dec 2023 17:56:33 -0800 Subject: [PATCH 3/4] Address PR comments - Remove `fn_sig()` from Instance. - Change return value of `AssertMessage::description` to `Cow<>`. - Add assert to instance `ty()`. - Generalize uint / int type creation. --- compiler/rustc_smir/src/rustc_smir/context.rs | 18 ++---- compiler/stable_mir/src/compiler_interface.rs | 9 +-- compiler/stable_mir/src/mir/body.rs | 55 ++++++++++++------- compiler/stable_mir/src/mir/mono.rs | 7 +-- compiler/stable_mir/src/ty.rs | 12 ++-- tests/ui-fulldeps/stable-mir/check_defs.rs | 2 +- 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/context.rs b/compiler/rustc_smir/src/rustc_smir/context.rs index 5516ca0a5993f..70313cc021e79 100644 --- a/compiler/rustc_smir/src/rustc_smir/context.rs +++ b/compiler/rustc_smir/src/rustc_smir/context.rs @@ -5,7 +5,9 @@ use rustc_middle::ty; use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths}; -use rustc_middle::ty::{GenericPredicates, Instance, ParamEnv, ScalarInt, ValTree}; +use rustc_middle::ty::{ + GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree, +}; use rustc_span::def_id::LOCAL_CRATE; use stable_mir::compiler_interface::Context; use stable_mir::mir::alloc::GlobalAlloc; @@ -13,8 +15,8 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef}; use stable_mir::mir::Body; use stable_mir::target::{MachineInfo, MachineSize}; use stable_mir::ty::{ - AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig, - GenericArgs, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef, + AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs, + LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef, }; use stable_mir::{Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol}; use std::cell::RefCell; @@ -324,18 +326,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> { fn instance_ty(&self, def: InstanceDef) -> stable_mir::ty::Ty { let mut tables = self.0.borrow_mut(); let instance = tables.instances[def]; + assert!(!instance.has_non_region_param(), "{instance:?} needs further substitution"); instance.ty(tables.tcx, ParamEnv::reveal_all()).stable(&mut *tables) } - fn instance_sig(&self, def: InstanceDef) -> FnSig { - let mut tables = self.0.borrow_mut(); - let instance = tables.instances[def]; - let ty = instance.ty(tables.tcx, ParamEnv::reveal_all()); - let sig = if ty.is_fn() { ty.fn_sig(tables.tcx) } else { instance.args.as_closure().sig() }; - // Erase late bound regions. - tables.tcx.instantiate_bound_regions_with_erased(sig).stable(&mut *tables) - } - fn instance_def_id(&self, def: InstanceDef) -> stable_mir::DefId { let mut tables = self.0.borrow_mut(); let def_id = tables.instances[def].def_id(); diff --git a/compiler/stable_mir/src/compiler_interface.rs b/compiler/stable_mir/src/compiler_interface.rs index 8ee82c241308b..2fac59e71fd5b 100644 --- a/compiler/stable_mir/src/compiler_interface.rs +++ b/compiler/stable_mir/src/compiler_interface.rs @@ -10,9 +10,9 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef}; use crate::mir::Body; use crate::target::MachineInfo; use crate::ty::{ - AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig, - GenericArgs, GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, - Span, TraitDecl, TraitDef, Ty, TyKind, VariantDef, + AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs, + GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, + TraitDef, Ty, TyKind, VariantDef, }; use crate::{ mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol, @@ -121,9 +121,6 @@ pub trait Context { /// Get the instance type with generic substitutions applied and lifetimes erased. fn instance_ty(&self, instance: InstanceDef) -> Ty; - /// Get the instance signature with . - fn instance_sig(&self, def: InstanceDef) -> FnSig; - /// Get the instance. fn instance_def_id(&self, instance: InstanceDef) -> DefId; diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 5023af9ab79ec..566a44a64a3d2 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -4,6 +4,7 @@ use crate::ty::{ VariantIdx, }; use crate::{Error, Opaque, Span, Symbol}; +use std::borrow::Cow; use std::io; /// The SMIR representation of a single function. #[derive(Clone, Debug)] @@ -264,51 +265,63 @@ pub enum AssertMessage { } impl AssertMessage { - pub fn description(&self) -> Result<&'static str, Error> { + pub fn description(&self) -> Result, Error> { match self { - AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow"), - AssertMessage::Overflow(BinOp::Sub, _, _) => Ok("attempt to subtract with overflow"), - AssertMessage::Overflow(BinOp::Mul, _, _) => Ok("attempt to multiply with overflow"), - AssertMessage::Overflow(BinOp::Div, _, _) => Ok("attempt to divide with overflow"), + AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow".into()), + AssertMessage::Overflow(BinOp::Sub, _, _) => { + Ok("attempt to subtract with overflow".into()) + } + AssertMessage::Overflow(BinOp::Mul, _, _) => { + Ok("attempt to multiply with overflow".into()) + } + AssertMessage::Overflow(BinOp::Div, _, _) => { + Ok("attempt to divide with overflow".into()) + } AssertMessage::Overflow(BinOp::Rem, _, _) => { - Ok("attempt to calculate the remainder with overflow") + Ok("attempt to calculate the remainder with overflow".into()) + } + AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow".into()), + AssertMessage::Overflow(BinOp::Shr, _, _) => { + Ok("attempt to shift right with overflow".into()) + } + AssertMessage::Overflow(BinOp::Shl, _, _) => { + Ok("attempt to shift left with overflow".into()) } - AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow"), - AssertMessage::Overflow(BinOp::Shr, _, _) => Ok("attempt to shift right with overflow"), - AssertMessage::Overflow(BinOp::Shl, _, _) => Ok("attempt to shift left with overflow"), AssertMessage::Overflow(op, _, _) => Err(error!("`{:?}` cannot overflow", op)), - AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero"), + AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero".into()), AssertMessage::RemainderByZero(_) => { - Ok("attempt to calculate the remainder with a divisor of zero") + Ok("attempt to calculate the remainder with a divisor of zero".into()) } AssertMessage::ResumedAfterReturn(CoroutineKind::Coroutine) => { - Ok("coroutine resumed after completion") + Ok("coroutine resumed after completion".into()) } AssertMessage::ResumedAfterReturn(CoroutineKind::Async(_)) => { - Ok("`async fn` resumed after completion") + Ok("`async fn` resumed after completion".into()) } AssertMessage::ResumedAfterReturn(CoroutineKind::Gen(_)) => { - Ok("`async gen fn` resumed after completion") + Ok("`async gen fn` resumed after completion".into()) } AssertMessage::ResumedAfterReturn(CoroutineKind::AsyncGen(_)) => { - Ok("`gen fn` should just keep returning `AssertMessage::None` after completion") + Ok("`gen fn` should just keep returning `AssertMessage::None` after completion" + .into()) } AssertMessage::ResumedAfterPanic(CoroutineKind::Coroutine) => { - Ok("coroutine resumed after panicking") + Ok("coroutine resumed after panicking".into()) } AssertMessage::ResumedAfterPanic(CoroutineKind::Async(_)) => { - Ok("`async fn` resumed after panicking") + Ok("`async fn` resumed after panicking".into()) } AssertMessage::ResumedAfterPanic(CoroutineKind::Gen(_)) => { - Ok("`async gen fn` resumed after panicking") + Ok("`async gen fn` resumed after panicking".into()) } AssertMessage::ResumedAfterPanic(CoroutineKind::AsyncGen(_)) => { - Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking") + Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking" + .into()) } - AssertMessage::BoundsCheck { .. } => Ok("index out of bounds"), + AssertMessage::BoundsCheck { .. } => Ok("index out of bounds".into()), AssertMessage::MisalignedPointerDereference { .. } => { - Ok("misaligned pointer dereference") + Ok("misaligned pointer dereference".into()) } } } diff --git a/compiler/stable_mir/src/mir/mono.rs b/compiler/stable_mir/src/mir/mono.rs index 6b8c69d14eec2..c126de23c4b6f 100644 --- a/compiler/stable_mir/src/mir/mono.rs +++ b/compiler/stable_mir/src/mir/mono.rs @@ -1,6 +1,6 @@ use crate::crate_def::CrateDef; use crate::mir::Body; -use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, FnSig, GenericArgs, IndexedVal, Ty}; +use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty}; use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque, Symbol}; use std::fmt::{Debug, Formatter}; @@ -115,11 +115,6 @@ impl Instance { }) } - /// Get this function signature with all types already instantiated. - pub fn fn_sig(&self) -> FnSig { - with(|cx| cx.instance_sig(self.def)) - } - /// Check whether this instance is an empty shim. /// /// Allow users to check if this shim can be ignored when called directly. diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index 1fb32621119a4..3d5e264104b3a 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -6,7 +6,6 @@ use super::{ use crate::crate_def::CrateDef; use crate::mir::alloc::{read_target_int, read_target_uint, AllocId}; use crate::target::MachineInfo; -use crate::ty::UintTy::U8; use crate::{Filename, Opaque}; use std::fmt::{self, Debug, Display, Formatter}; use std::ops::Range; @@ -77,9 +76,14 @@ impl Ty { Ty::from_rigid_kind(RigidTy::Bool) } - /// Create a type representing `u8`. - pub fn u8_ty() -> Ty { - Ty::from_rigid_kind(RigidTy::Uint(U8)) + /// Create a type representing a signed integer. + pub fn signed_ty(inner: IntTy) -> Ty { + Ty::from_rigid_kind(RigidTy::Int(inner)) + } + + /// Create a type representing an unsigned integer. + pub fn unsigned_ty(inner: UintTy) -> Ty { + Ty::from_rigid_kind(RigidTy::Uint(inner)) } } diff --git a/tests/ui-fulldeps/stable-mir/check_defs.rs b/tests/ui-fulldeps/stable-mir/check_defs.rs index d311be5982d2d..ad66751133214 100644 --- a/tests/ui-fulldeps/stable-mir/check_defs.rs +++ b/tests/ui-fulldeps/stable-mir/check_defs.rs @@ -69,7 +69,7 @@ fn extract_elem_ty(ty: Ty) -> Ty { /// Check signature and type of `Vec::::new` and its generic version. fn test_vec_new(instance: mir::mono::Instance) { - let sig = instance.fn_sig(); + let sig = instance.ty().kind().fn_sig().unwrap().skip_binder(); assert_matches!(sig.inputs(), &[]); let elem_ty = extract_elem_ty(sig.output()); assert_matches!(elem_ty.kind(), TyKind::RigidTy(RigidTy::Uint(UintTy::U8))); From 600438679d2ea06d0284497a42215f2f243fd1d9 Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Thu, 14 Dec 2023 22:04:14 -0800 Subject: [PATCH 4/4] Revert signature change for AssertMessage description --- compiler/stable_mir/src/mir/body.rs | 55 +++++++++++------------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index 566a44a64a3d2..5023af9ab79ec 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -4,7 +4,6 @@ use crate::ty::{ VariantIdx, }; use crate::{Error, Opaque, Span, Symbol}; -use std::borrow::Cow; use std::io; /// The SMIR representation of a single function. #[derive(Clone, Debug)] @@ -265,63 +264,51 @@ pub enum AssertMessage { } impl AssertMessage { - pub fn description(&self) -> Result, Error> { + pub fn description(&self) -> Result<&'static str, Error> { match self { - AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow".into()), - AssertMessage::Overflow(BinOp::Sub, _, _) => { - Ok("attempt to subtract with overflow".into()) - } - AssertMessage::Overflow(BinOp::Mul, _, _) => { - Ok("attempt to multiply with overflow".into()) - } - AssertMessage::Overflow(BinOp::Div, _, _) => { - Ok("attempt to divide with overflow".into()) - } + AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow"), + AssertMessage::Overflow(BinOp::Sub, _, _) => Ok("attempt to subtract with overflow"), + AssertMessage::Overflow(BinOp::Mul, _, _) => Ok("attempt to multiply with overflow"), + AssertMessage::Overflow(BinOp::Div, _, _) => Ok("attempt to divide with overflow"), AssertMessage::Overflow(BinOp::Rem, _, _) => { - Ok("attempt to calculate the remainder with overflow".into()) - } - AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow".into()), - AssertMessage::Overflow(BinOp::Shr, _, _) => { - Ok("attempt to shift right with overflow".into()) - } - AssertMessage::Overflow(BinOp::Shl, _, _) => { - Ok("attempt to shift left with overflow".into()) + Ok("attempt to calculate the remainder with overflow") } + AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow"), + AssertMessage::Overflow(BinOp::Shr, _, _) => Ok("attempt to shift right with overflow"), + AssertMessage::Overflow(BinOp::Shl, _, _) => Ok("attempt to shift left with overflow"), AssertMessage::Overflow(op, _, _) => Err(error!("`{:?}` cannot overflow", op)), - AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero".into()), + AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero"), AssertMessage::RemainderByZero(_) => { - Ok("attempt to calculate the remainder with a divisor of zero".into()) + Ok("attempt to calculate the remainder with a divisor of zero") } AssertMessage::ResumedAfterReturn(CoroutineKind::Coroutine) => { - Ok("coroutine resumed after completion".into()) + Ok("coroutine resumed after completion") } AssertMessage::ResumedAfterReturn(CoroutineKind::Async(_)) => { - Ok("`async fn` resumed after completion".into()) + Ok("`async fn` resumed after completion") } AssertMessage::ResumedAfterReturn(CoroutineKind::Gen(_)) => { - Ok("`async gen fn` resumed after completion".into()) + Ok("`async gen fn` resumed after completion") } AssertMessage::ResumedAfterReturn(CoroutineKind::AsyncGen(_)) => { - Ok("`gen fn` should just keep returning `AssertMessage::None` after completion" - .into()) + Ok("`gen fn` should just keep returning `AssertMessage::None` after completion") } AssertMessage::ResumedAfterPanic(CoroutineKind::Coroutine) => { - Ok("coroutine resumed after panicking".into()) + Ok("coroutine resumed after panicking") } AssertMessage::ResumedAfterPanic(CoroutineKind::Async(_)) => { - Ok("`async fn` resumed after panicking".into()) + Ok("`async fn` resumed after panicking") } AssertMessage::ResumedAfterPanic(CoroutineKind::Gen(_)) => { - Ok("`async gen fn` resumed after panicking".into()) + Ok("`async gen fn` resumed after panicking") } AssertMessage::ResumedAfterPanic(CoroutineKind::AsyncGen(_)) => { - Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking" - .into()) + Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking") } - AssertMessage::BoundsCheck { .. } => Ok("index out of bounds".into()), + AssertMessage::BoundsCheck { .. } => Ok("index out of bounds"), AssertMessage::MisalignedPointerDereference { .. } => { - Ok("misaligned pointer dereference".into()) + Ok("misaligned pointer dereference") } } }