Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables dependent macros #822

Merged
merged 1 commit into from
Aug 23, 2024
Merged

Enables dependent macros #822

merged 1 commit into from
Aug 23, 2024

Conversation

zslayton
Copy link
Contributor

@zslayton zslayton commented Aug 23, 2024

This builds on PR #821. It also borrows from and supersedes the changes in @jobarr-amzn's PR #818.

Modifies the TemplateCompiler to resolve macro IDs in the table of 'pending' macros (defined in the current encoding directive) rather than the active macro table. This allows macros to depend on previously defined macros.

Note

cargo fmt did some reflows that accounts for ~half of the diff. Fortunately, all of the formatting changes occur in a contiguous block at the end of the PR. I've added a PR tour comment showing where that begins.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@zslayton zslayton changed the base branch from main to miri-bugfixes August 23, 2024 15:59
@zslayton zslayton marked this pull request as ready for review August 23, 2024 16:11
Copy link
Contributor Author

@zslayton zslayton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️ PR Tour 🧭

Comment on lines -97 to +98
/// to a [`MacroTable`](crate::lazy::expanded::macro_table::MacroTable).
/// to a [`MacroTable`].
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️ This explicit path was made redundant because this module now imports MacroTable.

Comment on lines -145 to +146
// TODO: This is a rudimentary implementation that panics instead of performing thorough
// validation. Where it does surface errors, the messages are too terse.
// TODO: This is a rudimentary implementation that surfaces terse errors.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️ Updated this comment as the impl hasn't used panics in a while.

Comment on lines -453 to +459
fn compile_value<'top, D: Decoder>(
context: EncodingContextRef<'top>,
signature: &MacroSignature,
fn compile_value<D: Decoder>(
tdl_context: TdlContext,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️ Rather than passing a context, signature, and (now) pending_macros to the compiler for each subexpression, we now pass a single TdlContext (defined below on line 840). We can extend this TdlContext as needed in the future to avoid similar plumb-the-argument-through PRs.

Comment on lines -644 to +647
let address =
context.macro_table.address_for_name(name).ok_or_else(|| {
IonError::decoding_error(format!("unrecognized macro name: {name}"))
})?;
Ok((Some(name.to_string()), address))
MacroIdRef::LocalName(name)
} else {
IonResult::decoding_error("macro names must be an identifier")
return IonResult::decoding_error("macro names must be an identifier");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️ Rather than performing the actual lookup of the ID here, this method now saves the ID in a variable (macro_id on line 635 above) and then does the lookup on line 665 below.

I believe that we're going to want to eventually support depending on macros in the active macro table; when we go to add that, we'll just add another .or_else(|| Self::resolve_macro_id_in(...)) call after line 665.

@@ -5,19 +5,25 @@ use std::ops::{Deref, Range};
use bumpalo::collections::Vec as BumpVec;
use rustc_hash::FxHashMap;

use crate::{Bytes, Decimal, Int, IonResult, IonType, LazyExpandedFieldName, Str, Symbol, SymbolRef, Timestamp, try_or_some_err, Value};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗺️

Important

>> Everything from here on is just a formatting change done by rustfmt. <<

There are no logic changes.

@zslayton zslayton changed the title Dependent macros Enables dependent macros Aug 23, 2024
Base automatically changed from miri-bugfixes to main August 23, 2024 17:49
Comment on lines +670 to +672
// TODO: Should it be possible for a macro to reference (and therefore "close over") a macro
// in the current context? The referenced macro would not necessarily be accessible
// in the new encoding context, but would still need to be accessible internally.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be consistent with how the symbol table works, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in that the active symbol table remains available while reading a new symbol table. However, symbols don't depend on other symbol definitions (you can't put $10 in the symbols list of an LST), so this is new territory in that respect.

@zslayton zslayton merged commit a89a75f into main Aug 23, 2024
32 checks passed
@zslayton zslayton deleted the dependent-macros branch August 23, 2024 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants