Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.references.selector_use

Requirements index · View definition

evidence Valid

Example

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

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; and use beta::B; declarations followed by struct Packet;.
  • Anonymous use@<hash> selectors: The test asserts that before_selectors[0] starts with use@ and is exactly use@ plus 12 characters long.
  • Named declaration: The test asserts that before_selectors[2] equals struct[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 both use selectors are unchanged.
  • Changing an import target gives a new selector: The test changes alpha::A to alpha::Z. The old selector for the first use then fails to resolve. The selector for the untouched second use still 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]
// {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]
// {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
:::{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
:::{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
:::{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`
:::