Skip to content

refactors sus_panic and sus_unreachable #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
8 changes: 4 additions & 4 deletions STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ footguns, crashes, bugs, and UB.
1. All methods are `constexpr` unless they must call a non-`constexpr` function,
or they expose floating point NaNs (since constexpr NaNs change their bit
values).
* Consider `sus_panic()`/`sus_check()` as constexpr for these purposes, they will
* Consider `sus::panic()`/`sus_check()` as constexpr for these purposes, they will
correctly prevent compiling if the condition fails.
1. If you override on `const&`, then explicitly provide or delete the `&&`
override.
Expand Down Expand Up @@ -71,7 +71,7 @@ footguns, crashes, bugs, and UB.
`operator==(Option, Option)` and `operator==(Option<T>, Option<U>)` look redundant
but they are not, as the former allows conversions to Option for the rhs to happen
while the latter does not (it would have to deduce `U` and fails).

## Containers that hold references

Container types that hold references require extra care in a number of ways. To
Expand All @@ -87,13 +87,13 @@ properly build such a container type (e.g. `Option` and `Tuple`):
from an rvalue is okay, but when holding a value, giving a reference to it from an
rvalue is not.
* Use `static_assert(SafelyConstructibleFromReference<ToType, FromReferenceType&&>)`
in places that store the reference to ensure a reference to a temporary does not
in places that store the reference to ensure a reference to a temporary does not
get created due to an implicit conversion. The `FromReferenceType&&` here is should
be the input type as it's written in the function parameters.
* If a ctor type deduction guide is provided, the deduction should strip qualifiers
and references with `std::remove_cvref_t` on the deduced type arguments.
* Consider providing a construction marker type such as `some() -> SomeMarker` which
captures the parameters as references and lazily constructs the final type. This
captures the parameters as references and lazily constructs the final type. This
allows reference types to be preserved through to the construction of the
container without requiring the full type defn to be written every time.
* Notably, this is omitted for `Choice`, which needs to be reasonably used behind
Expand Down
2 changes: 1 addition & 1 deletion subdoc/lib/friendly_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ inline std::string friendly_record_type_name(RecordType t,
case RecordType::Struct: return capitalize ? "Struct" : "struct";
case RecordType::Union: return capitalize ? "Union" : "union";
}
sus_unreachable_unchecked(unsafe_fn);
sus::unreachable_unchecked(unsafe_fn);
}

} // namespace subdoc
16 changes: 8 additions & 8 deletions subdoc/lib/gen/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ inline std::filesystem::path construct_html_namespace_file_path(
return fmt::format("namespace.{}",
namespace_path[0u].as<Namespace::Tag::Named>());
}
sus_unreachable();
sus::unreachable();
}();

fname << name;
Expand Down Expand Up @@ -316,7 +316,7 @@ inline Option<std::string> construct_html_url_for_alias(
// aliases.
break;
}
sus_unreachable();
sus::unreachable();
});
}
case AliasTarget::Tag::AliasOfConcept: {
Expand All @@ -330,11 +330,11 @@ inline Option<std::string> construct_html_url_for_alias(
}
case ConceptRefOrName::Tag::Name: return sus::none();
}
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfMethod: {
// TODO: Link to method.
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfFunction: {
const LinkedFunction& fun =
Expand All @@ -347,11 +347,11 @@ inline Option<std::string> construct_html_url_for_alias(
}
case FunctionRefOrName::Tag::Name: return sus::none();
}
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfEnumConstant: {
// TODO: Link to constant.
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfVariable: {
const LinkedVariable& var =
Expand All @@ -364,10 +364,10 @@ inline Option<std::string> construct_html_url_for_alias(
}
case VariableRefOrName::Tag::Name: return sus::none();
}
sus_unreachable();
sus::unreachable();
}
}
sus_unreachable();
sus::unreachable();
} else {
// TODO: Link to the alias' page.
return sus::some("TODO");
Expand Down
4 changes: 2 additions & 2 deletions subdoc/lib/gen/generate.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct sus::error::ErrorImpl<subdoc::gen::GenerateError> {
return fmt::format("parsing doc comment markdown");
}
}
sus_unreachable();
sus::unreachable();
}
static Option<const sus::error::DynError&> source(
const GenerateError& e) noexcept {
Expand All @@ -88,6 +88,6 @@ struct sus::error::ErrorImpl<subdoc::gen::GenerateError> {
return sus::some(*p);
}
}
sus_unreachable();
sus::unreachable();
}
};
4 changes: 2 additions & 2 deletions subdoc/lib/gen/generate_alias.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ sus::Result<MarkdownToHtml, MarkdownToHtmlError> get_alias_comment(
}
case AliasTarget::Tag::AliasOfMethod: {
// TODO: Link to method.
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfFunction: {
const LinkedFunction& fun =
Expand All @@ -94,7 +94,7 @@ sus::Result<MarkdownToHtml, MarkdownToHtmlError> get_alias_comment(
}
case AliasTarget::Tag::AliasOfEnumConstant: {
// TODO: Link to constant.
sus_unreachable();
sus::unreachable();
}
case AliasTarget::Tag::AliasOfVariable: {
const LinkedVariable& var =
Expand Down
2 changes: 1 addition & 1 deletion subdoc/lib/gen/generate_concept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void generate_concept_overview(HtmlWriter::OpenDiv& record_div,
break; // Macro can't be an ancesor of a concept.
case CppPathConcept: return "concept-name";
}
sus_unreachable();
sus::unreachable();
}());
ancestor_anchor.add_href(e.link_href);
ancestor_anchor.write_text(e.name);
Expand Down
4 changes: 2 additions & 2 deletions subdoc/lib/gen/generate_cpp_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Vec<CppPathElement> generate_with_ancestors(
return std::string("(anonymous)");
case Namespace::Tag::Named: return sus::clone(ancestor.name);
}
sus_unreachable();
sus::unreachable();
}(),
.link_href = construct_html_url_for_namespace(ancestor),
.type =
Expand All @@ -51,7 +51,7 @@ Vec<CppPathElement> generate_with_ancestors(
case Namespace::Tag::Anonymous: return CppPathNamespace;
case Namespace::Tag::Named: return CppPathNamespace;
}
sus_unreachable();
sus::unreachable();
}(),
.search_weight = 1_f32,
});
Expand Down
2 changes: 1 addition & 1 deletion subdoc/lib/gen/generate_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ sus::Result<void, MarkdownToHtmlError> generate_function(
case CppPathConcept:
break; // Concept can't be an ancestor of a function.
}
sus_unreachable();
sus::unreachable();
}());
ancestor_anchor.add_href(e.link_href);
ancestor_anchor.write_text(e.name);
Expand Down
2 changes: 1 addition & 1 deletion subdoc/lib/gen/generate_macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ sus::Result<void, MarkdownToHtmlError> generate_macro(
case CppPathProject: return "project-name";
case CppPathMacro: return "macro-name";
default:
sus_unreachable(); // Macros are only in the global namespace.
sus::unreachable(); // Macros are only in the global namespace.
}
}());
ancestor_anchor.add_href(e.link_href);
Expand Down
4 changes: 2 additions & 2 deletions subdoc/lib/gen/generate_namespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void generate_namespace_overview(HtmlWriter::OpenDiv& namespace_div,
case CppPathConcept:
break; // Concept can't be an ancestor of a namespace.
}
sus_unreachable();
sus::unreachable();
}());
ancestor_anchor.add_href(e.link_href);
ancestor_anchor.write_text(e.name);
Expand Down Expand Up @@ -615,7 +615,7 @@ sus::Result<void, MarkdownToHtmlError> generate_namespace(
json.add_string("split_name", split_for_search(options.project_name));
break;
}
case Namespace::Tag::Anonymous: sus_unreachable();
case Namespace::Tag::Anonymous: sus::unreachable();
case Namespace::Tag::Named: {
json.add_string("type", "namespace");
json.add_string("name", element.name);
Expand Down
4 changes: 2 additions & 2 deletions subdoc/lib/gen/generate_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const FunctionElement& function_element_from_sorted(
case MethodType::NonStaticOperators: return element.methods.at(s.at<3>());
case MethodType::Conversions: return element.conversions.at(s.at<3>());
}
sus_unreachable();
sus::unreachable();
}

/// Compares two `SortedFunctionByName` for ordering. It compares by ignoring
Expand Down Expand Up @@ -135,7 +135,7 @@ void generate_record_overview(HtmlWriter::OpenDiv& record_div,
case CppPathConcept:
break; // Concept can't be an ancestor of a record.
}
sus_unreachable();
sus::unreachable();
}());
ancestor_anchor.add_href(e.link_href);
ancestor_anchor.write_text(e.name);
Expand Down
10 changes: 5 additions & 5 deletions subdoc/lib/requires.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::string template_arg_to_string(const clang::TemplateArgumentLoc& loc,
// How can this happen in a concept instantiation?
arg.dump();
fmt::println("");
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::Type: {
if (arg.getAsType()->isDependentType()) {
// A template argument that is a template parameter (from the function,
Expand All @@ -43,7 +43,7 @@ std::string template_arg_to_string(const clang::TemplateArgumentLoc& loc,
// How can this happen in a concept instantiation?
arg.dump();
fmt::println("");
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::NullPtr: return "nullptr";
case clang::TemplateArgument::ArgKind::Integral: {
return llvm_int_to_string(arg.getAsIntegral());
Expand All @@ -57,19 +57,19 @@ std::string template_arg_to_string(const clang::TemplateArgumentLoc& loc,
// How can this happen in a concept instantiation?
arg.dump();
fmt::println("");
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::TemplateExpansion:
// How can this happen in a concept instantiation?
arg.dump();
fmt::println("");
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::Expression:
return stmt_to_string(*arg.getAsExpr(), context.getSourceManager(),
preprocessor);
case clang::TemplateArgument::ArgKind::Pack:
return std::string("TODO: pack");
}
sus_unreachable();
sus::unreachable();
};

void requires_constraints_add_expr(RequiresConstraints& constraints,
Expand Down
24 changes: 12 additions & 12 deletions subdoc/lib/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ clang::DeclContext* find_context(const clang::Type* type,
case clang::NestedNameSpecifier::Super:
return spec->getAsRecordDecl()->getDeclContext();
}
sus_unreachable();
sus::unreachable();
} else if (auto* tag_type = clang::dyn_cast<clang::TagType>(type)) {
return tag_type->getDecl()->getDeclContext();
} else if (auto* spec_type =
Expand Down Expand Up @@ -201,7 +201,7 @@ clang::DeclContext* find_context(const clang::Type* type,
} else {
type->dump();
loc.dump(sm);
sus_unreachable(); // Find the context.
sus::unreachable(); // Find the context.
}
}

Expand All @@ -221,7 +221,7 @@ TypeOrValue build_template_param(
arg.dump();
fmt::println(stderr, "");
loc.dump(sm);
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::Type:
return TypeOrValue(TypeOrValueChoice::with<TypeOrValueChoice::Tag::Type>(
build_local_type_internal(arg.getAsType(), template_params, cx,
Expand Down Expand Up @@ -261,16 +261,16 @@ TypeOrValue build_template_param(
arg.dump();
fmt::println(stderr, "");
loc.dump(sm);
sus_unreachable();
sus::unreachable();
case clang::TemplateArgument::ArgKind::Expression:
return TypeOrValue(TypeOrValueChoice::with<TypeOrValueChoice::Tag::Value>(
stmt_to_string(*arg.getAsExpr(), sm, preprocessor)));
case clang::TemplateArgument::ArgKind::Pack:
// Packs are handled at a higher level since they produce multiple types.
loc.dump(sm);
sus_unreachable();
sus::unreachable();
}
sus_unreachable();
sus::unreachable();
};

clang::QualType unwrap_skipped_types(clang::QualType q) noexcept {
Expand Down Expand Up @@ -348,7 +348,7 @@ Type build_local_type_internal(
qualtype->dump();
loc.dump(sm);
fmt::println(stderr, "\nkind: {}", (int)kind);
sus_unreachable();
sus::unreachable();
}
spec = spec->getPrefix();
}
Expand All @@ -374,7 +374,7 @@ Type build_local_type_internal(
if (is_pack) {
qualtype->dump();
loc.dump(sm);
sus_unreachable();
sus::unreachable();
}

// Arrays come with the var name wrapped in parens, which must be removed.
Expand All @@ -397,7 +397,7 @@ Type build_local_type_internal(
if (auto* vararr = clang::dyn_cast<clang::VariableArrayType>(type)) {
qualtype->dump();
loc.dump(sm);
sus_unreachable(); // This is a C thing, not C++.
sus::unreachable(); // This is a C thing, not C++.
}

// For arrays the root qualifiers come from the element type.
Expand Down Expand Up @@ -496,7 +496,7 @@ Type build_local_type_internal(
// Partial specialization in another type?
partial->dump();
loc.dump(sm);
sus_unreachable();
sus::unreachable();
} else if (auto* full =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
rec_type->getDecl())) {
Expand Down Expand Up @@ -539,7 +539,7 @@ Type build_local_type_internal(
// No template parameters.
// qualtype->dump();
// loc.dump(sm);
// sus_unreachable();
// sus::unreachable();
}

// Find the context from which to collect the namespace/record paths.
Expand Down Expand Up @@ -593,7 +593,7 @@ Type build_local_type_internal(
if (auto_type->isConstrained()) {
qualtype->dump();
loc.dump(sm);
sus_panic_with_message("constrained auto without a concept?");
sus::panic("constrained auto without a concept?");
}
if (auto_type->isDecltypeAuto()) {
return sus::tuple("decltype(auto)", TypeCategory::TemplateVariable);
Expand Down
Loading