Merge remote-tracking branch 'origin/master' into pch/wiggle_error_transforms

This commit is contained in:
Pat Hickey
2020-05-30 13:20:30 -07:00
71 changed files with 1297 additions and 409 deletions

View File

@@ -10,8 +10,8 @@ pub struct wasmtime_error_t {
wasmtime_c_api_macros::declare_own!(wasmtime_error_t);
impl wasmtime_error_t {
pub(crate) fn to_trap(&self) -> Box<wasm_trap_t> {
Box::new(wasm_trap_t::new(Trap::new(format!("{:?}", self.error))))
pub(crate) fn to_trap(self) -> Box<wasm_trap_t> {
Box::new(wasm_trap_t::new(Trap::from(self.error)))
}
}

View File

@@ -53,7 +53,7 @@ pub extern "C" fn wasm_trap_new(
#[no_mangle]
pub extern "C" fn wasm_trap_message(trap: &wasm_trap_t, out: &mut wasm_message_t) {
let mut buffer = Vec::new();
buffer.extend_from_slice(trap.trap.borrow().message().as_bytes());
buffer.extend_from_slice(trap.trap.borrow().to_string().as_bytes());
buffer.reserve_exact(1);
buffer.push(0);
out.set_buffer(buffer);

View File

@@ -23,6 +23,7 @@ cfg-if = "0.1.9"
backtrace = "0.3.42"
rustc-demangle = "0.1.16"
lazy_static = "1.4"
log = "0.4.8"
wat = { version = "1.0.18", optional = true }
[target.'cfg(target_os = "windows")'.dependencies]

View File

@@ -2,7 +2,8 @@ use crate::{
Extern, ExternType, Func, FuncType, GlobalType, ImportType, Instance, IntoFunc, Module, Store,
Trap,
};
use anyhow::{anyhow, bail, Context, Result};
use anyhow::{anyhow, bail, Context, Error, Result};
use log::warn;
use std::collections::hash_map::{Entry, HashMap};
use std::rc::Rc;
@@ -403,12 +404,7 @@ impl Linker {
let export_name = export.name().to_owned();
let func = Func::new(&self.store, func_ty.clone(), move |_, params, results| {
// Create a new instance for this command execution.
let instance = Instance::new(&module, &imports).map_err(|error| match error
.downcast::<Trap>()
{
Ok(trap) => trap,
Err(error) => Trap::new(format!("{:?}", error)),
})?;
let instance = Instance::new(&module, &imports)?;
// `unwrap()` everything here because we know the instance contains a
// function export with the given name and signature because we're
@@ -436,14 +432,22 @@ impl Linker {
} else if export.name() == "__indirect_function_table" && export.ty().table().is_some()
{
// Allow an exported "__indirect_function_table" table for now.
} else if export.name() == "table" && export.ty().table().is_some() {
// Allow an exported "table" table for now.
} else if export.name() == "__data_end" && export.ty().global().is_some() {
// Allow an exported "__data_end" memory for compatibility with toolchains
// which use --export-dynamic, which unfortunately doesn't work the way
// we want it to.
warn!("command module exporting '__data_end' is deprecated");
} else if export.name() == "__heap_base" && export.ty().global().is_some() {
// Allow an exported "__data_end" memory for compatibility with toolchains
// which use --export-dynamic, which unfortunately doesn't work the way
// we want it to.
warn!("command module exporting '__heap_base' is deprecated");
} else if export.name() == "__rtti_base" && export.ty().global().is_some() {
// Allow an exported "__rtti_base" memory for compatibility with
// AssemblyScript.
warn!("command module exporting '__rtti_base' is deprecated; pass `--runtime half` to the AssemblyScript compiler");
} else {
bail!("command export '{}' is not a function", export.name());
}
@@ -529,7 +533,7 @@ impl Linker {
///
/// Each import of `module` will be looked up in this [`Linker`] and must
/// have previously been defined. If it was previously defined with an
/// incorrect signature or if it was not prevoiusly defined then an error
/// incorrect signature or if it was not previously defined then an error
/// will be returned because the import can not be satisfied.
///
/// Per the WebAssembly spec, instantiation includes running the module's
@@ -568,45 +572,41 @@ impl Linker {
}
fn compute_imports(&self, module: &Module) -> Result<Vec<Extern>> {
let mut imports = Vec::new();
module
.imports()
.map(|import| self.get(&import).ok_or_else(|| self.link_error(&import)))
.collect()
}
for import in module.imports() {
if let Some(item) = self.get(&import) {
imports.push(item);
fn link_error(&self, import: &ImportType) -> Error {
let mut options = Vec::new();
for i in self.map.keys() {
if &*self.strings[i.module] != import.module()
|| &*self.strings[i.name] != import.name()
{
continue;
}
let mut options = String::new();
for i in self.map.keys() {
if &*self.strings[i.module] != import.module()
|| &*self.strings[i.name] != import.name()
{
continue;
}
options.push_str(" * ");
options.push_str(&format!("{:?}", i.kind));
options.push_str("\n");
}
if options.len() == 0 {
bail!(
"unknown import: `{}::{}` has not been defined",
import.module(),
import.name()
)
}
bail!(
"incompatible import type for `{}::{}` specified\n\
desired signature was: {:?}\n\
signatures available:\n\n{}",
options.push(format!(" * {:?}\n", i.kind));
}
if options.is_empty() {
return anyhow!(
"unknown import: `{}::{}` has not been defined",
import.module(),
import.name(),
import.ty(),
options,
)
import.name()
);
}
Ok(imports)
options.sort();
anyhow!(
"incompatible import type for `{}::{}` specified\n\
desired signature was: {:?}\n\
signatures available:\n\n{}",
import.module(),
import.name(),
import.ty(),
options.concat(),
)
}
/// Returns the [`Store`] that this linker is connected to.
@@ -614,7 +614,7 @@ impl Linker {
&self.store
}
/// Returns an iterator over all items defined in this `Linker`.
/// Returns an iterator over all items defined in this `Linker`, in arbitrary order.
///
/// The iterator returned will yield 3-tuples where the first two elements
/// are the module name and item name for the external item, and the third
@@ -716,15 +716,14 @@ impl Linker {
}
}
/// Modules can be interpreted either as Commands (instance lifetime ends
/// when the start function returns) or Reactor (instance persists).
/// Modules can be interpreted either as Commands or Reactors.
enum ModuleKind {
/// The instance is a Command, and this is its start function. The
/// instance should be consumed.
/// The instance is a Command, meaning an instance is created for each
/// exported function and lives for the duration of the function call.
Command,
/// The instance is a Reactor, and this is its initialization function,
/// along with the instance itself, which should persist.
/// The instance is a Reactor, meaning one instance is created which
/// may live across multiple calls.
Reactor,
}

View File

@@ -905,7 +905,7 @@ impl Store {
/// });
///
/// let trap = run().unwrap_err();
/// assert!(trap.message().contains("wasm trap: interrupt"));
/// assert!(trap.to_string().contains("wasm trap: interrupt"));
/// # Ok(())
/// # }
/// ```

View File

@@ -20,6 +20,9 @@ enum TrapReason {
/// An `i32` exit status describing an explicit program exit.
I32Exit(i32),
/// A structured error describing a trap.
Error(Box<dyn std::error::Error + Send + Sync>),
}
impl fmt::Display for TrapReason {
@@ -27,6 +30,7 @@ impl fmt::Display for TrapReason {
match self {
TrapReason::Message(s) => write!(f, "{}", s),
TrapReason::I32Exit(status) => write!(f, "Exited with i32 exit status {}", status),
TrapReason::Error(e) => write!(f, "{}", e),
}
}
}
@@ -46,11 +50,12 @@ impl Trap {
/// # Example
/// ```
/// let trap = wasmtime::Trap::new("unexpected error");
/// assert_eq!("unexpected error", trap.message());
/// assert!(trap.to_string().contains("unexpected error"));
/// ```
pub fn new<I: Into<String>>(message: I) -> Self {
let info = FRAME_INFO.read().unwrap();
Trap::new_with_trace(&info, None, message.into(), Backtrace::new_unresolved())
let reason = TrapReason::Message(message.into());
Trap::new_with_trace(&info, None, reason, Backtrace::new_unresolved())
}
/// Creates a new `Trap` representing an explicit program exit with a classic `i32`
@@ -68,19 +73,7 @@ impl Trap {
pub(crate) fn from_runtime(runtime_trap: wasmtime_runtime::Trap) -> Self {
let info = FRAME_INFO.read().unwrap();
match runtime_trap {
wasmtime_runtime::Trap::User(error) => {
// Since we're the only one using the wasmtime internals (in
// theory) we should only see user errors which were originally
// created from our own `Trap` type (see the trampoline module
// with functions).
//
// If this unwrap trips for someone we'll need to tweak the
// return type of this function to probably be `anyhow::Error`
// or something like that.
*error
.downcast()
.expect("only `Trap` user errors are supported")
}
wasmtime_runtime::Trap::User(error) => Trap::from(error),
wasmtime_runtime::Trap::Jit {
pc,
backtrace,
@@ -100,7 +93,8 @@ impl Trap {
backtrace,
} => Trap::new_wasm(&info, None, trap_code, backtrace),
wasmtime_runtime::Trap::OOM { backtrace } => {
Trap::new_with_trace(&info, None, "out of memory".to_string(), backtrace)
let reason = TrapReason::Message("out of memory".to_string());
Trap::new_with_trace(&info, None, reason, backtrace)
}
}
}
@@ -125,14 +119,14 @@ impl Trap {
Interrupt => "interrupt",
User(_) => unreachable!(),
};
let msg = format!("wasm trap: {}", desc);
let msg = TrapReason::Message(format!("wasm trap: {}", desc));
Trap::new_with_trace(info, trap_pc, msg, backtrace)
}
fn new_with_trace(
info: &GlobalFrameInfo,
trap_pc: Option<usize>,
message: String,
reason: TrapReason,
native_trace: Backtrace,
) -> Self {
let mut wasm_trace = Vec::new();
@@ -158,24 +152,13 @@ impl Trap {
}
Trap {
inner: Arc::new(TrapInner {
reason: TrapReason::Message(message),
reason,
wasm_trace,
native_trace,
}),
}
}
/// Returns a reference the `message` stored in `Trap`.
///
/// In the case of an explicit exit, the exit status can be obtained by
/// calling `i32_exit_status`.
pub fn message(&self) -> &str {
match &self.inner.reason {
TrapReason::Message(message) => message,
TrapReason::I32Exit(_) => "explicitly exited",
}
}
/// If the trap was the result of an explicit program exit with a classic
/// `i32` exit status value, return the value, otherwise return `None`.
pub fn i32_exit_status(&self) -> Option<i32> {
@@ -226,4 +209,30 @@ impl fmt::Display for Trap {
}
}
impl std::error::Error for Trap {}
impl std::error::Error for Trap {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.inner.reason {
TrapReason::Error(e) => e.source(),
TrapReason::I32Exit(_) | TrapReason::Message(_) => None,
}
}
}
impl From<anyhow::Error> for Trap {
fn from(e: anyhow::Error) -> Trap {
Box::<dyn std::error::Error + Send + Sync>::from(e).into()
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for Trap {
fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Trap {
// If the top-level error is already a trap, don't be redundant and just return it.
if let Some(trap) = e.downcast_ref::<Trap>() {
trap.clone()
} else {
let info = FRAME_INFO.read().unwrap();
let reason = TrapReason::Error(e.into());
Trap::new_with_trace(&info, None, reason, Backtrace::new_unresolved())
}
}
}

View File

@@ -123,7 +123,7 @@ impl WastContext {
fn module(&mut self, instance_name: Option<&str>, module: &[u8]) -> Result<()> {
let instance = match self.instantiate(module)? {
Outcome::Ok(i) => i,
Outcome::Trap(e) => bail!("instantiation failed: {}", e.message()),
Outcome::Trap(e) => return Err(e).context("instantiation failed"),
};
if let Some(name) = instance_name {
self.linker.instance(name, &instance)?;
@@ -189,7 +189,7 @@ impl WastContext {
Outcome::Ok(values) => bail!("expected trap, got {:?}", values),
Outcome::Trap(t) => t,
};
let actual = trap.message();
let actual = trap.to_string();
if actual.contains(expected)
// `bulk-memory-operations/bulk.wast` checks for a message that
// specifies which element is uninitialized, but our traps don't

View File

@@ -1,12 +1,16 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use proc_macro2::Span;
use syn::{
braced, bracketed,
parse::{Parse, ParseStream},
punctuated::Punctuated,
Error, Ident, LitStr, Result, Token,
use {
proc_macro2::Span,
std::{
collections::HashMap,
iter::FromIterator,
path::{Path, PathBuf},
},
syn::{
braced, bracketed,
parse::{Parse, ParseStream},
punctuated::Punctuated,
Error, Ident, LitStr, Result, Token,
},
};
#[derive(Debug, Clone)]
@@ -26,7 +30,8 @@ pub enum ConfigField {
impl ConfigField {
pub fn parse_pair(ident: &str, value: ParseStream, err_loc: Span) -> Result<Self> {
match ident {
"witx" => Ok(ConfigField::Witx(value.parse()?)),
"witx" => Ok(ConfigField::Witx(WitxConf::Paths(value.parse()?))),
"witx_literal" => Ok(ConfigField::Witx(WitxConf::Literal(value.parse()?))),
"ctx" => Ok(ConfigField::Ctx(value.parse()?)),
"errors" => Ok(ConfigField::Error(value.parse()?)),
_ => Err(Error::new(err_loc, "expected `witx`, `ctx`, or `errors`")),
@@ -79,6 +84,15 @@ impl Config {
errors: errors.take().unwrap_or_default(),
})
}
/// Load the `witx` document for the configuration.
///
/// # Panics
///
/// This method will panic if the paths given in the `witx` field were not valid documents.
pub fn load_document(&self) -> witx::Document {
self.witx.load_document()
}
}
impl Parse for Config {
@@ -91,31 +105,110 @@ impl Parse for Config {
}
}
/// The witx document(s) that will be loaded from a [`Config`](struct.Config.html).
///
/// A witx interface definition can be provided either as a collection of relative paths to
/// documents, or as a single inlined string literal. Note that `(use ...)` directives are not
/// permitted when providing a string literal.
#[derive(Debug, Clone)]
pub struct WitxConf {
pub paths: Vec<PathBuf>,
pub enum WitxConf {
/// A collection of paths pointing to witx files.
Paths(Paths),
/// A single witx document, provided as a string literal.
Literal(Literal),
}
impl WitxConf {
/// Load the `witx` document.
///
/// # Panics
///
/// This method will panic if the paths given in the `witx` field were not valid documents, or
/// if any of the given documents were not syntactically valid.
pub fn load_document(&self) -> witx::Document {
match self {
Self::Paths(paths) => witx::load(paths.as_ref()).expect("loading witx"),
Self::Literal(doc) => witx::parse(doc.as_ref()).expect("parsing witx"),
}
}
/// If using the [`Paths`][paths] syntax, make all paths relative to a root directory.
///
/// [paths]: enum.WitxConf.html#variant.Paths
pub fn make_paths_relative_to<P: AsRef<Path>>(&mut self, root: P) {
self.paths.iter_mut().for_each(|p| {
if !p.is_absolute() {
*p = PathBuf::from(root.as_ref()).join(p.clone());
}
});
if let Self::Paths(paths) = self {
paths.as_mut().iter_mut().for_each(|p| {
if !p.is_absolute() {
*p = PathBuf::from(root.as_ref()).join(p.clone());
}
});
}
}
}
impl Parse for WitxConf {
/// A collection of paths, pointing to witx documents.
#[derive(Debug, Clone)]
pub struct Paths(Vec<PathBuf>);
impl Paths {
/// Create a new, empty collection of paths.
pub fn new() -> Self {
Default::default()
}
}
impl Default for Paths {
fn default() -> Self {
Self(Default::default())
}
}
impl AsRef<[PathBuf]> for Paths {
fn as_ref(&self) -> &[PathBuf] {
self.0.as_ref()
}
}
impl AsMut<[PathBuf]> for Paths {
fn as_mut(&mut self) -> &mut [PathBuf] {
self.0.as_mut()
}
}
impl FromIterator<PathBuf> for Paths {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = PathBuf>,
{
Self(iter.into_iter().collect())
}
}
impl Parse for Paths {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let _ = bracketed!(content in input);
let path_lits: Punctuated<LitStr, Token![,]> = content.parse_terminated(Parse::parse)?;
let paths = path_lits
Ok(path_lits
.iter()
.map(|lit| PathBuf::from(lit.value()))
.collect();
Ok(WitxConf { paths })
.collect())
}
}
/// A single witx document, provided as a string literal.
#[derive(Debug, Clone)]
pub struct Literal(String);
impl AsRef<str> for Literal {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl Parse for Literal {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self(input.parse::<syn::LitStr>()?.value()))
}
}

View File

@@ -1,4 +1,5 @@
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
use escaping::{escape_id, handle_2big_enum_variant, NamingConvention};
use heck::{ShoutySnakeCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use witx::{AtomType, BuiltinType, Id, Type, TypeRef};
@@ -17,16 +18,20 @@ impl Names {
runtime_mod,
}
}
pub fn ctx_type(&self) -> Ident {
self.ctx_type.clone()
}
pub fn runtime_mod(&self) -> TokenStream {
self.runtime_mod.clone()
}
pub fn type_(&self, id: &Id) -> TokenStream {
let ident = format_ident!("{}", id.as_str().to_camel_case());
let ident = escape_id(id, NamingConvention::CamelCase);
quote!(#ident)
}
pub fn builtin_type(&self, b: BuiltinType, lifetime: TokenStream) -> TokenStream {
match b {
BuiltinType::String => {
@@ -83,15 +88,12 @@ impl Names {
}
}
/// Convert an enum variant from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn enum_variant(&self, id: &Id) -> Ident {
// FIXME this is a hack - just a proof of concept.
if id.as_str().starts_with('2') {
format_ident!("TooBig")
} else if id.as_str() == "type" {
format_ident!("Type")
} else {
format_ident!("{}", id.as_str().to_camel_case())
}
handle_2big_enum_variant(id).unwrap_or_else(|| escape_id(id, NamingConvention::CamelCase))
}
pub fn flag_member(&self, id: &Id) -> Ident {
@@ -102,34 +104,44 @@ impl Names {
format_ident!("{}", id.as_str().to_shouty_snake_case())
}
/// Convert a struct member from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn struct_member(&self, id: &Id) -> Ident {
// FIXME this is a hack - just a proof of concept.
if id.as_str() == "type" {
format_ident!("type_")
} else {
format_ident!("{}", id.as_str().to_snake_case())
}
escape_id(id, NamingConvention::SnakeCase)
}
/// Convert a module name from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn module(&self, id: &Id) -> Ident {
format_ident!("{}", id.as_str().to_snake_case())
escape_id(id, NamingConvention::SnakeCase)
}
/// Convert a trait name from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn trait_name(&self, id: &Id) -> Ident {
format_ident!("{}", id.as_str().to_camel_case())
escape_id(id, NamingConvention::CamelCase)
}
/// Convert a function name from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn func(&self, id: &Id) -> Ident {
format_ident!("{}", id.as_str().to_snake_case())
escape_id(id, NamingConvention::SnakeCase)
}
/// Convert a parameter name from its [`Id`][witx] name to its Rust [`Ident`][id] representation.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn func_param(&self, id: &Id) -> Ident {
// FIXME this is a hack - just a proof of concept.
if id.as_str() == "in" {
format_ident!("in_")
} else {
format_ident!("{}", id.as_str().to_snake_case())
}
escape_id(id, NamingConvention::SnakeCase)
}
pub fn func_core_arg(&self, arg: &witx::CoreParamType) -> Ident {
@@ -192,3 +204,117 @@ impl Names {
)
}
}
/// Identifier escaping utilities.
///
/// This module most importantly exports an `escape_id` function that can be used to properly
/// escape tokens that conflict with strict and reserved keywords, as of Rust's 2018 edition.
///
/// Weak keywords are not included as their semantic rules do not have the same implications as
/// those of strict and reserved keywords. `union` for example, is permitted as the name of a
/// variable. `dyn` was promoted to a strict keyword beginning in the 2018 edition.
mod escaping {
use {
heck::{CamelCase, SnakeCase},
proc_macro2::Ident,
quote::format_ident,
witx::Id,
};
/// Identifier naming convention.
///
/// Because shouty snake case values (identifiers that look `LIKE_THIS`) cannot potentially
/// conflict with any Rust keywords, this enum only include snake and camel case variants.
pub enum NamingConvention {
/// Snake case. Used to denote values `LikeThis`.
CamelCase,
/// Snake case. Used to denote values `like_this`.
SnakeCase,
}
/// Given a witx [`Id`][witx] and a [`NamingConvention`][naming], return a [`Ident`] word of
/// Rust syntax that accounts for escaping both strict and reserved keywords. If an identifier
/// would have conflicted with a keyword, a trailing underscode will be appended.
///
/// [id]: https://docs.rs/proc-macro2/*/proc_macro2/struct.Ident.html
/// [naming]: enum.NamingConvention.html
/// [witx]: https://docs.rs/witx/*/witx/struct.Id.html
pub fn escape_id(id: &Id, conv: NamingConvention) -> Ident {
use NamingConvention::{CamelCase, SnakeCase};
match (conv, id.as_str()) {
// For camel-cased identifiers, `Self` is the only potential keyword conflict.
(CamelCase, "self") => format_ident!("Self_"),
(CamelCase, s) => format_ident!("{}", s.to_camel_case()),
// Snake-cased identifiers are where the bulk of conflicts can occur.
(SnakeCase, s) => {
let s = s.to_snake_case();
if STRICT.iter().chain(RESERVED).any(|k| *k == s) {
// If the camel-cased string matched any strict or reserved keywords, then
// append a trailing underscore to the identifier we generate.
format_ident!("{}_", s)
} else {
format_ident!("{}", s) // Otherwise, use the string as is.
}
}
}
}
/// Strict keywords.
///
/// > Strict keywords cannot be used as the names of:
/// > * Items
/// > * Variables and function parameters
/// > * Fields and variants
/// > * Type parameters
/// > * Lifetime parameters or loop labels
/// > * Macros or attributes
/// > * Macro placeholders
/// > * Crates
/// >
/// > - <cite>[The Rust Reference][ref]</cite>
///
/// This list also includes keywords that were introduced in the 2018 edition of Rust.
///
/// [ref]: https://doc.rust-lang.org/reference/keywords.html#strict-keywords
const STRICT: &[&str] = &[
"as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum",
"extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move",
"mut", "pub", "ref", "return", "self", "Self", "static", "struct", "super", "trait",
"true", "type", "unsafe", "use", "where", "while",
];
/// Reserved keywords.
///
/// > These keywords aren't used yet, but they are reserved for future use. They have the same
/// > restrictions as strict keywords. The reasoning behind this is to make current programs
/// > forward compatible with future versions of Rust by forbidding them to use these keywords.
/// >
/// > - <cite>[The Rust Reference][ref]</cite>
///
/// This list also includes keywords that were introduced in the 2018 edition of Rust.
///
/// [ref]: https://doc.rust-lang.org/reference/keywords.html#reserved-keywords
const RESERVED: &[&str] = &[
"abstract", "become", "box", "do", "final", "macro", "override", "priv", "try", "typeof",
"unsized", "virtual", "yield",
];
/// Handle WASI's [`errno::2big`][err] variant.
///
/// This is an unfortunate edge case that must account for when generating `enum` variants.
/// This will only return `Some(_)` if the given witx identifier *is* `2big`, otherwise this
/// function will return `None`.
///
/// This functionality is a short-term fix that keeps WASI working. Instead of expanding these sort of special cases,
/// we should replace this function by having the user provide a mapping of witx identifiers to Rust identifiers in the
/// arguments to the macro.
///
/// [err]: https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/docs.md#-errno-enumu16
pub fn handle_2big_enum_variant(id: &Id) -> Option<Ident> {
if id.as_str() == "2big" {
Some(format_ident!("TooBig"))
} else {
None
}
}
}

View File

@@ -95,7 +95,7 @@ pub fn from_witx(args: TokenStream) -> TokenStream {
std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"),
);
let doc = witx::load(&config.witx.paths).expect("loading witx");
let doc = config.load_document();
let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle));
let error_transform = wiggle_generate::ErrorTransform::new(&config.errors, &doc)

View File

@@ -0,0 +1,64 @@
//! Tests to check that keywords in `witx` files are escaped.
//!
//! No `#[test]` functions are defined below because the `wiggle::from_witx!` macro expanding into
//! syntactically correct Rust code at compile time is the subject under test.
/// Test that an enum variant that conflicts with a Rust keyword can be compiled properly.
mod enum_test {
wiggle::from_witx!({
witx_literal:
"(typename $self
(enum u8
$self
$2big
)
)",
ctx: DummyCtx,
});
}
/// Test module, trait, function, and function parameter names conflicting with Rust keywords.
///
/// We use `self` because the camel-cased trait name `Self` is *also* a strict keyword. This lets
/// us simultaneously test the name of the module and the generated trait.
mod module_trait_fn_and_arg_test {
use wiggle_test::WasiCtx;
wiggle::from_witx!({
witx_literal:
"(module $self
(@interface func (export \"fn\")
(param $use u32)
(param $virtual u32)
)
)",
ctx: WasiCtx,
});
impl<'a> self_::Self_ for WasiCtx<'a> {
#[allow(unused_variables)]
fn fn_(&self, use_: u32, virtual_: u32) -> Result<(), ()> {
unimplemented!();
}
}
}
/// Test that a struct and member names conflicting with Rust keywords can be compiled properly.
mod struct_test {
wiggle::from_witx!({
witx_literal:
"(typename $self
(struct
(field $become s32)
(field $mut s32)
)
)",
ctx: DummyCtx,
});
}
/// Test that a union variant that conflicts with a Rust keyword can be compiled properly.
mod union_test {
wiggle::from_witx!({
witx: ["tests/keywords_union.witx"],
ctx: DummyCtx,
});
}

View File

@@ -0,0 +1,15 @@
(typename $union
(enum u8
$self
$power
)
)
(typename $self
(union $union
;; A union variant that will expand to a strict keyword `Self`.
(field $self (@witx pointer f32))
;; Oh it's true, that there's power in a union!
(field $power (@witx pointer f32))
)
)