Skip to content
Merged
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 changelog.d/6787-path-matches-glob-feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix(path): link the regex engine when `matchesGlob` is reached through Win32 or dynamic sub-namespace dispatch.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
use super::crypto_ns::module_uses_global_crypto_namespace;
use crate::commands::compile::CompilationContext;

fn debug_hir_uses_regex(hir_debug: &str) -> bool {
hir_debug.contains("RegExp") // RegExp / RegExpDynamic / RegExpTest / RegExpExec / RegExpEscape / RegExpReplaceFn / RegExpExec{Index,Groups}
|| hir_debug.contains("StringMatch") // dedicated .match / .matchAll variants
// Covers both `PathMatchesGlob` and
// `PathWin32 { method: MatchesGlob, ... }`.
|| hir_debug.contains("MatchesGlob")
// Dynamic sub-namespace dispatch keeps the method name in a
// runtime-dispatch expression, but its Debug representation is not
// guaranteed to be a plain `method: "..."` field. Match the API name
// itself: a false positive only links the optional engine, whereas a
// false negative makes `matchesGlob` silently return false.
|| hir_debug.contains("matchesGlob")
|| hir_debug.contains("property: \"search\"")
|| hir_debug.contains("property: \"match\"")
|| hir_debug.contains("property: \"matchAll\"")
|| hir_debug.contains("property: \"glob\"")
|| hir_debug.contains("property: \"globSync\"")
}

/// Inspect a lowered module and set the optional-feature gates it needs.
pub(super) fn detect_optional_feature_usage(
ctx: &mut CompilationContext,
Expand Down Expand Up @@ -122,15 +141,7 @@ pub(super) fn detect_optional_feature_usage(
// non-functional in Perry so it can't create a regex at runtime.
{
let hir_debug: String = format!("{:?}{:?}", &hir_module.init, &hir_module.functions);
if hir_debug.contains("RegExp") // RegExp / RegExpDynamic / RegExpTest / RegExpExec / RegExpEscape / RegExpReplaceFn / RegExpExec{Index,Groups}
|| hir_debug.contains("StringMatch") // dedicated .match / .matchAll variants
|| hir_debug.contains("PathMatchesGlob")
|| hir_debug.contains("property: \"search\"")
|| hir_debug.contains("property: \"match\"")
|| hir_debug.contains("property: \"matchAll\"")
|| hir_debug.contains("property: \"glob\"")
|| hir_debug.contains("property: \"globSync\"")
{
if debug_hir_uses_regex(&hir_debug) {
ctx.uses_regex = true;
Comment on lines 143 to 145

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Include class HIR in regex detection.

The serialized input excludes hir_module.classes, so a matchesGlob call lowered inside a class or static method can leave ctx.uses_regex unset and omit perry-runtime/regex-engine. Include the classes in this format and add a regression case for class-contained dispatch.

Suggested fix
-        let hir_debug: String = format!("{:?}{:?}", &hir_module.init, &hir_module.functions);
+        let hir_debug: String =
+            format!("{:?}{:?}{:?}", &hir_module.init, &hir_module.functions, &hir_module.classes);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let hir_debug: String = format!("{:?}{:?}", &hir_module.init, &hir_module.functions);
if hir_debug.contains("RegExp") // RegExp / RegExpDynamic / RegExpTest / RegExpExec / RegExpEscape / RegExpReplaceFn / RegExpExec{Index,Groups}
|| hir_debug.contains("StringMatch") // dedicated .match / .matchAll variants
|| hir_debug.contains("PathMatchesGlob")
|| hir_debug.contains("property: \"search\"")
|| hir_debug.contains("property: \"match\"")
|| hir_debug.contains("property: \"matchAll\"")
|| hir_debug.contains("property: \"glob\"")
|| hir_debug.contains("property: \"globSync\"")
{
if debug_hir_uses_regex(&hir_debug) {
ctx.uses_regex = true;
let hir_debug: String =
format!("{:?}{:?}{:?}", &hir_module.init, &hir_module.functions, &hir_module.classes);
if debug_hir_uses_regex(&hir_debug) {
ctx.uses_regex = true;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry/src/commands/compile/collect_modules/feature_detect.rs` around
lines 143 - 145, Update the HIR serialization used by the regex detection in the
relevant feature-detection function to include hir_module.classes alongside init
and functions, so regex usage inside class or static-method HIR sets
ctx.uses_regex. Add a regression case covering class-contained matchesGlob
dispatch.

}
}
Expand Down Expand Up @@ -332,3 +343,18 @@ pub(super) fn detect_optional_feature_usage(
ctx.native_module_imports.insert("ioredis".to_string());
}
}

#[cfg(test)]
mod tests {
use super::debug_hir_uses_regex;

#[test]
fn regex_gate_detects_static_and_dynamic_path_matches_glob() {
assert!(debug_hir_uses_regex(
r#"PathWin32 { method: MatchesGlob, args: [] }"#
));
assert!(debug_hir_uses_regex(
r#"NativeMethodCall { module: String("path.win32"), method: String("matchesGlob"), args: [] }"#
));
}
}
Loading