Skip to content

Commit c0f1207

Browse files
committed
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.
1 parent a66cac9 commit c0f1207

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
lines changed

compiler/rustc_smir/src/rustc_smir/context.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
66
use rustc_middle::ty;
77
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
8-
use rustc_middle::ty::{GenericPredicates, Instance, ParamEnv, ScalarInt, ValTree};
8+
use rustc_middle::ty::{
9+
GenericPredicates, Instance, ParamEnv, ScalarInt, TypeVisitableExt, ValTree,
10+
};
911
use rustc_span::def_id::LOCAL_CRATE;
1012
use stable_mir::compiler_interface::Context;
1113
use stable_mir::mir::alloc::GlobalAlloc;
1214
use stable_mir::mir::mono::{InstanceDef, StaticDef};
1315
use stable_mir::mir::Body;
1416
use stable_mir::target::{MachineInfo, MachineSize};
1517
use stable_mir::ty::{
16-
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig,
17-
GenericArgs, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef,
18+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
19+
LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, VariantDef,
1820
};
1921
use stable_mir::{Crate, CrateItem, DefId, Error, Filename, ItemKind, Symbol};
2022
use std::cell::RefCell;
@@ -324,18 +326,10 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
324326
fn instance_ty(&self, def: InstanceDef) -> stable_mir::ty::Ty {
325327
let mut tables = self.0.borrow_mut();
326328
let instance = tables.instances[def];
329+
assert!(!instance.has_non_region_param(), "{instance:?} needs further substitution");
327330
instance.ty(tables.tcx, ParamEnv::reveal_all()).stable(&mut *tables)
328331
}
329332

330-
fn instance_sig(&self, def: InstanceDef) -> FnSig {
331-
let mut tables = self.0.borrow_mut();
332-
let instance = tables.instances[def];
333-
let ty = instance.ty(tables.tcx, ParamEnv::reveal_all());
334-
let sig = if ty.is_fn() { ty.fn_sig(tables.tcx) } else { instance.args.as_closure().sig() };
335-
// Erase late bound regions.
336-
tables.tcx.instantiate_bound_regions_with_erased(sig).stable(&mut *tables)
337-
}
338-
339333
fn instance_def_id(&self, def: InstanceDef) -> stable_mir::DefId {
340334
let mut tables = self.0.borrow_mut();
341335
let def_id = tables.instances[def].def_id();

compiler/stable_mir/src/compiler_interface.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef};
1010
use crate::mir::Body;
1111
use crate::target::MachineInfo;
1212
use crate::ty::{
13-
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, FnSig,
14-
GenericArgs, GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy,
15-
Span, TraitDecl, TraitDef, Ty, TyKind, VariantDef,
13+
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
14+
GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl,
15+
TraitDef, Ty, TyKind, VariantDef,
1616
};
1717
use crate::{
1818
mir, Crate, CrateItem, CrateItems, DefId, Error, Filename, ImplTraitDecls, ItemKind, Symbol,
@@ -121,9 +121,6 @@ pub trait Context {
121121
/// Get the instance type with generic substitutions applied and lifetimes erased.
122122
fn instance_ty(&self, instance: InstanceDef) -> Ty;
123123

124-
/// Get the instance signature with .
125-
fn instance_sig(&self, def: InstanceDef) -> FnSig;
126-
127124
/// Get the instance.
128125
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
129126

compiler/stable_mir/src/mir/body.rs

+34-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::ty::{
44
VariantIdx,
55
};
66
use crate::{Error, Opaque, Span, Symbol};
7+
use std::borrow::Cow;
78
use std::io;
89
/// The SMIR representation of a single function.
910
#[derive(Clone, Debug)]
@@ -264,51 +265,63 @@ pub enum AssertMessage {
264265
}
265266

266267
impl AssertMessage {
267-
pub fn description(&self) -> Result<&'static str, Error> {
268+
pub fn description(&self) -> Result<Cow<'static, str>, Error> {
268269
match self {
269-
AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow"),
270-
AssertMessage::Overflow(BinOp::Sub, _, _) => Ok("attempt to subtract with overflow"),
271-
AssertMessage::Overflow(BinOp::Mul, _, _) => Ok("attempt to multiply with overflow"),
272-
AssertMessage::Overflow(BinOp::Div, _, _) => Ok("attempt to divide with overflow"),
270+
AssertMessage::Overflow(BinOp::Add, _, _) => Ok("attempt to add with overflow".into()),
271+
AssertMessage::Overflow(BinOp::Sub, _, _) => {
272+
Ok("attempt to subtract with overflow".into())
273+
}
274+
AssertMessage::Overflow(BinOp::Mul, _, _) => {
275+
Ok("attempt to multiply with overflow".into())
276+
}
277+
AssertMessage::Overflow(BinOp::Div, _, _) => {
278+
Ok("attempt to divide with overflow".into())
279+
}
273280
AssertMessage::Overflow(BinOp::Rem, _, _) => {
274-
Ok("attempt to calculate the remainder with overflow")
281+
Ok("attempt to calculate the remainder with overflow".into())
282+
}
283+
AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow".into()),
284+
AssertMessage::Overflow(BinOp::Shr, _, _) => {
285+
Ok("attempt to shift right with overflow".into())
286+
}
287+
AssertMessage::Overflow(BinOp::Shl, _, _) => {
288+
Ok("attempt to shift left with overflow".into())
275289
}
276-
AssertMessage::OverflowNeg(_) => Ok("attempt to negate with overflow"),
277-
AssertMessage::Overflow(BinOp::Shr, _, _) => Ok("attempt to shift right with overflow"),
278-
AssertMessage::Overflow(BinOp::Shl, _, _) => Ok("attempt to shift left with overflow"),
279290
AssertMessage::Overflow(op, _, _) => Err(error!("`{:?}` cannot overflow", op)),
280-
AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero"),
291+
AssertMessage::DivisionByZero(_) => Ok("attempt to divide by zero".into()),
281292
AssertMessage::RemainderByZero(_) => {
282-
Ok("attempt to calculate the remainder with a divisor of zero")
293+
Ok("attempt to calculate the remainder with a divisor of zero".into())
283294
}
284295
AssertMessage::ResumedAfterReturn(CoroutineKind::Coroutine) => {
285-
Ok("coroutine resumed after completion")
296+
Ok("coroutine resumed after completion".into())
286297
}
287298
AssertMessage::ResumedAfterReturn(CoroutineKind::Async(_)) => {
288-
Ok("`async fn` resumed after completion")
299+
Ok("`async fn` resumed after completion".into())
289300
}
290301
AssertMessage::ResumedAfterReturn(CoroutineKind::Gen(_)) => {
291-
Ok("`async gen fn` resumed after completion")
302+
Ok("`async gen fn` resumed after completion".into())
292303
}
293304
AssertMessage::ResumedAfterReturn(CoroutineKind::AsyncGen(_)) => {
294-
Ok("`gen fn` should just keep returning `AssertMessage::None` after completion")
305+
Ok("`gen fn` should just keep returning `AssertMessage::None` after completion"
306+
.into())
295307
}
296308
AssertMessage::ResumedAfterPanic(CoroutineKind::Coroutine) => {
297-
Ok("coroutine resumed after panicking")
309+
Ok("coroutine resumed after panicking".into())
298310
}
299311
AssertMessage::ResumedAfterPanic(CoroutineKind::Async(_)) => {
300-
Ok("`async fn` resumed after panicking")
312+
Ok("`async fn` resumed after panicking".into())
301313
}
302314
AssertMessage::ResumedAfterPanic(CoroutineKind::Gen(_)) => {
303-
Ok("`async gen fn` resumed after panicking")
315+
Ok("`async gen fn` resumed after panicking".into())
304316
}
305317
AssertMessage::ResumedAfterPanic(CoroutineKind::AsyncGen(_)) => {
306-
Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking")
318+
Ok("`gen fn` should just keep returning `AssertMessage::None` after panicking"
319+
.into())
307320
}
308321

309-
AssertMessage::BoundsCheck { .. } => Ok("index out of bounds"),
322+
AssertMessage::BoundsCheck { .. } => Ok("index out of bounds".into()),
310323
AssertMessage::MisalignedPointerDereference { .. } => {
311-
Ok("misaligned pointer dereference")
324+
Ok("misaligned pointer dereference".into())
312325
}
313326
}
314327
}

compiler/stable_mir/src/mir/mono.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::crate_def::CrateDef;
22
use crate::mir::Body;
3-
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, FnSig, GenericArgs, IndexedVal, Ty};
3+
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
44
use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque, Symbol};
55
use std::fmt::{Debug, Formatter};
66

@@ -115,11 +115,6 @@ impl Instance {
115115
})
116116
}
117117

118-
/// Get this function signature with all types already instantiated.
119-
pub fn fn_sig(&self) -> FnSig {
120-
with(|cx| cx.instance_sig(self.def))
121-
}
122-
123118
/// Check whether this instance is an empty shim.
124119
///
125120
/// Allow users to check if this shim can be ignored when called directly.

compiler/stable_mir/src/ty.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use super::{
66
use crate::crate_def::CrateDef;
77
use crate::mir::alloc::{read_target_int, read_target_uint, AllocId};
88
use crate::target::MachineInfo;
9-
use crate::ty::UintTy::U8;
109
use crate::{Filename, Opaque};
1110
use std::fmt::{self, Debug, Display, Formatter};
1211
use std::ops::Range;
@@ -77,9 +76,14 @@ impl Ty {
7776
Ty::from_rigid_kind(RigidTy::Bool)
7877
}
7978

80-
/// Create a type representing `u8`.
81-
pub fn u8_ty() -> Ty {
82-
Ty::from_rigid_kind(RigidTy::Uint(U8))
79+
/// Create a type representing a signed integer.
80+
pub fn signed_ty(inner: IntTy) -> Ty {
81+
Ty::from_rigid_kind(RigidTy::Int(inner))
82+
}
83+
84+
/// Create a type representing an unsigned integer.
85+
pub fn unsigned_ty(inner: UintTy) -> Ty {
86+
Ty::from_rigid_kind(RigidTy::Uint(inner))
8387
}
8488
}
8589

tests/ui-fulldeps/stable-mir/check_defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn extract_elem_ty(ty: Ty) -> Ty {
6969

7070
/// Check signature and type of `Vec::<u8>::new` and its generic version.
7171
fn test_vec_new(instance: mir::mono::Instance) {
72-
let sig = instance.fn_sig();
72+
let sig = instance.ty().kind().fn_sig().unwrap().skip_binder();
7373
assert_matches!(sig.inputs(), &[]);
7474
let elem_ty = extract_elem_ty(sig.output());
7575
assert_matches!(elem_ty.kind(), TyKind::RigidTy(RigidTy::Uint(UintTy::U8)));

0 commit comments

Comments
 (0)