diff --git a/crates/rune/src/compile/context.rs b/crates/rune/src/compile/context.rs index dc87427ba..754a14a6a 100644 --- a/crates/rune/src/compile/context.rs +++ b/crates/rune/src/compile/context.rs @@ -7,7 +7,9 @@ use crate::no_std::sync::Arc; use crate::compile::meta; #[cfg(feature = "doc")] use crate::compile::Docs; -use crate::compile::{ComponentRef, ContextError, IntoComponent, Item, ItemBuf, MetaInfo, Names}; +#[cfg(feature = "emit")] +use crate::compile::MetaInfo; +use crate::compile::{ComponentRef, ContextError, IntoComponent, Item, ItemBuf, Names}; use crate::hash; use crate::module::{ Fields, InternalEnum, Module, ModuleAssociated, ModuleAttributeMacro, ModuleConstant, @@ -35,6 +37,7 @@ pub(crate) struct ContextMeta { } impl ContextMeta { + #[cfg(feature = "emit")] pub(crate) fn info(&self) -> MetaInfo { MetaInfo::new(&self.kind, self.hash, self.item.as_deref()) } diff --git a/crates/rune/src/compile/error.rs b/crates/rune/src/compile/error.rs index f4561789d..f77d41167 100644 --- a/crates/rune/src/compile/error.rs +++ b/crates/rune/src/compile/error.rs @@ -212,6 +212,7 @@ pub(crate) enum ErrorKind { }, ModAlreadyLoaded { item: ItemBuf, + #[cfg(feature = "emit")] existing: (SourceId, Span), }, MissingMacro { @@ -268,7 +269,9 @@ pub(crate) enum ErrorKind { UnsupportedPatternExpr, UnsupportedBinding, DuplicateObjectKey { + #[cfg(feature = "emit")] existing: Span, + #[cfg(feature = "emit")] object: Span, }, InstanceFunctionOutsideImpl, @@ -279,6 +282,7 @@ pub(crate) enum ErrorKind { ContinueOutsideOfLoop, SelectMultipleDefaults, ExpectedBlockSemiColon { + #[cfg(feature = "emit")] followed_span: Span, }, FnConstAsyncConflict, @@ -339,13 +343,16 @@ pub(crate) enum ErrorKind { name: Box, }, VariableMoved { + #[cfg(feature = "emit")] moved_at: Span, }, UnsupportedGenerics, NestedTest { + #[cfg(feature = "emit")] nested_span: Span, }, NestedBench { + #[cfg(feature = "emit")] nested_span: Span, }, MissingFunctionHash { @@ -356,6 +363,7 @@ pub(crate) enum ErrorKind { }, PatternMissingFields { item: ItemBuf, + #[cfg(feature = "emit")] fields: Box<[Box]>, }, MissingLabelLocation { @@ -412,21 +420,27 @@ pub(crate) enum ErrorKind { BadNumberLiteral, AmbiguousItem { item: ItemBuf, + #[cfg(feature = "emit")] locations: Vec<(Location, ItemBuf)>, }, AmbiguousContextItem { item: ItemBuf, + #[cfg(feature = "emit")] infos: Box<[MetaInfo]>, }, NotVisible { + #[cfg(feature = "emit")] chain: Vec, + #[cfg(feature = "emit")] location: Location, visibility: Visibility, item: ItemBuf, from: ItemBuf, }, NotVisibleMod { + #[cfg(feature = "emit")] chain: Vec, + #[cfg(feature = "emit")] location: Location, visibility: Visibility, item: ItemBuf, @@ -436,6 +450,7 @@ pub(crate) enum ErrorKind { item: ItemBuf, }, ImportCycle { + #[cfg(feature = "emit")] path: Vec, }, ImportRecursionLimit { diff --git a/crates/rune/src/compile/pool.rs b/crates/rune/src/compile/pool.rs index 0a0f200e9..67717392e 100644 --- a/crates/rune/src/compile/pool.rs +++ b/crates/rune/src/compile/pool.rs @@ -4,7 +4,9 @@ use core::fmt; use crate::no_std::collections::HashMap; -use crate::compile::{Item, ItemBuf, Location, Visibility}; +#[cfg(feature = "emit")] +use crate::compile::Location; +use crate::compile::{Item, ItemBuf, Visibility}; use crate::hash::Hash; /// The identifier of a module. @@ -34,6 +36,7 @@ impl fmt::Display for ItemId { #[non_exhaustive] pub(crate) struct ModMeta { /// The location of the module. + #[cfg(feature = "emit")] pub(crate) location: Location, /// The item of the module. pub(crate) item: ItemId, diff --git a/crates/rune/src/compile/v1/scopes.rs b/crates/rune/src/compile/v1/scopes.rs index 33676d394..ac3a6cc8e 100644 --- a/crates/rune/src/compile/v1/scopes.rs +++ b/crates/rune/src/compile/v1/scopes.rs @@ -143,11 +143,12 @@ impl<'hir> Scopes<'hir> { tracing::trace!(?var, "getting var"); q.visitor.visit_variable_use(self.source_id, var.span, span); - if let Some(moved_at) = var.moved_at { + if let Some(_moved_at) = var.moved_at { return Err(compile::Error::new( span, ErrorKind::VariableMoved { - moved_at: moved_at.span(), + #[cfg(feature = "emit")] + moved_at: _moved_at.span(), }, )); } @@ -177,11 +178,12 @@ impl<'hir> Scopes<'hir> { tracing::trace!(?var, "taking var"); q.visitor.visit_variable_use(self.source_id, var.span, span); - if let Some(moved_at) = var.moved_at { + if let Some(_moved_at) = var.moved_at { return Err(compile::Error::new( span, ErrorKind::VariableMoved { - moved_at: moved_at.span(), + #[cfg(feature = "emit")] + moved_at: _moved_at.span(), }, )); } diff --git a/crates/rune/src/hir/lowering.rs b/crates/rune/src/hir/lowering.rs index c53cf40c7..535a3aad6 100644 --- a/crates/rune/src/hir/lowering.rs +++ b/crates/rune/src/hir/lowering.rs @@ -372,11 +372,13 @@ pub(crate) fn expr_object<'hir>( let assignments = &mut *iter!(&ast.assignments, |(ast, _)| { let key = object_key(cx, &ast.key)?; - if let Some(existing) = keys_dup.insert(key.1, key.0) { + if let Some(_existing) = keys_dup.insert(key.1, key.0) { return Err(compile::Error::new( key.0, ErrorKind::DuplicateObjectKey { - existing: existing.span(), + #[cfg(feature = "emit")] + existing: _existing.span(), + #[cfg(feature = "emit")] object: key.0.span(), }, )); @@ -1206,11 +1208,13 @@ fn pat<'hir>(cx: &mut Ctxt<'hir, '_, '_>, ast: &ast::Pat) -> compile::Result(cx: &mut Ctxt<'hir, '_, '_>, ast: &ast::Pat) -> compile::Result Indexer<'a, 'arena> { .load(root, self.q.pool.module_item(mod_item), &*item_mod)?; if let Some(loaded) = self.loaded.as_mut() { - if let Some(existing) = loaded.insert(mod_item, (self.source_id, item_mod.span())) { + if let Some(_existing) = loaded.insert(mod_item, (self.source_id, item_mod.span())) { return Err(compile::Error::new( &*item_mod, ErrorKind::ModAlreadyLoaded { item: self.q.pool.module_item(mod_item).to_owned(), - existing, + #[cfg(feature = "emit")] + existing: _existing, }, )); } @@ -756,10 +757,13 @@ fn item_fn(idx: &mut Indexer<'_, '_>, mut ast: ast::ItemFn) -> compile::Result<( let is_test = match p.try_parse::(resolve_context!(idx.q), &ast.attributes)? { Some((attr, _)) => { - if let Some(nested_span) = idx.nested_item { + if let Some(_nested_span) = idx.nested_item { return Err(compile::Error::new( attr, - ErrorKind::NestedTest { nested_span }, + ErrorKind::NestedTest { + #[cfg(feature = "emit")] + nested_span: _nested_span, + }, )); } @@ -770,12 +774,15 @@ fn item_fn(idx: &mut Indexer<'_, '_>, mut ast: ast::ItemFn) -> compile::Result<( let is_bench = match p.try_parse::(resolve_context!(idx.q), &ast.attributes)? { Some((attr, _)) => { - if let Some(nested_span) = idx.nested_item { + if let Some(_nested_span) = idx.nested_item { let span = attr.span().join(ast.descriptive_span()); return Err(compile::Error::new( span, - ErrorKind::NestedBench { nested_span }, + ErrorKind::NestedBench { + #[cfg(feature = "emit")] + nested_span: _nested_span, + }, )); } @@ -940,6 +947,7 @@ fn statements(idx: &mut Indexer<'_, '_>, ast: &mut Vec) -> compile::R return Err(compile::Error::new( span, ErrorKind::ExpectedBlockSemiColon { + #[cfg(feature = "emit")] followed_span: stmt.span(), }, )); diff --git a/crates/rune/src/lib.rs b/crates/rune/src/lib.rs index c6902f01a..a04d3ae81 100644 --- a/crates/rune/src/lib.rs +++ b/crates/rune/src/lib.rs @@ -208,8 +208,8 @@ mod exported_macros; #[macro_use] pub mod ast; -#[cfg(feature = "fmt")] -pub mod fmt; +#[cfg(all(feature = "fmt", feature = "cli"))] +pub(crate) mod fmt; cfg_emit! { pub use ::codespan_reporting::term::termcolor; diff --git a/crates/rune/src/modules/collections.rs b/crates/rune/src/modules/collections.rs index 1bb5ec096..39c51d1fe 100644 --- a/crates/rune/src/modules/collections.rs +++ b/crates/rune/src/modules/collections.rs @@ -1,12 +1,16 @@ //! `std::collections` module. +#[cfg(feature = "std")] mod hash_map; +#[cfg(feature = "std")] mod hash_set; mod vec_deque; use crate::{ContextError, Module}; +#[cfg(feature = "std")] pub(crate) use self::hash_map::HashMap; +#[cfg(feature = "std")] pub(crate) use self::hash_set::HashSet; pub(crate) use self::vec_deque::VecDeque; use crate as rune; diff --git a/crates/rune/src/modules/iter.rs b/crates/rune/src/modules/iter.rs index 6704fa651..d6bea9b5e 100644 --- a/crates/rune/src/modules/iter.rs +++ b/crates/rune/src/modules/iter.rs @@ -3,10 +3,13 @@ use crate::no_std::prelude::*; use crate as rune; -use crate::modules::collections::{HashMap, HashSet, VecDeque}; +use crate::modules::collections::VecDeque; +#[cfg(feature = "std")] +use crate::modules::collections::{HashMap, HashSet}; +#[cfg(feature = "std")] +use crate::runtime::EnvProtocolCaller; use crate::runtime::{ - EnvProtocolCaller, FromValue, Function, Iterator, Object, OwnedTuple, Protocol, Value, Vec, - VmResult, + FromValue, Function, Iterator, Object, OwnedTuple, Protocol, Value, Vec, VmResult, }; use crate::{ContextError, Module}; @@ -49,7 +52,9 @@ pub fn module() -> Result { module.function_meta(collect_vec)?; module.function_meta(collect_vec_deque)?; + #[cfg(feature = "std")] module.function_meta(collect_hash_set)?; + #[cfg(feature = "std")] module.function_meta(collect_hash_map)?; module.function_meta(collect_tuple)?; module.function_meta(collect_object)?; @@ -1070,6 +1075,7 @@ fn collect_vec_deque(it: Iterator) -> VmResult { /// assert_eq!((0..3).iter().collect::(), HashSet::from([0, 1, 2])); /// ``` #[rune::function(instance, path = collect::)] +#[cfg(feature = "std")] fn collect_hash_set(it: Iterator) -> VmResult { let mut caller = EnvProtocolCaller; HashSet::from_iter(it, &mut caller) @@ -1087,6 +1093,7 @@ fn collect_hash_set(it: Iterator) -> VmResult { /// assert_eq!(actual, expected); /// ``` #[rune::function(instance, path = collect::)] +#[cfg(feature = "std")] fn collect_hash_map(it: Iterator) -> VmResult { let mut caller = EnvProtocolCaller; HashMap::from_iter(it, &mut caller) diff --git a/crates/rune/src/query/query.rs b/crates/rune/src/query/query.rs index 8fbf07258..a86914aee 100644 --- a/crates/rune/src/query/query.rs +++ b/crates/rune/src/query/query.rs @@ -1,4 +1,5 @@ use core::fmt; +#[cfg(feature = "emit")] use core::mem::take; use crate::no_std::borrow::Cow; @@ -284,6 +285,7 @@ impl<'a, 'arena> Query<'a, 'arena> { Err(Box::new(ErrorKind::AmbiguousContextItem { item: self.pool.item(item).to_owned(), + #[cfg(feature = "emit")] infos: metas.map(|i| i.info()).collect(), })) } @@ -414,6 +416,7 @@ impl<'a, 'arena> Query<'a, 'arena> { let item = self.insert_new_item(items, location, parent, visibility, docs)?; let query_mod = self.pool.alloc_module(ModMeta { + #[cfg(feature = "emit")] location: location.location(), item: item.item, visibility, @@ -437,6 +440,7 @@ impl<'a, 'arena> Query<'a, 'arena> { let location = Location::new(source_id, spanned); let module = self.pool.alloc_module(ModMeta { + #[cfg(feature = "emit")] location, item: ItemId::default(), visibility: Visibility::Public, @@ -1107,7 +1111,14 @@ impl<'a, 'arena> Query<'a, 'arena> { cur.push(c); let cur = self.pool.alloc_item(&cur); - let update = self.import_step(span, module, cur, used, &mut path)?; + let update = self.import_step( + span, + module, + cur, + used, + #[cfg(feature = "emit")] + &mut path, + )?; let update = match update { Some(update) => update, @@ -1120,7 +1131,13 @@ impl<'a, 'arena> Query<'a, 'arena> { }); if !visited.insert(self.pool.alloc_item(&item)) { - return Err(compile::Error::new(span, ErrorKind::ImportCycle { path })); + return Err(compile::Error::new( + span, + ErrorKind::ImportCycle { + #[cfg(feature = "emit")] + path, + }, + )); } module = update.module; @@ -1147,7 +1164,7 @@ impl<'a, 'arena> Query<'a, 'arena> { module: ModId, item: ItemId, used: Used, - path: &mut Vec, + #[cfg(feature = "emit")] path: &mut Vec, ) -> compile::Result> { // already resolved query. if let Some(meta) = self.inner.meta.get(&(item, Hash::EMPTY)) { @@ -1168,8 +1185,10 @@ impl<'a, 'arena> Query<'a, 'arena> { module, item, entry.item_meta.module, + #[cfg(feature = "emit")] entry.item_meta.location, entry.item_meta.visibility, + #[cfg(feature = "emit")] path, )?; @@ -1559,6 +1578,7 @@ impl<'a, 'arena> Query<'a, 'arena> { span, ErrorKind::AmbiguousItem { item: self.pool.item(cur.item_meta.item).to_owned(), + #[cfg(feature = "emit")] locations: locations .into_iter() .map(|(loc, item)| (loc, self.pool.item(item).to_owned())) @@ -1572,6 +1592,7 @@ impl<'a, 'arena> Query<'a, 'arena> { span, ErrorKind::AmbiguousItem { item: self.pool.item(cur.item_meta.item).to_owned(), + #[cfg(feature = "emit")] locations: locations .into_iter() .map(|(loc, item)| (loc, self.pool.item(item).to_owned())) @@ -1640,10 +1661,11 @@ impl<'a, 'arena> Query<'a, 'arena> { from: ModId, item: ItemId, module: ModId, - location: Location, + #[cfg(feature = "emit")] location: Location, visibility: Visibility, - chain: &mut Vec, + #[cfg(feature = "emit")] chain: &mut Vec, ) -> compile::Result<()> { + #[cfg(feature = "emit")] fn into_chain(chain: Vec) -> Vec { chain.into_iter().map(|c| c.location).collect() } @@ -1672,7 +1694,9 @@ impl<'a, 'arena> Query<'a, 'arena> { return Err(compile::Error::new( span, ErrorKind::NotVisibleMod { + #[cfg(feature = "emit")] chain: into_chain(take(chain)), + #[cfg(feature = "emit")] location: m.location, visibility: m.visibility, item: current_module, @@ -1686,7 +1710,9 @@ impl<'a, 'arena> Query<'a, 'arena> { return Err(compile::Error::new( span, ErrorKind::NotVisible { + #[cfg(feature = "emit")] chain: into_chain(take(chain)), + #[cfg(feature = "emit")] location, visibility, item: self.pool.item(item).to_owned(), diff --git a/crates/rune/src/runtime.rs b/crates/rune/src/runtime.rs index 6068cc0a6..ac18f9091 100644 --- a/crates/rune/src/runtime.rs +++ b/crates/rune/src/runtime.rs @@ -167,8 +167,10 @@ mod vm_call; pub(crate) use self::vm_call::VmCall; mod vm_error; +#[cfg(feature = "emit")] +pub(crate) use self::vm_error::VmErrorAt; +pub(crate) use self::vm_error::VmErrorKind; pub use self::vm_error::{try_result, TryFromResult, VmError, VmIntegerRepr, VmResult}; -pub(crate) use self::vm_error::{VmErrorAt, VmErrorKind}; mod vm_execution; pub use self::vm_execution::{ExecutionState, VmExecution, VmSendExecution}; @@ -183,5 +185,7 @@ pub use self::fmt::Formatter; mod control_flow; pub use self::control_flow::ControlFlow; +#[cfg(feature = "std")] mod hasher; +#[cfg(feature = "std")] pub use self::hasher::Hasher; diff --git a/crates/rune/src/runtime/type_info.rs b/crates/rune/src/runtime/type_info.rs index 7e7abd42a..8753564ae 100644 --- a/crates/rune/src/runtime/type_info.rs +++ b/crates/rune/src/runtime/type_info.rs @@ -20,6 +20,7 @@ pub enum TypeInfo { } impl TypeInfo { + #[cfg(feature = "emit")] pub(crate) fn type_hash(&self) -> Hash { match self { TypeInfo::StaticType(ty) => ty.hash, diff --git a/crates/rune/src/runtime/value.rs b/crates/rune/src/runtime/value.rs index fa6316d01..db9bbc7e1 100644 --- a/crates/rune/src/runtime/value.rs +++ b/crates/rune/src/runtime/value.rs @@ -12,12 +12,14 @@ use crate::no_std::vec; use crate::compile::ItemBuf; use crate::runtime::vm::CallResult; +#[cfg(feature = "std")] +use crate::runtime::Hasher; use crate::runtime::{ AccessKind, AnyObj, Bytes, ConstValue, ControlFlow, EnvProtocolCaller, Format, Formatter, - FromValue, FullTypeOf, Function, Future, Generator, GeneratorState, Hasher, Iterator, - MaybeTypeOf, Mut, Object, OwnedTuple, Protocol, ProtocolCaller, Range, RangeFrom, RangeFull, - RangeInclusive, RangeTo, RangeToInclusive, RawMut, RawRef, Ref, Shared, Stream, ToValue, Type, - TypeInfo, Variant, Vec, Vm, VmError, VmErrorKind, VmIntegerRepr, VmResult, + FromValue, FullTypeOf, Function, Future, Generator, GeneratorState, Iterator, MaybeTypeOf, Mut, + Object, OwnedTuple, Protocol, ProtocolCaller, Range, RangeFrom, RangeFull, RangeInclusive, + RangeTo, RangeToInclusive, RawMut, RawRef, Ref, Shared, Stream, ToValue, Type, TypeInfo, + Variant, Vec, Vm, VmError, VmErrorKind, VmIntegerRepr, VmResult, }; use crate::{Any, Hash}; @@ -1338,6 +1340,7 @@ impl Value { } /// Hash the current value. + #[cfg(feature = "std")] pub(crate) fn hash_with( &self, hasher: &mut Hasher, diff --git a/crates/rune/src/runtime/vm_error.rs b/crates/rune/src/runtime/vm_error.rs index 163052ed9..1322c2687 100644 --- a/crates/rune/src/runtime/vm_error.rs +++ b/crates/rune/src/runtime/vm_error.rs @@ -85,6 +85,7 @@ pub struct VmErrorLocation { #[non_exhaustive] pub struct VmErrorAt { /// Index into the backtrace which contains information of what caused this error. + #[cfg(feature = "emit")] index: usize, /// The kind of error. kind: VmErrorKind, @@ -92,6 +93,7 @@ pub struct VmErrorAt { impl VmErrorAt { /// Get the instruction which caused the error. + #[cfg(feature = "emit")] pub(crate) fn index(&self) -> usize { self.index } @@ -255,9 +257,11 @@ impl VmResult { match self { Self::Ok(ok) => Self::Ok(ok), Self::Err(mut err) => { + #[cfg(feature = "emit")] let index = err.inner.stacktrace.len(); err.inner.chain.push(VmErrorAt { + #[cfg(feature = "emit")] index, kind: VmErrorKind::from(error()), }); @@ -362,6 +366,7 @@ where Self { inner: Box::new(VmErrorInner { error: VmErrorAt { + #[cfg(feature = "emit")] index: 0, kind: VmErrorKind::from(error), }, @@ -386,12 +391,17 @@ impl From<[VmErrorKind; N]> for VmError { let mut chain = Vec::with_capacity(it.len()); for kind in it { - chain.push(VmErrorAt { index: 0, kind }); + chain.push(VmErrorAt { + #[cfg(feature = "emit")] + index: 0, + kind, + }); } Self { inner: Box::new(VmErrorInner { error: VmErrorAt { + #[cfg(feature = "emit")] index: 0, kind: first, }, @@ -529,6 +539,7 @@ pub(crate) enum VmErrorKind { target: TypeInfo, index: VmIntegerRepr, }, + #[cfg(feature = "std")] MissingIndexKey { target: TypeInfo, }, @@ -612,6 +623,7 @@ pub(crate) enum VmErrorKind { lhs: f64, rhs: f64, }, + #[cfg(feature = "std")] IllegalFloatOperation { value: f64, }, @@ -729,6 +741,7 @@ impl fmt::Display for VmErrorKind { VmErrorKind::MissingIndexInteger { target, index } => { write!(f, "Type `{target}` missing integer index `{index}`",) } + #[cfg(feature = "std")] VmErrorKind::MissingIndexKey { target } => { write!(f, "Type `{target}` missing index",) } @@ -811,6 +824,7 @@ impl fmt::Display for VmErrorKind { "Cannot perform a comparison of the floats {lhs} and {rhs}", ) } + #[cfg(feature = "std")] VmErrorKind::IllegalFloatOperation { value } => { write!(f, "Cannot perform operation on float `{value}`",) }