Skip to content

Rollup of 6 pull requests #118723

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

Merged
merged 15 commits into from
Dec 8, 2023
Merged
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_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ pub(crate) struct ExternItemCannotBeConst {
#[primary_span]
pub ident_span: Span,
#[suggestion(code = "static ", applicability = "machine-applicable")]
pub const_span: Span,
pub const_span: Option<Span>,
}

#[derive(Diagnostic)]
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,11 @@ impl<'a> Parser<'a> {
Ok(kind) => kind,
Err(kind) => match kind {
ItemKind::Const(box ConstItem { ty, expr, .. }) => {
let const_span = Some(span.with_hi(ident.span.lo()))
.filter(|span| span.can_be_used_for_suggestions());
self.sess.emit_err(errors::ExternItemCannotBeConst {
ident_span: ident.span,
const_span: span.with_hi(ident.span.lo()),
const_span,
});
ForeignItemKind::Static(ty, Mutability::Not, expr)
}
Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.tcx.mk_ty_from_kind(internal_kind).stable(&mut *tables)
}

#[allow(rustc::usage_of_qualified_ty)]
fn new_box_ty(&self, ty: stable_mir::ty::Ty) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut();
let inner = ty.internal(&mut *tables);
ty::Ty::new_box(tables.tcx, inner).stable(&mut *tables)
}

fn def_ty(&self, item: stable_mir::DefId) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut();
tables.tcx.type_of(item.internal(&mut *tables)).instantiate_identity().stable(&mut *tables)
Expand All @@ -276,6 +283,13 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
tables.types[ty].kind().stable(&mut *tables)
}

fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> stable_mir::ty::Ty {
let mut tables = self.0.borrow_mut();
let internal_kind = ty.internal(&mut *tables);
let internal_ty = tables.tcx.mk_ty_from_kind(internal_kind);
internal_ty.discriminant_ty(tables.tcx).stable(&mut *tables)
}

fn instance_body(&self, def: InstanceDef) -> Option<Body> {
let mut tables = self.0.borrow_mut();
let instance = tables.instances[def];
Expand Down Expand Up @@ -308,9 +322,9 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
matches!(instance.def, ty::InstanceDef::DropGlue(_, None))
}

fn mono_instance(&self, item: stable_mir::CrateItem) -> stable_mir::mir::mono::Instance {
fn mono_instance(&self, def_id: stable_mir::DefId) -> stable_mir::mir::mono::Instance {
let mut tables = self.0.borrow_mut();
let def_id = tables[item.0];
let def_id = tables[def_id];
Instance::mono(tables.tcx, def_id).stable(&mut *tables)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,7 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
let mut ty = ty;

match ty.kind() {
ty::Float(..)
| ty::Char
| ty::Str
| ty::Never
| ty::Foreign(..)
| ty::CoroutineWitness(..) => {}
ty::Float(..) | ty::Str | ty::Never | ty::Foreign(..) | ty::CoroutineWitness(..) => {}

ty::Bool => {
if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
Expand All @@ -792,6 +787,14 @@ fn transform_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, options: TransformTyOptio
}
}

ty::Char => {
if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
// Since #118032, char is guaranteed to have the same size, alignment, and function
// call ABI as u32 on all platforms.
ty = tcx.types.u32;
}
}

ty::Int(..) | ty::Uint(..) => {
if options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
// Note: C99 7.18.2.4 requires uintptr_t and intptr_t to be at least 16-bit wide.
Expand Down

This file was deleted.

This file was deleted.

17 changes: 7 additions & 10 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -761,18 +761,15 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
let defer_to_coercion = self.tcx().features().object_safe_for_dispatch;

if !defer_to_coercion {
let cause = self.cause(traits::WellFormed(None));
let component_traits = data.auto_traits().chain(data.principal_def_id());
let tcx = self.tcx();
self.out.extend(component_traits.map(|did| {
traits::Obligation::with_depth(
tcx,
cause.clone(),
if let Some(principal) = data.principal_def_id() {
self.out.push(traits::Obligation::with_depth(
self.tcx(),
self.cause(traits::WellFormed(None)),
depth,
param_env,
ty::Binder::dummy(ty::PredicateKind::ObjectSafe(did)),
)
}));
ty::Binder::dummy(ty::PredicateKind::ObjectSafe(principal)),
));
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion compiler/stable_mir/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub trait Context {
/// Create a new type from the given kind.
fn new_rigid_ty(&self, kind: RigidTy) -> Ty;

/// Create a new box type, `Box<T>`, for the given inner type `T`.
fn new_box_ty(&self, ty: Ty) -> Ty;

/// Returns the type of given crate item.
fn def_ty(&self, item: DefId) -> Ty;

Expand All @@ -102,6 +105,9 @@ pub trait Context {
/// Obtain the representation of a type.
fn ty_kind(&self, ty: Ty) -> TyKind;

// Get the discriminant Ty for this Ty if there's one.
fn rigid_ty_discriminant_ty(&self, ty: &RigidTy) -> Ty;

/// Get the body of an Instance which is already monomorphized.
fn instance_body(&self, instance: InstanceDef) -> Option<Body>;

Expand All @@ -119,7 +125,7 @@ pub trait Context {

/// Convert a non-generic crate item into an instance.
/// This function will panic if the item is generic.
fn mono_instance(&self, item: CrateItem) -> Instance;
fn mono_instance(&self, def_id: DefId) -> Instance;

/// Item requires monomorphization.
fn requires_monomorphization(&self, def_id: DefId) -> bool;
Expand Down
100 changes: 100 additions & 0 deletions compiler/stable_mir/src/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,38 @@ pub enum BinOp {
Offset,
}

impl BinOp {
/// Return the type of this operation for the given input Ty.
/// This function does not perform type checking, and it currently doesn't handle SIMD.
pub fn ty(&self, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
assert!(lhs_ty.kind().is_primitive());
assert!(rhs_ty.kind().is_primitive());
match self {
BinOp::Add
| BinOp::AddUnchecked
| BinOp::Sub
| BinOp::SubUnchecked
| BinOp::Mul
| BinOp::MulUnchecked
| BinOp::Div
| BinOp::Rem
| BinOp::BitXor
| BinOp::BitAnd
| BinOp::BitOr => {
assert_eq!(lhs_ty, rhs_ty);
lhs_ty
}
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked | BinOp::Offset => {
lhs_ty
}
BinOp::Eq | BinOp::Lt | BinOp::Le | BinOp::Ne | BinOp::Ge | BinOp::Gt => {
assert_eq!(lhs_ty, rhs_ty);
Ty::bool_ty()
}
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UnOp {
Not,
Expand Down Expand Up @@ -475,6 +507,63 @@ pub enum Rvalue {
Use(Operand),
}

impl Rvalue {
pub fn ty(&self, locals: &[LocalDecl]) -> Result<Ty, Error> {
match self {
Rvalue::Use(operand) => operand.ty(locals),
Rvalue::Repeat(operand, count) => {
Ok(Ty::new_array_with_const_len(operand.ty(locals)?, count.clone()))
}
Rvalue::ThreadLocalRef(did) => Ok(did.ty()),
Rvalue::Ref(reg, bk, place) => {
let place_ty = place.ty(locals)?;
Ok(Ty::new_ref(reg.clone(), place_ty, bk.to_mutable_lossy()))
}
Rvalue::AddressOf(mutability, place) => {
let place_ty = place.ty(locals)?;
Ok(Ty::new_ptr(place_ty, *mutability))
}
Rvalue::Len(..) => Ok(Ty::usize_ty()),
Rvalue::Cast(.., ty) => Ok(*ty),
Rvalue::BinaryOp(op, lhs, rhs) => {
let lhs_ty = lhs.ty(locals)?;
let rhs_ty = rhs.ty(locals)?;
Ok(op.ty(lhs_ty, rhs_ty))
}
Rvalue::CheckedBinaryOp(op, lhs, rhs) => {
let lhs_ty = lhs.ty(locals)?;
let rhs_ty = rhs.ty(locals)?;
let ty = op.ty(lhs_ty, rhs_ty);
Ok(Ty::new_tuple(&[ty, Ty::bool_ty()]))
}
Rvalue::UnaryOp(UnOp::Not | UnOp::Neg, operand) => operand.ty(locals),
Rvalue::Discriminant(place) => {
let place_ty = place.ty(locals)?;
place_ty
.kind()
.discriminant_ty()
.ok_or_else(|| error!("Expected a `RigidTy` but found: {place_ty:?}"))
}
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
Ok(Ty::usize_ty())
}
Rvalue::Aggregate(ak, ops) => match *ak {
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
AggregateKind::Tuple => Ok(Ty::new_tuple(
&ops.iter().map(|op| op.ty(locals)).collect::<Result<Vec<_>, _>>()?,
)),
AggregateKind::Adt(def, _, ref args, _, _) => Ok(def.ty_with_args(args)),
AggregateKind::Closure(def, ref args) => Ok(Ty::new_closure(def, args.clone())),
AggregateKind::Coroutine(def, ref args, mov) => {
Ok(Ty::new_coroutine(def, args.clone(), mov))
}
},
Rvalue::ShallowInitBox(_, ty) => Ok(Ty::new_box(*ty)),
Rvalue::CopyForDeref(place) => place.ty(locals),
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AggregateKind {
Array(Ty),
Expand Down Expand Up @@ -725,6 +814,17 @@ pub enum BorrowKind {
},
}

impl BorrowKind {
pub fn to_mutable_lossy(self) -> Mutability {
match self {
BorrowKind::Mut { .. } => Mutability::Mut,
BorrowKind::Shared => Mutability::Not,
// FIXME: There's no type corresponding to a shallow borrow, so use `&` as an approximation.
BorrowKind::Fake => Mutability::Not,
}
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MutBorrowKind {
Default,
Expand Down
20 changes: 18 additions & 2 deletions compiler/stable_mir/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ impl TryFrom<CrateItem> for Instance {

fn try_from(item: CrateItem) -> Result<Self, Self::Error> {
with(|context| {
if !context.requires_monomorphization(item.0) {
Ok(context.mono_instance(item))
let def_id = item.def_id();
if !context.requires_monomorphization(def_id) {
Ok(context.mono_instance(def_id))
} else {
Err(Error::new("Item requires monomorphization".to_string()))
}
Expand Down Expand Up @@ -219,6 +220,21 @@ impl TryFrom<CrateItem> for StaticDef {
}
}

impl TryFrom<Instance> for StaticDef {
type Error = crate::Error;

fn try_from(value: Instance) -> Result<Self, Self::Error> {
StaticDef::try_from(CrateItem::try_from(value)?)
}
}

impl From<StaticDef> for Instance {
fn from(value: StaticDef) -> Self {
// A static definition should always be convertible to an instance.
with(|cx| cx.mono_instance(value.def_id()))
}
}

impl StaticDef {
/// Return the type of this static definition.
pub fn ty(&self) -> Ty {
Expand Down
Loading