From 31930ebcac8afbbe0fbc605d7841896a8cb7be24 Mon Sep 17 00:00:00 2001 From: Wendy Yu Date: Fri, 19 Jan 2024 08:38:54 -0800 Subject: [PATCH] AnonTargetAttrTraversal Summary: This is a bit unfortuante - we already have a `traverse()` function, but it takes in a `dyn ConfiguredAttrTraversal` which lives in the buck2_node crate, which doesn't have access to promise artifacts. Adding a separate function seems like the easiest way to implement this - we just need to traverse the anon target attrs to get all the promise artifact attrs. Let me know if there are other suggestions, like moving the promise artifact attr into the buck2_node crate, or something like that Reviewed By: cjhopman Differential Revision: D52741553 fbshipit-source-id: bed794ba0b56c8ef0ad073fb0c3a9506a0055cb6 --- app/buck2_anon_target/src/anon_target_attr.rs | 44 +++++++++++++++++++ .../src/anon_target_attr_resolve.rs | 4 ++ 2 files changed, 48 insertions(+) diff --git a/app/buck2_anon_target/src/anon_target_attr.rs b/app/buck2_anon_target/src/anon_target_attr.rs index 7c963db8865f..8e88ade90053 100644 --- a/app/buck2_anon_target/src/anon_target_attr.rs +++ b/app/buck2_anon_target/src/anon_target_attr.rs @@ -37,6 +37,8 @@ use serde::Serialize; use serde::Serializer; use serde_json::to_value; +use crate::anon_target_attr_resolve::AnonTargetAttrTraversal; + #[derive(Debug, Clone, PartialEq, Eq, Hash, Allocative)] pub enum AnonTargetAttr { Bool(BoolLiteral), @@ -178,6 +180,48 @@ impl AnonTargetAttr { } } + #[allow(unused)] + pub fn traverse_anon_attr<'a>( + &'a self, + _traversal: &mut dyn AnonTargetAttrTraversal, + ) -> anyhow::Result<()> { + match self { + AnonTargetAttr::Bool(_) => Ok(()), + AnonTargetAttr::Int(_) => Ok(()), + AnonTargetAttr::String(_) => Ok(()), + AnonTargetAttr::EnumVariant(_) => Ok(()), + AnonTargetAttr::List(list) => { + for v in list.iter() { + v.traverse_anon_attr(_traversal)?; + } + Ok(()) + } + AnonTargetAttr::Tuple(list) => { + for v in list.iter() { + v.traverse_anon_attr(_traversal)?; + } + Ok(()) + } + AnonTargetAttr::Dict(dict) => { + for (k, v) in dict.iter() { + k.traverse_anon_attr(_traversal)?; + v.traverse_anon_attr(_traversal)?; + } + Ok(()) + } + AnonTargetAttr::None => Ok(()), + AnonTargetAttr::OneOf(l, _) => l.traverse_anon_attr(_traversal), + AnonTargetAttr::Dep(_) => Ok(()), + AnonTargetAttr::Artifact(_) => Ok(()), + AnonTargetAttr::Arg(_) => Ok(()), + AnonTargetAttr::PromiseArtifact(_) => { + // TODO(@wendyy) - use traversal here after updating the attr type + Ok(()) + } + AnonTargetAttr::Label(_) => Ok(()), + } + } + pub fn _unpack_list(&self) -> Option<&[AnonTargetAttr]> { match self { AnonTargetAttr::List(list) => Some(list), diff --git a/app/buck2_anon_target/src/anon_target_attr_resolve.rs b/app/buck2_anon_target/src/anon_target_attr_resolve.rs index d5e5735bc89d..feed424b0db6 100644 --- a/app/buck2_anon_target/src/anon_target_attr_resolve.rs +++ b/app/buck2_anon_target/src/anon_target_attr_resolve.rs @@ -146,6 +146,10 @@ pub(crate) struct AnonTargetDependentAnalysisResults<'v> { pub(crate) promised_artifacts: HashMap<&'v PromiseArtifactAttr, Artifact>, } +pub(crate) trait AnonTargetAttrTraversal { + fn promise_artifact(&mut self, promise_artifact: &PromiseArtifactAttr) -> anyhow::Result<()>; +} + impl AnonTargetDependents { pub(crate) fn get_dependents( anon_target: &AnonTargetKey,