Skip to content

Commit

Permalink
Remove context and with_context usage
Browse files Browse the repository at this point in the history
Summary: Removing/replacing `.context` and `.with_context` usage in starlark so we can more easily remove `anyhow` in starlark

Reviewed By: JakobDegen

Differential Revision: D62979606

fbshipit-source-id: 6aca5c1bfd2dce93ab66be005fe73e9ac0688a01
  • Loading branch information
Will-MingLun-Li authored and facebook-github-bot committed Sep 19, 2024
1 parent a6794fe commit a1ad8c7
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
8 changes: 5 additions & 3 deletions starlark/src/analysis/unused_loads/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use std::collections::HashMap;

use anyhow::Context;
use dupe::Dupe;
use starlark_syntax::codemap::CodeMap;
use starlark_syntax::codemap::FileSpanRef;
Expand Down Expand Up @@ -112,7 +111,10 @@ pub(crate) fn find_unused_loads(
let args = load.args.try_map(|arg| {
anyhow::Ok(LoadSymbol {
arg,
binding_id: arg.local.payload.context("payload is not set")?,
binding_id: arg
.local
.payload
.ok_or_else(|| anyhow::anyhow!("payload is not set"))?,
used: false,
})
})?;
Expand All @@ -131,7 +133,7 @@ pub(crate) fn find_unused_loads(
println!("visit ident: {:?}", ident);
let ResolvedIdent::Slot(Slot::Module(_), binding_id) = ident
.payload
.context("ident is not resolved (internal error)")?
.ok_or_else(|| anyhow::anyhow!("ident is not resolved (internal error)"))?
else {
return Ok(());
};
Expand Down
11 changes: 5 additions & 6 deletions starlark/src/eval/runtime/profile/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use std::fs;
use std::path::Path;

use anyhow::Context;

use crate::eval::runtime::profile::bc::BcPairsProfileData;
use crate::eval::runtime::profile::bc::BcPairsProfilerType;
use crate::eval::runtime::profile::bc::BcProfileData;
Expand Down Expand Up @@ -114,11 +112,12 @@ impl ProfileData {

/// Write to a file.
pub fn write(&self, path: &Path) -> crate::Result<()> {
fs::write(path, self.gen()?).with_context(|| {
format!(
"write profile `{}` data to `{}`",
fs::write(path, self.gen()?).map_err(|e| {
anyhow::anyhow!(
"Could not write profile `{}` data to `{}`: {}",
self.profile.profile_mode(),
path.display()
path.display(),
e,
)
})?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions starlark/src/eval/runtime/profile/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Write;

use anyhow::Context;
use dupe::Dupe;
use starlark_map::StarlarkHasherBuilder;
use starlark_syntax::codemap::CodeMaps;
use starlark_syntax::internal_error;

use crate::codemap::CodeMap;
use crate::codemap::CodeMapId;
Expand Down Expand Up @@ -181,7 +181,7 @@ impl StmtProfileState {
file: data
.files
.get(*file)
.context("no file corresponding to file id (internal error)")?
.ok_or_else(|| internal_error!("no file corresponding to file id"))?
.dupe(),
span: *span,
},
Expand Down
3 changes: 1 addition & 2 deletions starlark/src/values/layout/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::fmt;
use std::marker::PhantomData;

use allocative::Allocative;
use anyhow::Context;
use dupe::Clone_;
use dupe::Copy_;
use dupe::Dupe_;
Expand Down Expand Up @@ -177,7 +176,7 @@ where
type Frozen = FrozenValueTyped<'static, T::Frozen>;

fn freeze(self, freezer: &Freezer) -> anyhow::Result<Self::Frozen> {
FrozenValueTyped::new(self.0.freeze(freezer)?).context("Incorrect type")
FrozenValueTyped::new_err(self.0.freeze(freezer)?)
}
}

Expand Down
7 changes: 4 additions & 3 deletions starlark/src/values/types/int_or_big.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use std::ops::Not;
use std::ops::Sub;
use std::str::FromStr;

use anyhow::Context;
use dupe::Dupe;
use num_bigint::BigInt;
use num_bigint::Sign;
Expand Down Expand Up @@ -203,7 +202,8 @@ impl<'v> StarlarkIntRef<'v> {
let offset = if sig < 0 && a % b != 0 { 1 } else { 0 };
match a.checked_div(b) {
Some(div) => Ok(StarlarkInt::Small(
div.checked_sub_i32(offset).context("unreachable")?,
div.checked_sub_i32(offset)
.ok_or_else(|| anyhow::anyhow!("unreachable"))?,
)),
None => Self::floor_div_big_big(&a.to_bigint(), &b.to_bigint()),
}
Expand Down Expand Up @@ -284,7 +284,8 @@ impl<'v> StarlarkIntRef<'v> {
Ok(InlineInt::ZERO)
} else {
Ok(if b.signum() != r.signum() {
r.checked_add(b).context("unreachable")?
r.checked_add(b)
.ok_or_else(|| anyhow::anyhow!("unreachable"))?
} else {
r
})
Expand Down
6 changes: 3 additions & 3 deletions starlark/src/values/types/string/dot_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use std::str::FromStr;

use anyhow::Context as _;
use starlark_syntax::dot_format_parser::FormatConv;
use starlark_syntax::dot_format_parser::FormatParser;
use starlark_syntax::dot_format_parser::FormatToken;
Expand Down Expand Up @@ -176,8 +175,9 @@ fn format_capture<'v, T: Iterator<Item = Value<'v>>>(
conv(args.next_ordered()?, result);
Ok(())
} else if field.bytes().all(|c| c.is_ascii_digit()) {
let i = usize::from_str(field)
.with_context(|| format!("Error parsing `{field}` as a format string index"))?;
let i = usize::from_str(field).map_err(|e| {
anyhow::anyhow!("Error parsing `{field}` as a format string index: {e}")
})?;
conv(args.by_index(i)?, result);
Ok(())
} else {
Expand Down
3 changes: 1 addition & 2 deletions starlark/src/values/typing/type_compiled/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::hash::Hash;
use std::hash::Hasher;

use allocative::Allocative;
use anyhow::Context;
use dupe::Dupe;
use starlark_derive::starlark_module;
use starlark_derive::starlark_value;
Expand Down Expand Up @@ -280,7 +279,7 @@ impl<'v, V: ValueLike<'v>> TypeCompiled<V> {
self.to_value()
.0
.request_value::<&dyn TypeCompiledDyn>()
.context("Not TypeCompiledImpl (internal error)")
.ok_or_else(|| anyhow::anyhow!("Not TypeCompiledImpl (internal error)"))
}

/// Check if given value matches this type.
Expand Down
4 changes: 2 additions & 2 deletions starlark_bin/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::fmt::Display;
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::Context as _;
use clap::builder::StringValueParser;
use clap::builder::TypedValueParser;
use clap::Parser;
Expand Down Expand Up @@ -238,7 +237,8 @@ fn drain(
if json {
println!(
"{}",
serde_json::to_string(&LintMessage::new(x)).context("serializing lint to JSON")?
serde_json::to_string(&LintMessage::new(x))
.map_err(|e| anyhow::anyhow!("Failed to serialize lint to JSON: {e}"))?
);
} else if let Some(error) = x.full_error_with_span {
let mut error = error.to_owned();
Expand Down

0 comments on commit a1ad8c7

Please sign in to comment.