ring/polyfill/cold_error.rs
1// Copyright 2024 Brian Smith.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15/// Reduces boilerplate for defining error types where we want the compiler to
16/// optimize for the non-error path by assuming constructing an error is
17/// unlikely/cold code.
18///
19/// WARNING: Every struct/variant must contain some *non-constant* value so
20/// that the "invariant code" pass of the compiler doesn't recognize the
21/// constructor as being "invariant code" and optimizing it away;
22/// although such optimization would be nice to take advantage of, it
23/// seems to lose the `#[cold]` attribute.
24///
25/// Constructor functions ar marked `pub(super)` to ensure that instances can
26/// only be constructed from within the enclosing module (and its submodules).
27///
28/// XXX: #[inline(never)] is required to avoid the (MIR?) optimizer inlining
29/// away the function call and losing the `#[cold]` attribute in the process.
30/// We'd otherwise maybe prefer all constructors to be inline.
31///
32/// The type is defined in its own submodule `#mod_name` to hide the
33/// variant/struct constructor, ensuring instances are only constructed
34/// through the generated `$constructor` functions. The constructor methods
35/// work around the lack of the ability to mark an enum variant `#[cold]` and
36/// `#[inline(never)]`.
37macro_rules! cold_exhaustive_error {
38 // struct
39 {
40 struct $mod_name:ident::$Error:ident with $vis:vis constructor {
41 $field:ident: $ValueType:ty
42 }
43 } => {
44 mod $mod_name {
45 #[allow(unused_imports)]
46 use super::*; // So `$ValueType` is in scope.
47
48 pub struct $Error { #[allow(dead_code)] $field: $ValueType }
49
50 impl $Error {
51 #[cold]
52 #[inline(never)]
53 $vis fn new($field: $ValueType) -> Self {
54 Self { $field }
55 }
56 }
57 }
58 };
59 // struct with default constructor visibility.
60 {
61 struct $mod_name:ident::$Error:ident {
62 $field:ident: $ValueType:ty
63 }
64 } => {
65 cold_exhaustive_error! {
66 struct $mod_name::$Error with pub(super) constructor {
67 $field: $ValueType
68 }
69 }
70 };
71
72 // enum
73 {
74 enum $mod_name:ident::$Error:ident {
75 $(
76 $constructor:ident => $Variant:ident($ValueType:ty),
77 )+
78 }
79 } => {
80 mod $mod_name {
81 #[allow(unused_imports)]
82 use super::*; // So `$ValueType` is in scope.
83
84 pub enum $Error {
85 $(
86 $Variant(#[allow(dead_code)] $ValueType)
87 ),+
88 }
89
90 impl $Error {
91 $(
92 #[cold]
93 #[inline(never)]
94 pub(super) fn $constructor(value: $ValueType) -> Self {
95 Self::$Variant(value)
96 }
97 )+
98 }
99 }
100 };
101}