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_inline_and_foreign

Requirements index · View definition

evidence Valid

Example

A function run inside an inline mod local { ... } in src/lib.rs has selector mod[local]/fn[run]. A function defined in a separate src/local.rs file has a selector within that file, without reconstructing the path of a mod local; declaration elsewhere. A foreign function puts in an extern "C" block has selector extern[C]/fn[puts]. cite requirements.references.rust_item_selectors

Rationale

The example is covered by three {test} references in src/outline/tests.rs. Together they cover all three parts of its scenario. I read the tests and their helpers. I did not run them.

  • Inline module: rust_selectors_follow_syntactic_ancestry (line 35) and selectors_name_syntactic_ancestry_and_duplicates (line 662) both parse mod local { fn run() {} } as src/lib.rs or lib.rs. Both assert that mod[local]/fn[run] is among the outline’s selectors. The first test also checks that outline.resolve(selector) returns a node whose selector equals the one asked for.
  • Foreign function: both tests parse extern "C" { fn puts(); } and assert extern[C]/fn[puts]. The first also checks that an extern { fn other(); } block gets the default ABI name, extern[Rust]/fn[other].
  • Separate module file: functions_in_a_separate_module_file_are_selected_within_that_file (line 1059) parses lib.rs containing mod local; and gets exactly ["mod[local]"]. It parses local.rs containing pub fn run() {} and gets exactly ["fn[run]"]. It also asserts that no selector in local.rs contains mod[local]. This shows the outline does not rebuild the module path from the declaration in another file, which is the “without reconstructing the path” part of the example.

The helpers outline and selectors call the real parse and collect the node selectors, so the assertions exercise the real outline code rather than a stub. The tests cover both the positive outcomes and the cross-file non-resolution that the example requires.

Inputs

Tests

Input 3: outline src/outline/tests.rs fn[rust_selectors_follow_syntactic_ancestry]
// {test}`requirements.references.outline_selectors`
// {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.outlines`
#[test]
fn rust_selectors_follow_syntactic_ancestry() {
    let source = r#"
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); type Item; }
extern "C" { fn puts(); }
extern { fn other(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
macro_rules! make { () => {} }
union U { a: u8 }
enum E { A }
const C: u8 = 1;
static S: u8 = 1;
type Alias = u8;
extern crate core;
"#;
    let outline = outline("src/lib.rs", source);
    let selectors = selectors(&outline);
    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]",
        "trait[Trait]/type[Item]",
        "extern[C]/fn[puts]",
        "extern[Rust]/fn[other]",
        "fn[outer]/fn[inner]",
        "fn[repeated]#1",
        "fn[repeated]#2",
        "macro[make]",
        "union[U]",
        "enum[E]",
        "const[C]",
        "static[S]",
        "type[Alias]",
        "extern-crate[core]",
    ] {
        assert!(
            selectors.contains(&selector.to_owned()),
            "{selector}: {selectors:?}"
        );
        assert_eq!(
            outline.nodes[outline.resolve(selector).unwrap()].selector,
            selector
        );
    }
    assert_eq!(outline.resolve("").unwrap(), 0);
    assert!(outline.resolve("fn[repeated]").is_err());
    assert!(outline.resolve("fn[missing]").is_err());
}
Input 4: 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:?}"
        );
    }
}
Input 5: outline src/outline/tests.rs fn[functions_in_a_separate_module_file_are_selected_within_that_file]
// {test}`requirements.references.selector_inline_and_foreign`
#[test]
fn functions_in_a_separate_module_file_are_selected_within_that_file() {
    // `src/lib.rs` declares `mod local;`; `src/local.rs` holds its body. The
    // function's selector is relative to `src/local.rs`, without the module
    // path that the declaration in another file implies.
    let declaring = selectors(&outline("lib.rs", "mod local;\n"));
    assert_eq!(declaring, vec!["mod[local]".to_owned()]);
    let module = selectors(&outline("local.rs", "pub fn run() {}\n"));
    assert_eq!(module, vec!["fn[run]".to_owned()]);
    assert!(
        !module
            .iter()
            .any(|selector| selector.contains("mod[local]"))
    );
}

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.
:::

Other sources

Input 1: requirements selection requirements.references.selector_inline_and_foreign
:::{example} selector_inline_and_foreign
A function `run` inside an inline `mod local { ... }` in `src/lib.rs` has selector `mod[local]/fn[run]`. A function defined in a separate `src/local.rs` file has a selector within that file, without reconstructing the path of a `mod local;` declaration elsewhere. A foreign function `puts` in an `extern "C"` block has selector `extern[C]/fn[puts]`. {cite}`requirements.references.rust_item_selectors`
:::
Input 6: outline src/outline/tests.rs use@e843693d3cbe
use std::path::Path;
Input 7: outline src/outline/tests.rs use@bcaf6fa45a89
use super::{Outline, parse};
Input 8: outline src/outline/tests.rs fn[outline]
fn outline(path: &str, source: &str) -> Outline {
    parse(Path::new(path), source).unwrap_or_else(|error| panic!("{path}: {error}"))
}
Input 9: outline src/outline/tests.rs fn[selectors]
fn selectors(outline: &Outline) -> Vec<String> {
    outline.nodes[1..]
        .iter()
        .map(|node| node.selector.clone())
        .collect()
}
Input 10: outline src/outline/tests.rs fn[summary]
fn summary(outline: &Outline) -> Vec<String> {
    outline.nodes[1..]
        .iter()
        .map(|node| {
            format!(
                "{} {}-{}",
                regex::Regex::new("@[0-9a-f]{12}")
                    .unwrap()
                    .replace_all(&node.selector, "@"),
                node.line_start,
                node.line_end
            )
        })
        .collect()
}