From 023e372583b7bcc71b5c301c49501cc72204068f Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 23 Jan 2024 15:34:38 +0200 Subject: feat(tvix/eval): track pattern binding names These need to be preserved at least for builtins.toXML. Also, we incorrectly only wrote an in case ellipsis was true, but that's not the case. Change-Id: I6bff9c47c2922f878d5c43e48280cda9c9ddb692 Reviewed-on: https://cl.tvl.fyi/c/depot/+/10686 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: aspen --- tvix/eval/src/builtins/to_xml.rs | 11 ++++- tvix/eval/src/compiler/mod.rs | 14 ++++-- .../eval/src/tests/nix_tests/eval-okay-xml.exp.xml | 52 ++++++++++++++++++++++ tvix/eval/src/tests/nix_tests/eval-okay-xml.nix | 21 +++++++++ .../nix_tests/notyetpassing/eval-okay-xml.exp.xml | 52 ---------------------- .../nix_tests/notyetpassing/eval-okay-xml.nix | 21 --------- tvix/eval/src/value/function.rs | 4 ++ 7 files changed, 97 insertions(+), 78 deletions(-) create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-xml.exp.xml create mode 100644 tvix/eval/src/tests/nix_tests/eval-okay-xml.nix delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.exp.xml delete mode 100644 tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.nix (limited to 'tvix') diff --git a/tvix/eval/src/builtins/to_xml.rs b/tvix/eval/src/builtins/to_xml.rs index 250891b60d..f23cb22db3 100644 --- a/tvix/eval/src/builtins/to_xml.rs +++ b/tvix/eval/src/builtins/to_xml.rs @@ -90,15 +90,22 @@ fn value_variant_to_xml(w: &mut EventWriter, value: &Value) -> Resu match &c.lambda.formals { Some(formals) => { + let mut attrspat = XmlEvent::start_element("attrspat"); if formals.ellipsis { - w.write(XmlEvent::start_element("attrspat").attr("ellipsis", "1"))?; - w.write(XmlEvent::end_element())?; + attrspat = attrspat.attr("ellipsis", "1"); + } + if let Some(ref name) = &formals.name { + attrspat = attrspat.attr("name", name.as_str()); } + w.write(attrspat)?; + for arg in formals.arguments.iter() { w.write(XmlEvent::start_element("attr").attr("name", arg.0.as_str()))?; w.write(XmlEvent::end_element())?; } + + w.write(XmlEvent::end_element())?; } None => { // TODO(tazjin): tvix does not currently persist function diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 4bb734290c..53a99a453d 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -973,9 +973,16 @@ impl Compiler<'_> { /// cases. fn compile_param_pattern(&mut self, pattern: &ast::Pattern) -> (Formals, CodeIdx) { let span = self.span_for(pattern); - let set_idx = match pattern.pat_bind() { - Some(name) => self.declare_local(&name, name.ident().unwrap().to_string()), - None => self.scope_mut().declare_phantom(span, true), + + let (set_idx, pat_bind_name) = match pattern.pat_bind() { + Some(name) => { + let pat_bind_name = name.ident().unwrap().to_string(); + ( + self.declare_local(&name, pat_bind_name.clone()), + Some(pat_bind_name), + ) + } + None => (self.scope_mut().declare_phantom(span, true), None), }; // At call time, the attribute set is already at the top of the stack. @@ -1126,6 +1133,7 @@ impl Compiler<'_> { arguments, ellipsis, span, + name: pat_bind_name, }), throw_idx, ) diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-xml.exp.xml b/tvix/eval/src/tests/nix_tests/eval-okay-xml.exp.xml new file mode 100644 index 0000000000..20099326cc --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-xml.exp.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tvix/eval/src/tests/nix_tests/eval-okay-xml.nix b/tvix/eval/src/tests/nix_tests/eval-okay-xml.nix new file mode 100644 index 0000000000..9ee9f8a0b4 --- /dev/null +++ b/tvix/eval/src/tests/nix_tests/eval-okay-xml.nix @@ -0,0 +1,21 @@ +rec { + + x = 123; + + y = 567.890; + + a = "foo"; + + b = "bar"; + + c = "foo" + "bar"; + + f = {z, x, y}: if y then x else z; + + id = x: x; + + at = args@{x, y, z}: x; + + ellipsis = {x, y, z, ...}: x; + +} diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.exp.xml b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.exp.xml deleted file mode 100644 index 20099326cc..0000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.exp.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.nix b/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.nix deleted file mode 100644 index 9ee9f8a0b4..0000000000 --- a/tvix/eval/src/tests/nix_tests/notyetpassing/eval-okay-xml.nix +++ /dev/null @@ -1,21 +0,0 @@ -rec { - - x = 123; - - y = 567.890; - - a = "foo"; - - b = "bar"; - - c = "foo" + "bar"; - - f = {z, x, y}: if y then x else z; - - id = x: x; - - at = args@{x, y, z}: x; - - ellipsis = {x, y, z, ...}: x; - -} diff --git a/tvix/eval/src/value/function.rs b/tvix/eval/src/value/function.rs index 8131cffa02..7592e3d641 100644 --- a/tvix/eval/src/value/function.rs +++ b/tvix/eval/src/value/function.rs @@ -18,6 +18,10 @@ pub(crate) struct Formals { /// The span of the formals themselves, to use to emit errors pub(crate) span: Span, + + /// Optionally tracks a name for all function arguments (args@ style). + /// Used by toXML. + pub(crate) name: Option, } impl Formals { -- cgit 1.4.1