Add Type::Void variant

Not currently usable as a function argument or explicit return value,
but will be required when we introduce Result for the case of fallible
void functions, whose return type will be Result<()>.
This commit is contained in:
David Tolnay 2020-03-15 23:11:38 -07:00
parent 9542f227db
commit 2fb14e934b
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
6 changed files with 24 additions and 1 deletions

View File

@ -472,6 +472,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
Type::Str(_) => {
write!(out, "::rust::Str");
}
Type::Void(_) => unreachable!(),
}
}
@ -480,6 +481,7 @@ fn write_type_space(out: &mut OutFile, ty: &Type) {
match ty {
Type::Ident(_) | Type::RustBox(_) | Type::UniquePtr(_) | Type::Str(_) => write!(out, " "),
Type::Ref(_) => {}
Type::Void(_) => unreachable!(),
}
}

View File

@ -161,6 +161,7 @@ fn describe(ty: &Type, types: &Types) -> String {
Type::UniquePtr(_) => "unique_ptr".to_owned(),
Type::Ref(_) => "reference".to_owned(),
Type::Str(_) => "&str".to_owned(),
Type::Void(_) => "()".to_owned(),
}
}

View File

@ -9,9 +9,11 @@ pub mod ident;
mod impls;
mod parse;
pub mod set;
mod span;
mod tokens;
pub mod types;
use self::span::Span;
use proc_macro2::Ident;
use syn::{LitStr, Token};
@ -70,6 +72,7 @@ pub enum Type {
UniquePtr(Box<Ty1>),
Ref(Box<Ref>),
Str(Box<Ref>),
Void(Span),
}
pub struct Ty1 {

16
syntax/span.rs Normal file
View File

@ -0,0 +1,16 @@
use std::hash::{Hash, Hasher};
#[derive(Copy, Clone)]
pub struct Span(pub proc_macro2::Span);
impl Hash for Span {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
impl Eq for Span {}
impl PartialEq for Span {
fn eq(&self, _other: &Span) -> bool {
true
}
}

View File

@ -16,6 +16,7 @@ impl ToTokens for Type {
}
Type::RustBox(ty) | Type::UniquePtr(ty) => ty.to_tokens(tokens),
Type::Ref(r) | Type::Str(r) => r.to_tokens(tokens),
Type::Void(span) => tokens.extend(quote_spanned!(span.0=> ())),
}
}
}

View File

@ -23,7 +23,7 @@ impl<'a> Types<'a> {
fn visit<'a>(all: &mut Set<'a, Type>, ty: &'a Type) {
all.insert(ty);
match ty {
Type::Ident(_) | Type::Str(_) => {}
Type::Ident(_) | Type::Str(_) | Type::Void(_) => {}
Type::RustBox(ty) | Type::UniquePtr(ty) => visit(all, &ty.inner),
Type::Ref(r) => visit(all, &r.inner),
}