diff --git a/Cargo.lock b/Cargo.lock index 02f64f7e7..8c810a040 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -802,9 +802,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.56" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ "unicode-ident", ] diff --git a/core/src/ast/attrs.rs b/core/src/ast/attrs.rs index 2c71fa95f..7be6704e2 100644 --- a/core/src/ast/attrs.rs +++ b/core/src/ast/attrs.rs @@ -7,6 +7,7 @@ use syn::{Attribute, Ident, LitStr, Meta, Token}; /// The list of attributes on a type #[derive(Clone, PartialEq, Eq, Hash, Debug, Default)] +#[non_exhaustive] pub struct Attrs { pub cfg: Vec, pub attrs: Vec, @@ -89,6 +90,7 @@ impl Serialize for Attrs { /// and `all()`, `not()`, and `any()` combiners), and then be followed by one /// or more backend-specific attributes, which can be any valid meta-item #[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize)] +#[non_exhaustive] pub struct DiplomatBackendAttr { pub cfg: DiplomatBackendAttrCfg, #[serde(serialize_with = "serialize_meta")] @@ -103,6 +105,7 @@ where } #[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize)] +#[non_exhaustive] pub enum DiplomatBackendAttrCfg { Not(Box), Any(Vec), diff --git a/core/src/ast/docs.rs b/core/src/ast/docs.rs index 058384857..3cebb01e3 100644 --- a/core/src/ast/docs.rs +++ b/core/src/ast/docs.rs @@ -14,6 +14,7 @@ pub struct Docs(String, Vec); /// Note that this only controls markdown generated by this code. Existing markdown /// in the Rust documentation will not be sanitized in any way. #[derive(PartialEq, Eq, Clone, Debug)] +#[non_exhaustive] pub enum MarkdownStyle { /// Regular markdown with no specific extensions, compatible with most common flavors Normal, @@ -111,6 +112,7 @@ impl Docs { } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[non_exhaustive] pub enum RustLinkDisplay { /// A nice expanded representation that includes the type name /// @@ -125,6 +127,7 @@ pub enum RustLinkDisplay { } #[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug, PartialOrd, Ord)] +#[non_exhaustive] pub struct RustLink { pub path: Path, pub typ: DocType, @@ -189,6 +192,7 @@ impl fmt::Display for RustLink { } #[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug, PartialOrd, Ord)] +#[non_exhaustive] pub enum DocType { Struct, StructField, diff --git a/core/src/ast/enums.rs b/core/src/ast/enums.rs index 3945b7e9a..20f012fa4 100644 --- a/core/src/ast/enums.rs +++ b/core/src/ast/enums.rs @@ -6,6 +6,7 @@ use quote::ToTokens; /// A fieldless enum declaration in an FFI module. #[derive(Clone, Serialize, Debug, Hash, PartialEq, Eq)] +#[non_exhaustive] pub struct Enum { pub name: Ident, pub docs: Docs, diff --git a/core/src/ast/lifetimes.rs b/core/src/ast/lifetimes.rs index 5530d59db..83fbbeb7b 100644 --- a/core/src/ast/lifetimes.rs +++ b/core/src/ast/lifetimes.rs @@ -395,6 +395,7 @@ pub(crate) struct LifetimeNode { /// A lifetime, analogous to [`syn::Lifetime`]. #[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)] +#[non_exhaustive] pub enum Lifetime { /// The `'static` lifetime. Static, diff --git a/core/src/ast/methods.rs b/core/src/ast/methods.rs index ed7f1b97f..9653a6a8a 100644 --- a/core/src/ast/methods.rs +++ b/core/src/ast/methods.rs @@ -11,6 +11,7 @@ use crate::Env; /// Includes both static and non-static methods, which can be distinguished /// by inspecting [`Method::self_param`]. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Debug)] +#[non_exhaustive] pub struct Method { /// The name of the method as initially declared. pub name: Ident, @@ -244,6 +245,7 @@ impl Method { /// The `self` parameter taken by a [`Method`]. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Debug)] +#[non_exhaustive] pub struct SelfParam { /// The lifetime and mutability of the `self` param, if it's a reference. pub reference: Option<(Lifetime, Mutability)>, @@ -275,6 +277,7 @@ impl SelfParam { /// A parameter taken by a [`Method`], not including `self`. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Debug)] +#[non_exhaustive] pub struct Param { /// The name of the parameter in the original method declaration. pub name: Ident, @@ -316,6 +319,7 @@ pub enum LifetimeKind { #[derive(Default, Debug)] /// Parameters in a method that might be borrowed in the return type. +#[non_exhaustive] pub struct BorrowedParams<'a>( pub Option<&'a SelfParam>, pub Vec<(&'a Param, LifetimeKind)>, diff --git a/core/src/ast/modules.rs b/core/src/ast/modules.rs index db320d365..db4ccf37b 100644 --- a/core/src/ast/modules.rs +++ b/core/src/ast/modules.rs @@ -60,6 +60,7 @@ impl DiplomatStructAttribute { } #[derive(Clone, Serialize, Debug)] +#[non_exhaustive] pub struct Module { pub name: Ident, pub imports: Vec<(Path, Ident)>, @@ -243,6 +244,7 @@ fn extract_imports(base_path: &Path, use_tree: &UseTree, out: &mut Vec<(Path, Id } #[derive(Serialize, Clone, Debug)] +#[non_exhaustive] pub struct File { pub modules: BTreeMap, } diff --git a/core/src/ast/paths.rs b/core/src/ast/paths.rs index fd2e07fce..8bd912908 100644 --- a/core/src/ast/paths.rs +++ b/core/src/ast/paths.rs @@ -4,6 +4,7 @@ use std::fmt; use super::Ident; #[derive(Hash, Eq, PartialEq, Deserialize, Serialize, Clone, Debug, Ord, PartialOrd)] +#[non_exhaustive] pub struct Path { pub elements: Vec, } diff --git a/core/src/ast/structs.rs b/core/src/ast/structs.rs index 42904e680..2fb1db483 100644 --- a/core/src/ast/structs.rs +++ b/core/src/ast/structs.rs @@ -5,6 +5,7 @@ use super::{Attrs, Ident, LifetimeEnv, Method, Mutability, PathType, TypeName}; /// A struct declaration in an FFI module that is not opaque. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Debug)] +#[non_exhaustive] pub struct Struct { pub name: Ident, pub docs: Docs, @@ -54,6 +55,7 @@ impl Struct { /// Opaque structs cannot be passed by-value across the FFI boundary, so they /// must be boxed or passed as references. #[derive(Clone, Serialize, Debug, Hash, PartialEq, Eq)] +#[non_exhaustive] pub struct OpaqueStruct { pub name: Ident, pub docs: Docs, diff --git a/core/src/ast/types.rs b/core/src/ast/types.rs index 976bb7061..689d639cb 100644 --- a/core/src/ast/types.rs +++ b/core/src/ast/types.rs @@ -16,6 +16,7 @@ use crate::Env; /// A type declared inside a Diplomat-annotated module. #[derive(Clone, Serialize, Debug, Hash, PartialEq, Eq)] +#[non_exhaustive] pub enum CustomType { /// A non-opaque struct whose fields will be visible across the FFI boundary. Struct(Struct), @@ -127,6 +128,7 @@ impl CustomType { /// A symbol declared in a module, which can either be a pointer to another path, /// or a custom type defined directly inside that module #[derive(Clone, Serialize, Debug)] +#[non_exhaustive] pub enum ModSymbol { /// A symbol that is a pointer to another path. Alias(Path), @@ -138,6 +140,7 @@ pub enum ModSymbol { /// A named type that is just a path, e.g. `std::borrow::Cow<'a, T>`. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)] +#[non_exhaustive] pub struct PathType { pub path: Path, pub lifetimes: Vec, @@ -299,6 +302,7 @@ impl From for PathType { } #[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only two kinds of mutability we care about pub enum Mutability { Mutable, Immutable, @@ -361,6 +365,7 @@ impl Mutability { /// Unlike [`CustomType`], which represents a type declaration, [`TypeName`]s can compose /// types through references and boxing, and can also capture unresolved paths. #[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)] +#[non_exhaustive] pub enum TypeName { /// A built-in Rust scalar primitive. Primitive(PrimitiveType), @@ -882,6 +887,7 @@ impl TypeName { } } +#[non_exhaustive] pub enum LifetimeOrigin { Named, Reference, @@ -973,6 +979,7 @@ impl fmt::Display for PathType { /// A built-in Rust primitive scalar type. #[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)] #[allow(non_camel_case_types)] +#[allow(clippy::exhaustive_enums)] // there are only these (scalar types) pub enum PrimitiveType { i8, u8, diff --git a/core/src/ast/validity.rs b/core/src/ast/validity.rs index c1f37bfa6..e8c68e225 100644 --- a/core/src/ast/validity.rs +++ b/core/src/ast/validity.rs @@ -2,6 +2,7 @@ use super::{Ident, Path, TypeName}; #[cfg_attr(feature = "displaydoc", derive(displaydoc::Display))] #[derive(Debug, Clone)] +#[non_exhaustive] pub enum ValidityError { /// An oqaue type crosses the FFI boundary as a value. #[cfg_attr( diff --git a/core/src/hir/defs.rs b/core/src/hir/defs.rs index 649afed10..6622acba0 100644 --- a/core/src/hir/defs.rs +++ b/core/src/hir/defs.rs @@ -3,12 +3,14 @@ use super::{Attrs, Everywhere, IdentBuf, Method, OutputOnly, TyPosition, Type}; use crate::ast::Docs; +#[non_exhaustive] pub enum ReturnableStructDef<'tcx> { Struct(&'tcx StructDef), OutStruct(&'tcx OutStructDef), } #[derive(Copy, Clone, Debug)] +#[non_exhaustive] pub enum TypeDef<'tcx> { Struct(&'tcx StructDef), OutStruct(&'tcx OutStructDef), @@ -21,6 +23,7 @@ pub type OutStructDef = StructDef; /// Structs that can be either inputs or outputs in methods. #[derive(Debug)] +#[non_exhaustive] pub struct StructDef { pub docs: Docs, pub name: IdentBuf, @@ -38,6 +41,7 @@ pub struct StructDef { /// /// A struct marked with `#[diplomat::opaque]`. #[derive(Debug)] +#[non_exhaustive] pub struct OpaqueDef { pub docs: Docs, pub name: IdentBuf, @@ -47,6 +51,7 @@ pub struct OpaqueDef { /// The enum type. #[derive(Debug)] +#[non_exhaustive] pub struct EnumDef { pub docs: Docs, pub name: IdentBuf, @@ -60,6 +65,7 @@ pub type OutStructField = StructField; /// A field on a [`Struct`]s. #[derive(Debug)] +#[non_exhaustive] pub struct StructField { pub docs: Docs, pub name: IdentBuf, @@ -68,6 +74,7 @@ pub struct StructField { /// A variant of an [`Enum`]. #[derive(Debug)] +#[non_exhaustive] pub struct EnumVariant { pub docs: Docs, pub name: IdentBuf, diff --git a/core/src/hir/lifetimes.rs b/core/src/hir/lifetimes.rs index 914b414ef..d360ad17c 100644 --- a/core/src/hir/lifetimes.rs +++ b/core/src/hir/lifetimes.rs @@ -96,6 +96,7 @@ where /// Wrapper type for `TypeLifetime` and `MethodLifetime`, indicating that it may /// be the `'static` lifetime. #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] +#[allow(clippy::exhaustive_enums)] // this will only ever have two variants pub enum MaybeStatic { Static, NonStatic(T), diff --git a/core/src/hir/lowering.rs b/core/src/hir/lowering.rs index deb3154fa..3f11d2f8c 100644 --- a/core/src/hir/lowering.rs +++ b/core/src/hir/lowering.rs @@ -12,6 +12,7 @@ use strck_ident::IntoCk; /// An error from lowering the AST to the HIR. #[derive(Debug)] +#[non_exhaustive] pub enum LoweringError { /// The purpose of having this is that translating to the HIR has enormous /// potential for really detailed error handling and giving suggestions. diff --git a/core/src/hir/methods.rs b/core/src/hir/methods.rs index 0c1bbef94..648f76879 100644 --- a/core/src/hir/methods.rs +++ b/core/src/hir/methods.rs @@ -11,6 +11,7 @@ use super::{ /// A method exposed to Diplomat. #[derive(Debug)] +#[non_exhaustive] pub struct Method { pub docs: Docs, pub name: IdentBuf, @@ -24,6 +25,7 @@ pub struct Method { /// Type that the method returns. #[derive(Debug)] +#[non_exhaustive] pub enum SuccessType { Writeable, OutType(OutType), @@ -31,6 +33,7 @@ pub enum SuccessType { /// Whether or not the method returns a value or a result. #[derive(Debug)] +#[allow(clippy::exhaustive_enums)] // this only exists for fallible/infallible, breaking changes for more complex returns are ok pub enum ReturnType { Infallible(Option), Fallible(Option, Option), @@ -38,12 +41,14 @@ pub enum ReturnType { /// The `self` parameter of a method. #[derive(Debug)] +#[non_exhaustive] pub struct ParamSelf { pub ty: SelfType, } /// A parameter in a method. #[derive(Debug)] +#[non_exhaustive] pub struct Param { pub name: IdentBuf, pub ty: Type, diff --git a/core/src/hir/paths.rs b/core/src/hir/paths.rs index 446490c7d..e2d9930ec 100644 --- a/core/src/hir/paths.rs +++ b/core/src/hir/paths.rs @@ -5,6 +5,7 @@ use super::{ /// Path to a struct that may appear as an output. #[derive(Debug, Clone)] +#[non_exhaustive] pub enum ReturnableStructPath { Struct(StructPath), OutStruct(OutStructPath), @@ -15,6 +16,7 @@ pub type OutStructPath = StructPath; /// Path to a struct that can be used in inputs and outputs. #[derive(Debug, Clone)] +#[non_exhaustive] pub struct StructPath { pub lifetimes: TypeLifetimes, pub tcx_id: P::StructId, @@ -35,6 +37,7 @@ pub struct StructPath { /// 3. `OpaquePath`: Opaques in the `&self` position, which /// cannot be optional and must be borrowed for the same reason as above. #[derive(Debug, Clone)] +#[non_exhaustive] pub struct OpaquePath { pub lifetimes: TypeLifetimes, pub optional: Opt, @@ -46,6 +49,7 @@ pub struct OpaquePath { pub struct Optional(pub(super) bool); #[derive(Debug, Copy, Clone)] +#[allow(clippy::exhaustive_structs)] // marker type pub struct NonOptional; impl OpaquePath { @@ -79,6 +83,7 @@ impl OpaquePath { /// Path to an enum. #[derive(Debug, Clone)] +#[non_exhaustive] pub struct EnumPath { pub tcx_id: EnumId, } @@ -87,6 +92,7 @@ pub struct EnumPath { /// /// Since owned opaques cannot be used as inputs, this only appears in output types. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // only two answers to this question pub enum MaybeOwn { Own, Borrow(Borrow), diff --git a/core/src/hir/primitives.rs b/core/src/hir/primitives.rs index 4f68b41f1..b51a3e175 100644 --- a/core/src/hir/primitives.rs +++ b/core/src/hir/primitives.rs @@ -3,6 +3,7 @@ use crate::ast; /// 8, 16, 32, and 64-bit signed and unsigned integers. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only these pub enum IntType { I8, I16, @@ -16,6 +17,7 @@ pub enum IntType { /// Platform-dependent signed and unsigned size types. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only these pub enum IntSizeType { Isize, Usize, @@ -23,6 +25,7 @@ pub enum IntSizeType { /// 128-bit signed and unsigned integers. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only these pub enum Int128Type { I128, U128, @@ -30,6 +33,7 @@ pub enum Int128Type { /// 32 and 64-bit floating point numbers. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only these pub enum FloatType { F32, F64, @@ -37,6 +41,7 @@ pub enum FloatType { /// All primitive types. #[derive(Copy, Clone, Debug)] +#[allow(clippy::exhaustive_enums)] // there are only these pub enum PrimitiveType { Bool, Char, diff --git a/core/src/hir/ty_position.rs b/core/src/hir/ty_position.rs index ed6d75258..322f54cb0 100644 --- a/core/src/hir/ty_position.rs +++ b/core/src/hir/ty_position.rs @@ -109,6 +109,7 @@ pub trait TyPosition: Debug + Copy { /// /// The complement of this type is [`OutputOnly`]. #[derive(Debug, Copy, Clone)] +#[non_exhaustive] pub struct Everywhere; /// One of two types implementing [`TyPosition`], representing types that can @@ -116,6 +117,7 @@ pub struct Everywhere; /// /// The complement of this type is [`Everywhere`]. #[derive(Debug, Copy, Clone)] +#[non_exhaustive] pub struct OutputOnly; impl TyPosition for Everywhere { diff --git a/core/src/hir/type_context.rs b/core/src/hir/type_context.rs index cdc43e520..73f7c5d22 100644 --- a/core/src/hir/type_context.rs +++ b/core/src/hir/type_context.rs @@ -37,6 +37,7 @@ pub struct OpaqueId(usize); pub struct EnumId(usize); #[derive(Copy, Clone, Debug)] +#[non_exhaustive] pub enum TypeId { Struct(StructId), OutStruct(OutStructId), diff --git a/core/src/hir/types.rs b/core/src/hir/types.rs index 167e5646f..1d26d5b57 100644 --- a/core/src/hir/types.rs +++ b/core/src/hir/types.rs @@ -12,6 +12,7 @@ pub type OutType = Type; /// Type that may be used as input or output. #[derive(Debug)] +#[non_exhaustive] pub enum Type { Primitive(PrimitiveType), Opaque(OpaquePath), @@ -22,6 +23,7 @@ pub enum Type { /// Type that can appear in the `self` position. #[derive(Debug, Clone)] +#[non_exhaustive] pub enum SelfType { Opaque(OpaquePath), Struct(StructPath), @@ -29,6 +31,7 @@ pub enum SelfType { } #[derive(Copy, Clone, Debug)] +#[non_exhaustive] pub enum Slice { /// A string slice, e.g. `&str`. Str(MaybeStatic), @@ -46,6 +49,7 @@ pub enum Slice { // involve getting the implicit lifetime thing to be understood by Diplomat, but // should be doable. #[derive(Copy, Clone, Debug)] +#[non_exhaustive] pub struct Borrow { pub lifetime: MaybeStatic, pub mutability: Mutability, diff --git a/core/src/lib.rs b/core/src/lib.rs index a5b2b17ad..84853724a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -4,6 +4,7 @@ //! extracting APIs. #![allow(clippy::needless_lifetimes)] // we use named lifetimes for clarity +#![warn(clippy::exhaustive_structs, clippy::exhaustive_enums)] pub mod ast; #[cfg(feature = "hir")] diff --git a/tool/src/c/mod.rs b/tool/src/c/mod.rs index a4ddca00a..6482fd421 100644 --- a/tool/src/c/mod.rs +++ b/tool/src/c/mod.rs @@ -107,6 +107,7 @@ fn gen_struct_header<'a>( } writeln!(out, "}} {};", enm.name)?; } + &_ => unreachable!("unknown AST/HIR variant"), } writeln!(out, "#ifdef __cplusplus")?; @@ -286,6 +287,7 @@ pub fn gen_includes( (ast::CustomType::Opaque(_), false) => { panic!("Cannot pass opaque types by value") } + (&_, _) => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Box(underlying) => { @@ -333,6 +335,7 @@ pub fn gen_includes( ast::TypeName::StrReference(..) => {} ast::TypeName::PrimitiveSlice(..) => {} ast::TypeName::Unit => {} + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) diff --git a/tool/src/c/structs.rs b/tool/src/c/structs.rs index 744465911..0ec997cf3 100644 --- a/tool/src/c/structs.rs +++ b/tool/src/c/structs.rs @@ -31,6 +31,7 @@ pub fn gen_struct( } ast::CustomType::Enum(_) => {} + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) diff --git a/tool/src/c/types.rs b/tool/src/c/types.rs index 25cba35fa..5a86c40f0 100644 --- a/tool/src/c/types.rs +++ b/tool/src/c/types.rs @@ -19,6 +19,7 @@ pub fn gen_type( ast::CustomType::Enum(enm) => { write!(out, "{}", enm.name)?; } + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -62,6 +63,7 @@ pub fn gen_type( write!(out, "Diplomat{prim}View")?; } ast::TypeName::Unit => write!(out, "void")?, + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) @@ -103,6 +105,7 @@ pub fn name_for_type(typ: &ast::TypeName) -> ast::Ident { ast::Ident::from(format!("ref_prim_slice_{}", c_type_for_prim(prim))) } ast::TypeName::Unit => ast::Ident::from("void"), + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/c2/formatter.rs b/tool/src/c2/formatter.rs index 892922ebb..f5a0ef431 100644 --- a/tool/src/c2/formatter.rs +++ b/tool/src/c2/formatter.rs @@ -115,6 +115,7 @@ impl<'tcx> CFormatter<'tcx> { let prim = self.fmt_primitive_as_c(*p); format!("ref_{constness}prim_slice_{prim}").into() } + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/c2/ty.rs b/tool/src/c2/ty.rs index c8c9ac2b2..5dd6f24a4 100644 --- a/tool/src/c2/ty.rs +++ b/tool/src/c2/ty.rs @@ -27,6 +27,7 @@ impl<'tcx> super::CContext<'tcx> { TypeDef::Opaque(o) => context.gen_opaque_def(o, id), TypeDef::Struct(s) => context.gen_struct_def(s, id), TypeDef::OutStruct(s) => context.gen_struct_def(s, id), + _ => unreachable!("unknown AST/HIR variant"), } for method in ty.methods() { if method.attrs.disable { @@ -139,6 +140,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { "void".into() } SuccessType::OutType(o) => self.gen_ty_name(o, false), + &_ => unreachable!("unknown AST/HIR variant"), }, ReturnType::Fallible(ref ok, ref err) => { let (ok_type_name, ok_ty) = match ok { @@ -150,6 +152,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { Some(SuccessType::OutType(o)) => { (self.cx.formatter.fmt_type_name_uniquely(o), Some(o)) } + &Some(_) => unreachable!("unknown AST/HIR variant"), }; let err_type_name = match err { Some(o) => self.cx.formatter.fmt_type_name_uniquely(o), @@ -313,12 +316,14 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { let ptr_ty = match s { hir::Slice::Str(..) => "char".into(), hir::Slice::Primitive(_, prim) => self.cx.formatter.fmt_primitive_as_c(*prim), + &_ => unreachable!("unknown AST/HIR variant"), }; ( None, format!("struct {{ const {ptr_ty}* data; size_t len; }}").into(), ) } + _ => unreachable!("unknown AST/HIR variant"), }; // Todo(breaking): We can remove this requirement // and users will be forced to import more types diff --git a/tool/src/cpp/conversions.rs b/tool/src/cpp/conversions.rs index de7d35511..83bdff4ea 100644 --- a/tool/src/cpp/conversions.rs +++ b/tool/src/cpp/conversions.rs @@ -31,6 +31,7 @@ pub fn gen_rust_to_cpp( // TODO(#59): should emit a unique_ptr todo!("Receiving boxes of enums is not yet supported") } + &_ => unreachable!("unknown AST/HIR variant"), } } _o => todo!(), @@ -67,6 +68,7 @@ pub fn gen_rust_to_cpp( (_, ast::CustomType::Enum(enm)) => { format!("static_cast<{}>({})", enm.name, cpp) } + (_, &_) => unreachable!("unknown AST/HIR variant"), } } @@ -229,6 +231,7 @@ pub fn gen_rust_to_cpp( "slice".into() } ast::TypeName::Unit => cpp.to_string(), + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -333,6 +336,7 @@ pub fn gen_cpp_to_rust( } ast::CustomType::Enum(enm) => format!("static_cast({})", enm.name, cpp), + &_ => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Writeable => { diff --git a/tool/src/cpp/docs.rs b/tool/src/cpp/docs.rs index 369aedb39..c386a5680 100644 --- a/tool/src/cpp/docs.rs +++ b/tool/src/cpp/docs.rs @@ -101,6 +101,7 @@ pub fn gen_custom_type_docs( ast::CustomType::Struct(_) => writeln!(out, ".. cpp:struct:: {}", typ.name())?, ast::CustomType::Enum(_) => writeln!(out, ".. cpp:enum-struct:: {}", typ.name())?, ast::CustomType::Opaque(_) => writeln!(out, ".. cpp:class:: {}", typ.name())?, + &_ => unreachable!("unknown AST/HIR variant"), } let mut class_indented = indented(out).with_str(" "); diff --git a/tool/src/cpp/mod.rs b/tool/src/cpp/mod.rs index ea52247be..f7a2e38c2 100644 --- a/tool/src/cpp/mod.rs +++ b/tool/src/cpp/mod.rs @@ -132,6 +132,7 @@ pub fn gen_bindings( )?; } } + &&_ => unreachable!("unknown AST/HIR variant"), } for method in typ.methods() { @@ -174,6 +175,7 @@ pub fn gen_bindings( writeln!(out)?; gen_struct(typ, in_path, true, env, &library_config, docs_url_gen, out)?; } + &&_ => unreachable!("unknown AST/HIR variant"), } writeln!(out)?; @@ -218,6 +220,7 @@ pub fn gen_bindings( writeln!(out)?; gen_struct(typ, in_path, false, env, &library_config, docs_url_gen, out)?; } + &&_ => unreachable!("unknown AST/HIR variant"), } writeln!(out, "#endif")? @@ -280,6 +283,7 @@ fn gen_includes( seen_includes.insert(include); } } + &_ => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Box(underlying) => { @@ -346,6 +350,7 @@ fn gen_includes( ast::TypeName::StrReference(..) => {} ast::TypeName::PrimitiveSlice(..) => {} ast::TypeName::Unit => {} + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) diff --git a/tool/src/cpp/structs.rs b/tool/src/cpp/structs.rs index 115ffabbc..ed816cc2b 100644 --- a/tool/src/cpp/structs.rs +++ b/tool/src/cpp/structs.rs @@ -158,6 +158,7 @@ pub fn gen_struct( } ast::CustomType::Enum(_) => {} + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) diff --git a/tool/src/cpp/types.rs b/tool/src/cpp/types.rs index 135f0d57f..c22652059 100644 --- a/tool/src/cpp/types.rs +++ b/tool/src/cpp/types.rs @@ -66,6 +66,7 @@ fn gen_type_inner( ast::CustomType::Enum(enm) => { write!(out, "{}", enm.name)?; } + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -126,7 +127,7 @@ fn gen_type_inner( )?; } - _ => todo!(), + _ => unreachable!("unknown AST/HIR variant"), }, ast::TypeName::Result(ok, err, _) => { @@ -180,6 +181,7 @@ fn gen_type_inner( ast::TypeName::Unit => { write!(out, "void")?; } + &_ => unreachable!("unknown AST/HIR variant"), } if !handled_ref { diff --git a/tool/src/cpp2/ty.rs b/tool/src/cpp2/ty.rs index 605b6cf45..3cda5a95b 100644 --- a/tool/src/cpp2/ty.rs +++ b/tool/src/cpp2/ty.rs @@ -31,6 +31,7 @@ impl<'tcx> super::Cpp2Context<'tcx> { TypeDef::Opaque(o) => context.gen_opaque_def(o, id), TypeDef::Struct(s) => context.gen_struct_def(s, id), TypeDef::OutStruct(s) => context.gen_struct_def(s, id), + _ => unreachable!("unknown AST/HIR variant"), } drop(guard); @@ -459,6 +460,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { let ret = self.cx.formatter.fmt_borrowed_slice(&ret, b.mutability); ret.into_owned().into() } + _ => unreachable!("unknown AST/HIR variant"), } } @@ -468,6 +470,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { SelfType::Opaque(..) => "this->AsFFI()".into(), SelfType::Struct(..) => "this->AsFFI()".into(), SelfType::Enum(..) => "this->AsFFI()".into(), + _ => unreachable!("unknown AST/HIR variant"), } } @@ -560,6 +563,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { }, ] } + _ => unreachable!("unknown AST/HIR variant"), } } @@ -570,12 +574,14 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { ReturnType::Infallible(Some(ref ty)) => match ty { SuccessType::Writeable => self.cx.formatter.fmt_owned_str(), SuccessType::OutType(o) => self.gen_type_name(o), + &_ => unreachable!("unknown AST/HIR variant"), }, ReturnType::Fallible(ref ok, ref err) => { let ok_type_name = match ok { Some(SuccessType::Writeable) => self.cx.formatter.fmt_owned_str(), None => "std::monostate".into(), Some(SuccessType::OutType(o)) => self.gen_type_name(o), + &Some(_) => unreachable!("unknown AST/HIR variant"), }; let err_type_name = match err { Some(o) => self.gen_type_name(o), @@ -661,6 +667,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { .fmt_borrowed_slice(&prim_name, b.mutability); format!("{span}({var_name}_data, {var_name}_size)").into() } + _ => unreachable!("unknown AST/HIR variant"), } } @@ -685,6 +692,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { Some(SuccessType::Writeable) => self.cx.formatter.fmt_owned_str(), None => "std::monostate".into(), Some(SuccessType::OutType(o)) => self.gen_type_name(o), + &Some(_) => unreachable!("unknown AST/HIR variant"), }; let err_type_name = match err { Some(o) => self.gen_type_name(o), @@ -695,6 +703,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { Some(SuccessType::Writeable) => "std::move(output)".into(), None => "".into(), Some(SuccessType::OutType(o)) => self.gen_c_to_cpp_for_type(o, ok_path.into()), + &Some(_) => unreachable!("unknown AST/HIR variant"), }; let err_conversion = match err { Some(o) => self.gen_c_to_cpp_for_type(o, err_path.into()), @@ -704,6 +713,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> { format!("{var_name}.is_ok ? diplomat::result<{ok_type_name}, {err_type_name}>(diplomat::Ok<{ok_type_name}>({ok_conversion})) : diplomat::result<{ok_type_name}, {err_type_name}>(diplomat::Err<{err_type_name}>({err_conversion}))").into() ) } + ReturnType::Infallible(Some(_)) => unreachable!("unknown AST/HIR variant"), } } } diff --git a/tool/src/docs_util.rs b/tool/src/docs_util.rs index 8a2d6ab3c..207fbcec2 100644 --- a/tool/src/docs_util.rs +++ b/tool/src/docs_util.rs @@ -108,6 +108,7 @@ impl FromMarkdown for CppRst { ast::CustomType::Struct(strct) => write!(out, ":cpp:struct:`{}`", strct.name), ast::CustomType::Enum(enm) => write!(out, ":cpp:enum-struct:`{}`", enm.name), ast::CustomType::Opaque(opaque) => write!(out, ":cpp:class:`{}`", opaque.name), + &_ => unreachable!("unknown AST/HIR variant"), } } } diff --git a/tool/src/dotnet/conversions.rs b/tool/src/dotnet/conversions.rs index 2446676de..288db82cc 100644 --- a/tool/src/dotnet/conversions.rs +++ b/tool/src/dotnet/conversions.rs @@ -77,6 +77,7 @@ pub fn to_idiomatic_object( ast::CustomType::Enum(_) => { write!(out, "({name}){input_var_name}") } + &_ => unreachable!("unknown AST/HIR variant"), } } other => panic!("expected named type name, found `{}`", other), @@ -119,6 +120,7 @@ pub fn to_raw_object( ast::CustomType::Enum(_) => { write!(out, "(Raw.{name}){input_var_name}") } + &_ => unreachable!("unknown AST/HIR variant"), } } other => panic!("expected named type name, found `{}`", other), diff --git a/tool/src/dotnet/idiomatic.rs b/tool/src/dotnet/idiomatic.rs index 74031af82..710639408 100644 --- a/tool/src/dotnet/idiomatic.rs +++ b/tool/src/dotnet/idiomatic.rs @@ -185,6 +185,7 @@ pub fn gen( Ok(()) })?; } + &_ => unreachable!("unknown AST/HIR variant"), } Ok(()) @@ -212,6 +213,7 @@ fn gen_property_for_field( return Ok(()); } ast::CustomType::Enum(_) => {} + &_ => unreachable!("unknown AST/HIR variant"), } } _ => { @@ -723,6 +725,7 @@ fn gen_raw_conversion_type_name_decl_position( ast::CustomType::Enum(_) | ast::CustomType::Struct(_) => { gen_raw_type_name_decl_position(typ, in_path, env, out) } + &_ => unreachable!("unknown AST/HIR variant"), } } _ => gen_raw_type_name_decl_position(typ, in_path, env, out), @@ -790,6 +793,7 @@ fn requires_null_check(typ: &ast::TypeName, in_path: &ast::Path, env: &Env) -> b match path_type.resolve(in_path, env) { ast::CustomType::Opaque(_) => true, ast::CustomType::Struct(_) | ast::CustomType::Enum(_) => false, + &_ => unreachable!("unknown AST/HIR variant"), } } other => panic!("expected named type name, found `{}`", other), diff --git a/tool/src/dotnet/raw.rs b/tool/src/dotnet/raw.rs index 0430b3936..052c1cee1 100644 --- a/tool/src/dotnet/raw.rs +++ b/tool/src/dotnet/raw.rs @@ -144,6 +144,7 @@ pub fn gen<'ast>( Ok(()) }) } + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/dotnet/types.rs b/tool/src/dotnet/types.rs index fcbb57135..596486950 100644 --- a/tool/src/dotnet/types.rs +++ b/tool/src/dotnet/types.rs @@ -60,6 +60,7 @@ pub fn gen_type_name( ast::TypeName::Unit => { write!(out, "void") } + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -115,5 +116,6 @@ pub fn name_for_type(typ: &ast::TypeName) -> ast::Ident { format!("RefPrimSlice{}", prim.to_string().to_upper_camel_case()), ), ast::TypeName::Unit => ast::Ident::from("Void"), + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/dotnet/util.rs b/tool/src/dotnet/util.rs index 84e5b4fc9..861f2447c 100644 --- a/tool/src/dotnet/util.rs +++ b/tool/src/dotnet/util.rs @@ -63,6 +63,7 @@ pub fn collect_results<'ast>( | ast::TypeName::Named(_) | ast::TypeName::SelfType(_) | ast::TypeName::Primitive(_) => {} + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -124,5 +125,6 @@ fn collect_errors_impl<'ast>( | ast::TypeName::Writeable | ast::TypeName::StrReference(..) | ast::TypeName::PrimitiveSlice(..) => {} + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/js/conversions.rs b/tool/src/js/conversions.rs index a4ea21998..4ce55a2a1 100644 --- a/tool/src/js/conversions.rs +++ b/tool/src/js/conversions.rs @@ -207,6 +207,7 @@ pub fn gen_value_js_to_rust<'env>( ast::CustomType::Opaque(_) => { panic!("Opaque types cannot be sent as values"); } + &_ => unreachable!("unknown AST/HIR variant"), } } _ => invocation_params.push(param_name.to_string()), @@ -420,7 +421,8 @@ impl fmt::Display for InvocationIntoJs<'_> { } ast::CustomType::Enum(enm) => { write!(f, "{}_rust_to_js[{}]", enm.name, self.invocation.scalar()) - } + }, + &_ => unreachable!("unknown AST/HIR variant") } } ast::TypeName::Reference(.., inner) => Pointer { @@ -510,6 +512,7 @@ impl fmt::Display for InvocationIntoJs<'_> { } ast::TypeName::Writeable => todo!(), ast::TypeName::Unit => self.invocation.scalar().fmt(f), + &_ => unreachable!("unknown AST/HIR variant"), } } } @@ -728,6 +731,7 @@ impl fmt::Display for UnderlyingIntoJs<'_> { "{}_rust_to_js[diplomatRuntime.enumDiscriminant(wasm, {})]", enm.name, self.underlying, ), + &_ => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Reference(.., typ) => Pointer { @@ -780,6 +784,7 @@ impl fmt::Display for UnderlyingIntoJs<'_> { self.display_slice(SliceKind::Primitive(prim.into())).fmt(f) } ast::TypeName::Unit => "{}".fmt(f), + &_ => unreachable!("unknown AST/HIR variant"), } } } diff --git a/tool/src/js/mod.rs b/tool/src/js/mod.rs index cc60b6f0d..5cf821caf 100644 --- a/tool/src/js/mod.rs +++ b/tool/src/js/mod.rs @@ -237,6 +237,7 @@ impl<'env> Imports<'env> { self.collect_usages(typ, &ty_in_path, env, TypePosition::Inner) } } + &_ => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Reference(.., typ) diff --git a/tool/src/js/structs.rs b/tool/src/js/structs.rs index 95f42546c..c50a0f543 100644 --- a/tool/src/js/structs.rs +++ b/tool/src/js/structs.rs @@ -182,6 +182,7 @@ pub fn gen_struct( }) ) } + &_ => unreachable!("unknown AST/HIR variant"), } } @@ -521,6 +522,7 @@ pub fn gen_ts_type( ast::TypeName::Unit => { out.write_str("void")?; } + &_ => unreachable!("unknown AST/HIR variant"), } Ok(false) } diff --git a/tool/src/js/types.rs b/tool/src/js/types.rs index a3fb9ed87..96b207113 100644 --- a/tool/src/js/types.rs +++ b/tool/src/js/types.rs @@ -50,6 +50,7 @@ pub fn return_type_form(typ: &ast::TypeName, in_path: &ast::Path, env: &Env) -> (_, ast::CustomType::Opaque(_)) | (_, ast::CustomType::Enum(_)) => { ReturnTypeForm::Scalar } + (_, &_) => unreachable!("unknown AST/HIR variant"), } } @@ -79,6 +80,7 @@ pub fn return_type_form(typ: &ast::TypeName, in_path: &ast::Path, env: &Env) -> ast::TypeName::Primitive(_) => ReturnTypeForm::Scalar, ast::TypeName::Writeable => panic!("Cannot return writeable"), + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/layout.rs b/tool/src/layout.rs index b7b064d8e..ccb3bbe25 100644 --- a/tool/src/layout.rs +++ b/tool/src/layout.rs @@ -96,6 +96,7 @@ pub fn type_size_alignment(typ: &ast::TypeName, in_path: &ast::Path, env: &Env) (_, ast::CustomType::Opaque(_)) => { panic!("Size of opaque types is unknown") } + (_, &_) => unreachable!("unknown AST/HIR variant"), } } ast::TypeName::Primitive(p) => primitive_size_alignment(*p), @@ -108,6 +109,7 @@ pub fn type_size_alignment(typ: &ast::TypeName, in_path: &ast::Path, env: &Env) ast::TypeName::PrimitiveSlice(..) => Layout::new::<(usize_target, usize_target)>(), ast::TypeName::Writeable => panic!(), ast::TypeName::Unit => Layout::new::<()>(), + &_ => unreachable!("unknown AST/HIR variant"), } } diff --git a/tool/src/lib.rs b/tool/src/lib.rs index 746d8af87..5a85b2ed2 100644 --- a/tool/src/lib.rs +++ b/tool/src/lib.rs @@ -1,3 +1,6 @@ +// Enable once https://github.com/rust-lang/rust/issues/89554 is stable +// #![deny(non_exhaustive_omitted_patterns)] // diplomat_core uses non_exhaustive a lot; we should never miss its patterns + pub mod c; pub mod c2; pub mod common;