Skip to content

Commit e173c08

Browse files
authored
Merge pull request #23035 from github/tausbn/yeast-catch-translation-panics
yeast: Catch translation panics
2 parents 65865d0 + 8701be3 commit e173c08

7 files changed

Lines changed: 174 additions & 161 deletions

File tree

‎shared/yeast-macros/src/lower.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ impl Rule {
8585
#(#guard_bindings)*
8686
let ast = __ast;
8787
let #ctx = __user_ctx;
88-
Ok(#guard)
88+
let __guard: bool = #guard;
89+
__guard
8990
}),
9091
Box::new(|__ast: &yeast::Ast, __output: &mut yeast::OutputAst, __captures: yeast::captures::Captures, __source_range: Option<yeast::Range>, __user_ctx: &mut _, __translator: yeast::TranslatorHandle<'_, _>| {
9192
let __skip: &[&str] = &[#(#raw_capture_names),*];
9293
let __translated_captures =
93-
__translator.translate_captures(&__captures, __ast, __output, __user_ctx, __skip)?;
94+
__translator.translate_captures(&__captures, __ast, __output, __user_ctx, __skip);
9495
#(#raw_bindings)*
9596
#(#translated_bindings)*
9697
let mut #ctx = yeast::build::BuildCtx::with_translator(__ast, __output, __source_range, __user_ctx, __translator);
9798
let __result: Vec<yeast::OutputId> = { #replacement };
98-
let __result = #ctx.finish_rule(__result)?;
99-
Ok(__result)
99+
#ctx.finish_rule(__result)
100100
}),
101101
)
102102
}

‎shared/yeast/src/build.rs‎

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
/// When constructed by the framework (via the rule! macro), `BuildCtx` also
2929
/// carries a [`TranslatorHandle`] that the [`translate`] method delegates
3030
/// to. When constructed by hand (e.g. in tests), the translator is `None`
31-
/// and [`translate`] returns an error.
31+
/// and [`translate`] panics.
3232
pub struct BuildCtx<'a, C: 'a = ()> {
3333
ast: &'a Ast,
3434
pub output: &'a mut OutputAst,
@@ -44,10 +44,6 @@ pub struct BuildCtx<'a, C: 'a = ()> {
4444
/// Optional translator handle, populated when the context is built by
4545
/// the framework's rule driver. None when the context is built by hand.
4646
pub(crate) translator: Option<TranslatorHandle<'a, C>>,
47-
/// First failure encountered while auto-translating an input `Id` during
48-
/// template construction. Templates remain infallible expressions, so
49-
/// [`finish_rule`](Self::finish_rule) returns this error at the rule boundary.
50-
deferred_translation_error: Option<String>,
5147
/// Nodes built directly through this context without an explicit source
5248
/// range in either their content or constructor argument. Recursive
5349
/// translations use their own context and therefore do not contribute to
@@ -64,7 +60,6 @@ impl<'a, C> BuildCtx<'a, C> {
6460
source_range: None,
6561
user_ctx,
6662
translator: None,
67-
deferred_translation_error: None,
6863
created_nodes_without_source_range: BTreeSet::new(),
6964
}
7065
}
@@ -82,7 +77,6 @@ impl<'a, C> BuildCtx<'a, C> {
8277
source_range,
8378
user_ctx,
8479
translator: None,
85-
deferred_translation_error: None,
8680
created_nodes_without_source_range: BTreeSet::new(),
8781
}
8882
}
@@ -102,7 +96,6 @@ impl<'a, C> BuildCtx<'a, C> {
10296
source_range,
10397
user_ctx,
10498
translator: Some(translator),
105-
deferred_translation_error: None,
10699
created_nodes_without_source_range: BTreeSet::new(),
107100
}
108101
}
@@ -159,18 +152,15 @@ impl<'a, C> BuildCtx<'a, C> {
159152
/// Finish the current rule invocation by applying the matched source range
160153
/// to locally-created result roots.
161154
#[doc(hidden)]
162-
pub fn finish_rule(self, results: Vec<OutputId>) -> Result<Vec<OutputId>, String> {
163-
if let Some(error) = self.deferred_translation_error {
164-
return Err(error);
165-
}
155+
pub fn finish_rule(self, results: Vec<OutputId>) -> Vec<OutputId> {
166156
if let Some(source_range) = self.source_range {
167157
for &id in &results {
168158
if self.created_nodes_without_source_range.contains(&id) {
169159
self.output.extend_source_range(id, source_range);
170160
}
171161
}
172162
}
173-
Ok(results)
163+
results
174164
}
175165

176166
/// Assign an explicit source range to a newly-built result root.
@@ -300,40 +290,31 @@ impl<C: Clone> BuildCtx<'_, C> {
300290

301291
#[doc(hidden)]
302292
pub fn translate_into(&mut self, id: Id, out: &mut Vec<OutputId>) {
303-
if self.deferred_translation_error.is_some() {
304-
return;
305-
}
306-
match self.translate(id) {
307-
Ok(ids) => out.extend(ids),
308-
Err(error) => self.deferred_translation_error = Some(error),
309-
}
293+
out.extend(self.translate(id));
310294
}
311295

312296
/// Recursively translate every id in the given iterable via the
313297
/// framework's rule machinery and return the accumulated resulting node
314298
/// ids in order.
315299
///
316300
/// The single-`Id` case works too, because `Id: IntoIterator<Item
317-
/// = Id>` is a singleton iterator — so `ctx.translate(some_id)?`
301+
/// = Id>` is a singleton iterator — so `ctx.translate(some_id)`
318302
/// returns a `Vec<OutputId>` containing whatever `some_id` translated to.
319303
///
320-
/// Errors if this `BuildCtx` was constructed by hand (without a
304+
/// Panics if this `BuildCtx` was constructed by hand (without a
321305
/// translator handle) — for example, in unit tests that don't go
322306
/// through the rule driver.
323-
pub fn translate(
324-
&mut self,
325-
ids: impl IntoIterator<Item = Id>,
326-
) -> Result<Vec<OutputId>, String> {
307+
pub fn translate(&mut self, ids: impl IntoIterator<Item = Id>) -> Vec<OutputId> {
327308
let translator = self
328309
.translator
329310
.as_ref()
330-
.ok_or("translate() called on a BuildCtx without a translator handle")?;
311+
.expect("translate() called on a BuildCtx without a translator handle");
331312
let mut out = Vec::new();
332313
for id in ids {
333-
let translated = translator.translate(self.ast, self.output, self.user_ctx, id)?;
314+
let translated = translator.translate(self.ast, self.output, self.user_ctx, id);
334315
out.extend(translated);
335316
}
336-
Ok(out)
317+
out
337318
}
338319

339320
/// Run `f` with a temporary child [`BuildCtx`] whose `user_ctx` is
@@ -358,9 +339,9 @@ impl<C: Clone> BuildCtx<'_, C> {
358339
/// let val = ctx.scoped(|ctx| {
359340
/// ctx.reset();
360341
/// ctx.translate(val)
361-
/// })?;
342+
/// });
362343
/// // `ctx` here is untouched by the reset inside the closure.
363-
/// let other = ctx.translate(other_id)?;
344+
/// let other = ctx.translate(other_id);
364345
/// ```
365346
pub fn scoped<F, R>(&mut self, f: F) -> R
366347
where
@@ -373,15 +354,11 @@ impl<C: Clone> BuildCtx<'_, C> {
373354
source_range: self.source_range,
374355
user_ctx: &mut child_user_ctx,
375356
translator: self.translator,
376-
deferred_translation_error: None,
377357
created_nodes_without_source_range: BTreeSet::new(),
378358
};
379359
let result = f(&mut child);
380360
let created_nodes_without_source_range =
381361
std::mem::take(&mut child.created_nodes_without_source_range);
382-
if self.deferred_translation_error.is_none() {
383-
self.deferred_translation_error = child.deferred_translation_error.take();
384-
}
385362
drop(child);
386363
self.created_nodes_without_source_range
387364
.extend(created_nodes_without_source_range);

‎shared/yeast/src/captures.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ impl Captures {
5454
self.captures.entry(key).or_default().push(id);
5555
}
5656

57-
pub(crate) fn try_translate_except<E>(
57+
pub(crate) fn translate_except(
5858
&self,
5959
skip: &[&str],
60-
mut f: impl FnMut(Id) -> Result<Vec<OutputId>, E>,
61-
) -> Result<TranslatedCaptures, E> {
60+
mut f: impl FnMut(Id) -> Vec<OutputId>,
61+
) -> TranslatedCaptures {
6262
let mut translated = TranslatedCaptures::new();
6363
for (name, ids) in &self.captures {
6464
if skip.contains(name) {
6565
continue;
6666
}
6767
for &id in ids {
68-
for translated_id in f(id)? {
68+
for translated_id in f(id) {
6969
translated.insert(name, translated_id);
7070
}
7171
}
7272
}
73-
Ok(translated)
73+
translated
7474
}
7575

7676
pub fn merge(&mut self, other: &Captures) {

0 commit comments

Comments
 (0)