Skip to content

Commit

Permalink
add unreachables
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 21, 2023
1 parent b5a54c8 commit 80b1136
Show file tree
Hide file tree
Showing 22 changed files with 102 additions and 42 deletions.
9 changes: 6 additions & 3 deletions tool/src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ fn gen_struct_header<'a>(
)?;
}
writeln!(out, "}} {};", enm.name)?;
}
},
&_ => unreachable!("unknown AST/HIR variant")
}

writeln!(out, "#ifdef __cplusplus")?;
Expand Down Expand Up @@ -285,7 +286,8 @@ pub fn gen_includes<W: fmt::Write>(

(ast::CustomType::Opaque(_), false) => {
panic!("Cannot pass opaque types by value")
}
},
(&_, _) => unreachable!("unknown AST/HIR variant")
}
}
ast::TypeName::Box(underlying) => {
Expand Down Expand Up @@ -332,7 +334,8 @@ pub fn gen_includes<W: fmt::Write>(
ast::TypeName::Writeable => {}
ast::TypeName::StrReference(..) => {}
ast::TypeName::PrimitiveSlice(..) => {}
ast::TypeName::Unit => {}
ast::TypeName::Unit => {},
&_ => unreachable!("unknown AST/HIR variant")
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion tool/src/c/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ pub fn gen_struct<W: fmt::Write>(
writeln!(out, "}} {};", strct.name)?;
}

ast::CustomType::Enum(_) => {}
ast::CustomType::Enum(_) => {},
&_ => unreachable!("unknown AST/HIR variant")
}

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion tool/src/c/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ pub fn gen_type<W: fmt::Write>(

ast::CustomType::Enum(enm) => {
write!(out, "{}", enm.name)?;
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}

Expand Down Expand Up @@ -62,6 +63,7 @@ pub fn gen_type<W: fmt::Write>(
write!(out, "Diplomat{prim}View")?;
}
ast::TypeName::Unit => write!(out, "void")?,
&_ => unreachable!("unknown AST/HIR variant"),
}

Ok(())
Expand Down Expand Up @@ -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"),
}
}

Expand Down
3 changes: 2 additions & 1 deletion tool/src/c2/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ impl<'tcx> CFormatter<'tcx> {
let constness = borrow.mutability.if_mut_else("", "const_");
let prim = self.fmt_primitive_as_c(*p);
format!("ref_{constness}prim_slice_{prim}").into()
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}

Expand Down
9 changes: 7 additions & 2 deletions tool/src/c2/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -149,7 +151,8 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
None => ("void".into(), None),
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),
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions tool/src/cpp/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ pub fn gen_rust_to_cpp<W: Write>(
ast::CustomType::Enum(_) => {
// TODO(#59): should emit a unique_ptr
todo!("Receiving boxes of enums is not yet supported")
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}
_o => todo!(),
_o => unreachable!("unknown AST/HIR variant"),
},
ast::TypeName::Named(path_type) | ast::TypeName::SelfType(path_type) => {
match path_type.resolve_with_path(in_path, env) {
Expand Down Expand Up @@ -66,7 +67,8 @@ pub fn gen_rust_to_cpp<W: Write>(

(_, ast::CustomType::Enum(enm)) => {
format!("static_cast<{}>({})", enm.name, cpp)
}
},
(_, &_) => unreachable!("unknown AST/HIR variant")
}
}

Expand Down Expand Up @@ -129,7 +131,7 @@ pub fn gen_rust_to_cpp<W: Write>(
wrapped_value_id
}

_ => todo!(),
_ => unreachable!("unknown AST/HIR variant"),
},

ast::TypeName::Result(ok, err, _) => {
Expand Down Expand Up @@ -229,6 +231,7 @@ pub fn gen_rust_to_cpp<W: Write>(
"slice".into()
}
ast::TypeName::Unit => cpp.to_string(),
&_ => unreachable!("unknown AST/HIR variant"),
}
}

Expand Down Expand Up @@ -333,6 +336,7 @@ pub fn gen_cpp_to_rust<W: Write>(
}

ast::CustomType::Enum(enm) => format!("static_cast<capi::{}>({})", enm.name, cpp),
&_ => unreachable!("unknown AST/HIR variant"),
}
}
ast::TypeName::Writeable => {
Expand Down
1 change: 1 addition & 0 deletions tool/src/cpp/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub fn gen_custom_type_docs<W: fmt::Write>(
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(" ");
Expand Down
15 changes: 10 additions & 5 deletions tool/src/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ pub fn gen_bindings(
out,
)?;
}
}
},
&&_ => unreachable!("unknown AST/HIR variant")
}

for method in typ.methods() {
Expand Down Expand Up @@ -173,7 +174,8 @@ pub fn gen_bindings(
ast::CustomType::Struct(_) => {
writeln!(out)?;
gen_struct(typ, in_path, true, env, &library_config, docs_url_gen, out)?;
}
},
&&_ => unreachable!("unknown AST/HIR variant")
}

writeln!(out)?;
Expand Down Expand Up @@ -217,7 +219,8 @@ pub fn gen_bindings(
ast::CustomType::Struct(_) => {
writeln!(out)?;
gen_struct(typ, in_path, false, env, &library_config, docs_url_gen, out)?;
}
},
&&_ => unreachable!("unknown AST/HIR variant")
}

writeln!(out, "#endif")?
Expand Down Expand Up @@ -279,7 +282,8 @@ fn gen_includes<W: fmt::Write>(
writeln!(out, "{include}")?;
seen_includes.insert(include);
}
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}
ast::TypeName::Box(underlying) => {
Expand Down Expand Up @@ -345,7 +349,8 @@ fn gen_includes<W: fmt::Write>(
ast::TypeName::Writeable => {}
ast::TypeName::StrReference(..) => {}
ast::TypeName::PrimitiveSlice(..) => {}
ast::TypeName::Unit => {}
ast::TypeName::Unit => {},
&_ => unreachable!("unknown AST/HIR variant")
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion tool/src/cpp/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ pub fn gen_struct<W: fmt::Write>(
}
}

ast::CustomType::Enum(_) => {}
ast::CustomType::Enum(_) => {},
&_ => unreachable!("unknown AST/HIR variant")
}

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions tool/src/cpp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fn gen_type_inner<W: fmt::Write>(

ast::CustomType::Enum(enm) => {
write!(out, "{}", enm.name)?;
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}

Expand Down Expand Up @@ -126,7 +127,7 @@ fn gen_type_inner<W: fmt::Write>(
)?;
}

_ => todo!(),
_ => unreachable!("unknown AST/HIR variant"),
},

ast::TypeName::Result(ok, err, _) => {
Expand Down Expand Up @@ -179,7 +180,8 @@ fn gen_type_inner<W: fmt::Write>(

ast::TypeName::Unit => {
write!(out, "void")?;
}
},
&_ => unreachable!("unknown AST/HIR variant")
}

if !handled_ref {
Expand Down
18 changes: 14 additions & 4 deletions tool/src/cpp2/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -458,7 +459,8 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
let ret = self.cx.formatter.fmt_primitive_as_c(p);
let ret = self.cx.formatter.fmt_borrowed_slice(&ret, b.mutability);
ret.into_owned().into()
}
},
_ => unreachable!("unknown AST/HIR variant")
}
}

Expand All @@ -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"),
}
}

Expand Down Expand Up @@ -559,7 +562,8 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
expression: format!("{cpp_name}.size()").into(),
},
]
}
},
_ => unreachable!("unknown AST/HIR variant")
}
}

Expand All @@ -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),
Expand Down Expand Up @@ -660,7 +666,8 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
.formatter
.fmt_borrowed_slice(&prim_name, b.mutability);
format!("{span}({var_name}_data, {var_name}_size)").into()
}
},
_ => unreachable!("unknown AST/HIR variant")
}
}

Expand All @@ -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),
Expand All @@ -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()),
Expand All @@ -703,7 +712,8 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
Some(
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")
}
}
}
1 change: 1 addition & 0 deletions tool/src/docs_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl<W: fmt::Write> FromMarkdown<W> 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"),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions tool/src/dotnet/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn to_idiomatic_object<W: fmt::Write>(
}
ast::CustomType::Enum(_) => {
write!(out, "({name}){input_var_name}")
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}
other => panic!("expected named type name, found `{}`", other),
Expand Down Expand Up @@ -118,7 +119,8 @@ pub fn to_raw_object<W: fmt::Write>(
}
ast::CustomType::Enum(_) => {
write!(out, "(Raw.{name}){input_var_name}")
}
},
&_ => unreachable!("unknown AST/HIR variant")
}
}
other => panic!("expected named type name, found `{}`", other),
Expand Down
Loading

0 comments on commit 80b1136

Please sign in to comment.