Skip to content

Commit 8dd35c3

Browse files
committed
chore: replace SourceMapLocation with Range<usize>
1 parent 5d02555 commit 8dd35c3

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

crates/compilers/src/preprocessor/data.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::SourceMapLocation;
21
use foundry_compilers_artifacts::{Source, Sources};
32
use path_slash::PathExt;
43
use solar_parse::interface::{Session, SourceMap};
@@ -11,6 +10,8 @@ use std::{
1110
path::{Path, PathBuf},
1211
};
1312

13+
use super::span_to_range;
14+
1415
/// Keeps data about project contracts definitions referenced from tests and scripts.
1516
/// Contract id -> Contract data definition mapping.
1617
pub type PreprocessorData = BTreeMap<ContractId, ContractData>;
@@ -97,10 +98,8 @@ impl ContractData {
9798
let mut arg_index = 0;
9899
for param_id in ctor.parameters {
99100
let src = source.file.src.as_str();
100-
let loc =
101-
SourceMapLocation::from_span(source_map, hir.variable(*param_id).span);
102-
let mut new_src =
103-
src[loc.start..loc.end].replace(" memory ", " ").replace(" calldata ", " ");
101+
let loc = span_to_range(source_map, hir.variable(*param_id).span);
102+
let mut new_src = src[loc].replace(" memory ", " ").replace(" calldata ", " ");
104103
if let Some(ident) = hir.variable(*param_id).name {
105104
abi_encode_args.push(format!("args.{}", ident.name));
106105
} else {

crates/compilers/src/preprocessor/deps.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
22
data::{ContractData, PreprocessorData},
3-
SourceMapLocation,
3+
span_to_range,
44
};
55
use crate::Updates;
66
use itertools::Itertools;
@@ -11,7 +11,7 @@ use solar_sema::{
1111
};
1212
use std::{
1313
collections::{BTreeMap, BTreeSet, HashSet},
14-
ops::ControlFlow,
14+
ops::{ControlFlow, Range},
1515
path::{Path, PathBuf},
1616
};
1717

@@ -104,7 +104,7 @@ pub(crate) struct BytecodeDependency {
104104
/// Dependency kind.
105105
kind: BytecodeDependencyKind,
106106
/// Source map location of this dependency.
107-
loc: SourceMapLocation,
107+
loc: Range<usize>,
108108
/// HIR id of referenced contract.
109109
referenced_contract: ContractId,
110110
}
@@ -176,9 +176,8 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
176176
if let ExprKind::New(ty_new) = &ty.kind {
177177
if let TypeKind::Custom(item_id) = ty_new.kind {
178178
if let Some(contract_id) = item_id.as_contract() {
179-
let name_loc =
180-
SourceMapLocation::from_span(self.source_map, ty_new.span);
181-
let name = &self.src[name_loc.start..name_loc.end];
179+
let name_loc = span_to_range(self.source_map, ty_new.span);
180+
let name = &self.src[name_loc];
182181

183182
// Calculate offset to remove named args, e.g. for an expression like
184183
// `new Counter {value: 333} ( address(this))`
@@ -199,7 +198,7 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
199198
named_arg(self.src, named_args, "value", self.source_map),
200199
named_arg(self.src, named_args, "salt", self.source_map),
201200
),
202-
loc: SourceMapLocation::from_span(self.source_map, ty.span),
201+
loc: span_to_range(self.source_map, ty.span),
203202
referenced_contract: contract_id,
204203
});
205204
}
@@ -213,7 +212,7 @@ impl<'hir> Visit<'hir> for BytecodeDependencyCollector<'hir> {
213212
if let Some(contract_id) = contract_id.as_contract() {
214213
self.collect_dependency(BytecodeDependency {
215214
kind: BytecodeDependencyKind::CreationCode,
216-
loc: SourceMapLocation::from_span(self.source_map, expr.span),
215+
loc: span_to_range(self.source_map, expr.span),
217216
referenced_contract: contract_id,
218217
});
219218
}
@@ -236,8 +235,8 @@ fn named_arg(
236235
) -> Option<String> {
237236
named_args.unwrap_or_default().iter().find(|named_arg| named_arg.name.as_str() == arg).map(
238237
|named_arg| {
239-
let named_arg_loc = SourceMapLocation::from_span(source_map, named_arg.value.span);
240-
src[named_arg_loc.start..named_arg_loc.end].to_string()
238+
let named_arg_loc = span_to_range(source_map, named_arg.value.span);
239+
src[named_arg_loc].to_string()
241240
},
242241
)
243242
}

crates/compilers/src/preprocessor/mod.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,17 @@ use solar_parse::{
2323
use solar_sema::{thread_local::ThreadLocal, ParsingContext};
2424
use std::{
2525
collections::HashSet,
26+
ops::Range,
2627
path::{Path, PathBuf},
2728
};
2829

2930
mod data;
3031
mod deps;
3132

32-
/// Represents location of an item in the source map.
33-
/// Used to generate source code updates.
34-
#[derive(Debug)]
35-
struct SourceMapLocation {
36-
/// Source map location start.
37-
start: usize,
38-
/// Source map location end.
39-
end: usize,
40-
}
41-
42-
impl SourceMapLocation {
43-
/// Creates source map location from an item location within a source file.
44-
fn from_span(source_map: &SourceMap, span: Span) -> Self {
45-
Self {
46-
start: source_map.lookup_byte_offset(span.lo()).pos.to_usize(),
47-
end: source_map.lookup_byte_offset(span.hi()).pos.to_usize(),
48-
}
49-
}
33+
/// Returns the range of the given span in the source map.
34+
#[track_caller]
35+
fn span_to_range(source_map: &SourceMap, span: Span) -> Range<usize> {
36+
source_map.span_to_source(span).unwrap().1
5037
}
5138

5239
#[derive(Debug)]

0 commit comments

Comments
 (0)