Skip to content

Commit

Permalink
Make AST non_exhaustive
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 21, 2023
1 parent 1fa336e commit b5a54c8
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/ast/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute>,
pub attrs: Vec<DiplomatBackendAttr>,
Expand Down Expand Up @@ -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")]
Expand All @@ -103,6 +105,7 @@ where
}

#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize)]
#[non_exhaustive]
pub enum DiplomatBackendAttrCfg {
Not(Box<DiplomatBackendAttrCfg>),
Any(Vec<DiplomatBackendAttrCfg>),
Expand Down
4 changes: 4 additions & 0 deletions core/src/ast/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Docs(String, Vec<RustLink>);
/// 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,
Expand Down Expand Up @@ -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
///
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions core/src/ast/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)>,
Expand Down
2 changes: 2 additions & 0 deletions core/src/ast/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl DiplomatStructAttribute {
}

#[derive(Clone, Serialize, Debug)]
#[non_exhaustive]
pub struct Module {
pub name: Ident,
pub imports: Vec<(Path, Ident)>,
Expand Down Expand Up @@ -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<String, Module>,
}
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ident>,
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/ast/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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<Lifetime>,
Expand Down Expand Up @@ -299,6 +302,7 @@ impl From<Path> 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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -882,6 +887,7 @@ impl TypeName {
}
}

#[non_exhaustive]
pub enum LifetimeOrigin {
Named,
Reference,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/ast/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions core/src/hir/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -21,6 +23,7 @@ pub type OutStructDef = StructDef<OutputOnly>;

/// Structs that can be either inputs or outputs in methods.
#[derive(Debug)]
#[non_exhaustive]
pub struct StructDef<P: TyPosition = Everywhere> {
pub docs: Docs,
pub name: IdentBuf,
Expand All @@ -38,6 +41,7 @@ pub struct StructDef<P: TyPosition = Everywhere> {
///
/// A struct marked with `#[diplomat::opaque]`.
#[derive(Debug)]
#[non_exhaustive]
pub struct OpaqueDef {
pub docs: Docs,
pub name: IdentBuf,
Expand All @@ -47,6 +51,7 @@ pub struct OpaqueDef {

/// The enum type.
#[derive(Debug)]
#[non_exhaustive]
pub struct EnumDef {
pub docs: Docs,
pub name: IdentBuf,
Expand All @@ -60,6 +65,7 @@ pub type OutStructField = StructField<OutputOnly>;

/// A field on a [`Struct`]s.
#[derive(Debug)]
#[non_exhaustive]
pub struct StructField<P: TyPosition = Everywhere> {
pub docs: Docs,
pub name: IdentBuf,
Expand All @@ -68,6 +74,7 @@ pub struct StructField<P: TyPosition = Everywhere> {

/// A variant of an [`Enum`].
#[derive(Debug)]
#[non_exhaustive]
pub struct EnumVariant {
pub docs: Docs,
pub name: IdentBuf,
Expand Down
1 change: 1 addition & 0 deletions core/src/hir/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Static,
NonStatic(T),
Expand Down
1 change: 1 addition & 0 deletions core/src/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions core/src/hir/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{

/// A method exposed to Diplomat.
#[derive(Debug)]
#[non_exhaustive]
pub struct Method {
pub docs: Docs,
pub name: IdentBuf,
Expand All @@ -24,26 +25,30 @@ pub struct Method {

/// Type that the method returns.
#[derive(Debug)]
#[non_exhaustive]
pub enum SuccessType {
Writeable,
OutType(OutType),
}

/// 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<SuccessType>),
Fallible(Option<SuccessType>, Option<OutType>),
}

/// 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,
Expand Down
6 changes: 6 additions & 0 deletions core/src/hir/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -15,6 +16,7 @@ pub type OutStructPath = StructPath<OutputOnly>;

/// Path to a struct that can be used in inputs and outputs.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StructPath<P: TyPosition = Everywhere> {
pub lifetimes: TypeLifetimes,
pub tcx_id: P::StructId,
Expand All @@ -35,6 +37,7 @@ pub struct StructPath<P: TyPosition = Everywhere> {
/// 3. `OpaquePath<NonOptional, Borrow>`: 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<Opt, Owner> {
pub lifetimes: TypeLifetimes,
pub optional: Opt,
Expand All @@ -46,6 +49,7 @@ pub struct OpaquePath<Opt, Owner> {
pub struct Optional(pub(super) bool);

#[derive(Debug, Copy, Clone)]
#[allow(clippy::exhaustive_enums)] // marker type
pub struct NonOptional;

impl<Owner: OpaqueOwner> OpaquePath<Optional, Owner> {
Expand Down Expand Up @@ -79,6 +83,7 @@ impl<Opt> OpaquePath<Opt, Borrow> {

/// Path to an enum.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct EnumPath {
pub tcx_id: EnumId,
}
Expand All @@ -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),
Expand Down
Loading

0 comments on commit b5a54c8

Please sign in to comment.