avoid thunking trivial values
This commit is contained in:
+60
-53
@@ -39,12 +39,12 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>, T, E: std::fmt::Display>
|
||||
}
|
||||
|
||||
pub trait DowngradeContext<'id: 'ir, 'ir> {
|
||||
fn new_expr(&self, expr: Ir<'ir, GhostRef<'id, 'ir>>) -> IrRef<'id, 'ir>;
|
||||
fn maybe_thunk(&mut self, ir: IrRef<'id, 'ir>) -> GhostMaybeThunkRef<'id, 'ir>;
|
||||
fn new_expr(&self, expr: Ir<'ir, GhostRoRef<'id, 'ir>>) -> GhostRoIrRef<'id, 'ir>;
|
||||
fn maybe_thunk(&mut self, ir: GhostRoIrRef<'id, 'ir>) -> GhostRoMaybeThunkRef<'id, 'ir>;
|
||||
|
||||
fn intern_string(&mut self, sym: impl AsRef<str>) -> StringId;
|
||||
fn resolve_sym(&self, id: StringId) -> Symbol<'_>;
|
||||
fn lookup(&self, sym: StringId, span: TextRange) -> Result<GhostMaybeThunkRef<'id, 'ir>>;
|
||||
fn lookup(&self, sym: StringId, span: TextRange) -> Result<GhostRoMaybeThunkRef<'id, 'ir>>;
|
||||
|
||||
fn get_current_source(&self) -> Source;
|
||||
|
||||
@@ -53,11 +53,11 @@ pub trait DowngradeContext<'id: 'ir, 'ir> {
|
||||
F: FnOnce(&mut Self) -> R;
|
||||
fn with_let_scope<F, R>(&mut self, bindings: &[StringId], f: F) -> Result<R>
|
||||
where
|
||||
F: FnOnce(&mut Self) -> Result<(Vec<'ir, IrRef<'id, 'ir>>, R)>;
|
||||
F: FnOnce(&mut Self) -> Result<(Vec<'ir, GhostRoMaybeThunkRef<'id, 'ir>>, R)>;
|
||||
fn with_with_scope<F, R>(&mut self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut Self) -> R;
|
||||
fn with_thunk_scope<F, R>(&mut self, f: F) -> (R, Vec<'ir, (ThunkId, IrRef<'id, 'ir>)>)
|
||||
fn with_thunk_scope<F, R>(&mut self, f: F) -> (R, Vec<'ir, (ThunkId, GhostRoIrRef<'id, 'ir>)>)
|
||||
where
|
||||
F: FnOnce(&mut Self) -> R;
|
||||
|
||||
@@ -65,11 +65,11 @@ pub trait DowngradeContext<'id: 'ir, 'ir> {
|
||||
}
|
||||
|
||||
pub trait Downgrade<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>>;
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>>;
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for Expr {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
use Expr::*;
|
||||
match self {
|
||||
Apply(apply) => apply.downgrade(ctx),
|
||||
@@ -114,7 +114,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Assert {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let assertion = self.condition().require(ctx, span)?;
|
||||
let assertion_raw = assertion.to_string();
|
||||
@@ -130,7 +130,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::IfElse {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let cond = self.condition().require(ctx, span)?.downgrade(ctx)?;
|
||||
let consq = self.body().require(ctx, span)?.downgrade(ctx)?;
|
||||
@@ -142,7 +142,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
macro_rules! path {
|
||||
($ty:ident) => {
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::$ty {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
downgrade_path(self.parts(), ctx)
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@ path!(PathAbs);
|
||||
path!(PathRel);
|
||||
path!(PathHome);
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::PathSearch {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let path = {
|
||||
let temp = self.content().require(ctx, span)?;
|
||||
@@ -180,7 +180,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Str {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let normalized = self.normalized_parts();
|
||||
let is_single_literal = normalized.len() == 1
|
||||
&& matches!(normalized.first(), Some(ast::InterpolPart::Literal(_)));
|
||||
@@ -210,7 +210,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Literal {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let expr = match self.kind() {
|
||||
ast::LiteralKind::Integer(int) => Ir::Int(int.value().require(ctx, span)?),
|
||||
@@ -225,7 +225,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Ident {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let text = self.ident_token().require(ctx, span)?.to_string();
|
||||
let sym = ctx.intern_string(text);
|
||||
@@ -235,7 +235,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::AttrSet {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let rec = self.rec_token().is_some();
|
||||
if !rec {
|
||||
let attrs = downgrade_attrs(self, ctx)?;
|
||||
@@ -251,7 +251,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::List {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let bump = ctx.bump();
|
||||
let items = self
|
||||
.items()
|
||||
@@ -265,7 +265,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::BinOp {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let lhs = self.lhs().require(ctx, span)?.downgrade(ctx)?;
|
||||
let rhs = self.rhs().require(ctx, span)?.downgrade(ctx)?;
|
||||
@@ -275,7 +275,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::HasAttr {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let lhs = self.expr().require(ctx, span)?.downgrade(ctx)?;
|
||||
let rhs = downgrade_attrpath(self.attrpath().require(ctx, span)?, ctx)?;
|
||||
@@ -284,7 +284,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::UnaryOp {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let rhs = self.expr().require(ctx, span)?.downgrade(ctx)?;
|
||||
let kind = self.operator().require(ctx, span)?.into();
|
||||
@@ -293,7 +293,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Select {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let expr = self.expr().require(ctx, span)?.downgrade(ctx)?;
|
||||
let attrpath = downgrade_attrpath(self.attrpath().require(ctx, span)?, ctx)?;
|
||||
@@ -312,7 +312,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::LegacyLet {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let entries: Vec<'ir, _> = self.entries().collect_in(ctx.bump());
|
||||
let attrset_expr = downgrade_let_bindings(entries, ctx, |ctx, binding_keys| {
|
||||
@@ -341,7 +341,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::LetIn {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let entries: Vec<'ir, _> = self.entries().collect_in(ctx.bump());
|
||||
let span = self.syntax().text_range();
|
||||
let body_expr = self.body().require(ctx, span)?;
|
||||
@@ -351,7 +351,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::With {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let namespace = self.namespace().require(ctx, span)?.downgrade(ctx)?;
|
||||
let namespace = ctx.maybe_thunk(namespace);
|
||||
@@ -370,14 +370,14 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Lambda {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let raw_param = self.param().require(ctx, span)?;
|
||||
let body_ast = self.body().require(ctx, span)?;
|
||||
|
||||
struct Ret<'id, 'ir> {
|
||||
param: Option<Param<'ir>>,
|
||||
body: IrRef<'id, 'ir>,
|
||||
body: GhostRoIrRef<'id, 'ir>,
|
||||
}
|
||||
|
||||
let (ret, thunks) = ctx.with_thunk_scope(|ctx| {
|
||||
@@ -434,7 +434,7 @@ impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> fo
|
||||
}
|
||||
|
||||
impl<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>> Downgrade<'id, 'ir, Ctx> for ast::Apply {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<IrRef<'id, 'ir>> {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let span = self.syntax().text_range();
|
||||
let func = self.lambda().require(ctx, span)?.downgrade(ctx)?;
|
||||
let arg = self.argument().require(ctx, span)?.downgrade(ctx)?;
|
||||
@@ -836,8 +836,15 @@ fn make_attrpath_value_entry<'ir>(path: Vec<'ir, ast::Attr>, value: ast::Expr) -
|
||||
}
|
||||
|
||||
struct FinalizedAttrSet<'id, 'ir> {
|
||||
stcs: HashMap<'ir, StringId, (GhostMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
dyns: Vec<'ir, (IrRef<'id, 'ir>, GhostMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
stcs: HashMap<'ir, StringId, (GhostRoMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
dyns: Vec<
|
||||
'ir,
|
||||
(
|
||||
GhostRoIrRef<'id, 'ir>,
|
||||
GhostRoMaybeThunkRef<'id, 'ir>,
|
||||
TextRange,
|
||||
),
|
||||
>,
|
||||
}
|
||||
|
||||
fn downgrade_attrs<'id, 'ir>(
|
||||
@@ -852,7 +859,7 @@ fn downgrade_attrs<'id, 'ir>(
|
||||
fn downgrade_attr<'id, 'ir>(
|
||||
attr: ast::Attr,
|
||||
ctx: &mut impl DowngradeContext<'id, 'ir>,
|
||||
) -> Result<Attr<IrRef<'id, 'ir>>> {
|
||||
) -> Result<Attr<GhostRoIrRef<'id, 'ir>>> {
|
||||
use ast::Attr::*;
|
||||
use ast::InterpolPart::*;
|
||||
match attr {
|
||||
@@ -913,7 +920,7 @@ fn downgrade_attr<'id, 'ir>(
|
||||
fn downgrade_attrpath<'id, 'ir>(
|
||||
attrpath: ast::Attrpath,
|
||||
ctx: &mut impl DowngradeContext<'id, 'ir>,
|
||||
) -> Result<Vec<'ir, Attr<IrRef<'id, 'ir>>>> {
|
||||
) -> Result<Vec<'ir, Attr<GhostRoIrRef<'id, 'ir>>>> {
|
||||
let bump = ctx.bump();
|
||||
attrpath
|
||||
.attrs()
|
||||
@@ -922,7 +929,7 @@ fn downgrade_attrpath<'id, 'ir>(
|
||||
}
|
||||
|
||||
struct PatternBindings<'id, 'ir> {
|
||||
body: IrRef<'id, 'ir>,
|
||||
body: GhostRoIrRef<'id, 'ir>,
|
||||
required: Vec<'ir, (StringId, TextRange)>,
|
||||
optional: Vec<'ir, (StringId, TextRange)>,
|
||||
}
|
||||
@@ -931,7 +938,7 @@ fn downgrade_pattern_bindings<'id, 'ir, Ctx>(
|
||||
pat_entries: impl Iterator<Item = ast::PatEntry>,
|
||||
alias: Option<StringId>,
|
||||
ctx: &mut Ctx,
|
||||
body_fn: impl FnOnce(&mut Ctx, &[StringId]) -> Result<IrRef<'id, 'ir>>,
|
||||
body_fn: impl FnOnce(&mut Ctx, &[StringId]) -> Result<GhostRoIrRef<'id, 'ir>>,
|
||||
) -> Result<PatternBindings<'id, 'ir>>
|
||||
where
|
||||
Ctx: DowngradeContext<'id, 'ir>,
|
||||
@@ -990,6 +997,7 @@ where
|
||||
}
|
||||
|
||||
let arg = ctx.new_expr(Ir::Arg { layer: 0 });
|
||||
let arg_thunk = ctx.maybe_thunk(arg);
|
||||
ctx.with_let_scope(&keys, |ctx| {
|
||||
let vals = params
|
||||
.into_iter()
|
||||
@@ -1001,21 +1009,16 @@ where
|
||||
span,
|
||||
} = param;
|
||||
let default = default.map(|default| default.downgrade(ctx)).transpose()?;
|
||||
// let default = if let Some(default) = default {
|
||||
// let default = default.clone().downgrade(ctx)?;
|
||||
// Some(ctx.maybe_thunk(default))
|
||||
// } else {
|
||||
// None
|
||||
// };
|
||||
|
||||
Ok(ctx.new_expr(Ir::Select {
|
||||
let expr = ctx.new_expr(Ir::Select {
|
||||
expr: arg,
|
||||
attrpath: Vec::from_iter_in([Attr::Str(sym, sym_span)], bump),
|
||||
default,
|
||||
span,
|
||||
}))
|
||||
});
|
||||
Ok(ctx.maybe_thunk(expr))
|
||||
})
|
||||
.chain(alias.into_iter().map(|_| Ok(arg)))
|
||||
.chain(alias.into_iter().map(|_| Ok(arg_thunk)))
|
||||
.collect_in::<Result<_>>(bump)?;
|
||||
|
||||
let body = body_fn(ctx, &keys)?;
|
||||
@@ -1035,10 +1038,10 @@ fn downgrade_let_bindings<'id, 'ir, Ctx, F>(
|
||||
entries: Vec<'ir, ast::Entry>,
|
||||
ctx: &mut Ctx,
|
||||
body_fn: F,
|
||||
) -> Result<IrRef<'id, 'ir>>
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>>
|
||||
where
|
||||
Ctx: DowngradeContext<'id, 'ir>,
|
||||
F: FnOnce(&mut Ctx, &[StringId]) -> Result<IrRef<'id, 'ir>>,
|
||||
F: FnOnce(&mut Ctx, &[StringId]) -> Result<GhostRoIrRef<'id, 'ir>>,
|
||||
{
|
||||
downgrade_rec_attrs_impl::<_, _, false>(entries, ctx, |ctx, binding_keys, _dyns| {
|
||||
body_fn(ctx, binding_keys)
|
||||
@@ -1048,7 +1051,7 @@ where
|
||||
fn downgrade_rec_bindings<'id, 'ir, Ctx>(
|
||||
entries: Vec<'ir, ast::Entry>,
|
||||
ctx: &mut Ctx,
|
||||
) -> Result<IrRef<'id, 'ir>>
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>>
|
||||
where
|
||||
Ctx: DowngradeContext<'id, 'ir>,
|
||||
{
|
||||
@@ -1069,14 +1072,18 @@ fn downgrade_rec_attrs_impl<'id, 'ir, Ctx, F, const ALLOW_DYN: bool>(
|
||||
entries: Vec<'ir, ast::Entry>,
|
||||
ctx: &mut Ctx,
|
||||
body_fn: F,
|
||||
) -> Result<IrRef<'id, 'ir>>
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>>
|
||||
where
|
||||
Ctx: DowngradeContext<'id, 'ir>,
|
||||
F: FnOnce(
|
||||
&mut Ctx,
|
||||
&[StringId],
|
||||
&[(IrRef<'id, 'ir>, GhostMaybeThunkRef<'id, 'ir>, TextRange)],
|
||||
) -> Result<IrRef<'id, 'ir>>,
|
||||
&[(
|
||||
GhostRoIrRef<'id, 'ir>,
|
||||
GhostRoMaybeThunkRef<'id, 'ir>,
|
||||
TextRange,
|
||||
)],
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>>,
|
||||
{
|
||||
let mut pending = PendingAttrSet::new_in(ctx.bump());
|
||||
pending.collect_entries(entries.iter().cloned(), ctx)?;
|
||||
@@ -1091,7 +1098,7 @@ where
|
||||
let vals = {
|
||||
let mut temp = Vec::with_capacity_in(keys.len(), ctx.bump());
|
||||
for sym in &keys {
|
||||
temp.push(ctx.new_expr(Ir::MaybeThunk(finalized.stcs.get(sym).expect("WTF").0)));
|
||||
temp.push(finalized.stcs.get(sym).expect("WTF").0);
|
||||
}
|
||||
temp
|
||||
};
|
||||
@@ -1102,7 +1109,7 @@ where
|
||||
fn collect_inherit_lookups<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>>(
|
||||
entries: &[ast::Entry],
|
||||
ctx: &mut Ctx,
|
||||
) -> Result<HashMap<'ir, StringId, (GhostMaybeThunkRef<'id, 'ir>, TextRange)>> {
|
||||
) -> Result<HashMap<'ir, StringId, (GhostRoMaybeThunkRef<'id, 'ir>, TextRange)>> {
|
||||
let mut inherit_lookups = HashMap::new_in(ctx.bump());
|
||||
for entry in entries {
|
||||
if let ast::Entry::Inherit(inherit) = entry
|
||||
@@ -1142,7 +1149,7 @@ fn collect_binding_syms<'id: 'ir, 'ir, Ctx: DowngradeContext<'id, 'ir>, const AL
|
||||
|
||||
fn finalize_pending_set<'id, 'ir, Ctx: DowngradeContext<'id, 'ir>, const ALLOW_DYN: bool>(
|
||||
pending: PendingAttrSet,
|
||||
inherit_lookups: &HashMap<StringId, (GhostMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
inherit_lookups: &HashMap<StringId, (GhostRoMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
ctx: &mut Ctx,
|
||||
) -> Result<FinalizedAttrSet<'id, 'ir>> {
|
||||
let mut stcs = HashMap::new_in(ctx.bump());
|
||||
@@ -1170,9 +1177,9 @@ fn finalize_pending_set<'id, 'ir, Ctx: DowngradeContext<'id, 'ir>, const ALLOW_D
|
||||
|
||||
fn finalize_pending_value<'id, 'ir, Ctx: DowngradeContext<'id, 'ir>, const ALLOW_DYN: bool>(
|
||||
value: PendingValue,
|
||||
inherit_lookups: &HashMap<StringId, (GhostMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
inherit_lookups: &HashMap<StringId, (GhostRoMaybeThunkRef<'id, 'ir>, TextRange)>,
|
||||
ctx: &mut Ctx,
|
||||
) -> Result<IrRef<'id, 'ir>> {
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
match value {
|
||||
PendingValue::Expr(expr) => expr.downgrade(ctx),
|
||||
PendingValue::InheritFrom(from_expr, sym, span) => {
|
||||
@@ -1212,7 +1219,7 @@ fn finalize_pending_value<'id, 'ir, Ctx: DowngradeContext<'id, 'ir>, const ALLOW
|
||||
fn downgrade_path<'id, 'ir>(
|
||||
parts: impl IntoIterator<Item = ast::InterpolPart<ast::PathContent>>,
|
||||
ctx: &mut impl DowngradeContext<'id, 'ir>,
|
||||
) -> Result<IrRef<'id, 'ir>> {
|
||||
) -> Result<GhostRoIrRef<'id, 'ir>> {
|
||||
let bump = ctx.bump();
|
||||
let parts = parts
|
||||
.into_iter()
|
||||
|
||||
+54
-16
@@ -14,14 +14,16 @@ pub mod downgrade;
|
||||
|
||||
pub type HashMap<'ir, K, V> = hashbrown::HashMap<K, V, hashbrown::DefaultHashBuilder, &'ir Bump>;
|
||||
|
||||
pub type IrRef<'id, 'ir> = <GhostRef<'id, 'ir> as RefExt<'ir>>::IrRef;
|
||||
pub type GhostIrRef<'id, 'ir> = <GhostRef<'id, 'ir> as RefExt<'ir>>::IrRef;
|
||||
pub type GhostRoIrRef<'id, 'ir> = <GhostRoRef<'id, 'ir> as RefExt<'ir>>::IrRef;
|
||||
pub type RawIrRef<'ir> = <RawRef<'ir> as RefExt<'ir>>::IrRef;
|
||||
pub type GhostMaybeThunkRef<'id, 'ir> = <GhostRef<'id, 'ir> as RefExt<'ir>>::MaybeThunkRef;
|
||||
pub type GhostRoMaybeThunkRef<'id, 'ir> = <GhostRoRef<'id, 'ir> as RefExt<'ir>>::MaybeThunkRef;
|
||||
|
||||
impl<'id, 'ir> Ir<'ir, GhostRef<'id, 'ir>> {
|
||||
impl<'id, 'ir> Ir<'ir, GhostRoRef<'id, 'ir>> {
|
||||
/// Freeze a mutable IR reference into a read-only one, consuming the
|
||||
/// `GhostToken` to prevent any further mutation.
|
||||
pub fn freeze(this: IrRef<'id, 'ir>, _: GhostToken<'id>) -> RawIrRef<'ir> {
|
||||
pub fn freeze(this: GhostRoIrRef<'id, 'ir>, _: GhostToken<'id>) -> RawIrRef<'ir> {
|
||||
// SAFETY: The transmute is sound because:
|
||||
// - `GhostCell<'id, T>` is `#[repr(transparent)]` over `T`, so
|
||||
// `&'ir GhostCell<'id, T>` and `&'ir T` have identical layout.
|
||||
@@ -39,7 +41,39 @@ impl<'id, 'ir> Ir<'ir, GhostRef<'id, 'ir>> {
|
||||
// Consuming the `GhostToken` guarantees no `borrow_mut` calls can
|
||||
// occur afterwards, so the shared `&Ir` references reachable from a
|
||||
// `RawIrRef<'ir>` can never alias with mutable references.
|
||||
unsafe { std::mem::transmute::<IrRef<'id, 'ir>, RawIrRef<'ir>>(this) }
|
||||
unsafe { std::mem::transmute::<GhostRoIrRef<'id, 'ir>, RawIrRef<'ir>>(this) }
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct GhostRoCell<'id, T: ?Sized>(GhostCell<'id, T>);
|
||||
|
||||
impl<'id, T> From<GhostCell<'id, T>> for GhostRoCell<'id, T> {
|
||||
fn from(value: GhostCell<'id, T>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'id, T: ?Sized> From<&GhostCell<'id, T>> for &GhostRoCell<'id, T> {
|
||||
fn from(value: &GhostCell<'id, T>) -> Self {
|
||||
// SAFETY: `GhostRoCell` is `#[repr(transparent)]` over `GhostCell`
|
||||
// TODO: document mutability
|
||||
unsafe { std::mem::transmute(value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'id, T: ?Sized> From<&T> for &GhostRoCell<'id, T> {
|
||||
fn from(value: &T) -> Self {
|
||||
// SAFETY: `GhostRoCell` is `#[repr(transparent)]` over `GhostCell`,
|
||||
// which is `#[repr(transparent)]` over `T`
|
||||
// TODO: document mutability
|
||||
unsafe { std::mem::transmute(value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'id, T: ?Sized> GhostRoCell<'id, T> {
|
||||
pub fn borrow<'a>(&'a self, token: &'a GhostToken<'id>) -> &'a T {
|
||||
self.0.borrow(token)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,13 +89,13 @@ pub enum MaybeThunk {
|
||||
Thunk(ThunkId),
|
||||
Arg { layer: u8 },
|
||||
Builtin(BuiltinId),
|
||||
BuiltinConst(StringId),
|
||||
Builtins,
|
||||
ReplBinding(StringId),
|
||||
ScopedImportBinding(StringId),
|
||||
WithLookup(StringId),
|
||||
}
|
||||
|
||||
|
||||
pub trait Ref<'ir> {
|
||||
type Ref<T>
|
||||
where
|
||||
@@ -80,11 +114,15 @@ impl<'ir, T: Ref<'ir> + 'ir> RefExt<'ir> for T {
|
||||
}
|
||||
|
||||
pub struct GhostRef<'id, 'ir>(PhantomData<&'ir GhostCell<'id, ()>>);
|
||||
pub struct GhostRoRef<'id, 'ir>(PhantomData<&'ir GhostRoCell<'id, ()>>);
|
||||
pub struct RawRef<'ir>(PhantomData<&'ir ()>);
|
||||
|
||||
impl<'id, 'ir> Ref<'ir> for GhostRef<'id, 'ir> {
|
||||
type Ref<T: 'ir> = &'ir GhostCell<'id, T>;
|
||||
}
|
||||
impl<'id, 'ir> Ref<'ir> for GhostRoRef<'id, 'ir> {
|
||||
type Ref<T: 'ir> = &'ir GhostRoCell<'id, T>;
|
||||
}
|
||||
impl<'ir> Ref<'ir> for RawRef<'ir> {
|
||||
type Ref<T: 'ir> = &'ir T;
|
||||
}
|
||||
@@ -285,38 +323,38 @@ pub struct Param<'ir> {
|
||||
|
||||
pub fn new_global_env(
|
||||
strings: &mut DefaultStringInterner,
|
||||
) -> hashbrown::HashMap<StringId, Ir<'static, RawRef<'static>>> {
|
||||
) -> hashbrown::HashMap<StringId, MaybeThunk> {
|
||||
let mut global_env = hashbrown::HashMap::new();
|
||||
let builtins_sym = StringId(strings.get_or_intern("builtins"));
|
||||
global_env.insert(builtins_sym, Ir::Builtins);
|
||||
global_env.insert(builtins_sym, MaybeThunk::Builtins);
|
||||
|
||||
for (idx, &(name, _)) in BUILTINS.iter().enumerate() {
|
||||
let id = BuiltinId::try_from_primitive(idx as u8).expect("infallible");
|
||||
let name = StringId(strings.get_or_intern(name));
|
||||
global_env.insert(name, Ir::Builtin(id));
|
||||
global_env.insert(name, MaybeThunk::Builtin(id));
|
||||
}
|
||||
|
||||
let consts = [
|
||||
(
|
||||
"__currentSystem",
|
||||
Ir::BuiltinConst(StringId(strings.get_or_intern("currentSystem"))),
|
||||
MaybeThunk::BuiltinConst(StringId(strings.get_or_intern("currentSystem"))),
|
||||
),
|
||||
("__langVersion", Ir::Int(6)),
|
||||
("__langVersion", MaybeThunk::Int(6)),
|
||||
(
|
||||
"__nixVersion",
|
||||
Ir::BuiltinConst(StringId(strings.get_or_intern("nixVersion"))),
|
||||
MaybeThunk::BuiltinConst(StringId(strings.get_or_intern("nixVersion"))),
|
||||
),
|
||||
(
|
||||
"__storeDir",
|
||||
Ir::BuiltinConst(StringId(strings.get_or_intern("storeDir"))),
|
||||
MaybeThunk::BuiltinConst(StringId(strings.get_or_intern("storeDir"))),
|
||||
),
|
||||
(
|
||||
"__nixPath",
|
||||
Ir::BuiltinConst(StringId(strings.get_or_intern("nixPath"))),
|
||||
MaybeThunk::BuiltinConst(StringId(strings.get_or_intern("nixPath"))),
|
||||
),
|
||||
("null", Ir::Null),
|
||||
("true", Ir::Bool(true)),
|
||||
("false", Ir::Bool(false)),
|
||||
("null", MaybeThunk::Null),
|
||||
("true", MaybeThunk::Bool(true)),
|
||||
("false", MaybeThunk::Bool(false)),
|
||||
];
|
||||
|
||||
for (name, ir) in consts {
|
||||
|
||||
Reference in New Issue
Block a user