Skip to content

Commit ad5b35d

Browse files
cjdbdanakj
andauthored
refactors sus_panic and sus_unreachable (#470)
These macros have been changed to functions, which will provide better ergonomics for C++. Care was taken to ensure that calls to `sus::panic` weren't substantially worse than expanding `sus_panic`. In addition, `sus_panic_with_message` was refactored into `sus::panic`. The same logic was applied to refactoring `sus_unreachable`, although the assembly wasn't inspected. --------- Co-authored-by: Dana Jansens <[email protected]>
1 parent 06ae338 commit ad5b35d

40 files changed

+280
-239
lines changed

STYLE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ footguns, crashes, bugs, and UB.
77
1. All methods are `constexpr` unless they must call a non-`constexpr` function,
88
or they expose floating point NaNs (since constexpr NaNs change their bit
99
values).
10-
* Consider `sus_panic()`/`sus_check()` as constexpr for these purposes, they will
10+
* Consider `sus::panic()`/`sus_check()` as constexpr for these purposes, they will
1111
correctly prevent compiling if the condition fails.
1212
1. If you override on `const&`, then explicitly provide or delete the `&&`
1313
override.
@@ -71,7 +71,7 @@ footguns, crashes, bugs, and UB.
7171
`operator==(Option, Option)` and `operator==(Option<T>, Option<U>)` look redundant
7272
but they are not, as the former allows conversions to Option for the rhs to happen
7373
while the latter does not (it would have to deduce `U` and fails).
74-
74+
7575
## Containers that hold references
7676

7777
Container types that hold references require extra care in a number of ways. To
@@ -87,13 +87,13 @@ properly build such a container type (e.g. `Option` and `Tuple`):
8787
from an rvalue is okay, but when holding a value, giving a reference to it from an
8888
rvalue is not.
8989
* Use `static_assert(SafelyConstructibleFromReference<ToType, FromReferenceType&&>)`
90-
in places that store the reference to ensure a reference to a temporary does not
90+
in places that store the reference to ensure a reference to a temporary does not
9191
get created due to an implicit conversion. The `FromReferenceType&&` here is should
9292
be the input type as it's written in the function parameters.
9393
* If a ctor type deduction guide is provided, the deduction should strip qualifiers
9494
and references with `std::remove_cvref_t` on the deduced type arguments.
9595
* Consider providing a construction marker type such as `some() -> SomeMarker` which
96-
captures the parameters as references and lazily constructs the final type. This
96+
captures the parameters as references and lazily constructs the final type. This
9797
allows reference types to be preserved through to the construction of the
9898
container without requiring the full type defn to be written every time.
9999
* Notably, this is omitted for `Choice`, which needs to be reasonably used behind

subdoc/lib/friendly_names.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ inline std::string friendly_record_type_name(RecordType t,
6767
case RecordType::Struct: return capitalize ? "Struct" : "struct";
6868
case RecordType::Union: return capitalize ? "Union" : "union";
6969
}
70-
sus_unreachable_unchecked(unsafe_fn);
70+
sus::unreachable_unchecked(unsafe_fn);
7171
}
7272

7373
} // namespace subdoc

subdoc/lib/gen/files.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ inline std::filesystem::path construct_html_namespace_file_path(
101101
return fmt::format("namespace.{}",
102102
namespace_path[0u].as<Namespace::Tag::Named>());
103103
}
104-
sus_unreachable();
104+
sus::unreachable();
105105
}();
106106

107107
fname << name;
@@ -316,7 +316,7 @@ inline Option<std::string> construct_html_url_for_alias(
316316
// aliases.
317317
break;
318318
}
319-
sus_unreachable();
319+
sus::unreachable();
320320
});
321321
}
322322
case AliasTarget::Tag::AliasOfConcept: {
@@ -330,11 +330,11 @@ inline Option<std::string> construct_html_url_for_alias(
330330
}
331331
case ConceptRefOrName::Tag::Name: return sus::none();
332332
}
333-
sus_unreachable();
333+
sus::unreachable();
334334
}
335335
case AliasTarget::Tag::AliasOfMethod: {
336336
// TODO: Link to method.
337-
sus_unreachable();
337+
sus::unreachable();
338338
}
339339
case AliasTarget::Tag::AliasOfFunction: {
340340
const LinkedFunction& fun =
@@ -347,11 +347,11 @@ inline Option<std::string> construct_html_url_for_alias(
347347
}
348348
case FunctionRefOrName::Tag::Name: return sus::none();
349349
}
350-
sus_unreachable();
350+
sus::unreachable();
351351
}
352352
case AliasTarget::Tag::AliasOfEnumConstant: {
353353
// TODO: Link to constant.
354-
sus_unreachable();
354+
sus::unreachable();
355355
}
356356
case AliasTarget::Tag::AliasOfVariable: {
357357
const LinkedVariable& var =
@@ -364,10 +364,10 @@ inline Option<std::string> construct_html_url_for_alias(
364364
}
365365
case VariableRefOrName::Tag::Name: return sus::none();
366366
}
367-
sus_unreachable();
367+
sus::unreachable();
368368
}
369369
}
370-
sus_unreachable();
370+
sus::unreachable();
371371
} else {
372372
// TODO: Link to the alias' page.
373373
return sus::some("TODO");

subdoc/lib/gen/generate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct sus::error::ErrorImpl<subdoc::gen::GenerateError> {
7070
return fmt::format("parsing doc comment markdown");
7171
}
7272
}
73-
sus_unreachable();
73+
sus::unreachable();
7474
}
7575
static Option<const sus::error::DynError&> source(
7676
const GenerateError& e) noexcept {
@@ -88,6 +88,6 @@ struct sus::error::ErrorImpl<subdoc::gen::GenerateError> {
8888
return sus::some(*p);
8989
}
9090
}
91-
sus_unreachable();
91+
sus::unreachable();
9292
}
9393
};

subdoc/lib/gen/generate_alias.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ sus::Result<MarkdownToHtml, MarkdownToHtmlError> get_alias_comment(
7777
}
7878
case AliasTarget::Tag::AliasOfMethod: {
7979
// TODO: Link to method.
80-
sus_unreachable();
80+
sus::unreachable();
8181
}
8282
case AliasTarget::Tag::AliasOfFunction: {
8383
const LinkedFunction& fun =
@@ -94,7 +94,7 @@ sus::Result<MarkdownToHtml, MarkdownToHtmlError> get_alias_comment(
9494
}
9595
case AliasTarget::Tag::AliasOfEnumConstant: {
9696
// TODO: Link to constant.
97-
sus_unreachable();
97+
sus::unreachable();
9898
}
9999
case AliasTarget::Tag::AliasOfVariable: {
100100
const LinkedVariable& var =

subdoc/lib/gen/generate_concept.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void generate_concept_overview(HtmlWriter::OpenDiv& record_div,
7777
break; // Macro can't be an ancesor of a concept.
7878
case CppPathConcept: return "concept-name";
7979
}
80-
sus_unreachable();
80+
sus::unreachable();
8181
}());
8282
ancestor_anchor.add_href(e.link_href);
8383
ancestor_anchor.write_text(e.name);

subdoc/lib/gen/generate_cpp_path.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Vec<CppPathElement> generate_with_ancestors(
4141
return std::string("(anonymous)");
4242
case Namespace::Tag::Named: return sus::clone(ancestor.name);
4343
}
44-
sus_unreachable();
44+
sus::unreachable();
4545
}(),
4646
.link_href = construct_html_url_for_namespace(ancestor),
4747
.type =
@@ -51,7 +51,7 @@ Vec<CppPathElement> generate_with_ancestors(
5151
case Namespace::Tag::Anonymous: return CppPathNamespace;
5252
case Namespace::Tag::Named: return CppPathNamespace;
5353
}
54-
sus_unreachable();
54+
sus::unreachable();
5555
}(),
5656
.search_weight = 1_f32,
5757
});

subdoc/lib/gen/generate_function.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ sus::Result<void, MarkdownToHtmlError> generate_function(
321321
case CppPathConcept:
322322
break; // Concept can't be an ancestor of a function.
323323
}
324-
sus_unreachable();
324+
sus::unreachable();
325325
}());
326326
ancestor_anchor.add_href(e.link_href);
327327
ancestor_anchor.write_text(e.name);

subdoc/lib/gen/generate_macro.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ sus::Result<void, MarkdownToHtmlError> generate_macro(
145145
case CppPathProject: return "project-name";
146146
case CppPathMacro: return "macro-name";
147147
default:
148-
sus_unreachable(); // Macros are only in the global namespace.
148+
sus::unreachable(); // Macros are only in the global namespace.
149149
}
150150
}());
151151
ancestor_anchor.add_href(e.link_href);

subdoc/lib/gen/generate_namespace.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void generate_namespace_overview(HtmlWriter::OpenDiv& namespace_div,
196196
case CppPathConcept:
197197
break; // Concept can't be an ancestor of a namespace.
198198
}
199-
sus_unreachable();
199+
sus::unreachable();
200200
}());
201201
ancestor_anchor.add_href(e.link_href);
202202
ancestor_anchor.write_text(e.name);
@@ -615,7 +615,7 @@ sus::Result<void, MarkdownToHtmlError> generate_namespace(
615615
json.add_string("split_name", split_for_search(options.project_name));
616616
break;
617617
}
618-
case Namespace::Tag::Anonymous: sus_unreachable();
618+
case Namespace::Tag::Anonymous: sus::unreachable();
619619
case Namespace::Tag::Named: {
620620
json.add_string("type", "namespace");
621621
json.add_string("name", element.name);

0 commit comments

Comments
 (0)