Skip to content
Merged
4 changes: 4 additions & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl<'tcx> Tables<'tcx> {
stable_mir::ty::ImplDef(self.create_def_id(did))
}

pub fn region_def(&mut self, did: DefId) -> stable_mir::ty::RegionDef {
stable_mir::ty::RegionDef(self.create_def_id(did))
}

pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
stable_mir::ty::Prov(self.create_alloc_id(aid))
}
Expand Down
39 changes: 35 additions & 4 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//!
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.

use hir::def::DefKind;
use crate::rustc_smir::hir::def::DefKind;
use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
use rustc_hir as hir;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{alloc_range, AllocId};
Expand Down Expand Up @@ -1500,9 +1501,39 @@ impl<'tcx> Stable<'tcx> for ty::ImplPolarity {
impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
type T = stable_mir::ty::Region;

fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
// FIXME: add a real implementation of stable regions
opaque(self)
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
Region { kind: self.kind().stable(tables) }
}
}

impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> {
type T = stable_mir::ty::RegionKind;

fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
use stable_mir::ty::RegionKind;
match self {
ty::ReEarlyBound(early_reg) => RegionKind::ReEarlyBound(EarlyBoundRegion {
def_id: tables.region_def(early_reg.def_id),
index: early_reg.index,
name: early_reg.name.to_string(),
}),
ty::ReLateBound(db_index, bound_reg) => RegionKind::ReLateBound(
db_index.as_u32(),
BoundRegion { var: bound_reg.var.as_u32(), kind: bound_reg.kind.stable(tables) },
),
ty::ReStatic => RegionKind::ReStatic,
ty::RePlaceholder(place_holder) => {
RegionKind::RePlaceholder(stable_mir::ty::Placeholder {
universe: place_holder.universe.as_u32(),
bound: BoundRegion {
var: place_holder.bound.var.as_u32(),
kind: place_holder.bound.kind.stable(tables),
},
})
}
ty::ReErased => RegionKind::ReErased,
_ => unimplemented!(),
}
}
}

Expand Down
47 changes: 45 additions & 2 deletions compiler/stable_mir/src/fold.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::ops::ControlFlow;

use crate::Opaque;
use crate::{
ty::{self, BoundRegion, BoundRegionKind},
Opaque,
};

use super::ty::{
Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind,
GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
GenericArgs, Promoted, Region, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
};

pub trait Folder: Sized {
Expand All @@ -15,6 +18,9 @@ pub trait Folder: Sized {
fn fold_const(&mut self, c: &Const) -> ControlFlow<Self::Break, Const> {
c.super_fold(self)
}
fn visit_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break, Region> {
reg.super_fold(self)
}
}

pub trait Foldable: Sized + Clone {
Expand Down Expand Up @@ -106,6 +112,43 @@ impl Foldable for GenericArgs {
}
}

impl Foldable for Region {
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
folder.visit_reg(self)
}
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
let mut kind = self.kind.clone();
match &mut kind {
crate::ty::RegionKind::ReEarlyBound(_) => {}
crate::ty::RegionKind::ReLateBound(_, bound_reg) => {
*bound_reg = bound_reg.fold(folder)?
}
crate::ty::RegionKind::ReStatic => {}
crate::ty::RegionKind::RePlaceholder(bound_reg) => {
bound_reg.bound = bound_reg.bound.fold(folder)?
}
crate::ty::RegionKind::ReErased => {}
}
ControlFlow::Continue(ty::Region { kind: kind }.into())
}
}

impl Foldable for BoundRegion {
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
ControlFlow::Continue(BoundRegion { var: self.var, kind: self.kind.fold(folder)? })
}
}

impl Foldable for BoundRegionKind {
fn super_fold<V: Folder>(&self, _folder: &mut V) -> ControlFlow<V::Break, Self> {
match self {
BoundRegionKind::BrAnon => ControlFlow::Continue(self.clone()),
BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(self.clone()),
BoundRegionKind::BrEnv => ControlFlow::Continue(self.clone()),
}
}
}

impl Foldable for GenericArgKind {
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
let mut this = self.clone();
Expand Down
46 changes: 44 additions & 2 deletions compiler/stable_mir/src/ty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
mir::Safety,
mir::{Body, Mutability},
with, AllocId, DefId,
with, AllocId, DefId, Symbol,
};
use crate::Opaque;
use std::fmt::{self, Debug, Formatter};
Expand Down Expand Up @@ -34,7 +34,46 @@ pub struct Const {
}

type Ident = Opaque;
pub type Region = Opaque;

#[derive(Debug, Clone)]
pub struct Region {
pub kind: RegionKind,
}

#[derive(Debug, Clone)]
pub enum RegionKind {
ReEarlyBound(EarlyBoundRegion),
ReLateBound(DebruijnIndex, BoundRegion),
ReStatic,
RePlaceholder(Placeholder<BoundRegion>),
ReErased,
}

pub(crate) type DebruijnIndex = u32;

#[derive(Debug, Clone)]
pub struct EarlyBoundRegion {
pub def_id: RegionDef,
pub index: u32,
pub name: Symbol,
}

pub(crate) type BoundVar = u32;

#[derive(Debug, Clone)]
pub struct BoundRegion {
pub var: BoundVar,
pub kind: BoundRegionKind,
}

pub(crate) type UniverseIndex = u32;

#[derive(Debug, Clone)]
pub struct Placeholder<T> {
pub universe: UniverseIndex,
pub bound: T,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span(pub usize);

Expand Down Expand Up @@ -152,6 +191,9 @@ pub struct ConstDef(pub DefId);
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ImplDef(pub DefId);

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct RegionDef(pub DefId);

#[derive(Clone, Debug)]
pub struct GenericArgs(pub Vec<GenericArgKind>);

Expand Down
43 changes: 41 additions & 2 deletions compiler/stable_mir/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::ops::ControlFlow;

use crate::Opaque;
use crate::{
ty::{BoundRegion, BoundRegionKind},
Opaque,
};

use super::ty::{
Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs,
Promoted, RigidTy, TermKind, Ty, UnevaluatedConst,
Promoted, Region, RigidTy, TermKind, Ty, UnevaluatedConst,
};

pub trait Visitor: Sized {
Expand All @@ -15,6 +18,9 @@ pub trait Visitor: Sized {
fn visit_const(&mut self, c: &Const) -> ControlFlow<Self::Break> {
c.super_visit(self)
}
fn visit_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break> {
reg.super_visit(self)
}
}

pub trait Visitable {
Expand Down Expand Up @@ -101,6 +107,39 @@ impl Visitable for GenericArgs {
}
}

impl Visitable for Region {
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
visitor.visit_reg(self)
}

fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self.kind.clone() {
crate::ty::RegionKind::ReEarlyBound(_) => {}
crate::ty::RegionKind::ReLateBound(_, bound_reg) => bound_reg.visit(visitor)?,
crate::ty::RegionKind::ReStatic => {}
crate::ty::RegionKind::RePlaceholder(bound_reg) => bound_reg.bound.visit(visitor)?,
crate::ty::RegionKind::ReErased => {}
}
ControlFlow::Continue(())
}
}

impl Visitable for BoundRegion {
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
self.kind.visit(visitor)
}
}

impl Visitable for BoundRegionKind {
fn super_visit<V: Visitor>(&self, _visitor: &mut V) -> ControlFlow<V::Break> {
match self {
BoundRegionKind::BrAnon => ControlFlow::Continue(()),
BoundRegionKind::BrNamed(_, _) => ControlFlow::Continue(()),
BoundRegionKind::BrEnv => ControlFlow::Continue(()),
}
}
}

impl Visitable for GenericArgKind {
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
match self {
Expand Down