Skip to content

Commit 9ac31cd

Browse files
authored
fix: eip712, bind-json solar cleanups (#10789)
* fix: eip712, bind-json solar cleanups * rm hir field
1 parent c49d363 commit 9ac31cd

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

crates/forge/src/cmd/bind_json.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl PreprocessedState {
263263
let mut sess = Session::builder().with_stderr_emitter().build();
264264
sess.dcx = sess.dcx.set_flags(|flags| flags.track_diagnostics = false);
265265

266-
let result = sess.enter_parallel(|| -> Result<()> {
266+
sess.enter_parallel(|| -> Result<()> {
267267
// Set up the parsing context with the project paths, without adding the source files
268268
let mut parsing_context = solar_pcx_from_solc_project(&sess, &project, &input, false);
269269

@@ -297,9 +297,9 @@ impl PreprocessedState {
297297
if let Ok(Some(gcx)) = parsing_context.parse_and_lower(&hir_arena) {
298298
let hir = &gcx.get().hir;
299299
let resolver = Resolver::new(gcx);
300-
for id in &resolver.struct_ids() {
301-
if let Some(schema) = resolver.resolve_struct_eip712(*id) {
302-
let def = hir.strukt(*id);
300+
for id in resolver.struct_ids() {
301+
if let Some(schema) = resolver.resolve_struct_eip712(id) {
302+
let def = hir.strukt(id);
303303
let source = hir.source(def.source);
304304

305305
if !target_files.contains(&source.file.stable_id) {
@@ -327,9 +327,9 @@ impl PreprocessedState {
327327
}
328328
}
329329
Ok(())
330-
});
330+
})?;
331331

332-
eyre::ensure!(result.is_ok() && sess.dcx.has_errors().is_ok(), "failed parsing");
332+
eyre::ensure!(sess.dcx.has_errors().is_ok(), "errors occurred");
333333

334334
Ok(StructsState { structs_to_write, target_path })
335335
}

crates/forge/src/cmd/eip712.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,42 @@ impl Eip712Args {
5454
let mut sess = Session::builder().with_stderr_emitter().build();
5555
sess.dcx = sess.dcx.set_flags(|flags| flags.track_diagnostics = false);
5656

57-
let result = sess.enter(|| -> Result<()> {
57+
sess.enter_parallel(|| -> Result<()> {
5858
// Set up the parsing context with the project paths and sources.
5959
let parsing_context =
6060
solar_pcx_from_build_opts(&sess, self.build, Some(vec![self.target_path]))?;
6161

6262
// Parse and resolve
6363
let hir_arena = ThreadLocal::new();
64-
if let Ok(Some(gcx)) = parsing_context.parse_and_lower(&hir_arena) {
65-
let resolver = Resolver::new(gcx);
66-
67-
let outputs = resolver
68-
.struct_ids()
69-
.iter()
70-
.filter_map(|id| {
71-
let resolved = resolver.resolve_struct_eip712(*id)?;
72-
Some(Eip712Output {
73-
path: resolver.get_struct_path(*id),
74-
hash: keccak256(resolved.as_bytes()),
75-
typ: resolved,
76-
})
64+
let Ok(Some(gcx)) = parsing_context.parse_and_lower(&hir_arena) else {
65+
return Err(eyre::eyre!("failed parsing"));
66+
};
67+
let resolver = Resolver::new(gcx);
68+
69+
let outputs = resolver
70+
.struct_ids()
71+
.filter_map(|id| {
72+
let resolved = resolver.resolve_struct_eip712(id)?;
73+
Some(Eip712Output {
74+
path: resolver.get_struct_path(id),
75+
hash: keccak256(resolved.as_bytes()),
76+
typ: resolved,
7777
})
78-
.collect::<Vec<_>>();
79-
80-
if self.json {
81-
sh_println!("{json}", json = serde_json::to_string_pretty(&outputs)?)?;
82-
} else {
83-
for output in &outputs {
84-
sh_println!("{output}")?;
85-
}
78+
})
79+
.collect::<Vec<_>>();
80+
81+
if self.json {
82+
sh_println!("{json}", json = serde_json::to_string_pretty(&outputs)?)?;
83+
} else {
84+
for output in &outputs {
85+
sh_println!("{output}")?;
8686
}
8787
}
8888

8989
Ok(())
90-
});
90+
})?;
9191

92-
eyre::ensure!(result.is_ok() && sess.dcx.has_errors().is_ok(), "failed parsing");
92+
eyre::ensure!(sess.dcx.has_errors().is_ok(), "errors occurred");
9393

9494
Ok(())
9595
}
@@ -99,25 +99,29 @@ impl Eip712Args {
9999
///
100100
/// Requires a reference to the source HIR.
101101
pub struct Resolver<'hir> {
102-
hir: &'hir Hir<'hir>,
103102
gcx: GcxWrapper<'hir>,
104103
}
105104

106105
impl<'hir> Resolver<'hir> {
107106
/// Constructs a new [`Resolver`] for the supplied [`Hir`] instance.
108107
pub fn new(gcx: GcxWrapper<'hir>) -> Self {
109-
Self { hir: &gcx.get().hir, gcx }
108+
Self { gcx }
109+
}
110+
111+
#[inline]
112+
fn hir(&self) -> &'hir Hir<'hir> {
113+
&self.gcx.get().hir
110114
}
111115

112116
/// Returns the [`StructId`]s of every user-defined struct in source order.
113-
pub fn struct_ids(&self) -> Vec<StructId> {
114-
self.hir.strukt_ids().collect()
117+
pub fn struct_ids(&self) -> impl Iterator<Item = StructId> {
118+
self.hir().strukt_ids()
115119
}
116120

117121
/// Returns the path for a struct, with the format: `file.sol > MyContract > MyStruct`
118122
pub fn get_struct_path(&self, id: StructId) -> String {
119-
let strukt = self.hir.strukt(id).name.as_str();
120-
match self.hir.strukt(id).contract {
123+
let strukt = self.hir().strukt(id).name.as_str();
124+
match self.hir().strukt(id).contract {
121125
Some(cid) => {
122126
let full_name = self.gcx.get().contract_fully_qualified_name(cid).to_string();
123127
let relevant = Path::new(&full_name)
@@ -141,7 +145,7 @@ impl<'hir> Resolver<'hir> {
141145
/// not supported by EIP-712 (mappings, function types, errors, etc).
142146
pub fn resolve_struct_eip712(&self, id: StructId) -> Option<String> {
143147
let mut subtypes = BTreeMap::new();
144-
subtypes.insert(self.hir.strukt(id).name.as_str().into(), id);
148+
subtypes.insert(self.hir().strukt(id).name.as_str().into(), id);
145149
self.resolve_eip712_inner(id, &mut subtypes, true, None)
146150
}
147151

@@ -152,11 +156,11 @@ impl<'hir> Resolver<'hir> {
152156
append_subtypes: bool,
153157
rename: Option<&str>,
154158
) -> Option<String> {
155-
let def = self.hir.strukt(id);
159+
let def = self.hir().strukt(id);
156160
let mut result = format!("{}(", rename.unwrap_or(def.name.as_str()));
157161

158162
for (idx, field_id) in def.fields.iter().enumerate() {
159-
let field = self.hir.variable(*field_id);
163+
let field = self.hir().variable(*field_id);
160164
let ty = self.resolve_type(self.gcx.get().type_of_hir_ty(&field.ty), subtypes)?;
161165

162166
write!(result, "{ty} {name}", name = field.name?.as_str()).ok()?;
@@ -204,7 +208,7 @@ impl<'hir> Resolver<'hir> {
204208
}
205209
TyKind::Udvt(ty, _) => self.resolve_type(ty, subtypes),
206210
TyKind::Struct(id) => {
207-
let def = self.hir.strukt(id);
211+
let def = self.hir().strukt(id);
208212
let name = match subtypes.iter().find(|(_, cached_id)| id == **cached_id) {
209213
Some((name, _)) => name.to_string(),
210214
None => {
@@ -219,9 +223,8 @@ impl<'hir> Resolver<'hir> {
219223
subtypes.insert(name.clone(), id);
220224

221225
// Recursively resolve fields to populate subtypes
222-
for field_id in def.fields {
223-
let field_ty =
224-
self.gcx.get().type_of_hir_ty(&self.hir.variable(*field_id).ty);
226+
for &field_id in def.fields {
227+
let field_ty = self.gcx.get().type_of_item(field_id.into());
225228
self.resolve_type(field_ty, subtypes)?;
226229
}
227230
name

0 commit comments

Comments
 (0)