Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion compiler-backend/functional/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn convert(mut context: Context<'_, impl checking::ExternalQueries>) -> Conversi
}
}
// StyleX consumes the emitted expressions, including literals substituted for local bindings.
context.validate_stylex_uses(&declarations)?;
let stylex_theme_imports = context.validate_stylex_uses(&declarations)?;

let dependencies = context.dependencies.iter().map(|(&file_id, dependency)| ModuleDependency {
file_id,
Expand All @@ -220,6 +220,7 @@ fn convert(mut context: Context<'_, impl checking::ExternalQueries>) -> Conversi
dependencies: dependencies.into(),
surface,
declarations: declarations.into(),
stylex_theme_imports,
storage: context.storage,
})
}
Expand Down
16 changes: 7 additions & 9 deletions compiler-backend/functional/src/convert/stylex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,14 @@ where
Ok(intrinsic)
}

/// Returns the imported theme values that StyleX evaluates statically in this module.
pub(super) fn validate_stylex_uses(
&self,
declarations: &[Declaration],
) -> ConversionResult<()> {
) -> ConversionResult<FxHashSet<GlobalId>> {
// StyleX intrinsics and expressions only arise from references to the virtual modules.
if !self.references_stylex_module && !self.module_is_virtual(self.file_id) {
return Ok(());
return Ok(FxHashSet::default());
}
let mut bindings = StyleXStaticBindings::default();
for declaration in declarations {
Expand Down Expand Up @@ -334,7 +335,8 @@ where
&mut bindings,
)?;
}
Ok(())
let theme_imports = bindings.imports.into_iter().filter(|&(_, theme)| theme);
Ok(theme_imports.map(|(global, _)| global).collect())
}

fn validate_stylex_expression(
Expand Down Expand Up @@ -768,13 +770,9 @@ where
Ok(matches!(
context.storage[expression].kind,
ExpressionKind::StyleX(StyleXExpression::Call {
target: StyleXCallTarget::Root(
StyleXRootCall::DefineVars
| StyleXRootCall::DefineConsts
| StyleXRootCall::DefineMarker
),
target: StyleXCallTarget::Root(call),
..
})
}) if call.defines_theme_value()
))
}

Expand Down
11 changes: 11 additions & 0 deletions compiler-backend/functional/src/stylex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ impl StyleXRootCall {
StyleXRootCall::FirstThatWorks => "firstThatWorks",
}
}

/// StyleX hashes these definitions by file path, so it accepts them only in theme files and
/// resolves references to them only through imports of theme files.
pub fn defines_theme_value(self) -> bool {
matches!(
self,
StyleXRootCall::DefineVars
| StyleXRootCall::DefineConsts
| StyleXRootCall::DefineMarker
)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
4 changes: 4 additions & 0 deletions compiler-backend/functional/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use files::FileId;
use indexing::{DeriveId, InstanceId, TermItemId, TypeItemId};
use la_arena::{Arena, Idx};
use lowering::TypeId as SourceTypeId;
use rustc_hash::FxHashSet;
use smol_str::SmolStr;

use crate::stylex::StyleXExpression;
Expand All @@ -21,6 +22,9 @@ pub struct Module {
pub dependencies: Arc<[ModuleDependency]>,
pub surface: ModuleSurface,
pub declarations: Arc<[Declaration]>,
/// Imported `defineVars`, `defineConsts`, and `defineMarker` values that StyleX evaluates
/// statically, which it resolves only through imports of the defining module's theme file.
pub stylex_theme_imports: FxHashSet<GlobalId>,
pub storage: Storage,
}

Expand Down
Loading
Loading