Repository navigation
FIX: Handle Rust crate aliases in PackageHallucinationScorer - #4803
Shivam Sharma (ShivamSharma43) wants to merge 5 commits into
Conversation
`extern crate foo as bar;` was not matched, so a hallucinated crate behind an alias went undetected, and bare paths through an alias bound by `use` or `extern crate` (`json::to_string` after `use serde_json as json;`) were reported as hallucinated crates. Capture the crate name and alias from `extern crate`, collect `as` aliases from use trees, and drop aliases from bare path roots. Absolute `::name::` paths still name crates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Rust crate extraction still mishandles raw-identifier aliases and applies alias suppression beyond its scope, causing hallucinated crates to be missed.
2 open issues
What changed in this PR
Updates Rust package extraction in PackageHallucinationScorer to address alias handling reported in #4802.
Changes:
- Recognizes aliased
extern cratedeclarations. - Excludes aliases from bare-path checks while preserving absolute-path checks.
- Adds extraction and scoring regression cases.
| File | Description |
|---|---|
tests/unit/score/test_package_hallucination_scorer.py |
Adds coverage for aliases, hallucinated crates, and absolute paths. |
pyrit/score/true_false/regex/package_hallucination_scorer.py |
Extracts aliased crate declarations and filters alias-based bare paths. |
Review effort: Balanced
| re.compile(r"\buse\s+([\w\s#:{},*]+);"), | ||
| re.compile(r"extern crate\s+([a-zA-Z0-9_]+);"), | ||
| # The crate name and the alias that ``extern crate serde_json as json;`` binds. | ||
| re.compile(r"extern crate\s+([a-zA-Z0-9_]+)(?:\s+as\s+([a-zA-Z0-9_]+))?\s*;"), |
| # A bare path that starts with an alias, such as ``json::to_string`` after ``use serde_json as json;``, | ||
| # goes through the aliased crate or item, which its ``use`` or ``extern crate`` statement already names. | ||
| # An absolute path such as ``::json::to_string`` names a crate, so it was kept above. | ||
| references |= rust_bare_paths - rust_aliases |
There was a problem hiding this comment.
Fixed in b1efa32. An alias now hides only the bare paths in its own scope: the innermost block that declares it, or the whole text when it's declared at the top level. It doesn't reach into a nested mod item. A crate-root extern crate x as y; stays visible everywhere, because it joins the extern prelude. I added your example plus cases for sibling functions, nested blocks, nested modules and an alias used before its use in the same block.
`extern crate fake_crate as r#alias;` did not match the extern crate pattern at all, so the crate escaped checking. Accept an optional r# prefix on the alias, as the use-tree alias collector already does. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A use alias such as `use serde_json as ghost_db;` inside `mod helper { ... }`
no longer hides a `ghost_db::connect()` path elsewhere, which still names a
crate. An alias reaches its own block, or the whole text at the top level,
but not nested `mod` items. A crate-root `extern crate x as y;` joins the
extern prelude and stays visible everywhere.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Description
PackageHallucinationScorerfor Rust missed a crate imported asextern crate fake_crate as fake;and reported the alias in paths such asjson::to_string(afteruse serde_json as json;) as a hallucinated crate. This PR extracts the aliased crate and stops treating aliases as crate roots in bare paths.extern cratepattern now accepts an optionalas aliasand whitespace before the;, and captures both the crate name and the alias. The crate name is checked as before.asin ausetree (use serde_json as json;,use ghost_db::{self as db}) and inextern crateare collected, and bare path roots that match one are dropped. The aliased crate is still checked through itsuseorextern cratestatement.::json::to_stringare unchanged:::namealways names a crate, sojsonis still checked there.extern crateandusealiases, an alias of a hallucinated crate, and an absolute path whose root matches an alias.Before / after with
known_packages={"serde_json"}:Fixes #4802
🤖 Generated with Claude Code