Skip to content

Commit 21d6cba

Browse files
cramertjcopybara-github
authored andcommitted
Generate unusable functions and trait impls on error
This CL replaces bindings generation error comments with generated functions and trait impls that will cause a descriptive compiler error at time-of-use. PiperOrigin-RevId: 715464169 Change-Id: Ia04a23bae1949574ad2a88d8b7eef58daa7c0ec2
1 parent 9f423af commit 21d6cba

File tree

10 files changed

+474
-225
lines changed

10 files changed

+474
-225
lines changed

common/errors.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ impl Errors {
4747
/// Consolidates any errors into a single error message.
4848
////
4949
/// Returns `Ok` if no errors have been added.
50-
pub fn consolidate(&self) -> Result<(), Error> {
50+
pub fn consolidate(&self) -> Result<(), error_report::ErrorList> {
5151
let errors = self.list.take();
5252
if !errors.is_empty() {
53-
return Err(Error::from(error_report::ErrorList::from(errors)));
53+
return Err(error_report::ErrorList::from(errors));
5454
}
5555
Ok(())
5656
}
@@ -59,7 +59,10 @@ impl Errors {
5959
/// list into a single error message.
6060
///
6161
/// Returns `res` unchanged if it is `Ok`.
62-
pub fn consolidate_on_err<T>(&self, res: Result<T, Error>) -> Result<T, Error> {
62+
pub fn consolidate_on_err<T>(
63+
&self,
64+
res: Result<T, Error>,
65+
) -> Result<T, error_report::ErrorList> {
6366
res.map_err(|error| {
6467
self.add(error);
6568
self.consolidate().unwrap_err()

common/errors_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use error_report::{anyhow, ErrorReport, ErrorReporting};
66
use errors::Errors;
7-
// NOTE: verify_that imported for macro use. This should not be required.
8-
use googletest::{expect_eq, fail, gtest, verify_eq, verify_that};
7+
use googletest::{expect_eq, fail, gtest, OrFail};
98

109
#[gtest]
11-
fn test_errors_consolidate_on_empty_list_returns_ok() {
10+
fn test_errors_consolidate_on_empty_list_returns_ok() -> googletest::Result<()> {
1211
let errors = Errors::new();
13-
expect_eq!(errors.consolidate(), Ok(()));
12+
errors.consolidate().or_fail()?;
13+
Ok(())
1414
}
1515

1616
#[gtest]
@@ -23,7 +23,7 @@ fn test_errors_consolidate_on_nonempty_list_returns_reportable_error() -> google
2323
};
2424

2525
let report = ErrorReport::new();
26-
report.report(&error);
26+
report.report(&error.into());
2727
expect_eq!(
2828
serde_json::from_str::<serde_json::Value>(&report.to_json_string()).unwrap(),
2929
serde_json::json!({

rs_bindings_from_cc/generate_bindings/db.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

55
use crate::generate_function::{
6-
generate_function, get_binding, is_record_clonable, overloaded_funcs, FunctionId, ImplKind,
6+
generate_function, get_binding, is_record_clonable, overloaded_funcs, FunctionId,
7+
GeneratedFunction, ImplKind,
78
};
89
use crate::generate_struct_and_union::collect_unqualified_member_functions;
910
use crate::rs_snippet::RsTypeKind;
1011
use crate::rs_type_kind;
11-
use crate::ApiSnippets;
1212
use arc_anyhow::Result;
1313
use error_report::ErrorReporting;
1414
use ffi_types::SourceLocationDocComment;
@@ -67,7 +67,6 @@ memoized::query_group! {
6767
#[input]
6868
fn errors(&self) -> Rc<dyn ErrorReporting>;
6969

70-
7170
/// A collection of errors that should cause bindings generation to fail.
7271
///
7372
/// These errors should be issued only in response to misusage of Crubit itself, such as
@@ -80,7 +79,7 @@ memoized::query_group! {
8079

8180
fn rs_type_kind(&self, rs_type: RsType) -> Result<RsTypeKind>;
8281

83-
fn generate_function(&self, func: Rc<Func>, record_overwrite: Option<Rc<Record>>) -> Result<Option<(Rc<ApiSnippets>, Rc<FunctionId>)>>;
82+
fn generate_function(&self, func: Rc<Func>, record_overwrite: Option<Rc<Record>>) -> Result<Option<GeneratedFunction>>;
8483

8584
fn overloaded_funcs(&self) -> Rc<HashSet<Rc<FunctionId>>>;
8685

0 commit comments

Comments
 (0)