requirements.references.selector_use
Requirements index · View definition
Example
For consecutive sibling declarations
use alpha::A;anduse beta::B;, each selector is an anonymoususe@<hash>segment derived from that declaration’s structure. Inserting a declaration before them leaves both selectors unchanged; changing an import target gives that declaration a new selector. A named declaration such asstruct Packet;instead has selectorstruct[Packet].![]()
Rationale
anonymous_selectors_use_identity_hashes in src/outline/tests.rs carries a {test} reference to selector_use. It covers each part of the example’s scenario.
- Setup: The source has consecutive
use alpha::A;anduse beta::B;declarations followed bystruct Packet;. - Anonymous
use@<hash>selectors: The test asserts thatbefore_selectors[0]starts withuse@and is exactlyuse@plus 12 characters long. - Named declaration: The test asserts that
before_selectors[2]equalsstruct[Packet]. - Insertion leaves selectors unchanged: The test inserts
use gamma::C;before the two imports. It then checks that every selector from the original outline still resolves in the new outline. That holds only if bothuseselectors are unchanged. - Changing an import target gives a new selector: The test changes
alpha::Atoalpha::Z. The old selector for the firstusethen fails to resolve. The selector for the untouched secondusestill resolves.
The test also checks that two identical use declarations get #1 and #2 suffixes. That is extra coverage and does not conflict with the example.
selectors_name_syntactic_ancestry_and_duplicates also cites selector_use. It asserts that the first two selectors start with use@ and differ from each other. This supports the example but is not needed for it.
The example’s expected outcomes are all asserted, so I found no gaps.
Inputs
Tests
Input 4: outline src/outline/tests.rs fn[anonymous_selectors_use_identity_hashes]
- File:
src/outline/tests.rs - Selector:
fn[anonymous_selectors_use_identity_hashes] - Kind:
fn src/outline/tests.rs:304–336
// {test}`requirements.references.selector_use`
// {test}`requirements.references.outline_selectors`
// {test}`requirements.references.structural_hash`
#[test]
fn anonymous_selectors_use_identity_hashes() {
let before = outline(
"src/lib.rs",
"use alpha::A;\nuse beta::B;\nstruct Packet;\n",
);
let inserted = outline(
"src/lib.rs",
"use gamma::C;\n\nuse alpha::A;\nuse beta::B;\nstruct Packet;\n",
);
let changed = outline(
"src/lib.rs",
"use alpha::Z;\nuse beta::B;\nstruct Packet;\n",
);
let before_selectors = selectors(&before);
assert!(before_selectors[0].starts_with("use@"));
assert_eq!(before_selectors[0].len(), "use@".len() + 12);
assert_eq!(before_selectors[2], "struct[Packet]");
for selector in &before_selectors {
assert!(inserted.resolve(selector).is_ok(), "{selector}");
}
assert!(changed.resolve(&before_selectors[0]).is_err());
assert!(changed.resolve(&before_selectors[1]).is_ok());
let twice = outline("src/lib.rs", "use a::A;\nuse a::A;\n");
let twice = selectors(&twice);
assert!(
twice[0].ends_with("#1") && twice[1].ends_with("#2"),
"{twice:?}"
);
}
Input 5: outline src/outline/tests.rs fn[selectors_name_syntactic_ancestry_and_duplicates]
- File:
src/outline/tests.rs - Selector:
fn[selectors_name_syntactic_ancestry_and_duplicates] - Kind:
fn src/outline/tests.rs:840–885
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.selector_use`
// {test}`requirements.references.outlines`
#[test]
fn selectors_name_syntactic_ancestry_and_duplicates() {
let source = r#"
use alpha::A;
use beta::B;
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); }
extern "C" { fn puts(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
"#;
let selectors = selectors(&outline("lib.rs", source));
assert!(selectors[0].starts_with("use@") && selectors[1].starts_with("use@"));
assert_ne!(selectors[0], selectors[1]);
for selector in [
"mod[local]/fn[run]",
"mod[external]",
"struct[Thing]",
"impl[Thing]/fn[run]",
"impl[Thing/Trait]/fn[run]",
"impl[%5Bu8;%204%5D]/fn[edge]",
"trait[Trait]/fn[read]",
"extern[C]/fn[puts]",
"fn[outer]/fn[inner]",
"fn[repeated]#1",
"fn[repeated]#2",
] {
assert!(
selectors.contains(&selector.to_owned()),
"{selector}: {selectors:?}"
);
}
}
Referenced definitions
Input 2: requirements selection requirements.references.rust_item_selectors
requirements.references.rust_item_selectors:md/requirements/references.md:344–354
:::{req} rust_item_selectors
The Rust outline profile under {cite}`requirements.references.outlines` MUST
identify items by segments describing their syntactic ancestry within the file,
such as `fn[run]`, `struct[Packet]`, and `mod[local]/fn[run]`, and MUST NOT
resolve Rust modules across files. Functions, methods, structs, enums, unions,
traits, impls, modules, macros, constants, statics, type aliases, and extern
blocks MUST be outline nodes. Impl segments MUST be named by their syntactic self
type and optional trait path, as `impl[Thing]` and `impl[Thing/Trait]`. `use`
declarations and other items without a suitable name MUST be anonymous. Selector
spelling MUST be documented.
:::
Input 3: requirements selection requirements.references.outline_selectors
requirements.references.outline_selectors:md/requirements/references.md:380–392
:::{req} outline_selectors
An outline selector MUST be a `/`-separated path of segments from the file's
root node to an outline node; the empty selector MUST select the root node. A
named node's segment MUST be `kind[name]`. An anonymous node's segment MUST be
`kind@hash`, where `hash` is the first twelve hexadecimal digits of its identity
hash under {cite}`requirements.references.structural_hash`. Siblings whose
segments would otherwise be identical MUST be disambiguated by `#N`, their
one-based position among those siblings in source order, except `line` nodes:
identical lines are interchangeable, so a `line` selector MUST resolve when any
line with that content exists in the file. In names, `%`, `[`, `]`, `/`, `#`,
`@`, and whitespace MUST be percent-encoded as UTF-8 bytes, so selectors are
reversible. Line numbers MUST NOT form part of a selector.
:::
Other sources
Input 1: requirements selection requirements.references.selector_use
requirements.references.selector_use:md/requirements/references.md:457–459
:::{example} selector_use
For consecutive sibling declarations `use alpha::A;` and `use beta::B;`, each selector is an anonymous `use@<hash>` segment derived from that declaration's structure. Inserting a declaration before them leaves both selectors unchanged; changing an import target gives that declaration a new selector. A named declaration such as `struct Packet;` instead has selector `struct[Packet]`. {cite}`requirements.references.rust_item_selectors` {cite}`requirements.references.outline_selectors`
:::