Skip to content

Commit ddeaa7f

Browse files
committed
Improved comments
1 parent 4bda375 commit ddeaa7f

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/lazy/binary/raw/v1_1/value.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,10 @@ impl<'top> LazyRawBinaryValue_1_1<'top> {
290290
header: Header {
291291
// It is an int, that's true.
292292
ion_type: IonType::Int,
293-
// Nonsense values for now
293+
// Eventually we'll refactor `EncodedValue` to accommodate values that don't have
294+
// a header (i.e., parameters with tagless encodings). See:
295+
// https://github.com/amazon-ion/ion-rust/issues/805
296+
// For now, we'll populate these fields with nonsense values and ignore them.
294297
ion_type_code: OpcodeType::Nop,
295298
low_nibble: 0,
296299
},

src/lazy/expanded/compiler.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@ impl ExpansionAnalysis {
4848
}
4949
}
5050

51-
/// When static analysis can detect that a template body will always expand to a single value,
52-
/// information inferred about that value is stored in this type. When this template backs a
53-
/// lazy value, having these fields available allows the lazy value to answer basic queries without
54-
/// needing to fully evaluate the template.
51+
/// When the [`TemplateCompiler`] is able to determine that a macro's template will always produce
52+
/// exactly one value, that macro is considered a "singleton macro." Singleton macros offer
53+
/// a few benefits:
54+
///
55+
/// * Because evaluation will produce be exactly one value, the reader can hand out a LazyValue
56+
/// holding the e-expression as its backing data. Other macros cannot do this because if you're
57+
/// holding a LazyValue and the macro later evaluates to 0 values or 100 values, there's not a way
58+
/// for the application to handle those outcomes.
59+
/// * Expanding a singleton macro doesn't require an evaluator with a stack because as soon as
60+
/// you've gotten a value, you're done--no need to `pop()` and preserve state.
61+
///
62+
/// Information inferred about a singleton macro's output value is stored in an `ExpansionSingleton`.
63+
/// When a singleton macro backs a lazy value, having these fields available allows the lazy value to
64+
/// answer basic queries without needing to fully evaluate the template.
5565
#[derive(Copy, Clone, Debug, PartialEq)]
5666
pub struct ExpansionSingleton {
5767
pub(crate) is_null: bool,

src/lazy/expanded/macro_evaluator.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::fmt::{Debug, Formatter};
1616
use std::ops::Range;
1717

1818
use bumpalo::collections::{String as BumpString, Vec as BumpVec};
19-
use ice_code::ice;
2019

2120
use crate::lazy::decoder::{Decoder, HasSpan, LazyRawValueExpr};
2221
use crate::lazy::expanded::e_expression::{
@@ -408,16 +407,16 @@ impl<'top, D: Decoder> MacroExpansion<'top, D> {
408407
}
409408

410409
/// Expands the current macro with the expectation that it will produce exactly one value.
410+
/// For more information about singleton macros, see
411+
/// [`ExpansionSingleton`](crate::lazy::expanded::compiler::ExpansionSingleton).
411412
#[inline(always)]
412413
pub(crate) fn expand_singleton(mut self) -> IonResult<LazyExpandedValue<'top, D>> {
413414
// We don't need to construct an evaluator because this is guaranteed to produce exactly
414415
// one value.
415416
match self.next_step()? {
416-
// If the expansion produces anything other than a final value, there's a bug.
417417
MacroExpansionStep::FinalStep(Some(ValueExpr::ValueLiteral(value))) => Ok(value),
418-
_ => ice!(IonResult::decoding_error(format!(
419-
"expansion of {self:?} was required to produce exactly one value",
420-
))),
418+
// If the expansion produces anything other than a final value, there's a bug.
419+
_ => unreachable!("expansion of {self:?} was required to produce exactly one value"),
421420
}
422421
}
423422

src/lazy/expanded/macro_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl MacroTable {
179179
pub const NUM_SYSTEM_MACROS: usize = Self::SYSTEM_MACRO_KINDS.len();
180180
// When a user defines new macros, this is the first ID that will be assigned. This value
181181
// is expected to change as development continues. It is currently used in several unit tests.
182-
pub const FIRST_USER_MACRO_ID: usize = 4;
182+
pub const FIRST_USER_MACRO_ID: usize = Self::NUM_SYSTEM_MACROS;
183183

184184
pub fn new() -> Self {
185185
let macros_by_id = vec![

src/lazy/system_reader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ mod tests {
10861086
&[
10871087
Symbol::from("foo"),
10881088
Symbol::from("bar"),
1089-
Symbol::from("baz")
1089+
Symbol::from("baz"),
10901090
]
10911091
);
10921092

@@ -1096,11 +1096,11 @@ mod tests {
10961096
// This directive defines two more.
10971097
assert_eq!(new_macro_table.len(), 2 + MacroTable::NUM_SYSTEM_MACROS);
10981098
assert_eq!(
1099-
new_macro_table.macro_with_id(4),
1099+
new_macro_table.macro_with_id(MacroTable::FIRST_USER_MACRO_ID),
11001100
new_macro_table.macro_with_name("seventeen")
11011101
);
11021102
assert_eq!(
1103-
new_macro_table.macro_with_id(5),
1103+
new_macro_table.macro_with_id(MacroTable::FIRST_USER_MACRO_ID + 1),
11041104
new_macro_table.macro_with_name("twelve")
11051105
);
11061106

0 commit comments

Comments
 (0)