Skip to content

Commit 74345c7

Browse files
authored
Unrolled build for rust-lang#116024
Rollup merge of rust-lang#116024 - ouz-a:smir_region, r=oli-obk Implement Region for smir Adds Region and it's relevant types to smir and covers them with stable implementation r? `@oli-obk`
2 parents 56ada88 + 34f10e2 commit 74345c7

File tree

5 files changed

+121
-13
lines changed

5 files changed

+121
-13
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ impl<'tcx> Tables<'tcx> {
8686
stable_mir::ty::ImplDef(self.create_def_id(did))
8787
}
8888

89+
pub fn region_def(&mut self, did: DefId) -> stable_mir::ty::RegionDef {
90+
stable_mir::ty::RegionDef(self.create_def_id(did))
91+
}
92+
8993
pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
9094
stable_mir::ty::Prov(self.create_alloc_id(aid))
9195
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use hir::def::DefKind;
10+
use crate::rustc_smir::hir::def::DefKind;
11+
use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
1112
use rustc_hir as hir;
1213
use rustc_middle::mir;
1314
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@@ -1500,9 +1501,39 @@ impl<'tcx> Stable<'tcx> for ty::ImplPolarity {
15001501
impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
15011502
type T = stable_mir::ty::Region;
15021503

1503-
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1504-
// FIXME: add a real implementation of stable regions
1505-
opaque(self)
1504+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1505+
Region { kind: self.kind().stable(tables) }
1506+
}
1507+
}
1508+
1509+
impl<'tcx> Stable<'tcx> for ty::RegionKind<'tcx> {
1510+
type T = stable_mir::ty::RegionKind;
1511+
1512+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1513+
use stable_mir::ty::RegionKind;
1514+
match self {
1515+
ty::ReEarlyBound(early_reg) => RegionKind::ReEarlyBound(EarlyBoundRegion {
1516+
def_id: tables.region_def(early_reg.def_id),
1517+
index: early_reg.index,
1518+
name: early_reg.name.to_string(),
1519+
}),
1520+
ty::ReLateBound(db_index, bound_reg) => RegionKind::ReLateBound(
1521+
db_index.as_u32(),
1522+
BoundRegion { var: bound_reg.var.as_u32(), kind: bound_reg.kind.stable(tables) },
1523+
),
1524+
ty::ReStatic => RegionKind::ReStatic,
1525+
ty::RePlaceholder(place_holder) => {
1526+
RegionKind::RePlaceholder(stable_mir::ty::Placeholder {
1527+
universe: place_holder.universe.as_u32(),
1528+
bound: BoundRegion {
1529+
var: place_holder.bound.var.as_u32(),
1530+
kind: place_holder.bound.kind.stable(tables),
1531+
},
1532+
})
1533+
}
1534+
ty::ReErased => RegionKind::ReErased,
1535+
_ => unreachable!("{self:?}"),
1536+
}
15061537
}
15071538
}
15081539

compiler/stable_mir/src/fold.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ use crate::Opaque;
44

55
use super::ty::{
66
Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind,
7-
GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
7+
GenericArgs, Promoted, Region, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst,
88
};
99

1010
pub trait Folder: Sized {
1111
type Break;
12-
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> {
12+
fn fold_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> {
1313
ty.super_fold(self)
1414
}
1515
fn fold_const(&mut self, c: &Const) -> ControlFlow<Self::Break, Const> {
1616
c.super_fold(self)
1717
}
18+
fn fold_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break, Region> {
19+
reg.super_fold(self)
20+
}
1821
}
1922

2023
pub trait Foldable: Sized + Clone {
@@ -26,7 +29,7 @@ pub trait Foldable: Sized + Clone {
2629

2730
impl Foldable for Ty {
2831
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
29-
folder.visit_ty(self)
32+
folder.fold_ty(self)
3033
}
3134
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
3235
let mut kind = self.kind();
@@ -106,6 +109,15 @@ impl Foldable for GenericArgs {
106109
}
107110
}
108111

112+
impl Foldable for Region {
113+
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
114+
folder.fold_reg(self)
115+
}
116+
fn super_fold<V: Folder>(&self, _: &mut V) -> ControlFlow<V::Break, Self> {
117+
ControlFlow::Continue(self.clone())
118+
}
119+
}
120+
109121
impl Foldable for GenericArgKind {
110122
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> {
111123
let mut this = self.clone();
@@ -136,7 +148,10 @@ impl Foldable for RigidTy {
136148
}
137149
RigidTy::Slice(inner) => *inner = inner.fold(folder)?,
138150
RigidTy::RawPtr(ty, _) => *ty = ty.fold(folder)?,
139-
RigidTy::Ref(_, ty, _) => *ty = ty.fold(folder)?,
151+
RigidTy::Ref(reg, ty, _) => {
152+
*reg = reg.fold(folder)?;
153+
*ty = ty.fold(folder)?
154+
}
140155
RigidTy::FnDef(_, args) => *args = args.fold(folder)?,
141156
RigidTy::FnPtr(sig) => *sig = sig.fold(folder)?,
142157
RigidTy::Closure(_, args) => *args = args.fold(folder)?,
@@ -214,7 +229,7 @@ pub enum Never {}
214229
impl Folder for GenericArgs {
215230
type Break = Never;
216231

217-
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> {
232+
fn fold_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> {
218233
ControlFlow::Continue(match ty.kind() {
219234
TyKind::Param(p) => self[p],
220235
_ => *ty,

compiler/stable_mir/src/ty.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{
22
mir::Safety,
33
mir::{Body, Mutability},
4-
with, AllocId, DefId,
4+
with, AllocId, DefId, Symbol,
55
};
66
use crate::Opaque;
77
use std::fmt::{self, Debug, Formatter};
@@ -34,7 +34,46 @@ pub struct Const {
3434
}
3535

3636
type Ident = Opaque;
37-
pub type Region = Opaque;
37+
38+
#[derive(Debug, Clone)]
39+
pub struct Region {
40+
pub kind: RegionKind,
41+
}
42+
43+
#[derive(Debug, Clone)]
44+
pub enum RegionKind {
45+
ReEarlyBound(EarlyBoundRegion),
46+
ReLateBound(DebruijnIndex, BoundRegion),
47+
ReStatic,
48+
RePlaceholder(Placeholder<BoundRegion>),
49+
ReErased,
50+
}
51+
52+
pub(crate) type DebruijnIndex = u32;
53+
54+
#[derive(Debug, Clone)]
55+
pub struct EarlyBoundRegion {
56+
pub def_id: RegionDef,
57+
pub index: u32,
58+
pub name: Symbol,
59+
}
60+
61+
pub(crate) type BoundVar = u32;
62+
63+
#[derive(Debug, Clone)]
64+
pub struct BoundRegion {
65+
pub var: BoundVar,
66+
pub kind: BoundRegionKind,
67+
}
68+
69+
pub(crate) type UniverseIndex = u32;
70+
71+
#[derive(Debug, Clone)]
72+
pub struct Placeholder<T> {
73+
pub universe: UniverseIndex,
74+
pub bound: T,
75+
}
76+
3877
#[derive(Clone, Copy, PartialEq, Eq)]
3978
pub struct Span(pub usize);
4079

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

194+
#[derive(Clone, PartialEq, Eq, Debug)]
195+
pub struct RegionDef(pub DefId);
196+
155197
#[derive(Clone, Debug)]
156198
pub struct GenericArgs(pub Vec<GenericArgKind>);
157199

compiler/stable_mir/src/visitor.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::Opaque;
44

55
use super::ty::{
66
Allocation, Binder, Const, ConstDef, ExistentialPredicate, FnSig, GenericArgKind, GenericArgs,
7-
Promoted, RigidTy, TermKind, Ty, UnevaluatedConst,
7+
Promoted, Region, RigidTy, TermKind, Ty, UnevaluatedConst,
88
};
99

1010
pub trait Visitor: Sized {
@@ -15,6 +15,9 @@ pub trait Visitor: Sized {
1515
fn visit_const(&mut self, c: &Const) -> ControlFlow<Self::Break> {
1616
c.super_visit(self)
1717
}
18+
fn visit_reg(&mut self, reg: &Region) -> ControlFlow<Self::Break> {
19+
reg.super_visit(self)
20+
}
1821
}
1922

2023
pub trait Visitable {
@@ -101,6 +104,16 @@ impl Visitable for GenericArgs {
101104
}
102105
}
103106

107+
impl Visitable for Region {
108+
fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
109+
visitor.visit_reg(self)
110+
}
111+
112+
fn super_visit<V: Visitor>(&self, _: &mut V) -> ControlFlow<V::Break> {
113+
ControlFlow::Continue(())
114+
}
115+
}
116+
104117
impl Visitable for GenericArgKind {
105118
fn super_visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
106119
match self {
@@ -128,7 +141,10 @@ impl Visitable for RigidTy {
128141
}
129142
RigidTy::Slice(inner) => inner.visit(visitor),
130143
RigidTy::RawPtr(ty, _) => ty.visit(visitor),
131-
RigidTy::Ref(_, ty, _) => ty.visit(visitor),
144+
RigidTy::Ref(reg, ty, _) => {
145+
reg.visit(visitor);
146+
ty.visit(visitor)
147+
}
132148
RigidTy::FnDef(_, args) => args.visit(visitor),
133149
RigidTy::FnPtr(sig) => sig.visit(visitor),
134150
RigidTy::Closure(_, args) => args.visit(visitor),

0 commit comments

Comments
 (0)