8000 rune: Enable more miri tests · rune-rs/rune@7631826 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7631826

Browse files
committed
rune: Enable more miri tests
1 parent 35f8997 commit 7631826

File tree

13 files changed

+224
-94
lines changed

13 files changed

+224
-94
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ jobs:
132132
strategy:
133133
fail-fast: false
134134
matrix:
135-
include:
136-
- crate: rune
137-
target: >
138-
runtime:: hir::arena:: --skip runtime::vm:: --skip runtime::vm_execution::
139-
- crate: rune-alloc
135+
crate:
136+
- rune
137+
- rune-core
138+
- rune-alloc
140139
steps:
141140
- uses: actions/checkout@v4
142141
- uses: dtolnay/rust-toolchain@nightly
143142
with:
144143
components: miri
145144
- uses: Swatinem/rust-cache@v2
146-
- run: cargo miri test -p ${{matrix.crate}} --all-features -- ${{matrix.target}}
145+
- run: cargo miri test -p ${{matrix.crate}} --all-features --all-targets
146+
- run: cargo miri test -p ${{matrix.crate}} --all-features --doc
147147

148148
test_lib:
149149
runs-on: ubuntu-latest

crates/rune/src/build.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl core::error::Error for BuildError {
9191
/// Note: these must be built with the `emit` feature enabled (default) to give
9292
/// access to `rune::termcolor`.
9393
///
94-
/// ```
94+
/// ```no_run
9595
/// use rune::termcolor::{ColorChoice, StandardStream};
9696
/// use rune::{Context, Source, Vm};
9797
/// use std::sync::Arc;
@@ -101,9 +101,9 @@ impl core::error::Error for BuildError {
101101
///
102102
/// let mut sources = rune::Sources::new();
103103
///
104-
/// sources.insert(Source::new("entry", r#"
104+
/// sources.insert(Source::memory(r#"
105105
/// pub fn main() {
106-
/// println("Hello World");
106+
/// println!("Hello World");
107107
/// }
108108
/// "#)?)?;
109109
///
@@ -237,12 +237,14 @@ impl<'a> compile::CompileVisitor for CompileVisitorGroup<'a> {
237237
}
238238

239239
impl<'a, S> Build<'a, S> {
240-
/// Modify the current [Build] to use the given [Context] while building.
240+
/// Modify the current [`Build`] to use the given [`Context`] while
241+
/// building.
242+
///
243+
/// If unspecified the empty context constructed with [`Context::new`] will
244+
/// be used. Since this counts as building without a context,
245+
/// [`Vm::without_runtime`] can be used when running the produced [`Unit`].
241246
///
242-
/// If unspecified the empty context constructed with [Context::new] will be
243-
/// used. Since this counts as building without a context,
244-
/// [Vm::without_context][crate::runtime::Vm] can be used when running the
245-
/// produced [Unit].
247+
/// [`Vm::without_runtime`]: crate::Vm::without_runtime
246248
#[inline]
247249
pub fn with_context(mut self, context: &'a Context) -> Self {
248250
self.context = Some(context);

crates/rune/src/compile/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl Context {
390390
/// This is not a cheap operation, since it requires cloning things out of
391391
/// the build-time [Context] which are necessary at runtime.
392392
///
393-
/// ```
393+
/// ```no_run
394394
/// use rune::{Context, Vm, Unit};
395395
/// use std::sync::Arc;
396396
///

crates/rune/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
//!
7474
//! [`termcolor`]: https://docs.rs/termcolor
7575
//!
76-
//! ```rust
76+
//! ```no_run
7777
//! use rune::{Context, Diagnostics, Source, Sources, Vm};
7878
//! use rune::termcolor::{ColorChoice, StandardStream};
7979
//! use std::sync::Arc;

crates/rune/src/modules/capture_io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//!
33
//! # Examples
44
//!
5-
//! ```
6-
//! use rune::{Context, ContextError};
5+
//! ```no_run
6+
//! use rune::Context;
77
//! use rune::modules::capture_io::{self, CaptureIo};
88
//!
99
//! let io = CaptureIo::new();
1010
//!
1111
//! let mut context = rune::Context::with_config(false)?;
1212
//! context.install(capture_io::module(&io)?)?;
13-
//! # Ok::<_, ContextError>(())
13+
//! # Ok::<_, rune::ContextError>(())
1414
//! ```
1515
1616
use core::mem::take;

crates/rune/src/modules/disable_io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
//!
33
//! # Examples
44
//!
5-
//! ```
6-
//! use rune::{Context, ContextError};
5+
//! ```no_run
6+
//! use rune::Context;
77
//! use rune::modules::disable_io;
88
//!
99
//! let mut context = rune::Context::with_config(false)?;
1010
//! context.install(disable_io::module()?)?;
11-
//! # Ok::<_, ContextError>(())
11+
//! # Ok::<_, rune::ContextError>(())
1212
//! ```
1313
1414
use crate as rune;

crates/rune/src/runtime/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,11 @@ pub(crate) use self::unit::UnitFn;
165165
pub use self::unit::{Unit, UnitStorage};
166166

167167
mod value;
168-
#[cfg(any(test, feature = "cli"))]
169-
pub(crate) use self::value::OwnedRepr;
170168
pub use self::value::{
171169
Accessor, EmptyStruct, Inline, RawValueGuard, Rtti, Struct, TupleStruct, TypeValue, Value,
172170
ValueMutGuard, ValueRefGuard, VariantRtti,
173171
};
174-
pub(crate) use self::value::{BorrowRefRepr, MutRepr, Mutable, RefRepr};
172+
pub(crate) use self::value::{BorrowRefRepr, MutRepr, Mutable, OwnedRepr, RefRepr};
175173

176174
mod variant;
177175
pub use self::variant::{Variant, VariantData};

crates/rune/src/runtime/value/data.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate as rune;
99
use crate::alloc::prelude::*;
1010
use crate::runtime::{OwnedTuple, TypeInfo};
1111

12-
use super::{Rtti, Value};
12+
use super::{FromValue, Mutable, OwnedRepr, Rtti, RuntimeError, Value};
1313

1414
/// A empty with a well-defined type.
1515
#[derive(TryClone)]
@@ -31,6 +31,17 @@ impl EmptyStruct {
3131
}
3232
}
3333

34+
impl FromValue for EmptyStruct {
35+
fn from_value(value: Value) -> Result<Self, RuntimeError> {
36+
match value.take_repr()? {
37+
OwnedRepr::Inline(value) => Err(RuntimeError::expected_unit_struct(value.type_info())),
38+
OwnedRepr::Mutable(Mutable::EmptyStruct(value)) => Ok(value),
39+
OwnedRepr::Mutable(value) => Err(RuntimeError::expected_unit_struct(value.type_info())),
40+
OwnedRepr::Any(value) => Err(RuntimeError::expected_unit_struct(value.type_info())),
41+
}
42+
}
43+
}
44+
3445
impl fmt::Debug for EmptyStruct {
3546
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3647
write!(f, "{}", self.rtti.item)
@@ -78,6 +89,19 @@ impl TupleStruct {
7889
}
7990
}
8091

92+
impl FromValue for TupleStruct {
93+
fn from_value(value: Value) -> Result<Self, RuntimeError> {
94+
match value.take_repr()? {
95+
OwnedRepr::Inline(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())),
96+
OwnedRepr::Mutable(Mutable::TupleStruct(value)) => Ok(value),
97+
OwnedRepr::Mutable(value) => {
98+
Err(RuntimeError::expected_tuple_struct(value.type_info()))
99+
}
100+
OwnedRepr::Any(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())),
101+
}
102+
}
103+
}
104+
81105
impl fmt::Debug for TupleStruct {
82106
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83107
write!(f, "{}{:?}", self.rtti.item, self.data)
@@ -133,6 +157,17 @@ impl Struct {
133157
}
134158
}
135159

160+
impl FromValue for Struct {
161+
fn from_value(value: Value) -> Result<Self, RuntimeError> {
162+
match value.take_repr()? {
163+
OwnedRepr::Inline(value) => Err(RuntimeError::expected_struct(value.type_info())),
164+
OwnedRepr::Mutable(Mutable::Struct(value)) => Ok(value),
165+
OwnedRepr::Mutable(value) => Err(RuntimeError::expected_struct(value.type_info())),
166+
OwnedRepr::Any(value) => Err(RuntimeError::expected_struct(value.type_info())),
167+
}
168+
}
169+
}
170+
136171
impl fmt::Debug for Struct {
137172
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138173
write!(f, "{} {{", self.rtti.item)?;

crates/rune/src/runtime/variant.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use crate as rune;
77
use crate::alloc::clone::TryClone;
88
use crate::alloc::Box;
99

10-
use super::{Accessor, OwnedTuple, ProtocolCaller, TypeInfo, Value, VariantRtti, Vec, VmResult};
10+
use super::{
11+
Accessor, FromValue, Mutable, OwnedRepr, OwnedTuple, ProtocolCaller, RuntimeError, Tuple,
12+
TypeInfo, Value, VariantRtti, Vec, VmResult,
13+
};
1114

1215
/// The variant of a type.
1316
#[derive(TryClone)]
@@ -26,6 +29,14 @@ impl Variant {
2629
}
2730
}
2831

32+
/// Try to access variant data as a tuple.
33+
pub fn as_tuple(&self) -> Option<&Tuple> {
34+
match &self.data {
35+
VariantData::Tuple(tuple) => Some(tuple),
36+
_ => None,
37+
}
38+
}
39+
2940
/// Construct a unit variant.
3041
pub(crate) fn unit(rtti: Arc<VariantRtti>) -> Self {
3142
Self {
@@ -61,7 +72,7 @@ impl Variant {
6172
}
6273

6374
/// Access the underlying variant data mutably.
64-
pub fn data_mut(&mut self) -> &mut VariantData {
75+
pub(crate) fn data_mut(&mut self) -> &mut VariantData {
6576
&mut self.data
6677
}
6778

@@ -165,6 +176,17 @@ impl Variant {
165176
}
166177
}
167178

179+
impl FromValue for Variant {
180+
fn from_value(value: Value) -> Result<Self, RuntimeError> {
181+
match value.take_repr()? {
182+
OwnedRepr::Inline(value) => Err(RuntimeError::expected_variant(value.type_info())),
183+
OwnedRepr::Mutable(Mutable::Variant(value)) => Ok(value),
184+
OwnedRepr::Mutable(value) => Err(RuntimeError::expected_variant(value.type_info())),
185+
OwnedRepr::Any(value) => Err(RuntimeError::expected_variant(value.type_info())),
186+
}
187+
}
188+
}
189+
168190
/// The data of the variant.
169191
#[derive(TryClone)]
170192
pub enum VariantData {

crates/rune/src/runtime/vm.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,11 @@ impl Vm {
318318
///
319319
/// # Examples
320320
///
321-
/// ```
321+
/// ```no_run
322322
/// use rune::{Context, Unit, Vm};
323323
///
324324
/// use std::sync::Arc;
325325
///
326-
/// let context = Context::with_default_modules()?;
327-
/// let context = Arc::new(context.runtime()?);
328-
///
329326
/// let mut sources = rune::sources! {
330327
/// entry => {
331328
/// pub fn max(a, b) {
@@ -338,10 +335,13 @@ impl Vm {
338335
/// }
339336
/// };
340337
///
338+
/// let context = Context::with_default_modules()?;
339+
/// let runtime = Arc::new(context.runtime()?);
340+
///
341341
/// let unit = rune::prepare(&mut sources).build()?;
342342
/// let unit = Arc::new(unit);
343343
///
344-
/// let vm = Vm::new(context, unit);
344+
/// let vm = Vm::new(runtime, unit);
345345
///
346346
/// // Looking up an item from the source.
347347
/// let dynamic_max = vm.lookup_function(["max"])?;
@@ -394,18 +394,12 @@ impl Vm {
394394
///
395395
/// # Examples
396396
///
397-
/// ```,no_run
397+
/// ```no_run
398398
/// use rune::{Context, Unit};
399399
/// use std::sync::Arc;
400400
///
401-
/// let context = Context::with_default_modules()?;
402-
/// let context = Arc::new(context.runtime()?);
403-
///
404-
/// // Normally the unit would be created by compiling some source,
405-
/// // and since this one is empty it won't do anything.
406401
/// let unit = Arc::new(Unit::default());
407-
///
408-
/// let mut vm = rune::Vm::new(context, unit);
402+
/// let mut vm = rune::Vm::without_runtime(unit);
409403
///
410404
/// let output = vm.execute(["main"], (33i64,))?.complete().into_result()?;
411405
/// let output: i64 = rune::from_value(output)?;
@@ -417,18 +411,14 @@ impl Vm {
417411
/// You can use a `Vec<Value>` to provide a variadic collection of
418412
/// arguments.
419413
///
420-
/// ```,no_run
414+
// 88E1 / ```no_run
421415
/// use rune::{Context, Unit};
422416
/// use std::sync::Arc;
423417
///
424-
/// let context = Context::with_default_modules()?;
425-
/// let context = Arc::new(context.runtime()?);
426-
///
427418
/// // Normally the unit would be created by compiling some source,
428419
/// // and since this one is empty it won't do anything.
429420
/// let unit = Arc::new(Unit::default());
430-
///
431-
/// let mut vm = rune::Vm::new(context, unit);
421+
/// let mut vm = rune::Vm::without_runtime(unit);
432422
///
433423
/// let mut args = Vec::new();
434424
/// args.push(rune::to_value(1u32)?);

0 commit comments

Comments
 (0)
0