Skip to content

Commit

Permalink
rune: Allow empty values to be const constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jul 16, 2024
1 parent 5fb5b0d commit 4fc0395
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 72 deletions.
2 changes: 1 addition & 1 deletion crates/rune/src/compile/ir/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn lit(c: &mut Ctxt<'_, '_>, span: Span, hir: hir::Lit<'_>) -> compile::Result<i
#[instrument(span = span)]
fn expr_tuple(c: &mut Ctxt<'_, '_>, span: Span, hir: &hir::ExprSeq<'_>) -> compile::Result<ir::Ir> {
if hir.items.is_empty() {
let value = Value::empty().with_span(span)?;
let value = Value::unit().with_span(span)?;
return Ok(ir::Ir::new(span, value));
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rune/src/compile/ir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn eval_ir_assign(
.scopes
.mut_target(&ir.target, move |t| ir.op.assign(ir, t, value))?;

Ok(Value::empty().with_span(ir)?)
Ok(Value::unit().with_span(ir)?)
}

fn eval_ir_binary(
Expand Down Expand Up @@ -170,7 +170,7 @@ fn eval_ir_branches(
return eval_ir_scope(branch, interp, used);
}

Ok(Value::empty().with_span(ir)?)
Ok(Value::unit().with_span(ir)?)
}

fn eval_ir_call(
Expand Down Expand Up @@ -214,7 +214,7 @@ fn eval_ir_decl(
interp.budget.take(ir)?;
let value = eval_ir(&ir.value, interp, used)?;
interp.scopes.decl(&ir.name, value).with_span(ir)?;
Ok(Value::empty().with_span(ir)?)
Ok(Value::unit().with_span(ir)?)
}

fn eval_ir_loop(
Expand Down Expand Up @@ -265,7 +265,7 @@ fn eval_ir_loop(

Ok(value)
} else {
Ok(Value::empty().with_span(ir)?)
Ok(Value::unit().with_span(ir)?)
}
}

Expand Down Expand Up @@ -299,7 +299,7 @@ fn eval_ir_scope(
let value = if let Some(last) = &ir.last {
eval_ir(last, interp, used)?
} else {
Value::empty().with_span(ir)?
Value::unit().with_span(ir)?
};

interp.scopes.pop(guard).with_span(ir)?;
Expand All @@ -314,7 +314,7 @@ fn eval_ir_set(
interp.budget.take(ir)?;
let value = eval_ir(&ir.value, interp, used)?;
interp.scopes.set_target(&ir.target, value)?;
Ok(Value::empty().with_span(ir)?)
Ok(Value::unit().with_span(ir)?)
}

fn eval_ir_template(
Expand Down
4 changes: 3 additions & 1 deletion crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,8 @@ fn expr_break<'a, 'hir>(

converge!(expr(cx, hir, &mut needs)?, free(needs));
needs.free()?;
} else if let Some(out) = output {
cx.asm.push(Inst::unit(out), span)?;
}

// Drop loop temporaries.
Expand Down Expand Up @@ -2980,8 +2982,8 @@ fn expr_loop<'a, 'hir>(
cx.asm.push(Inst::unit(out), span)?;
}

// NB: breaks produce their own value / perform their own cleanup.
cx.asm.label(&break_label)?;

linear.free()?;
cx.breaks.pop();
Ok(Asm::new(span, ()))
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/modules/capture_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ fn dbg_impl(
vm_try!(writeln!(o, "{:?}", value).map_err(VmError::panic));
}

vm_try!(out.store(stack, Value::empty));
vm_try!(out.store(stack, Value::unit));
VmResult::Ok(())
}
4 changes: 2 additions & 2 deletions crates/rune/src/modules/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
};

futures.push(SelectFuture::new(index, future));
vm_try!(results.try_push(vm_try!(Value::empty())));
vm_try!(results.try_push(Value::empty()));
}

while !futures.is_empty() {
Expand Down Expand Up @@ -85,7 +85,7 @@ where
#[rune::function]
async fn join(value: Value) -> VmResult<Value> {
match &*vm_try!(value.borrow_kind_ref()) {
ValueKind::EmptyTuple => VmResult::Ok(vm_try!(Value::empty())),
ValueKind::EmptyTuple => VmResult::Ok(vm_try!(Value::unit())),
ValueKind::Tuple(tuple) => VmResult::Ok(vm_try!(
try_join_impl(tuple.iter(), tuple.len(), |vec| VmResult::Ok(vm_try!(
Value::tuple(vec)
Expand Down
9 changes: 9 additions & 0 deletions crates/rune/src/runtime/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub struct AccessError {
}

impl AccessError {
#[inline]
pub(crate) const fn empty() -> Self {
Self {
kind: AccessErrorKind::Empty,
}
}

#[inline]
pub(crate) fn new(kind: AccessErrorKind) -> Self {
Self { kind }
Expand All @@ -35,6 +42,7 @@ impl AccessError {
impl fmt::Display for AccessError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.kind {
AccessErrorKind::Empty => write!(f, "Empty value"),
AccessErrorKind::UnexpectedType { expected, actual } => write!(
f,
"Expected data of type `{expected}`, but found `{actual}`",
Expand Down Expand Up @@ -98,6 +106,7 @@ impl From<AccessErrorKind> for AccessError {

#[derive(Debug, PartialEq)]
pub(crate) enum AccessErrorKind {
Empty,
UnexpectedType { expected: RawStr, actual: RawStr },
NotAccessibleRef { error: NotAccessibleRef },
NotAccessibleMut { error: NotAccessibleMut },
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ConstValue {
/// otherwise.
pub fn as_value(&self) -> alloc::Result<Value> {
Ok(match self {
Self::EmptyTuple => Value::empty()?,
Self::EmptyTuple => Value::unit()?,
Self::Byte(b) => Value::try_from(*b)?,
Self::Char(c) => Value::try_from(*c)?,
Self::Bool(b) => Value::try_from(*b)?,
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
};

let state = if execution.is_resumed() {
vm_try!(execution.resume_with(vm_try!(Value::empty())))
vm_try!(execution.resume_with(Value::empty()))
} else {
vm_try!(execution.resume())
};
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ impl InstValue {
/// Convert into a value that can be pushed onto the stack.
pub fn into_value(self) -> alloc::Result<Value> {
match self {
Self::EmptyTuple => Value::empty(),
Self::EmptyTuple => Value::unit(),
Self::Bool(v) => Value::try_from(v),
Self::Byte(v) => Value::try_from(v),
Self::Char(v) => Value::try_from(v),
Expand Down
1 change: 1 addition & 0 deletions crates/rune/src/runtime/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::alloc::{self, Box};
use crate::runtime::{Access, AccessError, BorrowMut, BorrowRef, RawAccessGuard, Snapshot};

/// A shared value.
#[repr(transparent)]
pub(crate) struct Shared<T: ?Sized> {
inner: ptr::NonNull<SharedBox<T>>,
}
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ impl Stack {
return Ok(());
}

let empty = Value::empty()?;
self.stack.try_resize(self.top + size, empty)?;
self.stack.try_resize_with(self.top + size, Value::empty)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
};

let state = if execution.is_resumed() {
vm_try!(execution.async_resume_with(vm_try!(Value::empty())).await)
vm_try!(execution.async_resume_with(Value::empty()).await)
} else {
vm_try!(execution.async_resume().await)
};
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ macro_rules! impl_tuple {

impl ToValue for () {
fn to_value(self) -> VmResult<Value> {
VmResult::Ok(vm_try!(Value::empty()))
VmResult::Ok(vm_try!(Value::unit()))
}
}
};
Expand Down
Loading

0 comments on commit 4fc0395

Please sign in to comment.