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 81dbcfe commit c2715cd
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tool/src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn gen_struct_header<'a>(
}
writeln!(out, "}} {};", enm.name)?;
}
&_ => unreachable!("unknown AST/HIR variant"),
}

writeln!(out, "#ifdef __cplusplus")?;
Expand Down Expand Up @@ -286,6 +287,7 @@ 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 @@ -333,6 +335,7 @@ pub fn gen_includes<W: fmt::Write>(
ast::TypeName::StrReference(..) => {}
ast::TypeName::PrimitiveSlice(..) => {}
ast::TypeName::Unit => {}
&_ => unreachable!("unknown AST/HIR variant"),
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tool/src/c/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn gen_struct<W: fmt::Write>(
}

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

Ok(())
Expand Down
3 changes: 3 additions & 0 deletions tool/src/c/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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
1 change: 1 addition & 0 deletions tool/src/c2/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
5 changes: 5 additions & 0 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 @@ -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),
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
4 changes: 4 additions & 0 deletions tool/src/cpp/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn gen_rust_to_cpp<W: Write>(
// TODO(#59): should emit a unique_ptr
todo!("Receiving boxes of enums is not yet supported")
}
&_ => unreachable!("unknown AST/HIR variant"),
}
}
_o => todo!(),
Expand Down Expand Up @@ -67,6 +68,7 @@ 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 @@ -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
5 changes: 5 additions & 0 deletions tool/src/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub fn gen_bindings(
)?;
}
}
&&_ => unreachable!("unknown AST/HIR variant"),
}

for method in typ.methods() {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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")?
Expand Down Expand Up @@ -280,6 +283,7 @@ fn gen_includes<W: fmt::Write>(
seen_includes.insert(include);
}
}
&_ => unreachable!("unknown AST/HIR variant"),
}
}
ast::TypeName::Box(underlying) => {
Expand Down Expand Up @@ -346,6 +350,7 @@ fn gen_includes<W: fmt::Write>(
ast::TypeName::StrReference(..) => {}
ast::TypeName::PrimitiveSlice(..) => {}
ast::TypeName::Unit => {}
&_ => unreachable!("unknown AST/HIR variant"),
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tool/src/cpp/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub fn gen_struct<W: fmt::Write>(
}

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

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion tool/src/cpp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ 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 @@ -180,6 +181,7 @@ fn gen_type_inner<W: fmt::Write>(
ast::TypeName::Unit => {
write!(out, "void")?;
}
&_ => unreachable!("unknown AST/HIR variant"),
}

if !handled_ref {
Expand Down
10 changes: 10 additions & 0 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 @@ -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"),
}
}

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 @@ -560,6 +563,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
},
]
}
_ => 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 @@ -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"),
}
}

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 @@ -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"),
}
}
}
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
2 changes: 2 additions & 0 deletions tool/src/dotnet/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ 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 @@ -119,6 +120,7 @@ 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
4 changes: 4 additions & 0 deletions tool/src/dotnet/idiomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub fn gen(
Ok(())
})?;
}
&_ => unreachable!("unknown AST/HIR variant"),
}

Ok(())
Expand Down Expand Up @@ -212,6 +213,7 @@ fn gen_property_for_field(
return Ok(());
}
ast::CustomType::Enum(_) => {}
&_ => unreachable!("unknown AST/HIR variant"),
}
}
_ => {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions tool/src/dotnet/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub fn gen<'ast>(
Ok(())
})
}
&_ => unreachable!("unknown AST/HIR variant"),
}
}

Expand Down
2 changes: 2 additions & 0 deletions tool/src/dotnet/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn gen_type_name(
ast::TypeName::Unit => {
write!(out, "void")
}
&_ => unreachable!("unknown AST/HIR variant"),
}
}

Expand Down Expand Up @@ -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"),
}
}
2 changes: 2 additions & 0 deletions tool/src/dotnet/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn collect_results<'ast>(
| ast::TypeName::Named(_)
| ast::TypeName::SelfType(_)
| ast::TypeName::Primitive(_) => {}
&_ => unreachable!("unknown AST/HIR variant"),
}
}

Expand Down Expand Up @@ -124,5 +125,6 @@ fn collect_errors_impl<'ast>(
| ast::TypeName::Writeable
| ast::TypeName::StrReference(..)
| ast::TypeName::PrimitiveSlice(..) => {}
&_ => unreachable!("unknown AST/HIR variant"),
}
}
Loading

0 comments on commit c2715cd

Please sign in to comment.