Skip to content

Commit

Permalink
Remove unit type
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Aug 20, 2023
1 parent dab429a commit 64fe2a2
Show file tree
Hide file tree
Showing 58 changed files with 650 additions and 502 deletions.
11 changes: 1 addition & 10 deletions crates/rune-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,7 @@ impl fmt::Display for Hash {

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
return f.debug_tuple("Hash").field(&Hex(self.0)).finish();

#[repr(transparent)]
struct Hex(u64);

impl fmt::Debug for Hex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
write!(f, "0x{:x}", self.0)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ impl Context {
to_value: path(m, ["runtime", "ToValue"]),
token_stream: path(m, ["macros", "TokenStream"]),
try_result: path(m, ["runtime", "try_result"]),
owned_tuple: path(m, ["runtime", "OwnedTuple"]),
tuple: path(m, ["runtime", "Tuple"]),
type_info: path(m, ["runtime", "TypeInfo"]),
type_name: path(&core, ["any", "type_name"]),
type_of: path(m, ["runtime", "TypeOf"]),
unit_struct: path(m, ["runtime", "UnitStruct"]),
unsafe_to_value: path(m, ["runtime", "UnsafeToValue"]),
unsafe_to_ref: path(m, ["runtime", "UnsafeToRef"]),
unsafe_to_mut: path(m, ["runtime", "UnsafeToMut"]),
Expand Down Expand Up @@ -772,11 +772,11 @@ pub(crate) struct Tokens {
pub(crate) to_value: syn::Path,
pub(crate) token_stream: syn::Path,
pub(crate) try_result: syn::Path,
pub(crate) owned_tuple: syn::Path,
pub(crate) tuple: syn::Path,
pub(crate) type_info: syn::Path,
pub(crate) type_name: syn::Path,
pub(crate) type_of: syn::Path,
pub(crate) unit_struct: syn::Path,
pub(crate) unsafe_to_value: syn::Path,
pub(crate) unsafe_to_ref: syn::Path,
pub(crate) unsafe_to_mut: syn::Path,
Expand Down
15 changes: 10 additions & 5 deletions crates/rune-macros/src/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@ impl Expander {
value,
from_value,
vm_result,
tuple,
..
} = &self.tokens;

let (expanded, expected) = match &st.fields {
syn::Fields::Unit => {
let expanded = quote_spanned! {
input.span() =>
#value::Unit => {
#value::EmptyTuple => {
#vm_result::Ok(Self)
}
#value::UnitStruct(..) => {
#value::EmptyStruct(..) => {
#vm_result::Ok(Self)
}
};

(expanded, &self.tokens.unit_struct)
(expanded, &self.tokens.owned_tuple)
}
syn::Fields::Unnamed(f) => {
let expanded = self.expand_unnamed(f)?;
let borrow_ref = self.tokens.vm_try(quote!(tuple.borrow_ref()));

let expanded = quote_spanned! {
f.span() =>
#value::EmptyTuple => {
let tuple = #tuple::new(&[]);
#vm_result::Ok(Self(#expanded))
}
#value::Tuple(tuple) => {
let tuple = #borrow_ref;
#vm_result::Ok(Self(#expanded))
Expand All @@ -54,7 +59,7 @@ impl Expander {
}
};

(expanded, &self.tokens.tuple)
(expanded, &self.tokens.owned_tuple)
}
syn::Fields::Named(f) => {
let expanded = self.expand_named(f)?;
Expand Down Expand Up @@ -157,7 +162,7 @@ impl Expander {
};

match variant.data() {
#variant_data::Unit => match name {
#variant_data::Empty => match name {
#(#unit_matches,)* #missing,
},
#variant_data::Tuple(tuple) => match name {
Expand Down
4 changes: 2 additions & 2 deletions crates/rune-macros/src/to_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Expander {
let Tokens {
to_value,
value,
tuple,
owned_tuple,
vm_result,
..
} = &self.tokens;
Expand All @@ -76,7 +76,7 @@ impl Expander {
unnamed.span() =>
let mut tuple = Vec::with_capacity(#cap);
#(#to_values;)*
#vm_result::Ok(#value::from(#tuple::from(tuple)))
#vm_result::Ok(#value::from(#owned_tuple::from(tuple)))
})
}

Expand Down
68 changes: 4 additions & 64 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use crate::compile::meta;
use crate::compile::Docs;
use crate::compile::{ComponentRef, ContextError, IntoComponent, Item, ItemBuf, MetaInfo, Names};
use crate::module::{
Fields, Function, InternalEnum, Module, ModuleAssociated, ModuleAttributeMacro, ModuleConstant,
ModuleFunction, ModuleMacro, ModuleType, TypeSpecification, UnitType,
Fields, InternalEnum, Module, ModuleAssociated, ModuleAttributeMacro, ModuleConstant,
ModuleFunction, ModuleMacro, ModuleType, TypeSpecification,
};
use crate::runtime::{
AttributeMacroHandler, ConstValue, FunctionHandler, MacroHandler, Protocol, RuntimeContext,
StaticType, TypeCheck, TypeInfo, TypeOf, VariantRtti,
StaticType, TypeCheck, TypeInfo, VariantRtti,
};
use crate::Hash;

Expand Down Expand Up @@ -138,6 +138,7 @@ impl Context {
this.install(crate::modules::cmp::module()?)?;
this.install(crate::modules::collections::module()?)?;
this.install(crate::modules::f64::module()?)?;
this.install(crate::modules::tuple::module()?)?;
this.install(crate::modules::fmt::module()?)?;
this.install(crate::modules::future::module()?)?;
this.install(crate::modules::generator::module()?)?;
Expand Down Expand Up @@ -228,10 +229,6 @@ impl Context {
self.install_constant(module, m)?;
}

if let Some(unit_type) = &module.unit_type {
self.install_unit_type(module, unit_type)?;
}

for internal_enum in &module.internal_enums {
self.install_internal_enum(module, internal_enum)?;
}
Expand Down Expand Up @@ -789,63 +786,6 @@ impl Context {
Ok(())
}

/// Install unit type.
fn install_unit_type(
&mut self,
module: &Module,
unit_type: &UnitType,
) -> Result<(), ContextError> {
let item = module.item.extended(&*unit_type.name);

self.install_type_info(ContextType {
item: item.clone(),
hash: crate::runtime::static_type::UNIT_TYPE.hash,
type_check: Some(TypeCheck::Unit),
type_info: crate::runtime::static_type::UNIT_TYPE.type_info(),
type_parameters: Hash::EMPTY,
})?;

let hash = <() as TypeOf>::type_hash();

let signature = meta::Signature {
#[cfg(feature = "doc")]
is_async: false,
#[cfg(feature = "doc")]
deprecated: None,
#[cfg(feature = "doc")]
args: Some(0),
#[cfg(feature = "doc")]
return_type: Some(hash),
#[cfg(feature = "doc")]
argument_types: Box::from([]),
};

let constructor = || ();
let handler: Arc<FunctionHandler> =
Arc::new(move |stack, args| constructor.fn_call(stack, args));

self.insert_native_fn(hash, &handler)?;

self.install_meta(ContextMeta {
hash,
item: Some(item.clone()),
kind: meta::Kind::Struct {
fields: meta::Fields::Unnamed(0),
constructor: Some(signature),
parameters: Hash::EMPTY,
},
#[cfg(feature = "doc")]
docs: unit_type.docs.clone(),
})?;

self.constants.insert(
Hash::associated_function(hash, Protocol::INTO_TYPE_NAME),
ConstValue::String(item.to_string()),
);

Ok(())
}

/// Install generator state types.
fn install_internal_enum(
&mut self,
Expand Down
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 @@ -217,7 +217,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() {
return Ok(ir::Ir::new(span, ir::Value::Unit));
return Ok(ir::Ir::new(span, ir::Value::EmptyTuple));
}

let mut items = Vec::new();
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 @@ -58,7 +58,7 @@ fn eval_ir_assign(
.scopes
.mut_target(&ir.target, move |t| ir.op.assign(ir, t, value))?;

Ok(ir::Value::Unit)
Ok(ir::Value::EmptyTuple)
}

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

Ok(ir::Value::Unit)
Ok(ir::Value::EmptyTuple)
}

fn eval_ir_call(
Expand Down Expand Up @@ -218,7 +218,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(ir::Value::Unit)
Ok(ir::Value::EmptyTuple)
}

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

Ok(value)
} else {
Ok(ir::Value::Unit)
Ok(ir::Value::EmptyTuple)
}
}

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

interp.scopes.pop(guard).with_span(ir)?;
Expand All @@ -317,7 +317,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(ir::Value::Unit)
Ok(ir::Value::EmptyTuple)
}

fn eval_ir_template(
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::compile::{self, IrErrorKind, ItemId, ModId, WithSpan};
use crate::hir;
use crate::parse::NonZeroId;
use crate::query::{Query, Used};
use crate::runtime::{ConstValue, Object, Tuple};
use crate::runtime::{ConstValue, Object, OwnedTuple};

/// The interpreter that executed [Ir][crate::ir::Ir].
pub struct Interpreter<'a, 'arena> {
Expand Down Expand Up @@ -197,7 +197,7 @@ impl ir::Scopes {
}
}
actual => {
return Err(compile::Error::expected_type::<_, Tuple>(
return Err(compile::Error::expected_type::<_, OwnedTuple>(
ir_target, &actual,
))
}
Expand Down Expand Up @@ -229,7 +229,7 @@ impl ir::Scopes {
}
}
actual => {
return Err(compile::Error::expected_type::<_, Tuple>(
return Err(compile::Error::expected_type::<_, OwnedTuple>(
ir_target, &actual,
))
}
Expand Down Expand Up @@ -292,7 +292,7 @@ impl ir::Scopes {
}
}
actual => {
return Err(compile::Error::expected_type::<_, Tuple>(
return Err(compile::Error::expected_type::<_, OwnedTuple>(
ir_target, &actual,
));
}
Expand Down Expand Up @@ -365,7 +365,7 @@ impl ir::Scopes {

op(value)
}
actual => Err(compile::Error::expected_type::<_, Tuple>(
actual => Err(compile::Error::expected_type::<_, OwnedTuple>(
ir_target, &actual,
)),
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rune/src/compile/ir/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::runtime::{Bytes, ConstValue, Shared, TypeInfo};
#[derive(Debug, Clone)]
pub enum Value {
/// A constant unit.
Unit,
EmptyTuple,
/// A byte.
Byte(u8),
/// A character.
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Value {
/// Convert a constant value into an interpreter value.
pub(crate) fn from_const(value: &ConstValue) -> Self {
match value {
ConstValue::Unit => Self::Unit,
ConstValue::EmptyTuple => Self::EmptyTuple,
ConstValue::Byte(b) => Self::Byte(*b),
ConstValue::Char(c) => Self::Char(*c),
ConstValue::Bool(b) => Self::Bool(*b),
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Value {
S: Copy + Spanned,
{
Ok(match self {
Value::Unit => ConstValue::Unit,
Value::EmptyTuple => ConstValue::EmptyTuple,
Value::Byte(b) => ConstValue::Byte(b),
Value::Char(c) => ConstValue::Char(c),
Value::Bool(b) => ConstValue::Bool(b),
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Value {
/// Get the type information of the value.
pub(crate) fn type_info(&self) -> TypeInfo {
match self {
Self::Unit => TypeInfo::StaticType(rt::static_type::UNIT_TYPE),
Self::EmptyTuple => TypeInfo::StaticType(rt::static_type::TUPLE_TYPE),
Self::Byte(..) => TypeInfo::StaticType(rt::static_type::BYTE_TYPE),
Self::Char(..) => TypeInfo::StaticType(rt::static_type::CHAR_TYPE),
Self::Bool(..) => TypeInfo::StaticType(rt::static_type::BOOL_TYPE),
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/compile/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ impl Prelude {
this.add_prelude("f64", ["f64"]);
this.add_prelude("i64", ["i64"]);
this.add_prelude("char", ["char"]);
this.add_prelude("Tuple", ["Tuple"]);
this.add_prelude("dbg", ["io", "dbg"]);
this.add_prelude("drop", ["mem", "drop"]);
this.add_prelude("Err", ["result", "Result", "Err"]);
Expand All @@ -35,6 +34,7 @@ impl Prelude {
this.add_prelude("is_writable", ["is_writable"]);
this.add_prelude("line", ["macros", "builtin", "line"]);
this.add_prelude("None", ["option", "Option", "None"]);
this.add_prelude("Tuple", ["tuple", "Tuple"]);
this.add_prelude("Object", ["object", "Object"]);
this.add_prelude("Ok", ["result", "Result", "Ok"]);
this.add_prelude("Option", ["option", "Option"]);
Expand All @@ -45,7 +45,6 @@ impl Prelude {
this.add_prelude("Some", ["option", "Option", "Some"]);
this.add_prelude("String", ["string", "String"]);
this.add_prelude("stringify", ["stringify"]);
this.add_prelude("unit", ["unit"]);
this.add_prelude("Vec", ["vec", "Vec"]);
this.add_prelude("Bytes", ["bytes", "Bytes"]);

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compile/unit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl UnitBuilder {
fields: meta::Fields::Empty,
..
} => {
let info = UnitFn::UnitStruct { hash: meta.hash };
let info = UnitFn::EmptyStruct { hash: meta.hash };

let signature = DebugSignature::new(
pool.item(meta.item_meta.item).to_owned(),
Expand Down
Loading

0 comments on commit 64fe2a2

Please sign in to comment.