-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Implement SMIR generic parameter instantiation #115532
Merged
+434
−40
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b43e3b9
Add type folder to SMIR
oli-obk 7f009e5
Fail to test argument instantiation since we don't have types for mos…
oli-obk a370f1b
Also use `Const` in `SMIR` instead of just `ConstantKind`
oli-obk 627fa80
Add types to all constants
oli-obk 98d26d9
Deopaquify `ParamConst`
oli-obk 202fbed
Allow fetching the SMIR body of FnDefs
oli-obk 0f4ff52
Implement and test monomorphization
oli-obk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
use std::ops::ControlFlow; | ||
|
||
use crate::rustc_internal::Opaque; | ||
|
||
use super::ty::{ | ||
Allocation, Binder, Const, ConstDef, ConstantKind, ExistentialPredicate, FnSig, GenericArgKind, | ||
GenericArgs, Promoted, RigidTy, TermKind, Ty, TyKind, UnevaluatedConst, | ||
}; | ||
|
||
pub trait Folder: Sized { | ||
type Break; | ||
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> { | ||
ty.super_fold(self) | ||
} | ||
fn fold_const(&mut self, c: &Const) -> ControlFlow<Self::Break, Const> { | ||
c.super_fold(self) | ||
} | ||
} | ||
|
||
pub trait Foldable: Sized + Clone { | ||
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
self.super_fold(folder) | ||
} | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self>; | ||
} | ||
|
||
impl Foldable for Ty { | ||
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
folder.visit_ty(self) | ||
} | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut kind = self.kind(); | ||
match &mut kind { | ||
super::ty::TyKind::RigidTy(ty) => *ty = ty.fold(folder)?, | ||
super::ty::TyKind::Alias(_, alias) => alias.args = alias.args.fold(folder)?, | ||
super::ty::TyKind::Param(_) => {} | ||
super::ty::TyKind::Bound(_, _) => {} | ||
} | ||
ControlFlow::Continue(kind.into()) | ||
} | ||
} | ||
|
||
impl Foldable for Const { | ||
fn fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
folder.fold_const(self) | ||
} | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut this = self.clone(); | ||
match &mut this.literal { | ||
super::ty::ConstantKind::Allocated(alloc) => *alloc = alloc.fold(folder)?, | ||
super::ty::ConstantKind::Unevaluated(uv) => *uv = uv.fold(folder)?, | ||
super::ty::ConstantKind::Param(_) => {} | ||
} | ||
this.ty = this.ty.fold(folder)?; | ||
ControlFlow::Continue(this) | ||
} | ||
} | ||
|
||
impl Foldable for Opaque { | ||
fn super_fold<V: Folder>(&self, _folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(self.clone()) | ||
} | ||
} | ||
|
||
impl Foldable for Allocation { | ||
fn super_fold<V: Folder>(&self, _folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(self.clone()) | ||
} | ||
} | ||
|
||
impl Foldable for UnevaluatedConst { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let UnevaluatedConst { def, args, promoted } = self; | ||
ControlFlow::Continue(UnevaluatedConst { | ||
def: def.fold(folder)?, | ||
args: args.fold(folder)?, | ||
promoted: promoted.fold(folder)?, | ||
}) | ||
} | ||
} | ||
|
||
impl Foldable for ConstDef { | ||
fn super_fold<V: Folder>(&self, _folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(self.clone()) | ||
} | ||
} | ||
|
||
impl<T: Foldable> Foldable for Option<T> { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(match self { | ||
Some(val) => Some(val.fold(folder)?), | ||
None => None, | ||
}) | ||
} | ||
} | ||
|
||
impl Foldable for Promoted { | ||
fn super_fold<V: Folder>(&self, _folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(self.clone()) | ||
} | ||
} | ||
|
||
impl Foldable for GenericArgs { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(GenericArgs(self.0.fold(folder)?)) | ||
} | ||
} | ||
|
||
impl Foldable for GenericArgKind { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut this = self.clone(); | ||
match &mut this { | ||
GenericArgKind::Lifetime(lt) => *lt = lt.fold(folder)?, | ||
GenericArgKind::Type(t) => *t = t.fold(folder)?, | ||
GenericArgKind::Const(c) => *c = c.fold(folder)?, | ||
} | ||
ControlFlow::Continue(this) | ||
} | ||
} | ||
|
||
impl Foldable for RigidTy { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut this = self.clone(); | ||
match &mut this { | ||
RigidTy::Bool | ||
| RigidTy::Char | ||
| RigidTy::Int(_) | ||
| RigidTy::Uint(_) | ||
| RigidTy::Float(_) | ||
| RigidTy::Never | ||
| RigidTy::Foreign(_) | ||
| RigidTy::Str => {} | ||
RigidTy::Array(t, c) => { | ||
*t = t.fold(folder)?; | ||
*c = c.fold(folder)?; | ||
} | ||
RigidTy::Slice(inner) => *inner = inner.fold(folder)?, | ||
RigidTy::RawPtr(ty, _) => *ty = ty.fold(folder)?, | ||
RigidTy::Ref(_, ty, _) => *ty = ty.fold(folder)?, | ||
RigidTy::FnDef(_, args) => *args = args.fold(folder)?, | ||
RigidTy::FnPtr(sig) => *sig = sig.fold(folder)?, | ||
RigidTy::Closure(_, args) => *args = args.fold(folder)?, | ||
RigidTy::Generator(_, args, _) => *args = args.fold(folder)?, | ||
RigidTy::Dynamic(pred, r, _) => { | ||
*pred = pred.fold(folder)?; | ||
*r = r.fold(folder)?; | ||
} | ||
RigidTy::Tuple(fields) => *fields = fields.fold(folder)?, | ||
RigidTy::Adt(_, args) => *args = args.fold(folder)?, | ||
} | ||
ControlFlow::Continue(this) | ||
} | ||
} | ||
|
||
impl<T: Foldable> Foldable for Vec<T> { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut this = self.clone(); | ||
for arg in &mut this { | ||
*arg = arg.fold(folder)?; | ||
} | ||
ControlFlow::Continue(this) | ||
} | ||
} | ||
|
||
impl<T: Foldable> Foldable for Binder<T> { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(Self { | ||
value: self.value.fold(folder)?, | ||
bound_vars: self.bound_vars.clone(), | ||
}) | ||
} | ||
} | ||
|
||
impl Foldable for ExistentialPredicate { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
let mut this = self.clone(); | ||
match &mut this { | ||
ExistentialPredicate::Trait(tr) => tr.generic_args = tr.generic_args.fold(folder)?, | ||
ExistentialPredicate::Projection(p) => { | ||
p.term = p.term.fold(folder)?; | ||
p.generic_args = p.generic_args.fold(folder)?; | ||
} | ||
ExistentialPredicate::AutoTrait(_) => {} | ||
} | ||
ControlFlow::Continue(this) | ||
} | ||
} | ||
|
||
impl Foldable for TermKind { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(match self { | ||
TermKind::Type(t) => TermKind::Type(t.fold(folder)?), | ||
TermKind::Const(c) => TermKind::Const(c.fold(folder)?), | ||
}) | ||
} | ||
} | ||
|
||
impl Foldable for FnSig { | ||
fn super_fold<V: Folder>(&self, folder: &mut V) -> ControlFlow<V::Break, Self> { | ||
ControlFlow::Continue(Self { | ||
inputs_and_output: self.inputs_and_output.fold(folder)?, | ||
c_variadic: self.c_variadic, | ||
unsafety: self.unsafety, | ||
abi: self.abi.clone(), | ||
}) | ||
} | ||
} | ||
|
||
pub enum Never {} | ||
|
||
/// In order to instantiate a `Foldable`'s generic parameters with specific arguments, | ||
/// `GenericArgs` can be used as a `Folder` that replaces all mentions of generic params | ||
/// with the entries in its list. | ||
impl Folder for GenericArgs { | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type Break = Never; | ||
|
||
fn visit_ty(&mut self, ty: &Ty) -> ControlFlow<Self::Break, Ty> { | ||
ControlFlow::Continue(match ty.kind() { | ||
TyKind::Param(p) => self[p], | ||
_ => *ty, | ||
}) | ||
} | ||
|
||
fn fold_const(&mut self, c: &Const) -> ControlFlow<Self::Break, Const> { | ||
ControlFlow::Continue(match &c.literal { | ||
ConstantKind::Param(p) => self[p.clone()].clone(), | ||
_ => c.clone(), | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need this clone now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a
Ty<'tcx>
, so we should probably copy it.The reason we need it is that
stable
takesself
by mutable reference, while we immutably borrow fromself
via the index operationsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I got this wrong. We need to clone the
TyKind
in case it comes out of aMaybeStable::Stable
entry ofself.types
. Sometimes SMIR creates its own types, and they get interned that way.