Skip to content

Commit abfba1c

Browse files
committed
Reduce generics
1 parent 35dded3 commit abfba1c

File tree

6 files changed

+133
-81
lines changed

6 files changed

+133
-81
lines changed

crates/rune/src/compile/assembly.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) struct Assembly {
3232
/// Instructions with spans.
3333
pub(crate) instructions: Vec<(AssemblyInst, Span)>,
3434
/// Comments associated with instructions.
35-
pub(crate) comments: HashMap<usize, Vec<Box<str>>>,
35+
pub(crate) comments: HashMap<usize, String>,
3636
/// The number of labels.
3737
pub(crate) label_count: usize,
3838
/// The collection of functions required by this assembly.
@@ -166,18 +166,28 @@ impl Assembly {
166166
}
167167

168168
/// Push a raw instruction.
169-
pub(crate) fn push_with_comment<C>(&mut self, raw: Inst, span: &dyn Spanned, comment: C)
170-
where
171-
C: fmt::Display,
172-
{
169+
pub(crate) fn push_with_comment(
170+
&mut self,
171+
raw: Inst,
172+
span: &dyn Spanned,
173+
comment: &dyn fmt::Display,
174+
) -> compile::Result<()> {
175+
use core::fmt::Write;
176+
173177
let pos = self.instructions.len();
174178

175-
self.comments
176-
.entry(pos)
177-
.or_default()
178-
.push(comment.to_string().into());
179+
let c = self.comments.entry(pos).or_default();
180+
181+
if !c.is_empty() {
182+
c.push_str("; ");
183+
}
184+
185+
if let Err(fmt::Error) = write!(c, "{}", comment) {
186+
return Err(compile::Error::msg(span, "Failed to write comment"));
187+
}
179188

180189
self.push(raw, span);
190+
Ok(())
181191
}
182192

183193
fn inner_push(&mut self, inst: AssemblyInst, span: &dyn Spanned) {

crates/rune/src/compile/unit_builder.rs

+71-16
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ impl UnitBuilder {
682682
assembly: Assembly,
683683
storage: &mut dyn UnitEncoder,
684684
) -> compile::Result<()> {
685+
use core::fmt::Write;
686+
685687
self.label_count = assembly.label_count;
686688

687689
let base = storage.extend_offsets(assembly.labels.len());
@@ -696,7 +698,7 @@ impl UnitBuilder {
696698
}
697699

698700
for (pos, (inst, span)) in assembly.instructions.into_iter().enumerate() {
699-
let mut comment = None::<Box<str>>;
701+
let mut comment = String::new();
700702

701703
let mut labels = Vec::new();
702704

@@ -724,7 +726,14 @@ impl UnitBuilder {
724726
index: label.index,
725727
})
726728
.with_span(location.span)?;
727-
comment = Some(format!("label:{}", label).into());
729+
730+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
731+
return Err(compile::Error::msg(
732+
location.span,
733+
"Failed to write comment",
734+
));
735+
}
736+
728737
storage
729738
.encode(Inst::Jump { jump })
730739
.with_span(location.span)?;
@@ -737,7 +746,14 @@ impl UnitBuilder {
737746
index: label.index,
738747
})
739748
.with_span(location.span)?;
740-
comment = Some(format!("label:{}", label).into());
749+
750+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
751+
return Err(compile::Error::msg(
752+
location.span,
753+
"Failed to write comment",
754+
));
755+
}
756+
741757
storage
742758
.encode(Inst::JumpIf { jump })
743759
.with_span(location.span)?;
@@ -750,7 +766,14 @@ impl UnitBuilder {
750766
index: label.index,
751767
})
752768
.with_span(location.span)?;
753-
comment = Some(format!("label:{}", label).into());
769+
770+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
771+
return Err(compile::Error::msg(
772+
location.span,
773+
"Failed to write comment",
774+
));
775+
}
776+
754777
storage
755778
.encode(Inst::JumpIfOrPop { jump })
756779
.with_span(location.span)?;
@@ -763,7 +786,14 @@ impl UnitBuilder {
763786
index: label.index,
764787
})
765788
.with_span(location.span)?;
766-
comment = Some(format!("label:{}", label).into());
789+
790+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
791+
return Err(compile::Error::msg(
792+
location.span,
793+
"Failed to write comment",
794+
));
795+
}
796+
767797
storage
768798
.encode(Inst::JumpIfNotOrPop { jump })
769799
.with_span(location.span)?;
@@ -776,7 +806,14 @@ impl UnitBuilder {
776806
index: label.index,
777807
})
778808
.with_span(location.span)?;
779-
comment = Some(format!("label:{}", label).into());
809+
810+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
811+
return Err(compile::Error::msg(
812+
location.span,
813+
"Failed to write comment",
814+
));
815+
}
816+
780817
storage
781818
.encode(Inst::JumpIfBranch { branch, jump })
782819
.with_span(location.span)?;
@@ -789,7 +826,14 @@ impl UnitBuilder {
789826
index: label.index,
790827
})
791828
.with_span(location.span)?;
792-
comment = Some(format!("label:{}", label).into());
829+
830+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
831+
return Err(compile::Error::msg(
832+
location.span,
833+
"Failed to write comment",
834+
));
835+
}
836+
793837
storage
794838
.encode(Inst::PopAndJumpIfNot { count, jump })
795839
.with_span(location.span)?;
@@ -802,7 +846,14 @@ impl UnitBuilder {
802846
index: label.index,
803847
})
804848
.with_span(location.span)?;
805-
comment = Some(format!("label:{}", label).into());
849+
850+
if let Err(fmt::Error) = write!(comment, "label:{}", label) {
851+
return Err(compile::Error::msg(
852+
location.span,
853+
"Failed to write comment",
854+
));
855+
}
856+
806857
storage
807858
.encode(Inst::IterNext { offset, jump })
808859
.with_span(location.span)?;
@@ -812,18 +863,22 @@ impl UnitBuilder {
812863
}
813864
}
814865

815-
if let Some(comments) = assembly.comments.get(&pos) {
816-
let actual = comment
817-
.take()
818-
.into_iter()
819-
.chain(comments.iter().cloned())
820-
.collect::<Vec<_>>()
821-
.join("; ");
822-
comment = Some(actual.into())
866+
if let Some(c) = assembly.comments.get(&pos) {
867+
if !comment.is_empty() {
868+
comment.push_str("; ");
869+
}
870+
871+
comment.push_str(c);
823872
}
824873

825874
let debug = self.debug.get_or_insert_with(Default::default);
826875

876+
let comment = if comment.is_empty() {
877+
None
878+
} else {
879+
Some(comment.into())
880+
};
881+
827882
debug.instructions.insert(
828883
at,
829884
DebugInst::new(location.source_id, span, comment, labels),

crates/rune/src/compile/v1/assemble.rs

+27-25
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'hir> Asm<'hir> {
190190
/// Assemble into an instruction.
191191
fn apply(self, cx: &mut Ctxt) -> compile::Result<()> {
192192
if let AsmKind::Var(var) = self.kind {
193-
var.copy(cx, &self.span, format_args!("var `{}`", var));
193+
var.copy(cx, &self.span, &format_args!("var `{}`", var))?;
194194
}
195195

196196
Ok(())
@@ -985,8 +985,8 @@ fn expr_assign<'hir>(
985985
cx.asm.push_with_comment(
986986
Inst::Replace { offset: var.offset },
987987
span,
988-
format_args!("var `{var}`"),
989-
);
988+
&format_args!("var `{var}`"),
989+
)?;
990990
true
991991
}
992992
// <expr>.<field> = <value>
@@ -1251,10 +1251,10 @@ fn expr_async_block<'hir>(
12511251
for capture in hir.captures.iter().copied() {
12521252
if hir.do_move {
12531253
let var = cx.scopes.take(&mut cx.q, capture, span)?;
1254-
var.do_move(cx.asm, span, "capture");
1254+
var.do_move(cx.asm, span, &"capture")?;
12551255
} else {
12561256
let var = cx.scopes.get(&mut cx.q, capture, span)?;
1257-
var.copy(cx, span, "capture");
1257+
var.copy(cx, span, &"capture")?;
12581258
}
12591259
}
12601260

@@ -1264,12 +1264,12 @@ fn expr_async_block<'hir>(
12641264
args: hir.captures.len(),
12651265
},
12661266
span,
1267-
"async block",
1268-
);
1267+
&"async block",
1268+
)?;
12691269

12701270
if !needs.value() {
12711271
cx.asm
1272-
.push_with_comment(Inst::Pop, span, "value is not needed");
1272+
.push_with_comment(Inst::Pop, span, &"value is not needed")?;
12731273
}
12741274

12751275
Ok(Asm::top(span))
@@ -1378,7 +1378,7 @@ fn expr_call<'hir>(
13781378
cx.scopes.alloc(span)?;
13791379
}
13801380

1381-
var.copy(cx, span, "call");
1381+
var.copy(cx, span, &"call")?;
13821382
cx.scopes.alloc(span)?;
13831383

13841384
cx.asm.push(Inst::CallFn { args }, span);
@@ -1456,10 +1456,10 @@ fn expr_call_closure<'hir>(
14561456
for capture in hir.captures.iter().copied() {
14571457
if hir.do_move {
14581458
let var = cx.scopes.take(&mut cx.q, capture, span)?;
1459-
var.do_move(cx.asm, span, "capture");
1459+
var.do_move(cx.asm, span, &"capture")?;
14601460
} else {
14611461
let var = cx.scopes.get(&mut cx.q, capture, span)?;
1462-
var.copy(cx, span, "capture");
1462+
var.copy(cx, span, &"capture")?;
14631463
}
14641464
}
14651465

@@ -1533,8 +1533,8 @@ fn expr_field_access<'hir>(
15331533
index,
15341534
},
15351535
span,
1536-
var,
1537-
);
1536+
&var,
1537+
)?;
15381538

15391539
if !needs.value() {
15401540
cx.q.diagnostics.not_used(cx.source_id, span, cx.context());
@@ -1592,14 +1592,15 @@ fn expr_for<'hir>(
15921592
expr(cx, &hir.iter, Needs::Value)?.apply(cx)?;
15931593

15941594
let iter_offset = cx.scopes.alloc(span)?;
1595+
15951596
cx.asm.push_with_comment(
15961597
Inst::CallAssociated {
15971598
hash: *Protocol::INTO_ITER,
15981599
args: 0,
15991600
},
16001601
span,
1601-
format_args!("into_iter (offset: {})", iter_offset),
1602-
);
1602+
&format_args!("into_iter (offset: {})", iter_offset),
1603+
)?;
16031604

16041605
(iter_offset, loop_scope_expected)
16051606
};
@@ -1620,16 +1621,16 @@ fn expr_for<'hir>(
16201621
offset: iter_offset,
16211622
},
16221623
&hir.iter,
1623-
"copy iterator (memoize)",
1624-
);
1624+
&"copy iterator (memoize)",
1625+
)?;
16251626

16261627
cx.asm.push_with_comment(
16271628
Inst::LoadInstanceFn {
16281629
hash: *Protocol::NEXT,
16291630
},
16301631
&hir.iter,
1631-
"load instance fn (memoize)",
1632-
);
1632+
&"load instance fn (memoize)",
1633+
)?;
16331634

16341635
Some(offset)
16351636
} else {
@@ -1656,16 +1657,16 @@ fn expr_for<'hir>(
16561657
offset: iter_offset,
16571658
},
16581659
&hir.iter,
1659-
"copy iterator",
1660-
);
1660+
&"copy iterator",
1661+
)?;
16611662

16621663
cx.asm.push_with_comment(
16631664
Inst::Copy {
16641665
offset: next_offset,
16651666
},
16661667
&hir.iter,
1667-
"copy next",
1668-
);
1668+
&"copy next",
1669+
)?;
16691670

16701671
cx.asm.push(Inst::CallFn { args: 1 }, span);
16711672

@@ -1691,8 +1692,9 @@ fn expr_for<'hir>(
16911692
args: 0,
16921693
},
16931694
span,
1694-
"next",
1695-
);
1695+
&"next",
1696+
)?;
1697+
16961698
cx.asm.push(
16971699
Inst::Replace {
16981700
offset: binding_offset,

0 commit comments

Comments
 (0)