Skip to content

FIX: Handle Rust crate aliases in PackageHallucinationScorer - #4803

Open
Shivam Sharma (ShivamSharma43) wants to merge 5 commits into
microsoft:mainfrom
ShivamSharma43:fix/rust-crate-aliases
Open

Shivam Sharma (ShivamSharma43) wants to merge 5 commits into
microsoft:mainfrom
ShivamSharma43:fix/rust-crate-aliases

Conversation

@ShivamSharma43

Copy link
Copy Markdown
Contributor

Description

PackageHallucinationScorer for Rust missed a crate imported as extern crate fake_crate as fake; and reported the alias in paths such as json::to_string (after use serde_json as json;) as a hallucinated crate. This PR extracts the aliased crate and stops treating aliases as crate roots in bare paths.

  • The extern crate pattern now accepts an optional as alias and whitespace before the ;, and captures both the crate name and the alias. The crate name is checked as before.
  • Aliases bound with as in a use tree (use serde_json as json;, use ghost_db::{self as db}) and in extern crate are collected, and bare path roots that match one are dropped. The aliased crate is still checked through its use or extern crate statement.
  • Absolute paths such as ::json::to_string are unchanged: ::name always names a crate, so json is still checked there.
  • Add extraction and scoring cases for extern crate and use aliases, an alias of a hallucinated crate, and an absolute path whose root matches an alias.

Before / after with known_packages={"serde_json"}:

# before
extern crate serde_json as json; json::to_string(..)   -> True  'json'
extern crate fake_crate_xyz as fake;                    -> False ''
use serde_json as json; json::to_string(..)             -> True  'json'

# after
extern crate serde_json as json; json::to_string(..)   -> False ''
extern crate fake_crate_xyz as fake;                    -> True  'fake_crate_xyz'
use serde_json as json; json::to_string(..)             -> False ''

Fixes #4802

🤖 Generated with Claude Code

`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>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 12:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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 crate declarations.
  • 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@romanlutz Roman Lutz (romanlutz) self-assigned this Dec 3, 2026
`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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: PackageHallucinationScorer misses Rust crates imported as aliases and flags the alias in paths

3 participants